<!-- 查询管理-内部订单查询-斩仓记录查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" />
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['query_internal_liquidation_export'])"
                    @click="(code: string) => openComponent(code)" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons(['query_internal_liquidation_details'])"
                    @click="(code: string) => openComponent(code, row)" circle />
            </template>
            <template #footer>
                <app-pagination :total="total" v-model:page-size="pageSize" v-model:page-index="pageIndex"
                    @change="onSearch" />
            </template>
        </app-table>
        <component :is="componentMap.get(componentId)" v-bind="{ record, queryParams }" @closed="closeComponent"
            v-if="componentId" />
    </app-view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { getBuyOrSellList } from '@/constants/order'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useRequest } from '@/hooks/request'
import { useMarket } from '@/hooks/market'
import { useOperation } from '@/hooks/operation'
import { queryCommissionSheet } from '@/services/api/order'
import { i18n } from '@/stores'
import AppTable from '@pc/components/base/table/index.vue'
import AppPagination from '@pc/components/base/pagination/index.vue'
import AppOperation from '@pc/components/base/operation/index.vue'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'

const { getMarketOptions } = useMarket()
const { global: { t } } = i18n

const { componentMap, componentId, record, openComponent, closeComponent, getActionButtons } = useOperation<Model.CommissionSheetRsp>({
    onClose: () => onSearch()
})

const { dataList, total, pageSize, pageIndex, loading, run } = useRequest(queryCommissionSheet, {
    manual: true,
    params: {
        pageNum: 1,
        pageSize: 20,
        isHis: 0
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'orderId', label: '委托单号' },
    { field: 'accountNameId', label: '账户' },
    { field: 'goodsCodeName', label: '商品代码/名称' },
    { field: 'buyOrSell', label: '方向' },
    { field: 'orderPrice', label: '斩仓委托价' },
    { field: 'orderQty', label: '斩仓数量' },
    { field: 'orderAccount', label: '斩仓金额' },
    { field: 'orderTime', label: '斩仓时间' },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.CommissionSheetReq>({
    filters: [
        {
            field: 'marketId',
            label: '市场',
            required: true,
            options: () => getMarketOptions()
        },
        {
            field: 'goodsId',
            label: '商品',
            placeholder: '请输入商品ID'
        },
        {
            field: 'buyOrSell',
            label: '方向',
            options: () => getBuyOrSellList()
        },
        {
            field: 'accountId',
            label: '账户',
        },
        {
            field: 'orderId',
            label: '单号',
        },
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => resetFilters() }
    ]
})

// 处理请求参数
const processRequiredParams = () => {
    const qs = getQueryParams()
    const marketId = (qs.marketId ?? 0).toString()
    qs.tradeMode = +marketId.slice(0, 2)
    return qs
}

const onSearch = () => {
    const qs = processRequiredParams()
    run(qs)
}
</script>