<!-- 查询管理-内部订单查询-机构成交汇总 -->
<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.institutionsumm.marketid')">
                    <el-select v-model="marketId">
                        <el-option v-for="item in getMarketOptions([15, 40, 41, 48, 51, 71, 97, 99], true)"
                            :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('query.internal.institutionsumm.goodsid')" prop="goodsid">
                    <app-select-goods v-model="queryParams.goodsid" :params="{ marketId, goodsStatusIds: '3,6,7' }" />
                </el-form-item>
                <el-form-item :label="t('query.internal.institutionsumm.date')" prop="date">
                    <el-date-picker type="daterange" v-model="dateValue" value-format="YYYYMMDD"
                        :start-placeholder="t('common.startdate')" :end-placeholder="t('common.enddate')" />
                </el-form-item>
                <el-form-item :label="t('query.internal.institutionsumm.parentuserid')" prop="parentuserid">
                    <app-select-member v-model="queryParams.parentuserid" :params="{ usertype: '2,3' }" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSearch">{{ t('operation.search') }}</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_institutionsumm_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 { reactive, shallowRef } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { useMarket } from '@/hooks/market'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { queryInvestor } 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 AppSelectGoods from '@pc/components/modules/select-goods/index.vue'
import AppSelectMember from '@pc/components/modules/select-member/index.vue'
import { i18n } from '@/stores'

const { getMarketOptions } = useMarket()
const { global: { t } } = i18n
const formRef = shallowRef<FormInstance>()

const marketId = shallowRef<number>()
const dateValue = shallowRef<string[]>([])
const queryParams = reactive<Model.InvestorOrderReq>({
    pageNum: 1,
    pageSize: 20
})

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

const { dataList, total, pageSize, pageIndex, loading, run } = useRequest(queryInvestor, {
    manual: true,
    onError: (err) => {
        ElMessage.error(err)
    }
})

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'userid', label: 'query.internal.institutionsumm.userid' },
    { field: 'accountname', label: 'query.internal.institutionsumm.accountname' },
    { field: 'goodscode', label: 'query.internal.institutionsumm.goodscode' },
    { field: 'goodsname', label: 'query.internal.institutionsumm.goodsname' },
    { field: 'tradeqtysum', label: 'query.internal.institutionsumm.tradeqtysum' },
    { field: 'tradeamountsum', label: 'query.internal.institutionsumm.tradeamountsum' },
    { field: 'closeplsum', label: 'query.internal.institutionsumm.closeplsum' },
    { field: 'chargesum', label: 'query.internal.institutionsumm.chargesum' },
])

// 表单验证规则
const formRules: FormRules = {
    goodsid: [{ required: true }],
    date: [{
        required: true,
        validator: (rule, value, callback) => {
            if (dateValue.value.length === 2) {
                callback()
            } else {
                callback(new Error(t('common.pleasechoicedate')))
            }
        },
    }],
}

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

const openComponentOnClick = (code: string) => {
    processRequiredParams(() => openComponent(code))
}

// 处理请求参数
const processRequiredParams = (callback: (params: Model.InvestorOrderReq) => void, clear = false) => {
    formRef.value?.validate((valid) => {
        if (valid) {
            const [startDate, endDate] = dateValue.value
            queryParams.startdate = startDate
            queryParams.enddate = endDate
            callback(queryParams)
        }
    })
}
</script>