<template>
  <!-- 管理 - 财务审核 - 款项 -->
  <div class="business-review-js"
       :loading="loading">
    <Filter @search="updateColumn">
      <BtnList :btnList="commonBtn" />
    </Filter>
    <contextMenu :contextMenuList="forDataBtn">
      <a-table :columns="columns"
               class="topTable"
               :pagination="false"
               :expandedRowKeys="expandedRowKeys"
               :customRow="Rowclick"
               rowKey="key"
               :data-source="tableList">
        <!-- 额外的展开行 -->
        <template #expandedRowRender="{ record }">
          <BtnList :btnList="record.btnList" />
        </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>
    <!-- 点价记录详情 -->
    <FundsDetail :selectedRow="selectedRow"
                 @refresh="getTableData" />
    <!-- 交收审核 -->
    <FundsAudit :selectedRow="selectedRow"
                @refresh="getTableData" />
    <!-- 撤销点价登记 -->
    <FundsCancel :selectedRow="selectedRow"
                 @refresh="getTableData" />
  </div>
</template>

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

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

import { queryTableList, QryBussinessKxRsp } from './setup';
import { invoiceStatusName, kxtypeName, operateApplyTypeName, stateName } from '@/views/manage/finance-review/setup';
import { getContractTypeName, getApplyStatusName } from '@/common/constants/enumsName';
import { Column, ColumnType } from '@/common/setup/table';

export default defineComponent({
    name: 'business-review-js',
    components: {
        contextMenu,
        Filter,
        BtnList,
        FundsDetail,
        FundsAudit,
        FundsCancel,
    },
    setup() {
        // 表头数据
        const { columns, registerColumn, updateColumn } = getTableColumns();
        // 表格事件
        const { expandedRowKeys, selectedRow, Rowclick } = getTableEvent<QryBussinessKxRsp>({});
        // 表格操作按钮列表
        const { commonBtn, forDataBtn } = getBtnList('finance_review_funds', true);
        // 表格列表数据
        const { loading, tableList, queryTable } = queryTableList({});

        function getTableData() {
            // 获取列表数据
            queryTable().then(() => {
                tableList.value.forEach((el) => {
                    if (el.applystatus === 1) {
                        // 1:待审核
                        Object.assign(el, { btnList: forDataBtn.value });
                    } else {
                        const item = forDataBtn.value.find((e) => e.lable === '详情');
                        item && Object.assign(el, { btnList: [item] });
                    }
                });
            });
        }

        initData(() => {
            // 获取列表数据
            getTableData();
            // 注册表头信息 过滤
            registerColumn('table_pcweb_fincial_aduit_kx', ['contracttype', 'contractno', 'deliverygoodsname'], (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;
                }
            });
        });
        return {
            columns,

            expandedRowKeys,
            selectedRow,
            Rowclick,
            commonBtn,
            forDataBtn,
            loading,
            tableList,
            updateColumn,
            invoiceStatusName,
            getApplyStatusName,
            operateApplyTypeName,
            getContractTypeName,
            kxtypeName,
            stateName,
            getTableData,
        };
    },
});
</script>

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

