<template>
  <!-- 管理-业务审核-交收 -->
  <mtp-table-scroll>
    <template #header>
      <Filter @search="updateColumn"></Filter>
    </template>
    <template #default="{ scroll }">
      <a-table :columns="columns" class="srcollYTable" :pagination="false" :expandedRowKeys="expandedRowKeys" :customRow="Rowclick" :rowKey="(record,index)=>index" :scroll="scroll" :data-source="tableList">
        <!-- 额外的展开行 -->
        <template #expandedRowRender="{ record }">
          <mtp-table-button class="btn-list-sticky" :buttons="handlePermissionBtn(buttons, record)" :record="record" @click="openComponent" />
        </template>
        <template #operateapplytype="{ text }">
          <a>{{ getPriceTypeName(text) }}</a>
        </template>
        <template #contracttype="{ text }">
          <a>{{ getContractTypeName(text) }}</a>
        </template>
        <template #applystatus="{ text }">
          <a>{{ getApplyStatusName(text) }}</a>
        </template>
        <template #buyusernameOrsellusername="{ record }">
          <a>{{ getAnalogueName(record.contracttype, record.buyusername, record.sellusername) }}</a>
        </template>
        <template #addmargin||decmargin="{ record }">
          <a>{{ record.addmargin !== 0 && record.addmargin !== undefined ? '+' + record.addmargin : record.decmargin === 0 || record.decmargin === undefined ? '' : '-' + record.decmargin }} </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 { MtpTableButton, contextMenu, defineAsyncComponent, defineComponent, handleComposeTable, ComposeTableParam, queryTableList } from '@/common/export/commonTable';
import { Filter } from '../../components';
import { invoiceStatusName } from '@/views/manage/finance-review/setup';
import { Column, ColumnType } from '@/common/setup/table';
import { getPriceTypeName, getContractTypeName, getApplyStatusName } from '@/common/constants/enumsName';
import { getAnalogueName } from '@/views/information/spot-contract/setup';
import { EnumRouterName } from '@/common/constants/enumRouterName';
import { BtnListType } from '@/common/components/btnList/interface';
import { getTableButton } from '@/common/setup/table/button';
import { QueryBusinessJs } from '@/services/go/ermcp/business-review';
import { QryBussinessJsRsp } from '@/services/go/ermcp/business-review/interface';
import { useRoute } from 'vue-router';

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

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

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

    // 获取表头名称
    const getTableName = () => {
      switch (routeName) {
        // 交收待审核
        case 'settlement_checkpending':
          return 'table_pcweb_business_aduit_js';
        // 交收未审核
        case 'settlement_performance':
          return 'table_pcweb_business_aduit_yjs';
      }
    };

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

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

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

    return {
      ...handleComposeTable<QryBussinessJsRsp>(param),
      handlePermissionBtn,
      getAnalogueName,
      loading,
      tableList,
      getPriceTypeName,
      getContractTypeName,
      getApplyStatusName,
      invoiceStatusName,
      buttons,
    };
  },
});
</script>
