<!-- 查询管理-内部订单查询-持仓单查询 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" :rules="filterRules">
                <!-- 查询类型 -->
                <template #isHis="{ item }">
                    <el-form-item :label="item.label">
                        <el-select v-model="item.value">
                            <el-option :label="t('common.current')" :value="0" />
                            <el-option :label="t('common.history')" :value="1" />
                        </el-select>
                    </el-form-item>
                    <el-form-item :label="t('query.internal.order.date')" prop="date" v-if="item.value === 1">
                        <el-date-picker type="daterange" v-model="dateValue" value-format="YYYY-MM-DD"
                            :start-placeholder="t('common.start')" :end-placeholder="t('common.end')" />
                    </el-form-item>
                </template>
                <!-- 商品 -->
                <template #goodsId="{ item }">
                    <el-form-item :label="item.label">
                        <app-select-goods ref="goodsRef" v-model="item.value"
                            :params="{ marketId: queryParams.marketId ?? -1, goodsStatusIds: '3,6,7' }" />
                    </el-form-item>
                </template>
                <!-- 所属机构 -->
                <template #parentAreaId="{ item }">
                    <el-form-item :label="item.label">
                        <app-select-member2 v-model="item.value" />
                    </el-form-item>
                </template>
                <!-- 有效持仓 -->
                <template #validHolderQty="{ item }">
                    <el-form-item :label="item.label">
                        <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_internal_position_export'])"
                    @click="openComponentOnClick" />
            </template>
            <!-- 操作 -->
            <template #operate="{ row }">
                <app-operation size="small" :data-list="getActionButtons(['query_internal_position_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 { ref } from 'vue'
import { ElMessage, FormRules } from 'element-plus'
import { formatDate } from '@/filters'
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 { queryPositionSheet } 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 AppSelectGoods from '@pc/components/modules/select-goods/index.vue'
import AppSelectMember2 from '@pc/components/modules/select-member2/index.vue'
import { i18n } from '@/stores'

const { getMarketOptions } = useMarket()

const buyOrSellEnum = useEnum('buyOrSell') // 方向
const usertype2Enum = useEnum('usertype2') // 机构类型

const { global: { t } } = i18n

const goodsRef = ref() // 商品组件实例
const dateValue = ref<string[] | null>([])

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

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

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'tradeid', label: 'query.internal.position.tradeid' },
    { field: 'tradedate', label: 'query.internal.position.tradedate' },
    { field: 'accountNameId', label: 'query.internal.position.accountNameId' },
    { field: 'goodscode', label: 'query.internal.position.goodsCodeName' },
    { field: 'buyorsell', label: 'query.internal.position.buyorsell', formatValue: (val) => buyOrSellEnum.getEnumTypeName(val) },
    { field: 'openqty', label: 'query.internal.position.openqty' },
    { field: 'openprice', label: 'query.internal.position.openprice' },
    { field: 'tradeamount', label: 'query.internal.position.tradeamount' },
    { field: 'holderqty', label: 'query.internal.position.holderqty' },
    { field: 'holderprice', label: 'query.internal.position.holderprice' },
    { field: 'holderamount', label: 'query.internal.position.holderamount' },
    { field: 'actuallYplS', label: 'query.internal.position.actuallYplS' },
    { field: 'holderdays', label: 'query.internal.position.holderdays' },
    { field: 'tradetime', label: 'query.internal.position.tradetime', formatValue: (val) => formatDate(val) },
    { field: 'usertype', label: 'query.internal.position.usertype', formatValue: (val) => usertype2Enum.getEnumTypeName(val) },
    { field: 'memberusername', label: 'query.internal.position.memberusername' },
    { field: 'parentusername', label: 'query.internal.position.parentusername' },
    { field: 'marketname', label: 'query.internal.position.marketname' },
    { field: 'operate', label: 'common.operate', fixed: 'right' }
])

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.PositionSheetReq>({
    filters: [
        {
            field: 'marketId',
            label: t('query.internal.position.marketid'),
            required: true,
            options: () => getMarketOptions([15, 40, 41, 48, 51, 71, 97, 99], true),
            onChange: () => goodsRef.value.onReset()
        },
        {
            field: 'goodsId',
            label: t('query.internal.position.goodsId')
        },
        {
            field: 'buyOrSell',
            label: t('query.internal.position.buyOrSell'),
            options: () => buyOrSellEnum.getEnumOptions()
        },
        {
            field: 'isHis',
            label: t('query.internal.position.isHis'),
            value: 0
        },
        {
            field: 'tradeId',
            label: t('query.internal.position.tradeId')
        },
        {
            field: 'loginId',
            label: t('query.internal.position.loginId')
        },
        {
            field: 'accountId',
            label: t('query.internal.position.accountId'),
            placeholder: t('query.internal.position.tips2')
        },
        {
            field: 'parentAreaId',
            label: t('query.internal.position.parentAreaId')
        },
        {
            field: 'validHolderQty',
            label: t('query.internal.position.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: () => onReset() }
    ]
})

// 表单验证规则
const filterRules: FormRules = {
    date: [{
        required: true,
        validator: () => dateValue.value?.length === 2
    }]
}

const onSearch = (clear = false) => {
    const qs = getQueryParams(clear)
    const [startDate, endDate] = dateValue.value || []
    qs.startDate = startDate
    qs.endDate = endDate
    run(qs)
}

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

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