<template>
    <div>
        <el-dialog
          title="商品拣货位"
          v-if="goodsFormVisible"
          :visible.sync="goodsFormVisible"
          :before-close="close"
          width="66%">
          <el-form
            inline
            ref="goodsForm"
            label-position="top"
            :model="formData"
            :rules="rules">
            <div>
              <el-form-item
                label="父件件编码"
                prop="parentSkuCode">
                <PublicQuery
                title="父件编码"
                single
                :rowData.sync="parentSkuData"
                cfgKey="kitWmsKitParentSkuCode"
                baseUrl="/kit_web/kitweb"
                :valueField.sync="formData.parentSkuCode"
                :textField.sync="formData.parentSkuCode"
              ></PublicQuery>
              </el-form-item>
              <el-form-item
                label="父件名称"
                prop="parentSkuName">
                <el-input v-model="formData.parentSkuName" disabled></el-input>
              </el-form-item>
              <el-form-item
                label="包装规格"
                prop="packDesc">
                <PublicQuery
                title="包装规格"
                single
                disabled
                cfgKey="kitWmsPackage"
                baseUrl="/kit_web/kitweb"
                :valueField.sync="formData.packDesc"
                :textField.sync="formData.packDesc"
              ></PublicQuery>
              </el-form-item>
              <el-form-item
                label="包装单位"
                prop="uomDesc">
                <PublicQuery
                title="包装单位"
                single
                :rowData.sync="uomData"
                cfgKey="kitWmsUom"
                baseUrl="/kit_web/kitweb"
                :valueField.sync="formData.uomDesc"
                :textField.sync="formData.uomDesc"
              ></PublicQuery>
              </el-form-item>
              <el-form-item
                label="预加工数"
                prop="qtyPlanUom">
                <el-input v-model="formData.qtyPlanUom" @input="qtyChange"></el-input>
              </el-form-item>
              <el-form-item
                label="预加工数EA"
                prop="qtyPlanEa">
                <el-input v-model="formData.qtyPlanEa" disabled></el-input>
              </el-form-item>
              <el-form-item
                label="计划工作台"
                prop="planKitLoc">
                <PublicQuery
                title="计划工作台"
                single
                cfgKey="kitWmsLoc"
                baseUrl="/kit_web/kitweb"
                :valueField.sync="formData.planKitLoc"
                :textField.sync="formData.planKitLoc"
              ></PublicQuery>
              </el-form-item>
              <el-form-item
                label="上架规则"
                prop="paRuleName">
                <PublicQuery
                title="上架规则"
                single
                cfgKey="kitWmsPaRule"
                baseUrl="/kit_web/kitweb"
                :valueField.sync="formData.paRule"
                :textField.sync="formData.paRuleName"
              ></PublicQuery>
              </el-form-item>
              <el-form-item
                label="物流单号"
                prop="logisticNo">
                <el-input v-model="formData.logisticNo" disabled></el-input>
              </el-form-item>
              <el-form-item
                label="物流单行号"
                prop="logisticLineNo">
                <el-input v-model="formData.logisticLineNo  "></el-input>
              </el-form-item>
              <div v-if="formData.parentSkuCode">
                <hr>
                <LotAttrs
                  :formData="formData"
                  :queryParams="lotAttrsParams"
                ></LotAttrs>
              </div>
            </div>
          </el-form>
          <div class="more-query-btn">
            <el-button v-auth="50300030101" type="primary" @click="handleConfirm">确认</el-button>
            <el-button v-auth="50300030101" type="primary" @click="handleConfirmAdd">确认并添加</el-button>
          </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
  data () {
    return {
      formData: {},
      defaultForm: {
        status: '00',
        isCreateSub: 'N',
        qtyKitUom: '0',
        qtyKitEa: '0'
      },
      lotAttrsParams: {},
      parentSkuData: {},
      uomData: {},
      rules: {
        parentSkuCode: [{ required: true, message: '父件编码必填', trigger: 'change' }],
        packDesc: [{ required: true, message: '包装规格必填', trigger: 'change' }],
        uomDesc: [{ required: true, message: '包装单位必填', trigger: 'change' }],
        qtyPlanUom: [{ required: true, message: '预加工数必填', trigger: 'change' }],
      }
    }
  },
  props: {
    goodsFormVisible: Boolean,
    currentRow: {
      type: Object,
      default: () => ({})
    },
    rowIndex: '',
    editBool: Boolean,
    lineNo: Number
  },
  mounted () {

  },
  methods: {
    close () {
      this.$emit('close')
      this.$refs.goodsForm.resetFields()
    },

    // 数量改变
    qtyChange () {
      this.formData.qtyPlanEa = this.formData.qtyPlanUom
    },

    lineNum (i) {
      if (i < 1000) {
        return ('0000' + i).substr(-4)
      }
    },

    // 确认
    handleConfirm () {
      let data = Object.assign({ isChecked: 'false' }, this.formData, this.defaultForm)
      if (this.editBool) {
        this.$emit('editOK', data, this.rowIndex)
        this.$refs.goodsForm.resetFields()
      } else {
        this.formData.parentLineNo = this.lineNum(this.lineNo)
        let data = Object.assign({ isChecked: 'false' }, this.formData, this.defaultForm)
        this.$refs.goodsForm.validate((valid) => {
          if (valid) {
            this.$emit('handleConfirm', data)
            this.$refs.goodsForm.resetFields()
          }
        })
      }
    },

    // 确认并添加
    handleConfirmAdd () {
      let data = Object.assign({ isChecked: 'false' }, this.formData, this.defaultForm)
      if (this.editBool) {
        this.$emit('editOK', data, this.rowIndex)
        this.$refs.goodsForm.resetFields()
      } else {
        this.formData.parentLineNo = this.lineNum(this.lineNo)
        let data = Object.assign({ isChecked: 'false' }, this.formData, this.defaultForm)
        this.$refs.goodsForm.validate((valid) => {
          if (valid) {
            this.$emit('handleConfirmAdd', data)
            this.$refs.goodsForm.resetFields()
          }
        })
      }
    },
  },

  watch: {
    parentSkuData: {
      handler (item) {
        this.formData.parentSkuName = item.parentSkuName
        this.formData.packDesc = item.packDesc
        this.formData.uomDesc = item.uomDesc
        this.formData.packCode = item.packCode
        this.formData.uom = item.uom
        this.formData.uomQty = item.uomQty
        this.formData.formatCode = item.formatCode
      },
      deep: true
    },
    uomData: {
      handler (item) {
        this.formData.uom = item.cdprUnitLevel
      },
      deep: true
    },
    lineNo: {
      handler (val) {
        console.log(this.lineNum(val))
      },
      deep: true
    },
    currentRow: {
      handler (item) {
        this.formData = { ...item }
      },
      deep: true
    }
  }
}
</script>
