<!-- @desc:新建库区调整 @auth:songy @time : 2024/2/20 9:38 -->
<template>
  <div class="main-div" style="position: relative;">
    <DkCollapse @on-change="changeCollapse" ref="collapse" :height="tableHeight">
      <DkPanel prop="essentialInformation">
        <!--  单据信息  -->
        <DkForm slot="content" ref="formInline" v-model="formData" :label-max-words="5">
          <!--冻结单号-->
          <DkFormItem prop="freezeNo">
            <InputPop ref="freezeNo" v-model="formData.freezeNo" :readonly="true"/>
          </DkFormItem>
          <!--客户电话-->
          <DkFormItem prop="cusPhone">
            <SelectMagnifier v-model="formData.cusPhone"
                             :telephone="true"
                             :display-text="formData.cusPhone"
                             primary-key="cusId"
                             ref="cusPhone"
                             :disabled="checkColumnIsEdit('cusPhone')"
                             :type="this.$config.MagnifierType.customer"
                             :multiple="false"
                             @ok="selectCustomer"
            >
            </SelectMagnifier>
          </DkFormItem>
          <!--客户名称-->
          <DkFormItem prop="cusName">
            <InputPop ref="cusName"  v-model="formData.cusName" :readonly="true"/>
          </DkFormItem>
          <!--业务部门-->
          <DkFormItem prop="orgId" :label="$t('orgId')" :required="true">
            <SelectMagnifier v-model="formData.orgId" :display-text="formData.orgName"
                             :type="this.$config.MagnifierType.org"
                             @ok="orgOk"
                             :disabled="checkColumnIsEdit('orgId')"
                             :multiple="false"/>
          </DkFormItem>
          <!--业务员-->
          <DkFormItem prop="staffId" :label="$t('staffId')" :required="true">
            <SelectMagnifier v-model="formData.staffId" :display-text="formData.staffName"
                             :type="this.$config.MagnifierType.staff"
                             @ok="staffOk"
                             :disabled="checkColumnIsEdit('staffId')"
                             :multiple="false"/>
          </DkFormItem>
          <!--自动解冻时间-->
          <DkFormItem prop="autoUnfreezeDate" :data-type="$config.dataType.autoUnfreezeDate">
            <DatePickerPop v-model="formData.autoUnfreezeDate"  :readonly="checkColumnIsEdit('autoUnfreezeDate')"
                           :short-cut-flag="true"/>
          </DkFormItem>
          <!--商品总数量-->
          <DkFormItem prop="sumQuantity">
            <InputNumberPop ref="sumQuantity" v-model="formData.sumQuantity" :readonly="true"/>
          </DkFormItem>
          <!--备注-->
          <DkFormItem prop="remarks">
            <InputPop ref="remarks" v-model="formData.remarks" />
          </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="invId"
                     :new-row-flag="true" :height="tableHeight"
                     :columns="this.type === this.$config.formMode.add ? freezeItemColumns: unfreezeItemColumns"
                     @changeValue="getTotals"
          ></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.freezed" :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 {
  components: {},
  mixins: [formMixin],
  data() {
    let self = this
    return {
      formData: {
        orgId: null,
        makeStaff: self.$store.state.user.id,
        staffId: null,
        staffName: null,
        staffCode: null,
        cusId:null,
        cusPhone:null,
        cusName:null,
        orgName: null,
        orgCode: null,
        remarks: null,
        sumQuantity:null,
        annexPaths: null, //附件
        goodsList: [],
        staffList: [],//业务员数组
        orgList: [],//组织机构数组
      },
      freezeItemColumns: [
        // 选择库存商品
        {
          field: 'skuModel',
          type: 'tableSelect',
          controlId: 'invId',
          dataType: self.$config.tableSelectType.inventory,
          fieldUpdate: self.$updateColumns.freezeChooseIvt,
          searchDetailFlag: true
        },
        {
          field: 'skuName',
          type: 'tableSelect',
          controlId: 'invId',
          dataType: self.$config.tableSelectType.inventory,
          fieldUpdate: self.$updateColumns.freezeChooseIvt,
          searchDetailFlag: true
        },
        {
          field: 'skuCode',
          type: 'disabled',
        },
        {
          field: 'whName',
          type: 'disabled',
        },
        //库存数量
        {
          field: 'invQty',
          type: 'disabled',
        },
        //可用数量
        {
          field: 'usableQty',
          type: 'disabled',
        },

        //冻结数量
        {
          field: 'freezeQty',
          type: 'number',
          sum: true,
          formula: {
            box: ['freezeQty/packBox', 'freezeQty', 'packBox'],
            piece: ['freezeQty%packBox', 'freezeQty', 'packBox'],
          },
        },
        //包装
        {
          field: 'packBox',
          type: 'disabled',
        },
        //箱
        {
          field: 'box',
          type: 'number',
          digits: 0,
          sum: true,
          formula: {
            freezeQty: ['box*packBox+piece', 'box', 'packBox','piece'],
          },
        },
        //片
        {
          field: 'piece',
          type: 'number',
          digits: 0,
          sum: true,
          formula: {
            freezeQty: ['box*packBox+piece', 'piece','box','packBox'],
            box: ['(box*packBox+piece)%packBox', 'piece', 'box' , 'packBox','packBox'],
          },
        },
        //备注
        {
          field: 'remarks', type: 'text', equal: true,
        },
      ],
      unfreezeItemColumns: [
      {
        field: 'skuModel',
        type: 'disabled',
      },
      {
        field: 'skuName',
        type: 'disabled',
      },
      {
        field: 'skuCode',
        type: 'disabled',
      },
      // 选择库存商品
      {
        field: 'whName',
        type: 'disabled',
      },
      //冻结数量
      {
        field: 'freezeQty',
        type: 'disabled',
      },
      //解冻数量
      {
        field: 'unfreezeQty',
        type: 'number',
        digits: 0,
        sum: true,
        formula: {
          surplusQty: ['freezeQty-unfreezeQty', 'freezeQty', 'unfreezeQty'],
          box: ['unfreezeQty/packBox', 'unfreezeQty', 'packBox'],
          piece: ['unfreezeQty%packBox', 'unfreezeQty', 'packBox'],
        },
      },
        //包装
        {
          field: 'packBox',
          type: 'disabled',
        },
        //箱
        {
          field: 'box',
          type: 'number',
          digits: 0,
          sum: true,
          formula: {
            unfreezeQty: ['box*packBox+piece', 'box', 'packBox','piece'],
            surplusQty: ['freezeQty-(box*packBox+piece)', 'freezeQty', 'box', 'packBox','piece'],
          },
        },
        //片
        {
          field: 'piece',
          type: 'number',
          digits: 0,
          sum: true,
          formula: {
            unfreezeQty: ['box*packBox+piece', 'piece','box','packBox'],
            box: ['(box*packBox+piece)%packBox', 'piece', 'box' , 'packBox','packBox'],
            surplusQty: ['freezeQty-(box*packBox+piece)', 'freezeQty', 'box', 'packBox','piece'],
          },
        },
      //剩余冻结数量
      {
        field: 'surplusQty',
        type: 'disabled',
      },
      //备注
      {
        field: 'remarks', type: 'text', equal: true,
      },
    ],

    }
  },

  methods: {
    /**
     * @desc   : 获取客户相关
     * @author : 宋扬
     * @date   : 2024/3/14 15:05
     */
    selectCustomer(val) {
      if (val && val.length > 0) {
        this.formData.cusId = val[0].cusId
        this.formData.cusCode = val[0].cusCode
        this.formData.cusName = val[0].cusName
      }
    },
    /**
     * @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/14 16:36
     */
    getTotals(field, row, rowIndex) {
      let table = this.$refs['goodsTable'].getTableDataFilter();
      let sumQuantity = 0;  // 合计数量
      if (table && table.length > 0) {
        table.forEach(row => {
          if (row["freezeQty"]) {
            sumQuantity += parseFloat(row["freezeQty"]);
          }
        })
        this.formData.sumQuantity = sumQuantity;
      }
    },

    /**
     * @desc   : 赋值冻结实体信息
     * @author : 宋扬
     * @date   : 2024/3/18 10:57
     */
    setParams() {
        let table = this.$refs['goodsTable'].getTableDataFilter();
        // 新建时赋值数据
        if (this.type === this.$config.formMode.add) {
          table.forEach((it, index) => {
            // 赋值商品顺序列
            this.$set(it, 'itemIndex', index + 1);
            // 赋值剩余冻结量
            this.$set(it, 'surplusQty', it["freezeQty"]);
            // 赋值解冻量
            this.$set(it, 'unfreezeQty', 0);
          });
        }
        // 赋值冻结明细信息
        this.formData.itemList = table;
        // 赋值冻结主表信息
        this.params = {...this.formData}
    },

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

    /**
     * @desc   : 通过id查询
     * @author : 宋扬
     * @date   : 2024/3/19 15:27
     */
    detail(id) {
      return this.excuteNoParam(this.$service.freezeService, this.$service.freezeService.getFreezeForUpdate, [id],false);
    },

    /**
     * @desc   : 解冻时页面赋值
     * @author : 宋扬
     * @date   : 2024/3/19 16:09
     */
    setValuesByEdit(data) {
      if (data) {
        this.formData = data["data"];
        this.formData.itemList = data["dataItem"];
      }
    },

    /**
     * @desc   : 判断列是否可以编辑
     * @author : 宋扬
     * @date   : 2024/3/21 16:25
     */
    checkColumnIsEdit(field) {
      if (this.type === this.$config.formMode.unFreeze) {
        return true;
      }
    },

    /**
     * @desc   : 校验数据
     * @author : 宋扬
     * @date   : 2024/4/7 11:32
     */
    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.freezeQty > 0 && (!it.freezeQty || it.freezeQty === '' || it.freezeQty===0)){
          this.$Message.warning(this.$t('W_042', {'param': this.$t('freezeQty')}))
          return false;
        } else if (this.type === this.$config.formMode.add && it.usableQty<it.freezeQty){
          this.$Message.warning(this.$t('W_008', {'param1': this.$t('freezeQty')},{'param2':this.$t('usableQty')}))
          return false;
        }else if(this.type === this.$config.formMode.unFreeze && it.unfreezeQty<it.surplusQty ){
          this.$Message.warning(this.$t('W_008', {'param1': this.$t('unfreezeQty')},{'param2':this.$t('surplusQty')}))
          return false;
        }
      }
      return true;
    }
  },
  /**
   * @desc   : 在实例创建完成后被立即同步调用
   * @author : 宋扬
   * @date   : 2024/3/16 16:45
   */
  created() {
    this.focusItem = 'skuModel';    // 给第一个组件赋值，可以在混入中自动去聚焦
    this.primaryKey = 'freezeId'  // 设置主键Id
  },
}
</script>
