<!-- 挂牌大厅-我的出售 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" :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="tableColumns" :loading="loading">
            <template #header>
                <app-auth-operation :menus="['trade_sale_add']" @closed="onRefresh" />
            </template>
            <!-- 价格 -->
            <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 { useSaleOrder } from '@/business/trade/list'
import { useComposeTable } from '@pc/components/base/table'
import AppTable from '@pc/components/base/table/index.vue'
import AppAuthOperation from '@pc/components/modules/auth-operation/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppFilter from '@pc/components/base/table-filter/index.vue'

const { loading, dataList, total, pageIndex, pageSize, filterData, categoryList, getSaleOrderList } = useSaleOrder()
const { tableColumns } = useComposeTable<Ermcp.MySellOrderRsp>({ rowKey: 'wrtradeorderid', columnKey: 'listing_saleorder' })
const { filterOptons, getQueryParams } = useDataFilter<Ermcp.MySellOrderReq>()

// 3:委托成功 7:部成 -  状态有撤销
const handleOperateButtons = (row: Ermcp.MySellOrderRsp) => {
    if ([3, 7].includes(row.wrtradeorderstatus)) {
        return ['trade_sale_details', 'trade_sale_cancel']
    }
    return ['trade_sale_details']
}

const onSearch = (clear = false) => {
    if (clear) {
        filterData.value = {
            history: false,
            date: []
        }
    }
    getQueryParams((qs) => {
        pageIndex.value = 1
        getSaleOrderList(qs)
    }, clear)
}

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

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

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

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

onRefresh()
</script>