<template>
  <el-dialog :visible.sync="dialog"
             :title="title"
             width="450px"
             :before-close="handleClose">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">

      <el-form-item label="容器" prop="stockCode">
        <el-input v-model="form.stockCode" disabled style="width: 250px;"></el-input>
      </el-form-item>

      <el-form-item label="线边库区" prop="area">
        <el-select v-model="form.area" clearable disabled value-key="id" placeholder="请选择"
                   style="width: 250px;">
          <el-option
            v-for="area in areas"
            :key="area.id"
            :label="area.name"
            :value="area"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="线边点位" prop="point">
        <el-select v-model="form.point" clearable @focus="getPoint(form.area.name)" value-key="id" placeholder="请选择"
                   style="width: 250px;">
          <el-option
            v-for="point in points"
            :key="point.id"
            :label="point.name"
            :value="point"
          />
        </el-select>
      </el-form-item>


    </el-form>

    <span slot="footer" class="dialog-footer">
    <el-button @click="cancel">取 消</el-button>
    <el-button type="primary" @click="confirm">确 定</el-button>
    </span>


  </el-dialog>
</template>

<script>
import crud from '@/mixins/crud'
import {getAres} from "@/api/area";
import {getPoints} from "@/api/point";
import stockUrl from "@/api/stock";
import CRUD from "@crud/crud";

export default {
  mixins: [crud],
  dicts: ["item_type", "base_staus"],
  data() {
    return {
      title: '',
      itemCode: '',
      pointCode: '',
      areaName: '',
      areas: [],
      points: [],
      scene: '',//空车/满车
      CallStock: {
        areaName: '',
        pointCode: '',
        itemCode: '',
        scene: ''
      },
      rules: {
        stockCode: [
          {required: true, message: '容器必填', trigger: 'blur'}
        ],
        area: [
          {required: true, message: '线边库区必填', trigger: 'blur'}
        ],
        point: [
          {required: true, message: '线边点位必填', trigger: 'blur'}
        ]
      },
    }
  },
  methods: {
    handleClose(done) {
      done()
    },
    cancel() {
      this.dialog = false
      this.$refs['form'].resetFields();
    },
    callContainerClick(data) {
      this.form.stockCode = data.code;
      this.title = data.status == 'FREE' ? '呼叫空车' : '呼叫满车'
      this.scene = data.status == 'FREE' ? 1 : 2
      const areaMap = {
        '2F空车区': '2F空车产线',
        '1F空车区': '1F空车产线',
        '1F存储区': '2F产线叫料',
        '2F齐套C区': '2F产线叫料2',
        '2F空车产线': '2F空车产线',//已经呼叫到2F产线了
        '1F空车产线': '1F空车产线',//已经呼叫到1F产线了
        '2F产线叫料': '2F产线叫料',//已经呼叫到2F产线叫料了
        '2F产线叫料2': '2F产线叫料2'//已经呼叫到2F产线叫料2了
      };
      const areaName = areaMap[data.point.area.code];
      this.getArea(areaName)
    },
    getArea(areaName) {
      getAres({name: areaName}).then(res => {
        this.areas = res.content.map(function (obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj

        })
        this.form.area = this.areas[0]
      })
    },
    getPoint(areaName) {
      getPoints({areaName: areaName}).then(res => {
        this.points = res.content.map(function (obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj
        })
      })
    },
    confirm() {
      this.CallStock.pointCode = this.form.point.code;
      this.CallStock.scene = this.scene;

      const handleResponse = (rf) => {
        if (rf.status === 200) {
          this.dialog = false;
          this.$refs['form'].resetFields();
          this.$parent.crud.notify(rf.message, CRUD.NOTIFICATION_TYPE.SUCCESS);
          this.$parent.crud.toQuery();
        } else {
          this.$parent.crud.notify(rf.message, CRUD.NOTIFICATION_TYPE.ERROR);
        }
      };

      if (this.scene === 1) {
        // 呼叫空车
        stockUrl.callStock(this.CallStock).then(handleResponse);
      } else {
        // 呼叫满车，获取物料代码
        stockUrl.getItemCode(this.form.stockCode).then(res => {
          this.itemCode = res;
          if (!this.itemCode) {
            return this.$parent.crud.notify(`${this.form.stockCode}容器无库存,操作失败!`, CRUD.NOTIFICATION_TYPE.ERROR);
          }
          this.CallStock.itemCode = this.itemCode;
          stockUrl.callStock(this.CallStock).then(handleResponse);
        });
      }

    }
  }

}
</script>

<style scoped>
.java.hljs {
  color: #444;
  background: #ffffff !important;
}

::v-deep .el-dialog__body {
  padding: 0 20px 10px 20px !important;
}
</style>
