<template>
  <!-- 管理 - 财务审核 - 款项 -->
  <mtp-table-scroll>
    <template #header>
      <Filter @search="updateColumn"></Filter>
    </template>
    <template #default="{ scroll }">
      <a-table :columns="columns" class="srcollYTable" :scroll="scroll" :pagination="false" :expandedRowKeys="expandedRowKeys" :customRow="Rowclick" :rowKey="(record,index)=>index" :data-source="tableList">
        <!-- 额外的展开行 -->
        <template #expandedRowRender="{ record }">
          <mtp-table-button class="btn-list-sticky" :buttons="handlePermissionBtn(buttons, record)" :record="record" @click="openComponent" />
        </template>
        <!-- 现货合同类型 -->
        <template #contractstatus="{ record }">
          <a>{{ getApplyStatusName(record.applystatus) }}</a>
        </template>
        <!-- 合同类型-->
        <template #contracttype="{ record }">
          <a>{{ getContractTypeName(record.contracttype) }}</a>
        </template>
        <!-- 状态 -->
        <template #applystatus="{ text }">
          <a>{{ getApplyStatusName(text) }}</a>
        </template>
        <!-- 点价类型 -->
        <template #operateapplytype="{ text }">
          <a>{{ operateApplyTypeName(text) }}</a>
        </template>
        <!-- 款项类型 -->
        <template #kxtype="{ record }">
          <a>{{ stateName(record.deductamount, record.contracttype) }}</a>
        </template>
        <!-- 对手方 -->
        <template #buyusernameOrsellusername="{ record }">
          <a>{{ record.contracttype === 1 ? record.sellusername : record.buyusername }}</a>
        </template>
        <!-- 金额 如果是退款 则使用deductamount  如果是付款则使用payamount-->
        <template #payamount="{ record }">
          <a>{{ record.deductamount === undefined || record.deductamount === 0 ? record.payamount : record.deductamount }}</a>
        </template>
      </a-table>
    </template>
  </mtp-table-scroll>
  <!-- 右键 -->
  <contextMenu :contextMenu="contextMenu" @cancel="closeContext" :list="handlePermissionBtn(buttons, selectedRow)"> </contextMenu>
  <component :is="componentId" v-if="componentId" :selectedRow="selectedRow" @cancel="closeComponent"></component>
</template>

<script lang="ts">
import MtpTableScroll from '@/common/components/tableScroll/index.vue';
import { queryTableList, MtpTableButton, contextMenu, defineAsyncComponent, defineComponent, handleComposeTable, ComposeTableParam } from '@/common/export/commonTable';
import { Filter } from '../../components';
import { invoiceStatusName, operateApplyTypeName, stateName } from '@/views/manage/finance-review/setup';
import { getContractTypeName, getApplyStatusName } from '@/common/constants/enumsName';
import { Column, ColumnType } from '@/common/setup/table';
import { EnumRouterName } from '@/common/constants/enumRouterName';
import { BtnListType } from '@/common/components/btnList/interface';
import { getTableButton } from '@/common/setup/table/button';
import { QueryBusinessKx } from '@/services/go/ermcp/finance-review';
import { QryBussinessKxRsp } from '@/services/go/ermcp/finance-review/interface';
import { useRoute } from 'vue-router';

export default defineComponent({
  name: 'business-review-js',
  components: {
    MtpTableScroll,
    contextMenu,
    Filter,
    MtpTableButton,
    detail: defineAsyncComponent(() => import('../../components/fundsDetail/index.vue')),
    check: defineAsyncComponent(() => import('../../components/fundsAudit/index.vue')),
    cancel: defineAsyncComponent(() => import('../../components/fundsCancel/index.vue')),
  },
  setup() {
    const { name: routeName } = useRoute();
    // 权限按钮
    const buttons = getTableButton();

    // 表格列表数据
    const { loading, tableList, queryTable } = queryTableList<QryBussinessKxRsp>();

    // 获取列表数据
    const queryTableAction = () => {
      switch (routeName) {
        case 'funds_checkpending':
          queryTable(QueryBusinessKx, { applystatus: '1' });
          break;
        case 'funds_performance':
          queryTable(QueryBusinessKx, { applystatus: '2,3' });
          break;
      }
    };

    const filtrFn = (e: Column, item: ColumnType, filtered: any) => {
      if (e.columnfield === 'buyusernameOrsellusername') {
        item.onFilter = (value: string, record: QryBussinessKxRsp) => {
          const { contracttype, buyusername, sellusername } = record;
          if (contracttype === 1) {
            // 采购
            return sellusername.includes(value);
          } else {
            return buyusername.includes(value);
          }
        };
        item.filteredValue = filtered.buyusernameOrsellusername || null;
      }
    };

    // 获取表头名称
    const getTableName = () => {
      switch (routeName) {
        // 业务审核-发票
        case 'funds_checkpending':
          return 'table_pcweb_fincial_aduit_kx';
        // 业务审核-发票-已审核
        case 'funds_performance':
          return 'table_pcweb_fincial_aduit_ykx';
      }
    };

    // 表格通用逻辑
    const param: ComposeTableParam = {
      queryFn: queryTableAction,
      tableName: getTableName(),
      tableFilterCB: filtrFn,
    };

    function handlePermissionBtn(btnList: BtnListType[], item: QryBussinessKxRsp) {
      if (item) {
        const { applystatus } = item;
        if (applystatus === 1) {
          return btnList;
        } else {
          return btnList.filter((e) => e.code === 'detail');
        }
      } else {
        return [];
      }
    }

    return {
      ...handleComposeTable<QryBussinessKxRsp>(param),
      loading,
      tableList,
      invoiceStatusName,
      getApplyStatusName,
      operateApplyTypeName,
      getContractTypeName,
      stateName,
      handlePermissionBtn,
      buttons,
    };
  },
});
</script>
