<template>
  <a-modal
    centered
    class="shelfSet-newHw-modal"
    :footer="null"
    :maskClosable="false"
    :title="title"
    v-model="isShow"
    @cancel="isShow=false"
    :width="600">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="shelfSet-newHw-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol">
        <div v-if="type=='edit'">
          <a-form-model-item label="产品编码">{{ nowData&&nowData.product.code || '--' }}</a-form-model-item>
          <a-form-model-item label="产品名称">{{ nowData&&nowData.product.name || '--' }}</a-form-model-item>
          <a-form-model-item label="仓库">{{ nowData&&nowData.warehouseName || '--' }}</a-form-model-item>
        </div>
        <div v-if="type=='add'">
          <a-form-model-item label="产品编码" prop="productSn">
            <productCodeList ref="productCodeList" @change="productCodeChange" />
          </a-form-model-item>
          <a-form-model-item label="产品名称" v-if="productName">
            {{ productName || '--' }}
          </a-form-model-item>
          <a-form-model-item label="仓库" prop="warehouseSn">
            <warehouse
              v-model="form.warehouseSn"
              isPermission
              placeholder="请选择仓库"
              @load="warehouseLoad"
            />
          </a-form-model-item>
        </div>
        <a-form-model-item label="货位编号" prop="stackPlaceCode">
          <a-input v-model="form.stackPlaceCode" :maxLength="10" placeholder="请输入货位编号(最多10个字符)"></a-input>
          <div style="color:#fba200;font-size:12px;">货位编号允许为空,为空则表示此产品没有货位编号</div>
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button id="shelfSet-newHw-modal-back" @click="isShow=false" style="margin-right: 15px;">取消</a-button>
        <a-button type="primary" id="shelfSet-newHw-modal-save" @click="handleSave">保存</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { VSelect } from '@/components'
import { commonMixin } from '@/utils/mixin'
import productCodeList from '@/views/common/productCodeList.vue'
import warehouse from '@/views/common/chooseWarehouse.js'
import { stackPlaceSave } from '@/api/product'
export default {
  name: 'EditHwModal',
  components: { VSelect, productCodeList, warehouse },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    nowData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      form: {
        stackPlaceCode: '',
        productSn: undefined,
        productCode: undefined,
        warehouseSn: undefined
      },
      rules: {
        // stackPlaceCode: [
        //   { required: true, message: '请输入货位编号', trigger: 'blur' }
        // ],
        productSn: [
          { required: true, message: '请选择产品', trigger: 'blur' }
        ],
        warehouseSn: [
          { required: true, message: '请选择仓库', trigger: 'blur' }
        ]
      },
      productName: '',
      title: '',
      type: 'add'
    }
  },
  methods: {
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = _this.form
          form.stackPlaceCode = form.stackPlaceCode.toUpperCase()
          // 开始提交数据
          _this.spinning = true
          stackPlaceSave(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.isShow = false
              _this.$emit('ok', res.data)
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    },
    warehouseLoad (defSn, defObj) {
      const def = this.$store.state.app.defWarehouse || defObj
      this.form.warehouseSn = def && def.warehouseSn || undefined
    },
    productCodeChange (val) {
      this.form.productSn = val.key || undefined
      this.form.productCode = val.row && val.row.code ? val.row.code : undefined
      this.productName = val.row && val.row.name ? val.row.name : undefined
    },
    resetData () {
      this.productName = ''
      if (this.type == 'add') {
        this.$refs.productCodeList && this.$refs.productCodeList.resetForm()
        this.$refs.ruleForm.resetFields()
      }
      this.form.stackPlaceCode = ''
      this.form.stackPlaceSn = undefined
      this.form.productSn = undefined
      this.form.productCode = undefined
      this.form.warehouseSn = undefined
      this.form.id = undefined
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetData()
      } else {
        this.type = this.nowData ? 'edit' : 'add'
        this.title = (this.nowData ? '编辑' : '新增') + '货位编号'
        if (this.type == 'edit') {
          this.form.id = this.nowData.id
          this.form.stackPlaceCode = this.nowData.stackPlaceCode
          this.form.stackPlaceSn = this.nowData.stackPlaceSn
          this.form.productSn = this.nowData.productSn
          this.form.productCode = this.nowData.product.code
          this.form.warehouseSn = this.nowData.warehouseSn
        } else {
          this.form.stackPlaceCode = ''
          this.warehouseLoad()
        }
      }
    }
  }
}
</script>

<style lang="less">
.shelfSet-newHw-modal {
	.ant-modal-body {
		padding: 40px 40px 24px;
	}
  .ant-form-item{
    margin-bottom: 15px;
  }
	.btn-cont {
		text-align: center;
		margin: 35px 0 10px;
	}
}
</style>
