<!-- 查询管理-内部订单查询-持仓汇总查询 -->
<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>
                <!-- 机构 -->
                <template #areauserid="{ item }">
                    <el-form-item :label="item.label">
                        <app-select-member v-model="item.value" />
                    </el-form-item>
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['query_internal_positionsumm_export'])"
                    @click="openComponentOnClick" />
            </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 { useEnum } from '@/hooks/enum'
import { useMarket } from '@/hooks/market'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useOperation } from '@/hooks/operation'
import { queryPositionSumSheet } from '@/services/api/order'
import { i18n, useUserStore } 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'
import AppSelectMember from '@pc/components/modules/select-member/index.vue'

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

const { getMarketOptions } = useMarket()

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

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

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

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'goodscode', label: 'query.internal.positionsumm.goodsCodeName' },
    { field: 'accountidname', label: 'query.internal.positionsumm.accountidname' },
    { field: 'loginids', label: 'query.internal.positionsumm.loginids' },
    { field: 'buycurpositionqty', label: 'query.internal.positionsumm.buycurpositionqty' },
    { field: 'buycurholderamount', label: 'query.internal.positionsumm.buycurholderamount' },
    { field: 'buycuravgprice', label: 'query.internal.positionsumm.buycuravgprice' },
    { field: 'buyFloatPL', label: 'query.internal.positionsumm.buyFloatPL' },
    { field: 'sellcurpositionqty', label: 'query.internal.positionsumm.sellcurpositionqty' },
    { field: 'sellcurholderamount', label: 'query.internal.positionsumm.sellcurholderamount' },
    { field: 'sellcuravgprice', label: 'query.internal.positionsumm.sellcuravgprice' },
    { field: 'sellFloatPL', label: 'query.internal.positionsumm.sellFloatPL' },
    { field: 'qty', label: 'query.internal.positionsumm.qty' },
    { field: 'buyorsell', label: 'query.internal.positionsumm.buyorsell', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
    { field: 'floatPL', label: 'query.internal.positionsumm.floatPL' }
])

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.PositionSumSheetReq>({
    filters: [
        {
            field: 'marketid',
            label: t('query.internal.positionsumm.marketid'),
            required: true,
            options: () => getMarketOptions([15, 40, 41, 48, 51, 71, 97, 99], true),
            onChange: () => goodsRef.value.onReset()
        },
        {
            field: 'goodsid',
            label: t('query.internal.positionsumm.goodsid')
        },
        {
            field: 'accname',
            label: t('query.internal.positionsumm.accname'),
            placeholder: t('query.internal.positionsumm.tips2')
        },
        {
            field: 'areauserid',
            label: t('query.internal.positionsumm.areauserid'),
            visibility: () => userStore.userInfo.releType === 1
        },
        {
            field: 'loginid',
            label: t('query.internal.positionsumm.loginid')
        },
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => onReset() }
    ]
})

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

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

const openComponentOnClick = (code: string) => {
    //openComponent(code)
}
</script>