<!-- 询价消息-买方询价 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" :loading="loading" />
        </template>
        <!-- 表格数据 -->
        <app-table :data="dataList" v-model:columns="tableColumns" :loading="loading">
            <!-- 价格 -->
            <template #price="{ row }">
                {{ row.zscurrencytypedisplayunit + formatDecimal(row.price) }}
            </template>
            <!-- 克拉单价 -->
            <template #priceper="{ row }">
                {{ row.zscurrencytypedisplayunit + formatDecimal(row.priceper) }}
            </template>
            <!-- 我的出价 -->
            <template #applyprice="{ row }">
                {{ row.zscurrencytypedisplayunit + formatDecimal(row.applyprice) }}
            </template>
            <!-- 申请状态 -->
            <template #applystatus="{ value }">
                {{ getApplyStatusName(value) }}
            </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 { ElMessage } from 'element-plus'
import { formatDecimal } from '@/filters'
import { useDataFilter } from '@/hooks/datatable'
import { useBuyBargain } 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 } = useBuyBargain()
const { tableColumns } = useComposeTable<Ermcp.MyBargainApplyRsp>({ rowKey: 'wrtradeorderid', columnKey: 'bargain_buy' })
const { filterOptons, getQueryParams } = useDataFilter<Ermcp.MyBargainApplyReq>()

// “待确认”有操作 ”同意“、”拒绝“
const handleOperateButtons = (row: Ermcp.MyBargainApplyRsp) => {
    if (row.applystatus === ApplyStatus.Pending) {
        return ['bargain_buy_agree', 'bargain_buy_refuse']
    }
    return []
}

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

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

filterOptons.selectList = [
    {
        label: '状态',
        key: 'applystatus',
        selectedValue: 1,
        options: [
            { label: '全部', value: 0 },
            { label: '待确认', value: 1 }
        ],
    },
]

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

onRefresh()
</script>