<template>
  <a-modal
    centered
    class="shelfSet-newHw-modal"
    :footer="null"
    :maskClosable="false"
    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">
        <a-form-model-item label="产品编码">{{ nowData&&nowData.productVO.code || '--' }}</a-form-model-item>
        <a-form-model-item label="产品名称">{{ nowData&&nowData.productVO.name || '--' }}</a-form-model-item>
        <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 type="primary" id="shelfSet-newHw-modal-save" @click="handleSave">保存</a-button>
        <a-button id="shelfSet-newHw-modal-back" @click="isShow=false" style="margin-left: 15px;">取消</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { stackPlaceSave } from '@/api/product'
export default {
  name: 'EditHwModal',
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: 'edit'
    },
    nowData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      form: {
        shelfPlaceCode: '',
        productSn: undefined,
        productCode: undefined,
        price: '',
        cost: '',
        maxQty: ''
      },
      rules: {
        shelfPlaceCode: [
          { required: true, message: '请输入货位编号', trigger: 'blur' }
        ]
      },
      productName: ''
    }
  },
  methods: {
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          const params = {
            'stackPlaceCode': form.stackPlaceCode ? form.stackPlaceCode.toUpperCase() : '',
            'product': {
              'productSn': _this.nowData.productVO.productSn
            }
          }
          // 开始提交数据
          _this.spinning = true
          stackPlaceSave(params).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
        }
      })
    },
    resetData () {
      this.form.stackPlaceCode = undefined
      this.$refs.ruleForm.resetFields()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetData()
      } else {
        if (this.type == 'edit') {
          this.form.stackPlaceCode = this.nowData.stackPlaceCode
        }
      }
    }
  }
}
</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>
