<!-- 查询管理-内部订单查询-持仓汇总查询 -->
<template>
    <app-view>
        <template #header>
            <el-form ref="formRef" class="el-form--filter" :model="queryParams" :rules="formRules"
                :show-message="false">
                <el-form-item :label="t('query.internal.positionsumm.marketid')" prop="marketid">
                    <el-select v-model="queryParams.marketid">
                        <template v-for="item in getMarketOptions([15, 40, 41, 48, 51, 71, 97, 99], true)"
                            :key="item.value">
                            <el-option :label="item.label" :value="item.value" />
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('query.internal.positionsumm.goodsid')" prop="goodsid">
                    <el-input type="number" v-model="queryParams.goodsid" :placeholder="t('query.internal.positionsumm.tips1')" />
                </el-form-item>
                <el-form-item :label="t('query.internal.positionsumm.accname')" prop="accname">
                    <el-input v-model="queryParams.accname" :placeholder="t('query.internal.positionsumm.tips2')" />
                </el-form-item>
                <el-form-item :label="t('query.internal.positionsumm.areauserid')" prop="areauserid" v-if="userStore.userInfo.releType === 1">
                    <el-input type="number" v-model="queryParams.areauserid" :placeholder="t('query.internal.positionsumm.tips3')" />
                </el-form-item>
                <el-form-item :label="t('query.internal.positionsumm.loginid')" prop="loginid">
                    <el-input v-model="queryParams.loginid" :placeholder="t('common.pleaseenter')" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSearch(false)">{{ t('operation.search') }}</el-button>
                    <el-button type="primary" @click="onSearch(true)">{{ t('operation.reset') }}</el-button>
                </el-form-item>
            </el-form>
        </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, FormInstance, FormRules } from 'element-plus'
import { getBuyOrSellName } from '@/constants/order'
import { useMarket } from '@/hooks/market'
import { useRequest } from '@/hooks/request'
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'

const { getMarketOptions } = useMarket()

const userStore = useUserStore()
const { global: { t } } = i18n
const formRef = ref<FormInstance>()

const queryParams = ref<Partial<Model.PositionSumSheetReq>>({})

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: 'goodsCodeName', 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) => getBuyOrSellName(val) },
    { field: 'floatPL', label: 'query.internal.positionsumm.floatPL' }
])

// 表单验证规则
const formRules: FormRules = {
    marketid: [{ required: true }],
}

const onSearch = (clear = false) => {
    if (clear) {
        queryParams.value = {}
    } else {
        formRef.value?.validate((valid) => {
            if (valid) {
                run(queryParams.value)
            }
        })
    }
}

const openComponentOnClick = (code: string) => {
    formRef.value?.validate((valid) => {
        if (valid) {
            openComponent(code)
        }
    })
}
</script>