<!-- 订单管理-我的履约 -->
<template>
    <app-view>
        <template #header>
            <app-filter v-bind="{ inputList, buttonList }" :loading="loading">
                <template #after>
                    <el-checkbox label="历史" v-model="filterData.history" />
                    <el-date-picker type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
                        value-format="YYYYMMDD" v-model="filterData.date" v-if="filterData.history"
                        style="margin-left:12px" />
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" v-model:columns="columns" :loading="loading">
            <!-- 已付金额 -->
            <template #paidamount="{ row }">
                {{ row.buyorsell === BuyOrSell.Buy ? row.buypaidamount : row.sellreceivedamount }}
            </template>
            <!-- 履约剩余冻结 -->
            <template #freezeamountremain="{ row }">
                {{ row.buyorsell === BuyOrSell.Buy ? row.buyerfreezeamountremain : row.sellerfreezeamountremain }}
            </template>
            <!-- 履约状态 -->
            <template #performancestatus="{ value }">
                {{ getPerformanceStatusName(value) }}
            </template>
            <!-- 履约步骤 -->
            <template #steptypeid="{ value }">
                {{ getStepTypeName(value) }}
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-auth-operation :menus="handleTableButtons(row)" :options="{ selectedRow: row }"
                    @closed="getPerformanceList" />
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="getPerformanceList" />
            </template>
        </app-table>
    </app-view>
</template>

<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import { useRoute } from 'vue-router'
import { BuyOrSell, StepType, getStepTypeName, getPerformanceStatusName } from '@/constants/order'
import { usePerformance } from '@/business/order'
import AppAuthOperation from '@pc/components/modules/auth-operation/index.vue'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppFilter from '@pc/components/base/table-filter/index.vue'

const route = useRoute()

const buyorsell = (() => {
    switch (route.name) {
        case 'order_buy': {
            return BuyOrSell.Buy
        }
        case 'order_sell': {
            return BuyOrSell.Sell
        }
    }
})()

const { loading, dataList, columns, total, pageIndex, pageSize, filterData, inputList, buttonList, getPerformanceList } = usePerformance(buyorsell)

columns.value.forEach((e) => {
    if (e.prop === 'paidamount') {
        if (buyorsell === BuyOrSell.Buy) {
            e.label = '已付金额'
        } else {
            e.label = '已收金额'
        }
    }
})

const handleTableButtons = (item: Ermcp.MyPerformancRsp) => {
    if ([StepType.Payment, StepType.Pickup, StepType.ConfirmGoods, StepType.ConfirmInvoice].includes(item.steptypeid)) {
        return ['details', 'manual', 'delay', 'contracted', 'edit']
    }
    return ['details', 'contracted', 'edit']
}

getPerformanceList().catch((err) => ElMessage.error(err))
</script>