<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 #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>
    </contextMenu>
    <!-- 点价记录详情 -->
    <SettlementDetail :selectedRow="selectedRow"
                      @refresh="getTableData" />
    <!-- 交收审核 -->
    <SettlementAudit :selectedRow="selectedRow"
                     @refresh="getTableData" />
    <!-- 撤销点价登记 -->
    <SettlementCancel :selectedRow="selectedRow"
                      @refresh="getTableData" />
  </div>
</template>

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

import { SettlementDetail, SettlementAudit, SettlementCancel, Filter } from '../../components';

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

export default defineComponent({
    name: 'business-review-js',
    components: {
        contextMenu,
        Filter,
        BtnList,
        SettlementDetail,
        SettlementAudit,
        SettlementCancel,
    },
    setup() {
        // 表头数据
        const { columns, registerColumn, updateColumn } = getTableColumns();
        // 表格事件
        const { expandedRowKeys, selectedRow, Rowclick } = getTableEvent<QryBussinessJsRsp>({});
        // 表格操作按钮列表
        const { commonBtn, forDataBtn } = getBtnList('business_review_settlement', 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_business_aduit_js', ['contracttype', 'contractno', 'deliverygoodsname'], (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;
                }
            });
        });

        return {
            columns,
            getAnalogueName,
            expandedRowKeys,
            selectedRow,
            Rowclick,
            commonBtn,
            forDataBtn,
            loading,
            tableList,
            updateColumn,
            getPriceTypeName,
            getContractTypeName,
            getApplyStatusName,
            invoiceStatusName,
            getTableData,
        };
    },
});
</script>

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

