<!-- 查询管理-仓单类订单查询-委托单查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :options="filterOptons" />
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['query_wrorder_order_export'])"
                    @click="openComponentOnClick" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons(['query_wrorder_order_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 { shallowRef } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDate } from '@/filters'
import { useEnum } from '@/hooks/enum'
import { useMarket } from '@/hooks/market'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable'
import { useOperation } from '@/hooks/operation'
import { warehouseQuery } from '@/services/api/order'
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'
import AppOperation from '@pc/components/base/operation/index.vue'
import { i18n } from '@/stores'

const { global: { t } } = i18n

const buyOrSellEnum = useEnum('buyOrSell') // 方向
const wrtradetype2Enum = useEnum('wrtradetype2') // 委托类型
const wrTradeOrderStatusEnum = useEnum('WRTradeOrderStatus') // 状态

const { getMarketOptions } = useMarket()
const queryParams = shallowRef<Model.WarehouseQueryReq>()

const { filterOptons, getQueryParams } = useDataFilter<Model.WarehouseQueryReq>()

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'accountid', label: 'query.warehouse_receipt_order.order.accountid' },
    { field: 'wrtradeorderid', label: 'query.warehouse_receipt_order.order.wrtradeorderid' },
    { field: 'userid', label: 'query.warehouse_receipt_order.order.userid' },
    { field: 'marketname', label: 'query.warehouse_receipt_order.order.marketname' },
    { field: 'wrtradetype', label: 'query.warehouse_receipt_order.order.wrtradetype', formatValue: (val) => wrtradetype2Enum.getEnumTypeName(val) },
    { field: 'buyorsell', label: 'query.warehouse_receipt_order.order.buyorsell', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
    { field: 'wrstandard', label: 'query.warehouse_receipt_order.order.wrstandard' },
    { field: 'wrfactortypename', label: 'query.warehouse_receipt_order.order.wrfactortypename' },
    { field: 'orderqtyname', label: 'query.warehouse_receipt_order.order.orderqtyname' },
    { field: 'tradeqtyname', label: 'query.warehouse_receipt_order.order.tradeqtyname' },
    { field: 'wrtradeorderstatus', label: 'query.warehouse_receipt_order.order.wrtradeorderstatus', formatValue: (val) => wrTradeOrderStatusEnum.getEnumTypeName(val) },
    { field: 'validtime', label: 'query.warehouse_receipt_order.order.validtime' },
    { field: 'ordertime', label: 'query.warehouse_receipt_order.order.ordertime' },
    { field: 'tradedate', label: 'query.warehouse_receipt_order.order.tradedate', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

filterOptons.selectList = [
    {
        key: 'marketid',
        label: t('query.warehouse_receipt_order.order.marketid'),
        locked: true,
        options: () => getMarketOptions(),
    },
    {
        key: 'wrtradetype',
        label: t('query.warehouse_receipt_order.order.wrtradetype1'),
        options: () => wrtradetype2Enum.getEnumOptions(),
    },
    {
        key: 'buyorsell',
        label: t('query.warehouse_receipt_order.order.buyorsell'),
        options: () => buyOrSellEnum.getEnumOptions(),
    }
]

filterOptons.inputList = [
    { label: t('query.warehouse_receipt_order.order.tradeid'), keys: ['tradeid'] },
]

filterOptons.buttonList = [
    { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
    { label: t('operation.reset'), className: 'el-button--primary', onClick: () => onSearch(true) }
]

const onSearch = (clear = false) => {
    processRequiredParams((qs) => run(qs), clear)
}

const openComponentOnClick = (code: string) => {
    processRequiredParams(() => openComponent(code))
}

// 处理请求参数
const processRequiredParams = (callback: (params: Model.WarehouseQueryReq) => void, clear = false) => {
    const qs = getQueryParams(clear)
    if (qs.marketid) {
        queryParams.value = qs
        callback(qs)
    } else {
        ElMessage.warning(t('query.warehouse_receipt_order.order.tips1'))
    }
}
</script>