<!-- @desc:销售订单新建  @auth:付斌  @time:2024/02/20 15:15 -->
<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"  style="width:100%">
          <!--客户电话-->
          <DkFormItem prop="cusPhone" :required="true">
            <SelectMagnifier v-model="formData.cusPhone" ref="cusPhone" :telephone="true"
                             primary-key="objectId" :multiple="false"
                             :type="this.$config.MagnifierType.customer"
                             @on-input-blur="selectCustomer"
                             @ok="selectCustomer"/>
          </DkFormItem>
          <!--客户名称-->
          <DkFormItem prop="cusName">
            <InputPop ref="cusName" :disabled="true" v-model="formData.cusName"/>
          </DkFormItem>
          <!--业务部门-->
          <DkFormItem prop="orgId" :label="$t('orgId')">
            <SelectMagnifier v-model="formData.orgId" :display-text="formData.orgName"
                             :type="this.$config.MagnifierType.org"
                             :multiple="false" :disabled="true"/>
          </DkFormItem>
          <!--业务员-->
          <DkFormItem prop="staffId" :label="$t('staffId')">
            <SelectMagnifier v-model="formData.staffId" :display-text="formData.staffName"
                             :type="this.$config.MagnifierType.staff"
                             :multiple="false" :disabled="true"/>
          </DkFormItem>
          <!--收款时间-->
          <DkFormItem prop="accDate" :data-type="$config.dataType.date">
            <DatePickerPop v-model="formData.accDate" :short-cut-flag="true"/>
          </DkFormItem>
          <!--客户欠款-->
          <DkFormItem prop="receivableBalance">
            <InputNumberPop ref="receivableBalance" v-model="formData.receivableBalance" :disabled="true"/>
          </DkFormItem>
          <!--备注-->
          <DkFormItem prop="remarks" :span="12">
            <InputPop ref="remarks" v-model="formData.remarks"
                      maxlength=500 textareaFlag/>
          </DkFormItem>
        </DkForm>
      </DkPanel>
      <!--客户收款-->
      <DkPanel prop="customerReceipt">
        <div id="search-cond-div-receipt-info" ref="search-cond-div-goods-info" slot="content">
          <EditTable v-if="showEditTable" ref="receiptTable" :data="formData.itemList" showFooter
                     major-field="rpId"
                     :new-row-flag="true"
                     :columns="receiptColumns"
                     controlId="macId" @on-choose="getTotals" @changeValue="getTotals"/>
        </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.recPay" :accept="$config.uploadFileConfig.acceptPicType"
                     :format="['jpg','jpeg','png']"></DkPicWall>
        </div>
      </DkPanel>
      <DkForm ref="receivableFootFormData2" v-model="receivableFootFormData2" :labelMaxWords="6" name="dk-form"
              class="form-content-class">
        <!-- 收款金额 -->
        <DkFormItem prop="sumAmtRec">
          <InputNumberPop ref="sumAmtRec" v-model="receivableFootFormData2.sumAmtRec" :digits="2" :disabled="true"
                          :min="-9999999999"/>
        </DkFormItem>
      </DkForm>
    </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 {
      loading: false,
      negativeFlag: false,
      showEditTable: true,
      // 画面表单数据
      formData: {
        accDate: new Date(),
        rpType: null,
        objectId: null,//客户Id
        cusName: null,
        cusPhone: null,
        addressFull: null,              // 详细地址
        sumAmtRec: null,
        receivableBalance: null,
        remarks: null,
        annexPaths: null,
        staffId: this.$store.state.user.id,
        staffName: this.$store.state.user.staffName,
        orgId: this.$store.state.user.orgId,
        orgName: this.$store.state.user.orgName,
        makeStaff: self.$store.state.user.id,
        cpId: self.$store.state.user.cpId,
        itemList: [],
      },
      // 应收合计信息
      receivableFootFormData2: {
        sumAmtRec: 0,
      },
      receiptColumns: [
        {
          field: 'macName',
          controlId: 'macId',
          type: 'tableSelect',
          width: 200,
          dataType: self.$config.tableSelectType.moneyAccount,
          fieldUpdate: self.$updateColumns.receiptChooseMac,
          searchDetailFlag: true
        },
        // //账户余额
        // {
        //   field: 'balance',
        //   type: 'disabled',
        //   digits: 2,
        //   title: this.$t('accountBalance'),
        // },
        //收款金额
        {
          field: 'amtRec',
          type: 'number',
          digits: 2,
          negative: () => self.negativeFlag,
          sum: true,
        },
        {
          field: 'remarks', type: 'text', equal: true,
        },
      ],
    }
  },
  methods: {
    /**
     * @desc   : 加载数据
     * @author : 付斌
     * @date   : 2024-02-20 15:53
     */
    initData() {
    },
    /**
     *   @desc   : 获取客户资料并且监听每一次的客户修改
     *   @date   : 2022/5/24 15:04
     *   @author : 付斌
     */
    selectCustomer(val) {
      if (val && val.length > 0) {
        this.formData.objectId = val[0].cusId
        this.formData.cusPhone = val[0].cusPhone
        this.formData.cusName = val[0].cusName
        // this.formData.orgId = val[0].orgId
        // this.formData.orgName = val[0].orgName
        // this.formData.staffId = val[0].staffId
        // this.formData.staffName = val[0].staffName
        this.formData.addressFull = val[0].addressFull

        // 查询客户可用余额
        let params = {
          'objectId': this.formData.objectId
        }
        this.excute(this.$service.accountService, this.$service.accountService.selectByCond, params).then(res => {
          if (res.code === this.$config.SUCCESS_CODE && res.data.list.length > 0) {
            this.formData.receivableBalance = res.data.list[0].receivableBalance
          }
        })
      }
    },
    /**
     *   @desc   : 计算合计
     *   @date   : 2022/5/30 15:31
     *   @author : 付斌
     */
    getTotals(field, row, rowIndex) {
      let table = this.$refs['receiptTable'].tableData
      if (table && table.length > 0) {
        this.formData.sumAmtRec = table.sum('amtRec');
        this.receivableFootFormData2.sumAmtRec = this.formData.sumAmtRec;
      }
    },
    /**
     * @desc   : 通过id查询
     * @author : 付斌
     * @date   : 2022/03/04 13:17
     */
    detail(id) {
      return this.excuteNoParam(this.$service.cusReceiptService, this.$service.cusReceiptService.getRpForUpdate, [id]);
    },
    /**
     * @desc   : 编辑页面赋值
     * @author : 付斌
     * @date   : 2024-03-03 9:33
     */
    setValuesByEdit(data) {
      if (data) {
        this.formData = data["data"];
        this.formData.itemList = data["dataItem"];

        // 如果是退款，editTable要输入负数
        if (this.formData.rpType === this.$config.rpType.tuiShouKuan) {
          this.showEditTable = false
          this.negativeFlag = true;
          setTimeout(() => {
            this.showEditTable = true;
          }, 50)
        }
      }
    },
    /**
     * @desc   : 校验数据
     * @author : 付斌
     * @date   : 2024-03-11 14:37
     */
    validData() {
      let itemTable = this.$refs['receiptTable'].getTableDataFilter();
      if (!itemTable || itemTable.length == 0) {
        this.$Message.warning(this.$t('W_012'))
        return false;
      }
      for (let it of itemTable) {
        if (!it.amtRec || it.amtRec === '') {
          this.$Message.warning(this.$t('W_042', {'param': '收款金额不能'}))
          return false;
        }
      }
      return true;
    },
    /**
     * @desc   : 给参数赋值
     * @author : 付斌
     * @date   : 2023/6/25 14:31
     */
    setParams(arg) {
      if (this.type === this.$config.formMode.receipt) {
        this.formData.rpType = this.$config.rpType.shouKuan
      } else if (this.type === this.$config.formMode.refund) {
        this.formData.rpType = this.$config.rpType.tuiShouKuan
      }
      this.formData.accDate = new Date(this.formData.accDate).toDateStr();
      // 修改新增的表数据
      let itemTable = this.$refs['receiptTable'].getTableDataFilter();
      this.formData.itemList = itemTable
      this.params = {...this.formData}
      // itemTable.forEach((it, index) => {
      //   this.$set(it, 'residueAmt', it["amtRec"]);
      //   Object.assign(it, it, this.formData)
      // });
      // this.params = {"itemList": itemTable}
    },
    /**
     * @desc   : 保存数据
     * @author : 付斌
     * @date   : 2023/6/25 14:31
     */
    saveData() {
      if (this.type === this.$config.formMode.receipt) {
        return this.excute(this.$service.cusReceiptService, this.$service.cusReceiptService.insertReceipt, this.params);
      } else if (this.type === this.$config.formMode.refund) {
        return this.excute(this.$service.cusReceiptService, this.$service.cusReceiptService.insertRefund, this.params)
      } else if (this.type === this.$config.formMode.edit) {
        return this.excute(this.$service.cusReceiptService, this.$service.cusReceiptService.update, this.params)
      }
    },
    /**
     * @desc   :新建完毕后清空窗体
     * @author : 付斌
     * @date   : 2024-02-27 10:33
     */
    clear() {
      let self = this
      this.formData = {
        accDate: new Date(),
        rpType: null,
        objectId: null,//客户Id
        cusName: null,
        cusPhone: null,
        addressFull: null,              // 详细地址
        sumAmtRec: null,
        receivableBalance: null,
        remarks: null,
        annexPaths: null,
        staffId: this.$store.state.user.id,
        staffName: this.$store.state.user.staffName,
        orgId: this.$store.state.user.orgId,
        orgName: this.$store.state.user.orgName,
        makeStaff: self.$store.state.user.id,
        cpId: self.$store.state.user.cpId,
        itemList: [],
      }
    },
  }
  ,
  /**
   * @desc   : 在实例创建完成后被立即同步调用
   * @author : 付斌
   * @date   : 2022/3/3 10:32
   */
  created() {
    this.focusItem = 'rpId';    // 给第一个组件赋值，可以在混入中自动去聚焦
    this.primaryKey = 'rpId'  // 设置主键Id

    // 如果是退款，editTable要输入负数
    if (this.type === this.$config.formMode.refund) {
      this.showEditTable = false
      this.negativeFlag = true;
      setTimeout(() => {
        this.showEditTable = true;
      }, 50)
    }
  }
}
</script>
