<!-- 查询管理-内部订单查询-机构成交汇总 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption" :rules="filterRules">
                <!-- 商品 -->
                <template #goodsid="{ item }">
                    <el-form-item :label="t('query.internal.institutionsumm.marketid')">
                        <el-select v-model="marketId" @change="goodsRef.onReset()">
                            <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="item.label" prop="goodsid">
                        <app-select-goods ref="goodsRef" v-model="item.value"
                            :params="{ marketId, goodsStatusIds: '3,6,7' }" />
                    </el-form-item>
                </template>
                <!-- 日期 -->
                <template #startdate="{ item }">
                    <el-form-item :label="item.label" prop="startdate">
                        <el-date-picker type="daterange" v-model="dateValue" value-format="YYYYMMDD"
                            :start-placeholder="t('common.startdate')" :end-placeholder="t('common.enddate')" />
                    </el-form-item>
                </template>
                <!-- 所属机构 -->
                <template #parentuserid="{ item }">
                    <el-form-item :label="item.label">
                        <app-select-member2 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_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 { shallowRef } from 'vue'
import { ElMessage, FormRules } from 'element-plus'
import { useMarket } from '@/hooks/market'
import { useRequest } from '@/hooks/request'
import { useDataFilter } from '@/hooks/datatable-v2'
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 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 { global: { t } } = i18n
const goodsRef = shallowRef() // 商品组件实例

const marketId = shallowRef<number>()
const dateValue = shallowRef<string[]>([])

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

const { dataList, total, pageSize, pageIndex, loading, run } = useRequest(queryInvestor, {
    manual: true,
    params: {
        pageNum: 1,
        pageSize: 20,
    },
    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 { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.InvestorOrderReq>({
    filters: [
        {
            field: 'goodsid',
            label: t('query.internal.institutionsumm.goodsid'),
            required: true
        },
        {
            field: 'startdate',
            label: t('query.internal.institutionsumm.date'),
            required: true
        },
        {
            field: 'parentuserid',
            label: t('query.internal.institutionsumm.parentuserid')
        }
    ],
    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 = {
    startdate: [{
        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>