<template>
  <!-- 管理-业务审核-点价 -->
  <div class="business-review-dj"
       :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 #pricetype="{ text }">
          <a>{{ getPriceTypeName(text) }}</a>
        </template>

        <template #applystatus="{ text }">
          <a>{{ getApplyStatusName(text) }}</a>
        </template>

        <template #contracttype="{ text }">
          <a>{{ getContractTypeName(text) }}</a>
        </template>

        <template #contractstatus="{ text }">
          <a>{{ getContractStatusName(text) }}</a>
        </template>

        <template #buyusernameOrsellusername="{ record }">
          <a>{{ getAnalogueName(record.contracttype, record.buyusername, record.sellusername) }}</a>
        </template>
      </a-table>
    </contextMenu>
    <!-- 点价记录详情 -->
    <SomepriceDetail :selectedRow="selectedRow"
                     @refresh="getTableData" />
    <!-- 点价审核 -->
    <SomepriceAudit :selectedRow="selectedRow"
                    @refresh="getTableData" />
    <!-- 撤销点价登记 -->
    <SomepriceCancel :selectedRow="selectedRow"
                     @refresh="getTableData" />
  </div>
</template>

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

import { Filter, SomepriceDetail, SomepriceAudit, SomepriceCancel } from '../../components';
import { queryTableList, QryBusinessDjRsp } from './setup';
import { getPriceTypeName, getContractStatusName, getContractTypeName, getApplyStatusName } from '@/common/constants/enumsName';
import { getAnalogueName } from '@/views/information/spot-contract/setup';
import { Column, ColumnType } from '@/common/setup/table';
import { ref } from 'vue';

export default defineComponent({
    name: 'business-review-dj',
    components: {
        contextMenu,
        Filter,
        BtnList,
        SomepriceDetail,
        SomepriceAudit,
        SomepriceCancel,
    },
    setup() {
        // 表头数据
        const { columns, registerColumn, updateColumn } = getTableColumns();
        // 表格事件
        const { expandedRowKeys, selectedRow, Rowclick } = getTableEvent<QryBusinessDjRsp>({});
        // 表格操作按钮列表
        const { commonBtn, forDataBtn } = getBtnList('business_review_someprice', true);
        // 表格列表数据
        const { loading, tableList, queryTable } = queryTableList({});
        function handleBtn(applystatus: number) {
            if (applystatus !== 1) {
                // 1:待审核
                const item = forDataBtn.value.find((e) => e.lable === '详情');
                if (item) {
                    return ref([item]);
                }
            }
            return forDataBtn;
        }
        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_dj', ['contracttype', 'contractno', 'deliverygoodsname'], (e: Column, item: ColumnType, filtered: any) => {
                if (e.columnfield === 'buyusernameOrsellusername') {
                    item.onFilter = (value: string, record: QryBusinessDjRsp) => {
                        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,
            handleBtn,
            forDataBtn,
            loading,
            tableList,
            updateColumn,
            getPriceTypeName,
            getApplyStatusName,
            getContractTypeName,
            getContractStatusName,
            getAnalogueName,
            getTableData,
        };
    },
});
</script>

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

