<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: zwq
 * @LastEditTime: 2022-01-17 11:21:26
 * @Description:
-->
<template>
  <el-dialog
    :title="!dataForm.id ? 'btn.add' : 'btn.edit' | i18nFilter"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="120px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item :label="$t('module.basicData.Warehouse.LocationName')" prop="currLocationId">
        <el-select
          v-model="dataForm.currLocationId"
          :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.Warehouse.LocationName')])"
          clearable
          filterable
          :style="{width: '100%'}"
          @change="selectGetLabel1"
        >
          <el-option
            v-for="(item, index) in LocationList"
            :key="index"
            :label="item.locationName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('module.basicData.Warehouse.StorageBoxNumber')" prop="storageBoxId">
        <el-select
          v-model="dataForm.storageBoxId"
          :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.Warehouse.StorageBoxNumber')])"
          clearable
          filterable
          :style="{width: '100%'}"
          @change="selectGetLabel2"
        >
          <el-option
            v-for="(item, index) in storageBoxList"
            :key="index"
            :label="item.code"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('module.basicData.Warehouse.BoxStatus')" prop="status">
        <el-switch
          v-model="dataForm.status"
          :active-value="1"
          :inactive-value="0"
          active-color="#13ce66"
          inactive-color="#AAAAAA"
        />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.Warehouse.IsEmptyBox')" prop="isEmpty">
        <el-switch
          v-model="dataForm.isEmpty"
          :active-value="1"
          :inactive-value="0"
          active-color="#13ce66"
          inactive-color="#AAAAAA"
        />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ 'btn.cancel' | i18nFilter }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ 'btn.confirm' | i18nFilter }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { StorageBoxRackDetail, StorageBoxRackUpdate, StorageBoxRackAdd, StorageBoxRackCode, locationList } from '@/api/basicData/Warehouse/StorageBoxInfo'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        code: '',
        locationName: '',
        currLocationId: '',
        storageBoxId: '',
        status: '',
        isEmpty: ''
      },
      LocationList: [],
      storageBoxList: [],
      dataRule: {
        currLocationId: [
          {
            required: true,
            message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.Warehouse.LocationName')]),
            trigger: 'blur' }
        ],
        storageBoxId: [
          {
            required: true,
            message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.Warehouse.StorageBoxNumber')]),
            trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || ''
      this.visible = true
      const listQuery = {
        current: 1,
        size: 100
      }
      locationList(listQuery).then(response => {
        this.LocationList = response.data
      })
      StorageBoxRackCode(listQuery).then(res => {
        this.storageBoxList = res.data.records
      })
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          StorageBoxRackDetail(this.dataForm.id).then(res => {
            this.dataForm = res.data
          })
        }
      })
    },
    selectGetLabel1(vId) {
      let obj = {}
      obj = this.LocationList.find((item) => {
        return item.id === vId
      })
      this.dataForm.locationName = obj.locationName
    },
    selectGetLabel2(vId) {
      let obj = {}
      obj = this.storageBoxList.find((item) => {
        return item.id === vId
      })
      this.dataForm.code = obj.code
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = this.dataForm
          if (this.dataForm.id) {
            StorageBoxRackUpdate(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            StorageBoxRackAdd(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    }
  }
}
</script>
