<template>
  <!-- 卖履约 -->
  <section>
    <a-table :columns="columns"
             class="srcollYTable expandLeftTable"
             :scroll="{ x: '100%', y: '190px' }"
             :pagination="false"
             :loading="loading"
             :expandedRowKeys="expandedRowKeys"
             :customRow="Rowclick"
             :expandIcon="expandIcon"
             :expandIconAsCell="false"
             rowKey="key"
             :data-source="tableList">
      <!-- 额外的展开行 -->
      <template v-if="btnList.length"
                #expandedRowRender="{ record }">
        <BtnList :btnList="handleBtn(btnList, record)"
                 :record="record"
                 class="btn-list-sticky"
                 @click="openComponent" />
      </template>
      <!-- 履约类型 -->
      <template #performancetype="{ record }">
        <a>{{ getPerformanceTypeName(record.performancetype, record.haswr)}}</a>
      </template>

      <!-- 付款方式 -->
      <template #paymenttype="{ record }">
        <a>{{ getPaymentTypeName(record.paymenttype) }}</a>
      </template>

      <!-- 履约状态 -->
      <template #performancestatus="{ record }">
        <a>{{ getPerformanceStatusName(record.performancestatus) }}</a>
      </template>

      <!-- 当前步骤 -->
      <template #curstepname="{ record }">
        <a>{{ record.curstepname }}</a>
      </template>
    </a-table>
    <component :is="componentId"
               v-if="componentId"
               :buyOrSell="BuyOrSell.sell"
               :selectedRow="selectedRow"
               @cancel="closeComponent"></component>
  </section>
</template>

<script lang="ts">
import { enumOrderComponents } from '@/common/constants/enumOrderComponents';
import { queryTableList, BtnList, defineAsyncComponent, defineComponent, ModalEnum } from '@/common/export/commonTable';
import { QueryPerformancePlan, queryWrTradeDetail } from '@/services/go/wrtrade';
import { QueryPerformancePlanReq, WrPerformancePlan } from '@/services/go/wrtrade/interface';
import { getRecordItemTab } from '@/common/setup/order/orderData';
import { handleComposeOrderTable } from '@/common/setup/table/compose';
import { expandIcon } from '@/common/setup/table/clolumn';
import { ComposeOrderTableParam } from '@/common/setup/table/interface';
import { BtnListType } from '@/common/components/btnList/interface';
import { BuyOrSell } from '@/common/constants/enumCommon';
import { getPaymentTypeName, getPerformanceStatusName, getPerformanceTypeName } from '@/common/constants/enumsName';

export default defineComponent({
    name: enumOrderComponents.performance_information_sell_performance,
    components: {
        BtnList,
        [ModalEnum.performance_information_sell_performance_pay]: defineAsyncComponent(() => import('../components/pay/index.vue')),
        [ModalEnum.performance_information_sell_performance_extension]: defineAsyncComponent(() => import('../components/extension/index.vue')),
        [ModalEnum.performance_information_sell_performance_breach_contract]: defineAsyncComponent(() => import('../components/breach_contract/index.vue')),
        [ModalEnum.performance_information_sell_performance_detail]: defineAsyncComponent(() => import('../components/detail/index.vue')),
    },
    setup() {
        // 表格列表数据
        const { loading, tableList, queryTable } = queryTableList<WrPerformancePlan>();
        // 获取列表数据
        const queryTableAction = () => {
            const param: QueryPerformancePlanReq = {
                buyorsell: 1,
            };
            queryTable(QueryPerformancePlan, param);
        };
        // 表格通用逻辑
        const param: ComposeOrderTableParam = {
            queryFn: queryTableAction,
            tableName: 'table_pcweb_spot_trade_bottom_performance_info_sale',
            recordList: getRecordItemTab(),
        };
        function handleBtn(btnList: BtnListType[], record: WrPerformancePlan): BtnListType[] {
            //  performancestatus: number;//履约状态 - 1:初始化 2:正常 3:处理错误 4:违约待处理 5:违约处理中 6:完成 7.违约已完成 8:释放冻结失败 9:超时待处理 1number;
            const { curstepname, performancestatus } = record;
            let result: ModalEnum[] = [];
            if (performancestatus === 1) {
                result = [ModalEnum.performance_information_sell_performance_breach_contract];
            } else if (performancestatus === 2) {
                if (curstepname === '卖方发货') {
                    result = [ModalEnum.performance_information_sell_performance_extension, ModalEnum.performance_information_sell_performance_breach_contract, ModalEnum.performance_information_sell_performance_detail];
                } else if (curstepname === '买方支付') {
                    result = [ModalEnum.performance_information_sell_performance_extension, ModalEnum.performance_information_sell_performance_breach_contract, ModalEnum.performance_information_sell_performance_detail];
                } else if (curstepname === '卖方发票') {
                    result = [ModalEnum.performance_information_sell_performance_extension, ModalEnum.performance_information_sell_performance_breach_contract, ModalEnum.performance_information_sell_performance_detail];
                } else if (curstepname === '仓单转移') {
                    result = [ModalEnum.performance_information_sell_performance_extension, ModalEnum.performance_information_sell_performance_breach_contract, ModalEnum.performance_information_sell_performance_detail];
                }
            }
            return btnList.filter((e) => result.includes(e.code as ModalEnum));
        }
        return {
            ...handleComposeOrderTable<WrPerformancePlan>(param),
            loading,
            tableList,
            getPerformanceTypeName,
            getPaymentTypeName,
            getPerformanceStatusName,
            expandIcon,
            handleBtn,
            BuyOrSell,
        };
    },
});
</script>

<style lang="less">
</style>;
