<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" 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.scene = data.status == 'FREE' ? 1 : 2
      this.title = data.status == 'FREE' ? '呼叫空车' : '呼叫满车'
      this.pointCode = data.point.area.code
      this.getArea()

    },
    getArea() {
      if (this.scene == 1) {
        this.areaName = '1F产线'
      } else {
        this.areaName = '2F产线'
      }
      getAres({size: 5, name: this.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() {
      if (this.scene == 1) {
        this.areaName = '1F产线'
      } else {
        this.areaName = '2F产线'
      }
      getPoints({size: 5, areaName: this.areaName}).then(res => {
        this.points = res.content.map(function (obj) {
          if (obj.hasChildren) {
            obj.children = null
          }
          return obj

        })
      })
    },
    confirm() {
      if (this.scene == 1) {
        //呼叫空车
        this.areaName = this.pointCode.substring(0, 1) == 1 ? '1F空车区' : '2F空车区'
        this.CallStock.pointCode = this.form.point.name
        this.CallStock.areaName = this.areaName
        this.CallStock.scene = 1
        stockUrl.callStock(this.CallStock).then(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);
          }
        })

      } else {
        //呼叫满车

        //通过容器找到物料
        stockUrl.getItemCode(this.form.stockCode).then(res => {
          this.itemCode = res
          if (this.itemCode == '' || this.itemCode == undefined) {
            return this.$parent.crud.notify(this.form.stockCode + "容器无库存,操作失败!", CRUD.NOTIFICATION_TYPE.ERROR);
          }
          this.areaName = this.pointCode.substring(0, 1) == 1 ? '1F存储区' : '2F齐套C区'
          this.CallStock.itemCode = this.itemCode
          this.CallStock.pointCode = this.form.point.name
          this.CallStock.areaName = this.areaName
          this.CallStock.scene = 2
          console.log(this.CallStock)
          stockUrl.callStock(this.CallStock).then(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);
            }
          })

        })


      }
    }
  }

}
</script>

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

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