<!-- 查询管理-内部订单查询-成交汇总查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption">
                <template #cycletime="{ item }">
                    <el-form-item :label="item.label" prop="cycletime">
                        <el-date-picker v-model="item.value" value-format="YYYYMMDD"
                            :placeholder="t('common.pleasechoice')" />
                    </el-form-item>
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['query_internal_tradesumm_export'])"
                    @click="(code: string) => openComponent(code)" />
            </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 { useOperation } from '@/hooks/operation'
import { queryTradeSum } 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 buyOrSellEnum = useEnum('buyOrSell') // 方向

const { global: { t } } = i18n

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

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

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'tradeDate', label: 'query.internal.tradesumm.tradeDate' },
    { field: 'loginid', label: 'query.internal.tradesumm.loginid' },
    { field: 'currentname', label: 'query.internal.tradesumm.currentname' },
    { field: 'parentusername', label: 'query.internal.tradesumm.parentusername' },
    { field: 'goodscode', label: 'query.internal.tradesumm.goodscode' },
    //{ field: 'goodsname', label: 'query.internal.tradesumm.goodsname' },
    { field: 'buyorsell', label: 'query.internal.tradesumm.buyorselldisplay', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
    { field: 'tradeqty', label: 'query.internal.tradesumm.tradeqty' },
    { field: 'tradepriceavg', label: 'query.internal.tradesumm.tradepriceavg' },
    { field: 'tradeamount', label: 'query.internal.tradesumm.tradeamount' },
    { field: 'totalcharge', label: 'query.internal.tradesumm.totalcharge' }
])

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.TradeSumReq>({
    filters: [
        {
            field: 'cycletime',
            label: t('query.internal.tradesumm.cycletime'),
        },
        {
            field: 'goodsid',
            label: t('query.internal.tradesumm.goodsid'),
            placeholder: t('query.internal.tradesumm.tips1')
        },
        {
            field: 'buyorsell',
            label: t('query.internal.tradesumm.buyorsell'),
            options: () => buyOrSellEnum.getEnumOptions()
        },
        {
            field: 'loginid',
            label: t('query.internal.tradesumm.loginid'),
        },
        {
            field: 'tradetypes',
            label: t('query.internal.tradesumm.tradetypes'),
            options: () => [
                { label: t('query.internal.tradesumm.tradetypes1'), value: '1,2,10' },
                { label: t('query.internal.tradesumm.tradetypes2'), value: '3,4' },
                { label: t('query.internal.tradesumm.tradetypes3'), value: '6' },
                { label: t('query.internal.tradesumm.tradetypes4'), value: '7,9,11' },
                { label: t('query.internal.tradesumm.tradetypes5'), value: '29' },
                { label: t('query.internal.tradesumm.tradetypes6'), value: '12' },
                { label: t('query.internal.tradesumm.tradetypes7'), value: '5,8,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28' }
            ]
        },
    ],
    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>