<template>
  <a-modal
    centered
    class="shelfSet-newHw-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    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.shelfName || '--' }}</a-form-model-item>
        <a-form-model-item label="货位号" prop="shelfPlaceCode">
          <a-input v-model="form.shelfPlaceCode" @input="placeCodeBlur" placeholder="请输入货位号(字母+数字格式)"></a-input>
        </a-form-model-item>
        <a-form-model-item label="绑定产品" v-if="type=='add'">
          <productJqList ref="productJqList" :params="{customerSn:nowData&&nowData.customerSn}" @change="productJqChange"></productJqList>
        </a-form-model-item>
        <a-form-model-item label="产品名称" v-if="form.productSn">{{ productName || '--' }}</a-form-model-item>
        <a-form-model-item v-if="form.productSn" label="销售价" prop="price">
          <a-input-number
            id="shelfSet-newHw-price"
            v-model="form.price"
            :precision="2"
            :min="0"
            :max="999999"
            placeholder="请输入销售价（最多允许两位小数）"
            suffix="元"
            style="width: 80%;display: inline-block;" /> 元
        </a-form-model-item>
        <a-form-model-item v-if="form.productSn" label="结算价" prop="cost">
          <a-input-number
            id="shelfSet-newHw-cost"
            v-model="form.cost"
            :precision="2"
            :min="0"
            :max="999999"
            placeholder="请输入结算价（最多允许两位小数）"
            style="width: 80%;display: inline-block;" /> 元
        </a-form-model-item>
        <a-form-model-item v-if="form.productSn" label="最大库容" prop="maxQty">
          <a-input-number
            id="shelfSet-newHw-maxQty"
            v-model="form.maxQty"
            :precision="0"
            :min="0"
            :max="999999"
            placeholder="请输入最大库容（正整数或0）"
            suffix="元"
            style="width: 100%;display: inline-block;" />
        </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 { VSelect } from '@/components'
import productJqList from '@/views/common/productJqList.vue'
import { addPlaceAndProduct } from '@/api/shelf'
export default {
  name: 'AddHwModal',
  components: { VSelect, productJqList },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: ''
    },
    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' },
          {
          	pattern: /^[a-zA-Z]{1}[0-9]{1,29}$/g,
          	message: '货位号必需字母+数字，且不能超过30个字符'
          }
        ],
        price: [{ required: true, message: '请输入销售价', trigger: 'blur' }],
        cost: [{ required: true, message: '请输入结算价', trigger: 'blur' }],
        maxQty: [{ required: true, message: '请输入最大库容', trigger: 'blur' }]
      },
      productName: ''
    }
  },
  computed: {
    modalTit () {
      let title = ''
      if (this.type == 'edit') {
        title = '编辑货位'
      } else if (this.type == 'add') {
        title = '新增货位'
      }
      return title
    }
  },
  methods: {
    // 产品 change
    productJqChange (obj) {
      this.form.productSn = obj.key || undefined
      this.form.productCode = obj && obj.row && obj.row.code || ''
      this.form.price = obj && obj.row && obj.row.price || ''
      this.form.cost = obj && obj.row && obj.row.cost || ''
      this.productName = obj && obj.row && obj.row.name || ''
    },
    placeCodeBlur (v) {
    	this.$nextTick(() => {
    		this.form.shelfPlaceCode = this.form.shelfPlaceCode.toLocaleUpperCase()
    	})
    },
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = {}
          const form = JSON.parse(JSON.stringify(_this.form))
          params.shelfSn = _this.nowData && _this.nowData.shelfSn
          params.shelfPlaceCode = form.shelfPlaceCode.toLocaleUpperCase()
          if (this.type == 'edit') {
            params.id = this.nowData.id
          }
          // 绑定了产品
          if (form.productSn) {
            params.shelfProductApiEntity = {
              productSn: form.productSn,
              productCode: form.productCode,
              price: form.price,
              cost: form.cost,
              maxQty: form.maxQty
            }
          }
          // 开始提交数据
          _this.spinning = true
          addPlaceAndProduct(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.productSn = undefined
      this.form.productCode = undefined
      this.form.price = ''
      this.form.cost = ''
      this.form.maxQty = ''
      if (this.$refs.productJqList) {
        this.$refs.productJqList.resetForm()
      }
      this.$refs.ruleForm.resetFields(['productSn'])
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.resetData()
        this.productName = ''
      } else {
        if (this.type == 'edit') {
          this.form.shelfPlaceCode = this.nowData.shelfPlaceCode
        }
      }
    }
  }
}
</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>
