<!-- @desc:新建批号调整 @auth:songy @time : 2024/2/20 9:38 -->
<template>
  <div class="main-div" style="position: relative;">
    <DkCollapse @on-change="changeCollapse" ref="collapse">
      <DkPanel prop="essentialInformation">
        <!--  单据信息  -->
        <DkForm slot="content" ref="formInline" v-model="formData">
          <!--调整单号-->
          <DkFormItem prop="transferNo">
            <InputPop ref="transferNo" v-model="formData.transferNo" :readonly="true"/>
          </DkFormItem>
          <!--业务部门-->
          <DkFormItem prop="orgId" :label="$t('transferOrg')" :required="true">
            <SelectMagnifier v-model="formData.orgId" :display-text="formData.transferOrg"
                             :type="this.$config.MagnifierType.org"
                             @ok="orgOk"
                             :multiple="false"/>
          </DkFormItem>
          <!--业务员-->
          <DkFormItem prop="staffId" :label="$t('transferStaff')"  :required="true">
            <SelectMagnifier v-model="formData.staffId" :display-text="formData.transferStaff"
                             :type="this.$config.MagnifierType.staff"
                             @ok="staffOk"
                             :multiple="false"/>
          </DkFormItem>
          <!--原仓库  先去掉主表原和目标-->
<!--          <DkFormItem prop="oWhId" :label="$t('oWhName')" v-show="this.type==='add'">
            <SelectMagnifier v-model="formData.oWhId" :display-text="formData.whName"
                             :type="this.$config.MagnifierType.warehouse"
                             @ok="oWhOk"
                             :multiple="false"/>
          </DkFormItem>-->
          <!--目标仓库-->
<!--          <DkFormItem prop="whId" :label="$t('whName')" v-show="this.type==='add'">
            <SelectMagnifier v-model="formData.whId" :display-text="formData.whName"
                             :type="this.$config.MagnifierType.warehouse"
                             @ok="whOk"
                             :multiple="false"/>
          </DkFormItem>-->
          <!--原批号-->
<!--          <DkFormItem prop="oNonStdCode" v-show="this.type==='addTogether'">
            <InputPop ref="oNonStdCode" v-model="formData.oNonStdCode"/>
          </DkFormItem>-->
          <!--目标批号-->
<!--          <DkFormItem prop="nonStdCode" v-show="this.type==='addTogether'">
            <InputPop ref="nonStdCode" v-model="formData.nonStdCode"/>
          </DkFormItem>-->
          <!--备注-->
          <DkFormItem prop="remarks">
            <InputPop v-model="formData.remarks" textareaFlag/>
          </DkFormItem>
        </DkForm>
      </DkPanel>
      <!--商品信息-->
      <DkPanel prop="goodsInformation">
        <div id="search-cond-div-goods-info" ref="search-cond-div-goods-info" slot="content">
          <EditTable ref="goodsTable" :data="formData.itemList" showFooter
                     major-field="itemId"
                     control-id="oInvId"
                     :new-row-flag="true" :height="tableHeight+65"
                     :columns="this.type === this.$config.formMode.add ? transferWhItemColumns: transferStdItemColumns"
          ></EditTable>
        </div>
      </DkPanel>
      <!--附件-->
      <DkPanel prop="files">
        <div id="search-cond-div-files-info" ref="search-cond-div-files-info" slot="content">
          <DkPicWall v-model="formData.annexPaths"
                     :table="$config.tables.transfer" :accept="$config.uploadFileConfig.acceptPicType"></DkPicWall>
        </div>
      </DkPanel>
    </DkCollapse>
    <!--      下部分按钮区域-->
    <DkSaveButton ref="saveButton" :loading="loading" @save="save(true)" @close="close"/>
  </div>
</template>

<script>

import {formMixin} from '@/mixins/form'

export default {
  mixins: [formMixin],
  data() {
    let self = this
    return {
      //目标仓库
      whList: [],
      formData: {
        transferNo:null,
        orgId: self.$store.state.user.orgId,
        orgName: self.$store.state.user.orgName,
        transferType: null,
        makeStaff: self.$store.state.user.id,
        staffId: self.$store.state.user.staffId,
        staffName: self.$store.state.user.staffName,
       /* oWhId: null,
        oWhName: null,
        whId: null,
        whName: null,
        oNonStdCode:null,
        nonStdCode:null,*/
        annexPaths: null, //附件
        remarks: null,
        itemList: [],
      },
      transferWhItemColumns: [
        // 选择库存商品
        {
          field: 'skuModel',
          type: 'tableSelect',
          controlId: 'oInvId',
          dataType: self.$config.tableSelectType.inventory,
          fieldUpdate: self.$updateColumns.transferChooseIvt,
          searchDetailFlag: true
        },
        {
          field: 'skuName',
          type: 'tableSelect',
          controlId: 'oInvId',
          dataType: self.$config.tableSelectType.inventory,
          fieldUpdate: self.$updateColumns.transferChooseIvt,
          searchDetailFlag: true
        },
        {
          field: 'skuCode',
          type: 'disabled',
        },
        //原仓库
        {
          field: 'oWhName',
          type: 'disabled',
        },
        //原库存数量
        {
          field: 'oinvQty',
          type: 'disabled',
        },
        //原库存可用数量
        {
          field: 'usableQty',
          type: 'disabled',
        },
        //目标仓库
        {
          field: 'whId',
          type: 'select',
          options: () => self.whList,
          labelKey: 'whName',
          valueKey: 'whId',
          dataType: self.$config.tableSelectType.warehouse,
          fieldUpdate: [
            {updateField: 'whName', valueFiled: 'whName'},
            {updateField: 'whId', valueFiled: 'whId'},
          ],
          searchDetailFlag: true
        },
        //调整数量
        {
          field: 'transferQty',
          type: 'number',
          digits: 0,
          /*formula: {
            intoingAmt: ['transferQty*priceInto', 'transferQty', 'priceInto'],
          },*/
        },
        //入库单价 注释掉不用输入金额 后台默认0
       /* {
          field: 'priceInto',
          title: self.$t('transferPrice'),
          type: 'number',
          digits: 0,
          formula: {
            intoingAmt: ['transferQty*priceInto', 'transferQty', 'priceInto'],
          },
        },*/
        //入库金额
       /* {
          field: 'intoingAmt',
          title: self.$t('transferAmt'),
          type: 'number',
          digits: 0,
          formulaLate:true,
          formula: {
            priceInto: ['intoingAmt/transferQty', 'transferQty', 'intoingAmt'],
          },
        },*/
        //备注
        {
          field: 'remarks', type: 'text', equal: true,
        },
      ],
      transferStdItemColumns: [
        {
          field: 'skuModel',
          type: 'tableSelect',
          controlId: 'oInvId',
          dataType: self.$config.tableSelectType.inventory,
          fieldUpdate: self.$updateColumns.transferChooseIvt,
          searchDetailFlag: true
        },
        {
          field: 'skuName',
          type: 'tableSelect',
          controlId: 'oInvId',
          dataType: self.$config.tableSelectType.inventory,
          fieldUpdate: self.$updateColumns.transferChooseIvt,
          searchDetailFlag: true
        },
        {
          field: 'skuCode',
          type: 'disabled',
        },
        //原库存数量
        {
          field: 'oinvQty',
          type: 'disabled',
        },
        //原库存可用数量
        {
          field: 'usableQty',
          type: 'disabled',
        },
        //原非标号
        {
          field: 'oNonStdCode',
          type: 'disabled',
        },
        //目非标号
        {
          field: 'nonStdCode',
          type: 'text',
        },
        //调整数量
        {
          field: 'transferQty',
          type: 'number',
          digits: 0,
          sum: true,
          formula: {
            box: ['transferQty/packBox', 'freezeQty', 'packBox'],
            piece: ['transferQty%packBox', 'freezeQty', 'packBox'],
          },
        },
        //包装
        {
          field: 'packBox',
          type: 'disabled',
        },
        //箱
        {
          field: 'box',
          type: 'number',
          digits: 0,
          sum: true,
          formula: {
            transferQty: ['box*packBox+piece', 'box', 'packBox','piece'],
          },
        },
        //片
        {
          field: 'piece',
          type: 'number',
          digits: 0,
          sum: true,
          formula: {
            transferQty: ['box*packBox+piece', 'piece','box','packBox'],
            box: ['(box*packBox+piece)%packBox', 'piece', 'box' , 'packBox','packBox'],
          },
        },
        //入库单价 注释掉不用输入金额 后台默认0
        /*{
          field: 'priceInto',
          title: self.$t('transferPrice'),
          type: 'number',
          digits: 0,
          formula: {
            intoingAmt: ['transferQty*priceInto', 'transferQty', 'priceInto'],
          },
        },*/
        //入库金额
       /* {
          field: 'intoingAmt',
          title: self.$t('transferAmt'),
          type: 'number',
          formulaLate:true,
          digits: 0,
          formula: {
            priceInto: ['intoingAmt/transferQty', 'transferQty', 'intoingAmt'],
          },
        },*/
        //备注
        {
          field: 'remarks', type: 'text', equal: true,
        },
      ],
    }
  },

  methods: {

    /**
     * @desc   : 加载数据
     * @author : 宋扬
     * @date   : 2024/4/3 15:50
     */
    initData() {
      //获取仓库信息
      this.getWhData()
    },

    /**
     * @desc   : 仓库信息
     * @author : 宋扬
     * @date   : 2024/4/3 14:06
     */
    getWhData() {
      this.excute(this.$service.commonService, this.$service.commonService.getWarehouse,{},false).then(res => {
        if (res.code === this.$config.SUCCESS_CODE) {
          this.whList = res.data
        }
      })
    },

    /**
     * @desc   : 获取部门相关
     * @author : 宋扬
     * @date   : 2024/3/14 15:03
     */
    orgOk(val) {
      if (val && val.length > 0) {
        this.formData.orgId = val[0].orgId
        this.formData.orgCode = val[0].orgCode
        this.formData.orgName = val[0].orgName
      }
    },
    /**
     *   @desc   : 获取业务员相关
     *   @author : 宋扬
     *   @date   : 2024/3/14 15:03
     */
    staffOk(val) {
      if (val && val.length > 0) {
        this.formData.staffId = val[0].staffId
        this.formData.staffCode = val[0].staffCode
        this.formData.staffName = val[0].staffName
        if (this.formData.orgId == null) {
          this.formData.orgId = val[0].orgId
          this.formData.orgCode = val[0].orgCode
          this.formData.orgName = val[0].orgName
        }
      }
    },

    /**
     * @desc   : 原仓库信息
     * @author : 宋扬
     * @date   : 2024/3/26 15:03
     */
    oWhOk(val) {
      if (val && val.length > 0) {
        this.formData.oWhId = val[0].whId
        this.formData.oWhCode = val[0].whCode
        this.formData.oWhName = val[0].whName
      }
    },

    /**
     * @desc   : 目标仓库信息
     * @author : 宋扬
     * @date   : 2024/3/26 15:03
     */
    whOk(val) {
      if (val && val.length > 0) {
        this.formData.whId = val[0].whId
        this.formData.whCode = val[0].whCode
        this.formData.whName = val[0].whName
      }
    },
    /**
     * @desc   : 赋值盘点实体信息
     * @author : 宋扬
     * @date   : 2024/3/18 10:57
     */
    setParams(val){
      let table = this.$refs['goodsTable'].getTableDataFilter();
      // 新建时赋值数据
      if (this.type === this.$config.formMode.add
        || this.type === this.$config.formMode.addTogether ) {
        table.forEach((it, index) => {
          // 赋值商品顺序列
          this.$set(it, 'itemIndex', index + 1);
        });
      }
      // 赋值调整类型
      if (this.type === this.$config.formMode.add) {
        this.formData.transferType = this.$config.transferType.whTransfer
        // 判断源仓库明细是否有多个，则清空主表上源仓库信息
       /* let firstOWhId = table[0].oWhId;
        let filtersOWhId = table.filter((it,index)=> index != 0 && it.oWhId != firstOWhId);
        if(filtersOWhId && filtersOWhId.length > 0){
          this.formData.oWhId = null;
          this.formData.oWhCode = null;
          this.formData.oWhName = null;
        }
        // 判断目标仓库明细是否有多个，则清空主表上目标仓库信息
        let firstWhId = table[0].whId;
        let filtersWhId  = table.filter((it,index)=> index != 0 && it.whId != firstWhId);
        if(filtersWhId && filtersWhId.length > 0){
          this.formData.WhId = null;
          this.formData.WhCode = null;
          this.formData.WhName = null;
        }*/
      } else if (this.type === this.$config.formMode.addTogether){
        this.formData.transferType = this.$config.transferType.nscTransfer
        // 判断源非标号明细是否有多个，则清空主表上源非标号信息
       /* let firstOnsc = table[0].oNonStdCode;
        let filtersOnsc = table.filter((it,index)=> index != 0 && !it.oNonStdCode.equal(firstOnsc) );
        if(filtersOnsc && filtersOnsc.length > 0){
          this.formData.oNonStdCode = null;
        }
        // 判断目标非标号明细是否有多个，则清空主表上目标非标号信息
        let firstnsc = table[0].nonStdCode;
        let filtersnsc  = table.filter((it,index)=> index != 0 && !it.nonStdCode.equal(firstnsc));
        if(filtersnsc && filtersnsc.length > 0){
          this.formData.nonStdCode = null;
        }*/
      }
      // 赋值盘点明细信息
      this.formData.itemList = table;
      // 赋值盘点主表信息
      this.params = {...this.formData}
    },

    /**
     * @desc   : 保存数据
     * @author : 宋扬
     * @date   : 2024/3/15 10:03
     */
    saveData() {
      if (this.type === this.$config.formMode.add ||this.type === this.$config.formMode.addTogether) {
        return this.excute(this.$service.transferService, this.$service.transferService.insert, this.params);
      }
    },

    /**
     * @desc   : 校验数据
     * @author : 宋扬
     * @date   : 2024/4/7 13:10
     */
    validData(){
      let itemTable = this.$refs['goodsTable'].getTableDataFilter();
      //明细不能为空
      if (!itemTable || itemTable.length == 0) {
        this.$Message.warning(this.$t('W_012'))
        return false;
      }
      for (let it of itemTable){
        //冻结量不能为空或零
        if (it.transferQty > 0 && (!it.transferQty || it.transferQty === '' || it.transferQty===0)){
          this.$Message.warning(this.$t('W_042', {'param': this.$t('transferQty')}))
          return false;
        }else if ( it.usableQty<it.transferQty){
          this.$Message.warning(this.$t('W_008', {'param1': this.$t('transferQty')},{'param2':this.$t('usableQty')}))
          return false;
        }
      }
      return  true;
    },



  },
  /**
   * @desc   : 在实例创建完成后被立即同步调用
   * @author : 寇珊珊
   * @date   : 2022/3/3 10:32
   */
  created() {
    this.focusItem = 'skuModel';    // 给第一个组件赋值，可以在混入中自动去聚焦
    this.primaryKey = 'transferId'  // 设置主键Id
  },
}
</script>
