<!-- 交易数据 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" :loading="loading" />
        </template>
        <!-- 表格数据 -->
        <app-table :data="dataList" v-model:columns="tableColumns" :loading="loading">
            <!-- 日期 -->
            <template #reckondate="{ value }">
                {{ formatDate(value, 'YYYY-MM-DD') }}
            </template>
            <!-- 市场 -->
            <template #marketid="{ value }">
                {{ getTHJMarketName(value) }}
            </template>
            <!-- 吨数 -->
            <template #tradeqty="{ row }">
                {{ `${row.buytradeqty}/${row.selltradeqty}` }}
            </template>
            <!-- 笔数 -->
            <template #tradelot="{ row }">
                {{ `${row.buytradelot}/${row.selltradelot}` }}
            </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 { formatDate } from '@/filters'
import { Market, getTHJMarketList, getTHJMarketName } from '@/constants/market'
import { useDataFilter } from '@/hooks/datatable'
import { useRequest } from '@/hooks/request'
import { queryTHJTradeData } from '@/services/api/goods'
import AppTable from '@pc/components/base/table/index.vue'
import AppFilter from '@pc/components/base/table-filter/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'

const { filterOptons, getQueryParams } = useDataFilter<Model.THJTradeDataReq>()
const { loading, dataList, total, pageIndex, pageSize, run } = useRequest(queryTHJTradeData, {
    params: {
        pagesize: 20,
        marketid: Market.THJ,
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = shallowRef<Model.TableColumn[]>([
    { prop: 'reckondate', label: '日期' },
    { prop: 'marketid', label: '市场' },
    { prop: 'wrstandardname', label: '商品' },
    { prop: 'accountname', label: '下单好友' },
    { prop: 'tradeqty', label: '吨数(买/卖)' },
    { prop: 'tradelot', label: '笔数(买/卖)' },
])

filterOptons.selectList = [
    {
        label: '市场',
        key: 'marketid',
        locked: true,
        selectedValue: Market.THJ,
        options: getTHJMarketList(),
    },
]

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

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

const onRefresh = () => {
    getQueryParams((qs) => run(qs))
}
</script>