<!-- 挂牌大厅-出售大厅 -->
<template>
    <app-view flex>
        <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 #fixedprice="{ row }">
                {{ row.zscurrencytypedisplayunit + formatDecimal(row.fixedprice) }}
            </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 { useLoginStore, useFavoriteStore } from '@/stores'
import { useSellOrder } from '@/business/trade/list'
import AppAuthOperation from '@pc/components/modules/auth-operation/index.vue'
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 AppFilter from '@pc/components/base/table-filter/index.vue'

const { hasFavorite } = useFavoriteStore()
const { loading, dataList, total, pageIndex, pageSize, categoryList, getSellOrderList } = useSellOrder()
const { tableColumns } = useComposeTable<Ermcp.SellOrderRsp>({ rowKey: 'wrtradeorderid', columnKey: 'listing_sellorder' })
const { filterOptons, getQueryParams } = useDataFilter<Ermcp.SellOrderReq>()
const loginStore = useLoginStore()

// 未收藏的卖委托有收藏按钮
const handleOperateButtons = (row: Ermcp.SellOrderRsp) => {
    if (hasFavorite(row.goodsno) || row.userid === loginStore.userId) {
        return ['trade_sell_details']
    }
    return ['trade_sell_details', 'trade_sell_favorite']
}

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

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

filterOptons.selectList = [
    {
        label: '商品分类',
        key: 'zscategorys',
        options: categoryList,
    },
]

filterOptons.inputList = [
    { label: '卖方', keys: ['buyusername'] },
    { label: '商品', keys: ['zsallproperties'] },
]

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

onRefresh()
</script>