<!-- 查询管理-订单管理-订单查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption">
                <template #parantareaid="{ item }">
                    <el-form-item :label="item.label" prop="parantareaid">
                        <app-select-investor v-model="item.value" />
                    </el-form-item>
                </template>
                <template #validholderqty="{ item }">
                    <el-form-item :label="item.label" prop="validholderqty">
                        <el-switch v-model="item.value" :active-value="1" :inactive-value="0" />
                    </el-form-item>
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['query_order_list_export'])"
                    @click="(code: string) => openComponent(code)" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons(['query_order_list_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 { useDataFilter } from '@/hooks/datatable-v2'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { queryOrder } from '@/services/api/order'
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 AppSelectInvestor from '@pc/components/modules/select-investor/index.vue'
import { i18n } from '@/stores'

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

const { global: { t } } = i18n

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

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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'tradeid', label: 'query.order.list.tradeid' },
    { field: 'accountname', label: 'query.order.list.accountname' },
    { field: 'membername', label: 'query.order.list.membername' },
    { field: 'goodscode', label: 'query.order.list.goodscode' },
    { field: 'buyorsell', label: 'query.order.list.buyorsell', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
    { field: 'holderprice', label: 'query.order.list.holderprice' },
    { field: 'opennum', label: 'query.order.list.opennum' },
    { field: 'holdernum', label: 'query.order.list.holdernum' },
    { field: 'freezenum', label: 'query.order.list.freezenum' },
    { field: 'depositratedisplay', label: 'query.order.list.depositratedisplay' },
    { field: 'holderamount', label: 'query.order.list.holderamount' },
    { field: 'payeddeposit', label: 'query.order.list.payeddeposit' },
    { field: 'tradetimestr', label: 'query.order.list.tradetimestr', formatValue: (val) => formatDate(val) },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.OrderReq>({
    filters: [
        {
            field: 'accountname',
            label: t('query.order.list.accountname')
        },
        {
            field: 'goodsid',
            label: t('query.order.list.goodsid'),
            placeholder: t('query.order.list.tips1')
        },
        {
            field: 'buyorsell',
            label: t('query.order.list.buyorsell'),
            options: () => buyOrSellEnum.getEnumOptions()
        },
        {
            field: 'tradeid',
            label: t('query.order.list.tradeid')
        },
        {
            field: 'parantareaid',
            label: t('query.order.list.parantareaid'),
            placeholder: t('query.order.list.tips2')
        },
        {
            field: 'validholderqty',
            label: t('query.order.list.validholderqty'),
            value: 1
        },
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => resetFilters() }
    ]
})

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