<template>
<el-dialog
  class="creator"
  title="库存调整"
  width="70%"
  :visible="showCreator"
  @close="close"
>
  <div class="creator__body">
    <el-form
      inline
      ref="form"
      label-position="top"
      :model="formData"
      :rules="rules"
      :disabled="mode === 'detail'"
    >
      <div class="creator__info creator__info--base">
        <!--共用查询框-->
        <template v-for="item in publicQueries">
          <el-form-item
            :key="item.valueField"
            :label="item.label"
            :prop="item.valueField"
          >
            <PublicQuery
              single
              :disabled="item.disabled"
              :title="item.label"
              :baseUrl="baseUrl"
              :cfgKey="item.cfgKey"
              :valueField.sync="formData[item.valueField]"
              :textField.sync="item.textField === undefined ? false : formData[item.textField]"
              :rowData.sync="item.rowData"
              :before="item.before && item.before"
              @change="val => { item.onChange && item.onChange(val) }"
            ></PublicQuery>
          </el-form-item>
        </template>

        <el-form-item label="调整方式" prop="adMode">
          <el-select v-model="formData.adMode">
            <el-option label="增加件数" value="A"></el-option>
            <el-option label="减少件数" value="R"></el-option>
          </el-select>
        </el-form-item>

        <!--输入框-->
        <template v-for="item in inputs">
          <el-form-item
            :key="item.prop"
            :label="item.label"
            :prop="item.prop"
          >
            <el-input v-model="formData[item.prop]" :disabled="item.disabled" @input.native="val => { item.onInput && item.onInput() }"></el-input>
          </el-form-item>
        </template>
        <el-form-item label="是否序列号管理" prop="isSerial">
          <el-checkbox v-model="formData.isSerial" disabled></el-checkbox>
        </el-form-item>
      </div>
      <div class="creator__info creator__info--senior">
        <el-collapse v-model="activeNames">
          <el-collapse-item title="批次属性" name="1">
            <LotAttrs
              disabled
              :formData="formData"
              :queryParams="lotAttrsParams"
            ></LotAttrs>
          </el-collapse-item>
        </el-collapse>
      </div>
    </el-form>
  </div>
  <div class="creator__foot">
    <template v-if="mode === 'create' || (mode === 'edit' && detailData.status === '00')">
      <el-button v-auth="50300030206" type="primary" @click="save">保存</el-button>
      <el-button v-auth="50300030207" type="primary" @click="saveAndCreate">保存并新增</el-button>
      <el-button v-auth="50300030208" type="primary" @click="trigger = true">按库存获取</el-button>
    </template>
    <template>
      <el-button v-if="mainFormData.auditStatus !== '00' && (mainFormData.status === '00' || mainFormData.status === '10') && detailData.status === '00'" type="primary" @click="executeAd">执行调整</el-button>
    </template>
  </div>
  <PublicQuery
    style="display: none;"
    single
    title="商品批次库位库存"
    cfgKey="invWmsInvLotLoc"
    :baseUrl="baseUrl"
    :valueField.sync="formData.skuCode"
    :textField.sync="formData.skuCode"
    :rowData.sync="wholeSkuObj"
    :before="getByInvBefore"
    :trigger="trigger"
  ></PublicQuery>
</el-dialog>
</template>

<script>
export default {
  props: ['showCreator', 'ownerCode', 'adNo', 'mode', 'detailData', 'mainFormData'],
  watch: {
    detailData: {
      immediate: true,
      handler (val) {
        this.formData = {
          ownerCode: this.ownerCode,
          adNo: this.adNo,
          isSerial: 'N',
          ...val
        }
        this.lotAttrsParams = {
          ownerCode: val.ownerCode,
          skuCode: val.skuCode,
        }
      }
    },
    // 监听商品选择器对象变化
    skuObj (val) {
      // 包装规格
      this.formData.packCode = val.packCode
      this.formData.packDesc = val.packDesc
      // 包装单位
      this.formData.cdprDesc = val.rcvUomDesc
      this.formData.uom = val.rcvUom
      this.lotAttrsParams = {
        ownerCode: val.ownerCode,
        skuCode: val.skuCode,
      }
    },
    // 按库存获取商品对象
    wholeSkuObj (val) {
      Object.assign(this.formData, val)
      this.formData.packDesc = val.packName
      this.formData.adWeight = val.grossWeight.toFixed(2) // 调整重量
      this.formData.uom = val.printUom // 包装单位
      this.lotAttrsParams = {
        ownerCode: val.ownerCode,
        skuCode: val.skuCode,
      }
    }
  },
  computed: {
    skuObj () {
      return this.publicQueries[0].rowData
    }
  },
  created () {
  },
  mounted () {
  },

  data () {
    return {
      lotAttrsParams: {}, // 批次属性查询条件
      // 时间框
      datePickers: [
        { label: '生产日期', prop: 'lotAtt01' },
        { label: '失效日期', prop: 'lotAtt02' },
        { label: '入库日期', prop: 'lotAtt03' },
      ],
      // 共用查询框
      publicQueries: [
        { label: '商品', valueField: 'skuCode', textField: 'skuName', cfgKey: 'invWmsSkus', rowData: {}, before: this.skuBefore },
        { label: '批次号', valueField: 'lotNum', textField: 'lotNum', cfgKey: 'invWmsLotNum', rowData: {}, before: this.lotBefore, onChange: this.lotNumChange },
        { label: '库位', valueField: 'locCode', textField: 'locCode', cfgKey: 'invWmsLoc', rowData: {} },
        { label: '包装规格', valueField: 'packCode', textField: 'packDesc', cfgKey: 'invWmsPackage', rowData: {}, disabled: true },
        { label: '包装单位', valueField: 'uom', textField: 'cdprDesc', cfgKey: 'invWmsUom', rowData: {}, before: this.cdprBefore },
      ],
      // 输入框
      inputs: [
        { label: '跟踪号', prop: 'traceId' },
        { label: '调整数', prop: 'qtyAdUom', onInput: this.qtyAdUomChange },
        { label: '调整数EA', prop: 'qtyAdEa', disabled: true },
        { label: '调整重量', prop: 'adWeight' },
      ],
      // 表单对象
      formData: {},
      activeNames: ['1'],
      baseUrl: '/inv_web/invweb', // 多条件基础URL
      // 表单验证
      rules: {
        // formData写在 computed 就不能识别校验, 导致校验始终不通过
        skuCode: [{ required: true, message: '必填', trigger: 'change' }],
        locCode: [{ required: true, message: '必填', trigger: 'change' }],
        traceId: [{ required: true, message: '必填', trigger: 'change' }],
        packCode: [{ required: true, message: '必填', trigger: 'change' }],
        uom: [{ required: true, message: '必填', trigger: 'change' }],
        adMode: [{ required: true, message: '必填', trigger: 'change' }],
        qtyAdUom: [{ required: true, message: '必填', trigger: 'change' }],
        // lotAtt05: [{ required: true, message: '必填', trigger: 'change' }],
        // lotAtt06: [{ required: true, message: '必填', trigger: 'change' }],
        // lotAtt07: [{ required: true, message: '必填', trigger: 'change' }],
        // lotAtt08: [{ required: true, message: '必填', trigger: 'change' }],
      },
      showAdDetailList: false, // 显示调整明细
      trigger: false, // 触发隐藏弹窗的时机
      wholeSkuObj: {}, // 库存获取商品对象
    }
  },
  methods: {
    // 重置, 若只使用form的resetField, 当表单为prop传过来详情时会重置为详情数据而不是置空
    reset () {
      this.$refs.form.resetFields() // 单纯是为了清空校验
      this.formData = {
        ownerCode: this.ownerCode,
        adNo: this.adNo,
        isSerial: 'N',
      }
    },
    // 保存
    save () {
      this.saveAndCreate('needClose')
    },
    // 关闭
    close () {
      this.$emit('close')
    },
    // 保存并新增
    saveAndCreate (needClose) {
      this.$refs.form.validate(valid => {
        if (valid) {
          const url = this.$API.inventory.adjustment.saveAdDetailEntity
          const param = { wmAdDetailEntity: JSON.stringify(this.formData), wmAdSerialEntitys: JSON.stringify([]) }
          this.$https.post(url, param)
            .then(data => {
              // this.$message.success('操作成功')
              this.$emit('createDone') // 通知父组件新增完毕, 刷新列表
              this.reset()
              needClose === 'needClose' && this.close() // 保存按钮需要关闭弹窗
            })
        }
      })
    },
    // 执行调整
    executeAd () {
      const url = this.$API.inventory.adjustment.executeAdDetail
      const param = {
        adNo: this.detailData.adNo,
        lineNos: this.detailData.lineNo
      }
      this.$https.post(url, param)
        .then(() => {
          this.$emit('createDone')
          this.reset()
          this.close()
        })
    },
    /**
     * 调用共用查询框前操作
     * */
    // 商品
    skuBefore () {
      return {
        queryParams: {
          ownerCodeParam: this.ownerCode
        }
      }
    },
    // 批次号
    lotBefore () {
      if (!this.formData.skuCode) {
        this.$message.error('请先选择商品')
      } else {
        return {
          queryParams: {
            ownerCode: this.ownerCode,
            skuCode: this.formData.skuCode,
          }
        }
      }
    },
    // 包装单位
    cdprBefore () {
      return {
        queryParams: {
          packCode: this.formData.packCode,
        }
      }
    },
    // 按库存获取
    getByInvBefore () {
      this.trigger = false
      return {
        queryParams: {
          ownerCodeParam: this.ownerCode
        }
      }
    },

    // 输入框变化
    // 调整数变化
    qtyAdUomChange () {
      this.formData.qtyAdEa = (+this.formData.qtyAdUom).toFixed(2)
    },
    // 批次号变化
    lotNumChange (obj) {
      for (let i = 1; i <= 16; ++i) {
        const ii = i.toString().length === 1 ? `0${i}` : `${i}`
        this.$set(this.formData, `lotAtt${ii}`, obj[`lotAtt${ii}`])
      }
    },
  },
}
</script>

<style lang="scss" scoped>
  .el-collapse-item {
    margin-bottom: 20px;
  }
  .creator__foot {
    margin-top: 20px;
  }
</style>
