<!-- 挂牌大厅-我的询价 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" :loading="loading" />
        </template>
        <!-- 表格数据 -->
        <app-table :data="dataList" v-model:columns="columns" :loading="loading">
            <!-- 申请状态 -->
            <template #applystatus="{ value }">
                {{ getApplyStatusName(value) }}
            </template>
            <!-- 价格 -->
            <template #price="{ row }">
                {{ row.zscurrencytypedisplayunit + formatDecimal(row.price) }}
            </template>
            <!-- 我的出价 -->
            <template #applyprice="{ row }">
                {{ row.zscurrencytypedisplayunit + formatDecimal(row.applyprice) }}
            </template>
            <!-- 克拉单价 -->
            <template #priceper="{ row }">
                {{ row.zscurrencytypedisplayunit + formatDecimal(row.priceper) }}
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-auth-operation type="dropdown" :menus="handleOperateButtons(row)" :options="{ selectedRow: row }"
                    @closed="onRefresh" />
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onRefresh" />
            </template>
        </app-table>
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDecimal } from '@/filters'
import { useDataFilter } from '@/hooks/datatable'
import { BuyOrSell } from '@/constants/order'
import { useBargain } from '@/business/bargain'
import { getApplyStatusName, ApplyStatus } from '@/constants/order'
import { useComposeTable } from '@pc/components/base/table'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppAuthOperation from '@pc/components/modules/auth-operation/index.vue'
import AppFilter from '@pc/components/base/table-filter/index.vue'

const { loading, dataList, total, pageIndex, pageSize, getBargainOrderList } = useBargain()
const { filterOptons, getQueryParams } = useDataFilter<(Ermcp.MyBargainApplyReq | Ermcp.MyDelistingApplyReq) & { buyorsell: number }>()

// 求购表格列
const { tableColumns: buyColumns } = useComposeTable<Ermcp.MyBargainApplyRsp>({ rowKey: 'wrtradeorderid', columnKey: 'listing_bargain_buy' })
// 出售表格列
const { tableColumns: sellColumns } = useComposeTable<Ermcp.MyDelistingApplyRsp>({ rowKey: 'ladingbillid', columnKey: 'listing_bargain_sell' })
// 默认表格列
const columns = shallowRef<Ermcp.TableColumn[]>([])

// “待确认”状态 有撤销按钮
const handleOperateButtons = (row: Ermcp.MyBargainApplyRsp | Ermcp.MyDelistingApplyRsp) => {
    if (row.applystatus === ApplyStatus.Pending) {
        return ['trade_bargain_details', 'trade_bargain_cancel']
    }
    return ['trade_bargain_details']
}

// 切换表格列
const getColumns = (buyorsell?: number) => {
    switch (buyorsell) {
        case BuyOrSell.Buy: {
            columns.value = buyColumns.value
            break
        }
        case BuyOrSell.Sell: {
            columns.value = sellColumns.value
            break
        }
        default: {
            columns.value = []
        }
    }
}

const onSearch = (clear = false) => {
    getQueryParams((qs) => {
        pageIndex.value = 1
        getColumns(qs.buyorsell)
        getBargainOrderList(qs)
    }, clear)
}

const onRefresh = () => {
    getQueryParams((qs) => {
        getColumns(qs.buyorsell)
        getBargainOrderList(qs).catch((err) => ElMessage.error(err))
    })
}

filterOptons.selectList = [
    {
        label: '询价',
        key: 'buyorsell',
        selectedValue: BuyOrSell.Buy,
        options: [
            { label: '求购申请', value: BuyOrSell.Buy },
            { label: '出售申请', value: BuyOrSell.Sell }
        ],
    },
    {
        label: '状态',
        key: 'applystatus',
        selectedValue: 1,
        options: [
            { label: '全部', value: 0 },
            { label: '待确认', value: 1 }
        ],
    },
]

filterOptons.buttonList = [
    { lable: '查询', className: 'el-button--primary', onClick: () => onSearch() }
]

onRefresh()
</script>