<template>
  <a-modal
    centered
    class="shelfSet-bindProduct-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancle="isShow=false"
    :width="600">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="shelfSet-bindProduct-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="货位号">{{ nowData&&nowData.shelfPlaceCode || '--' }}</a-form-model-item>
        <a-form-model-item :label="type == 'replace' ? '原绑定产品':'绑定产品'" v-if="type != 'bind'">
          {{ productInfo&&productInfo.productCode }}
          <p style="margin: 0;">{{ productInfo&&productInfo.productName || '--' }}</p>
        </a-form-model-item>
        <a-form-model-item label="绑定产品" prop="productSn" v-if="type == 'bind'">
          <productJqList ref="productJqList" :params="{customerSn:nowData&&nowData.customerSn}" @change="productJqChange"></productJqList>
        </a-form-model-item>
        <a-form-model-item label="新绑定产品" prop="productSn" v-if="type == 'replace'">
          <productJqList ref="productJqList" :params="{customerSn:nowData&&nowData.customerSn}" @change="productJqChange"></productJqList>
          <span v-if="form.productSn">{{ productName || '--' }}</span>
        </a-form-model-item>
        <a-form-model-item label="销售价" prop="price">
          <a-row>
            <a-col :span="20">
              <a-input-number
                id="shelfSet-bindProduct-price"
                v-model="form.price"
                :precision="2"
                :min="0.01"
                :max="999999"
                placeholder="请输入正数（最多允许两位小数）"
                suffix="元"
                style="width: 100%;display: inline-block;" />
            </a-col>
            <a-col :span="4"><span style="margin-left: 10px;">元</span></a-col>
          </a-row>
        </a-form-model-item>
        <a-form-model-item label="结算价" prop="cost">
          <a-row>
            <a-col :span="20">
              <a-input-number
                id="shelfSet-bindProduct-cost"
                v-model="form.cost"
                :precision="2"
                :min="0"
                :max="999999"
                placeholder="请输入正数（最多允许两位小数）"
                suffix="元"
                style="width: 100%;display: inline-block;" />
            </a-col>
            <a-col :span="4"><span style="margin-left: 10px;">元</span></a-col>
          </a-row>
        </a-form-model-item>
        <a-form-model-item label="最大库容" prop="maxQty">
          <a-input-number
            id="shelfSet-bindProduct-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-bindProduct-modal-save" @click="handleSave">保存</a-button>
        <a-button id="shelfSet-bindProduct-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 { shelfProductSave, shelfProductDetail } from '@/api/shelf'
export default {
  name: 'ShelfSetBasicInfoModal',
  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: {
        productSn: undefined,
        productCode: undefined,
        price: '',
        cost: '',
        maxQty: ''
      },
      rules: {
        productSn: [{ required: true, message: '请选择绑定产品', trigger: 'change' }],
        price: [{ required: true, message: '请输入销售价', trigger: 'blur' }],
        cost: [{ required: true, message: '请输入结算价', trigger: 'blur' }],
        maxQty: [{ required: true, message: '请输入最大库容', trigger: 'blur' }]
      },
      productName: '',
      productInfo: null
    }
  },
  computed: {
    modalTit () {
      let title = ''
      if (this.type == 'edit') {
        title = '修改信息'
      } else if (this.type == 'bind') {
        title = '绑定产品'
      } else if (this.type == 'replace') {
        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 || ''
    },
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          form.shelfPlaceSn = _this.nowData && _this.nowData.shelfPlaceSn
          if (this.type != 'bind') { // 修改信息、更换产品
            form.shelfProductSn = _this.nowData && _this.nowData.shelfProductApiEntity && _this.nowData.shelfProductApiEntity.shelfProductSn
          }
          if (this.type == 'replace') { // 更换产品
            form.updateProductFlag = 1
          }
          _this.spinning = true
          shelfProductSave(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 {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 获取产品信息
    getProductInfo () {
      shelfProductDetail({ shelfPlaceSn: this.nowData && this.nowData.shelfPlaceSn }).then(res => {
        if (res.status == 200 && res.data) {
          this.productInfo = res.data
          if (this.type == 'edit') {
            this.form.productSn = res.data.productSn
            this.form.productCode = res.data.productCode
          }
          this.form.price = res.data.price
          this.form.cost = res.data.cost
          this.form.maxQty = res.data.maxQty
        } else {
          this.productInfo = null
          this.$refs.ruleForm.resetFields()
        }
      })
    },
    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 = ''
        this.productInfo = null
      } else {
        if (this.type != 'bind') { // 修改信息、更换产品
          this.getProductInfo()
        }
      }
    }
  }
}
</script>

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