<template>
  <!-- 管理 - 财务审核 - 款项 -->
  <div class="business-review-js"
       :loading="loading">
    <Filter @search="updateColumn">
    </Filter>
    <a-table :columns="columns"
             class="srcollYTable"
             :scroll="{ x: '100%', y: 'calc(100vh - 163px)' }"
             :pagination="false"
             :expandedRowKeys="expandedRowKeys"
             :customRow="Rowclick"
             rowKey="key"
             :data-source="tableList">
      <!-- 额外的展开行 -->
      <template #expandedRowRender="{ record }">
        <BtnList :btnList="handlePermissionBtn(firstBtn, record)"
                 :record="record"
                 class="btn-list-sticky"
                 @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>
    <!-- 右键 -->
    <contextMenu :contextMenu="contextMenu"
                 @cancel="closeContext"
                 :list="handlePermissionBtn(firstBtn, selectedRow)">
    </contextMenu>
    <component :is="componentId"
               v-if="componentId"
               :selectedRow="selectedRow"
               @cancel="closeComponent"></component>
  </div>
</template>

<script lang="ts">
import { BtnList, contextMenu, defineAsyncComponent, defineComponent, ModalEnum, handleComposeTable, ComposeTableParam } from '@/common/export/commonTable';
import { initData, getTableColumns, getTableEvent, getBtnList } from '@/common/export/table';

import { Filter, FundsAudit, FundsCancel, FundsDetail } from '../../components';

import { queryTableList, QryBussinessKxRsp } from './setup';
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';

const Detail = defineAsyncComponent(() => import('../../components/fundsDetail/index.vue'));
const Audit = defineAsyncComponent(() => import('../../components/fundsAudit/index.vue'));
const Cancel = defineAsyncComponent(() => import('../../components/fundsCancel/index.vue'));

export default defineComponent({
    name: 'business-review-js',
    components: {
        contextMenu,
        Filter,
        BtnList,
        [ModalEnum.detail]: Detail,
        [ModalEnum.finance_review_funds_audit]: Audit,
        [ModalEnum.finance_review_funds_cancel]: Cancel,
    },
    setup() {
        // 表格列表数据
        const { loading, tableList, queryTable } = queryTableList({});
        // 获取列表数据
        const queryTableAction = () => queryTable();
        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 param: ComposeTableParam = {
            queryFn: queryTableAction,
            menuType: EnumRouterName.finance_review_funds,
            tableName: 'table_pcweb_fincial_aduit_kx',
            tableFilterKey: ['contracttype', 'contractno', 'deliverygoodsname'],
            tableFilterCB: filtrFn,
            isDetail: true,
        };
        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,
        };
    },
});
</script>

<style lang="less">
.business-review-js {
}
</style
>;

