/*
 *@description:应收管理-费用结算
 *@author: hudw
 *@date: 2023-05-04 12:12:51
*/

<template>
  <div class="pages">
    <searchBar :fromItems="searchItems" ref="searchBar" :title="'应收管理-' + $route.meta.title" :options="options" @submit="searchSubmit"></searchBar>
    <auditModal :visible.sync="inMoneyVisible" :st="detailsState" :recordList="selectData" :options="options" :formExtendedField="formExtendedField" @ok="ok" title="生成结算单" />
    <!-- :setMainColumns="loadingBillColumnsFN" -->

    <mainTable
      ref="mainTable"
      :rowKey="'id'"
      title="派车单信息"
      :url="mainUrl"
      :options="options"
      :activeID="mainId"
      :tableExtendedField="tableExtendedField"
      @selectChange="selectChange"
      :operation="{ width: 200 }"
      @view="handleView"
      @clickRow="handleRowClick"
    >
      <p-button slot="extra" type="primary" :disabled="mainsSelectedRowKeys.length === 0" @click.stop="handleUpdate(selectData)">费用结算</p-button>
      <a-space slot="operation" slot-scope="slotProps">
        <a href="javascript:;" @click.stop="handleUpdate(slotProps.record)">费用结算</a>
      </a-space>
    </mainTable>
    <p-card :bordered="false" style="margin-top: 10px" :bodyStyle="{ minHeight: '300px' }">
      <a-tabs style="background-color: #fff" v-model="tabsKey">
        <a-tab-pane :key="0" tab="运单信息">
          <childrenTable ref="childrenTable_0" :options="options" :url="childrenShipBillTableUrl" :isShow="tabsKey === 0" :columns="shipbillTableColumns" :activeID="childrenId" :mainRowKey="mainId" />
        </a-tab-pane>
        <a-tab-pane :key="1" tab="商品信息">
          <childrenTable ref="childrenTable_1" :options="options" :url="childrenTableInfoUrl" :columns="goodsTableColumns" :isShow="tabsKey === 1" :activeID="childrenId" :mainRowKey="mainId" />
        </a-tab-pane>
        <a-tab-pane :key="2" tab="包裹信息">
          <childrenTable ref="childrenTable_2" :options="options" :url="packTableInfoUrl" :columns="parcelTableColumns" :isShow="tabsKey === 2" :activeID="childrenId" :mainRowKey="mainId" />
        </a-tab-pane>
        <a-tab-pane :key="3" tab="审批信息">
          <approveTable ref="childrenTable_3" :options="options" :url="auditTableInfoUrl"  :isShow="tabsKey === 3" :activeID="childrenId" />
        </a-tab-pane>
        <a-tab-pane :key="4" tab="费用信息">
          <childrenTable ref="childrenTable_4" :options="options" :url="adjustTableInfoUrl" :columns="feeColumns" :isShow="tabsKey === 4" :activeID="childrenId" :mainRowKey="mainId" />
        </a-tab-pane>
        <a-tab-pane :key="5" tab="操作日志"> <childrenTable ref="childrenTable_5" :url="logTableInfoUrl" :columns="loglTableColumns" :options="options" :isShow="tabsKey === 5" /></a-tab-pane>
      </a-tabs>
    </p-card>
  </div>
</template>
<script>
import useMixin from '@/views/BMS/$common/mixins/useMixin.js';
import dictionary from '@/views/BMS/$common/mixins/dictionary.js';
import auditModal from './auditModal/index.auditModal.vue';

import { logTableInfoUrl, childrenShipBillTableUrl, auditTableInfoUrl, packTableInfoUrl, childrenTableInfoUrl, adjustTableInfoUrl, mainTableInfoUrl, updateSubmit } from './api.js';

import { resultExtendedField } from '@/blocks/utils';
import feeColumns from './js/feeColumns';
import shipbillTableConfig from './js/shipbillTableConfig.js';
import goodsTableConfig from './js/goodsTableConfig.js';
import parcelTableConfig from './js/parcelTableConfig.js';
import logTableConfig from './js/logTableConfig.js';
import { loadingBill } from '@/views/BMS/$common/model/searchBar/formConfig.js';
import mainTable from './mainTable/index.mainTable.vue'; /**主表 */
import childrenTable from '@/views/BMS/$common/model/childrenTable/index.vue'; /**主表 */
import approveTable from '@/views/BMS/$common/model/childrenTable/approveTable.vue'; /** */

export default {
  mixins: [useMixin, dictionary],
  components: { mainTable, childrenTable, approveTable, auditModal },
  data() {
    const shipbillTableColumns = shipbillTableConfig(this);
    const goodsTableColumns = goodsTableConfig(this);
    const parcelTableColumns = parcelTableConfig(this);
    const loglTableColumns = logTableConfig(this);
    return {
      childrenShipBillTableUrl,
      auditTableInfoUrl,
      packTableInfoUrl,
      adjustTableInfoUrl,
      childrenTableInfoUrl,
      loglTableColumns,
      parcelTableColumns,
      goodsTableColumns,
      shipbillTableColumns,
      searchItems: loadingBill,
      loading: false,
      logTableInfoUrl,
      feeColumns: feeColumns(this),
      inMoneyVisible: false,
      tabsKey: 0,
      mainUrl: mainTableInfoUrl,
      selectMainArr: [],
      detailData: null,
      modalData: null,
      selectData:[],
      detailsState: 0 /**0=不显示,1=查看,2=新建,3=修改,4=复制 */,

      tableExtendedField: [],
      formExtendedField: [],
    };
  },
  watch: {
    tabsKey(value) {
      if (this.mainCurrent) {
        this.handleRowClick(null, this.mainId, this.mainCurrent);
      }
    },
  },
  mounted() {
    let _this = this;
    resultExtendedField({ extendedObjectCode: '20' }).then((data) => {
      if (data) {
        _this.tableExtendedField = data.table;
        _this.formExtendedField = data.form;
      }
    });
  },
  methods: {
    handleUpdate(data) {

      this.inMoneyVisible = true;
      if (Array.isArray(data)) {
        this.selectData = data

      } else {
        this.selectData = [data];

      }
      this.detailsState = 3;
    },
    selectChange(selectKey, selectData) {
      this.mainsSelectedRowKeys=selectKey
      this.selectData = selectData
    },
    handleView(data) {
      this.inMoneyVisible = true;
      this.selectData = [data];
      this.detailsState = 1;
    },

    handleRowClick(event, key, data) {
      this.mainId = key;

      if (key) {
        this.mainCurrent = data;
        let id = data.id;

        this.$nextTick(() => {
          for (let i = 0; i < 5; i++) {
            let ref = this.$refs['childrenTable_' + i];
            if (ref) {
              if (this.tabsKey === i) {
                ref.reload({ data: { id } });
              } else {
                ref.dataSource = [];
              }
            }
          }
        });
      } else {
        this.currentMainData = null;
      }
    },

    loadingBillRemoteBefore(vnode, res) {
      const { feeIds } = this.mainCurrent;
      res.data.includeFeeIdList = feeIds;
      delete res.data.id;
    },
    childrenTableClear() {
      this.$nextTick(() => {
        let table = this.$refs['childrenTable_' + this.tabsKey];
        if (table) {
          table.dataSource = [];
        }
      });
    },
    ok(data, clear) {
      updateSubmit(this, data, clear);
    },
  },
};
</script>
<style lang='less'>
</style>
