<!-- 查询管理-内部订单查询-斩仓记录查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption">
                <!-- 商品 -->
                <template #goodsId="{ item }">
                    <el-form-item :label="item.label" prop="goodsId">
                        <app-select-goods ref="goodsRef" v-model="item.value"
                            :params="{ marketId: queryParams.marketId ?? -1, goodsStatusIds: '3,6,7' }" />
                    </el-form-item>
                </template>
            </app-filter>
        </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 { useDataFilter } from '@/hooks/datatable-v2'
import { useEnum } from '@/hooks/enum'
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'
import AppSelectGoods from '@pc/components/modules/select-goods/index.vue'

const buyOrSellEnum = useEnum('buyOrSell') // 方向

const { getMarketOptions } = useMarket()
const { global: { t } } = i18n
const goodsRef = ref() // 商品组件实例

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: 'query.internal.liquidation.orderId' },
    { field: 'accountNameId', label: 'query.internal.liquidation.accountNameId' },
    { field: 'goodscode', label: 'query.internal.liquidation.goodsCodeName' },
    { field: 'buyOrSell', label: 'query.internal.liquidation.buyOrSell', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
    { field: 'orderPrice', label: 'query.internal.liquidation.orderPrice' },
    { field: 'orderQty', label: 'query.internal.liquidation.orderQty' },
    { field: 'orderAccount', label: 'query.internal.liquidation.orderAccount' },
    { field: 'orderTime', label: 'query.internal.liquidation.orderTime' },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.CommissionSheetReq>({
    filters: [
        {
            field: 'marketId',
            label: t('query.internal.liquidation.marketId'),
            required: true,
            options: () => getMarketOptions([15, 97, 99], true),
            onChange: () => goodsRef.value.onReset()
        },
        {
            field: 'goodsId',
            label: t('query.internal.liquidation.goodsId'),
            placeholder: t('query.internal.liquidation.tips1'),
        },
        {
            field: 'buyOrSell',
            label: t('query.internal.liquidation.buyOrSell'),
            options: () => buyOrSellEnum.getEnumOptions()
        },
        {
            field: 'accountId',
            label: t('query.internal.liquidation.accountId'),
        },
        {
            field: 'orderId',
            label: t('query.internal.liquidation.orderId'),
        },
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => onReset() }
    ]
})

// 处理请求参数
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)
}

const onReset = () => {
    resetFilters()
    goodsRef.value.onReset()
}
</script>