<!-- 查询管理-内部订单查询-成交汇总查询 -->
<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="请选择" />
                    </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 { getBuyOrSellList } from '@/constants/order'
import { useDataFilter } from '@/hooks/datatable-v2'
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 { 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: '交易日' },
    { field: 'loginid', label: '登录账号' },
    { field: 'currentname', label: '用户名称' },
    { field: 'parentusername', label: '所属机构' },
    { field: 'goodscode', label: '商品代码' },
    { field: 'goodsname', label: '商品名称' },
    { field: 'buyorselldisplay', label: '方向' },
    { field: 'tradeqty', label: '成交数量' },
    { field: 'tradepriceavg', label: '成交价格' },
    { field: 'tradeamount', label: '成交金额' },
    { field: 'totalcharge', label: '手续费' }
])

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.TradeSumReq>({
    filters: [
        {
            field: 'cycletime',
            label: '日期',
        },
        {
            field: 'goodsid',
            label: '商品',
            placeholder: '请输入商品ID'
        },
        {
            field: 'buyorsell',
            label: '方向',
            options: () => getBuyOrSellList()
        },
        {
            field: 'loginid',
            label: '登录账号',
        },
        {
            field: 'tradetypes',
            label: '成交类别',
            options: () => [
                { label: '交易成交', value: '1,2,10' },
                { label: '交收成交', value: '3,4' },
                { label: '风控斩仓', value: '6' },
                { label: '协议成交', value: '7,9,11' },
                { label: '持仓过户', value: '29' },
                { label: '系统强平', value: '12' },
                { label: '其它成交', 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>