<template>
  <a-modal
    centered
    class="productUniversalEdit-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="productUniversalEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="产品编码" prop="productSn">
            <productCodeList id="productUniversalEdit-productSn" ref="productCodeList" @change="productCodeChange" />
          </a-form-model-item>
          <a-form-model-item label="产品名称">
            {{ productName || '--' }}
          </a-form-model-item>
          <a-form-model-item label="通用产品编码" prop="productCommonSn">
            <productCodeList id="productUniversalEdit-productCommonSn" ref="productUniversalCodeList" @change="productUniversalCodeChange" />
          </a-form-model-item>
          <a-form-model-item label="通用产品名称">
            {{ productCommonName || '--' }}
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button type="primary" id="productUniversalEdit-save" @click="handleSave">保存</a-button>
          <a-button id="productUniversalEdit-cancel" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
        </div>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
// 组件
import { VSelect } from '@/components'
import productCodeList from '@/views/common/productCodeList.vue'
// 接口
import { productUniversalCodeSave, productUniversalCodeDetail } from '@/api/productUniversalCode'
export default {
  name: 'ProductLevelEditModal',
  components: { VSelect, productCodeList },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemId: {// 通用产品id
      type: [Number, String],
      default: ''
    }
  },
  computed: {
    // 弹窗标题
    modalTit () {
      return (this.itemId ? '编辑' : '新增') + '通用产品'
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      // 表单布局
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 16 }
      },
      form: {
        productCode: undefined, // 产品列表
        productSn: undefined, // 产品sn
        productCommonCode: undefined, // 通用产品编码
        productCommonSn: undefined// 通用产品sn
      },
      // 表单验证规则
      rules: {
        productSn: [
          { required: true, message: '请选择产品编码', trigger: 'change' }
        ],
        productCommonSn: [
          { required: true, message: '请选择通用产品编码', trigger: 'change' }
        ]
      },
      productName: '', // 产品名称
      productCommonName: ''// 通用产品名称
    }
  },
  methods: {
    // 产品编码 change
    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
    },
    // 通用产品编码 change
    productUniversalCodeChange (val) {
      this.form.productCommonSn = val.key || undefined
      this.form.productCommonCode = val.row && val.row.code ? val.row.code : undefined
      this.productCommonName = val.row && val.row.name ? val.row.name : undefined
    },
    // 获取详情数据
    getDetail () {
      productUniversalCodeDetail({ id: this.itemId }).then(res => {
        if (res.status == 200) {
          this.form.productSn = res.data.productSn || undefined
          this.form.productCode = res.data.productCode || undefined
          this.form.productCommonSn = res.data.productCommonSn || undefined
          this.form.productCommonCode = res.data.productCommonCode || undefined
          this.productName = res.data.product.name || ''
          this.productCommonName = res.data.productCommont.name || ''
          this.$refs.productCodeList.setData({ key: this.form.productSn, label: this.form.productCode })
          this.$refs.productUniversalCodeList.setData({ key: this.form.productCommonSn, label: this.form.productCommonCode })
        }
      })
    },
    // 保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(_this.form))
          params.id = _this.itemId ? this.itemId : undefined
          _this.spinning = true
          productUniversalCodeSave(params).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              setTimeout(() => {
                _this.$emit('ok')
                _this.isShow = false
                _this.spinning = false
              }, 1000)
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.ruleForm.resetFields()
        this.productName = ''
        this.productCommonName = ''
        this.$refs.productCodeList.resetForm()
        this.$refs.productUniversalCodeList.resetForm()
      }
    },
    itemId (newValue, oldValue) {
      if (this.isShow && newValue) {
        this.getDetail()
      }
    }
  }
}
</script>

<style lang="less">
  .productUniversalEdit-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 30px;
    }
  }
</style>
