<!-- 报表查询-经纪会员报表 -->
<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('report.broker.cycletype')" prop="cycletype">
                    <el-select v-model="queryParams.cycletype" @change="onTypeChange">
                        <el-option v-for="item in getReportTypeList()" :key="item.value" :label="item.label"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('report.broker.reckondate')" prop="reckondate">
                    <el-date-picker :type="dateType" v-model="dateValue" :format="dateFormat" :value-format="dateFormat"
                        :placeholder="t('common.pleasechoice')" :start-placeholder="t('common.start')"
                        :end-placeholder="t('common.end')" @change="onDateChange" />
                </el-form-item>
                <el-form-item :label="t('report.broker.quarter')" prop="quarter"
                    v-if="queryParams.cycletype === ReportType.Quarter">
                    <el-select v-model="queryParams.quarter" @change="onQuarterChange">
                        <template v-for="item in getQuarterList()" :key="item.value">
                            <el-option :label="item.label" :value="item.value" />
                        </template>
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('report.broker.accountid')" prop="accountid">
                    <app-select-member v-model="queryParams.accountid" :params="{ roles: '7' }" />
                </el-form-item>
                <el-form-item :label="t('report.broker.isson')" prop="isson">
                    <el-switch v-model="queryParams.type" active-value="1" inactive-value="0" />
                </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_report_broker_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, computed } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { useRequest } from '@/hooks/request'
import { orgReportQuery } from '@/services/api/report'
import { useOperation } from '@/hooks/operation'
import { getReportTypeList, ReportType, getQuarterList } from '@/constants/report'
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 AppSelectMember from '@pc/components/modules/select-member/index.vue'
import { i18n } from '@/stores'

const { global: { t } } = i18n
const formRef = ref<FormInstance>()
const dateValue = ref<string[] | string>([])

const queryParams = ref<Partial<Model.OrgReportQueryReq>>({
    cycletype: 0,
    type: 0
})

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

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

// 日期类型
const dateType = computed(() => {
    switch (queryParams.value.cycletype) {
        case ReportType.Week:
            return 'week'
        case ReportType.Month:
            return 'monthrange'
        case ReportType.Quarter:
            return 'year'
        case ReportType.Year:
            return 'yearrange'
        default:
            return 'daterange'
    }
})

// 日期格式
const dateFormat = computed(() => {
    switch (queryParams.value.cycletype) {
        case ReportType.Month:
            return 'YYYYMM'
        case ReportType.Quarter:
        case ReportType.Year:
            return 'YYYY'
        default:
            return 'YYYYMMDD'
    }
})

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'cycleTime', label: 'report.broker.cycleTime' },
    { field: 'accountName', label: 'report.broker.accountName' },
    { field: 'investorTotalNum', label: 'report.broker.investorTotalNum', width: 160 },
    { field: 'investorTrialNum', label: 'report.broker.investorTrialNum', width: 180 },
    { field: 'investorRetrialNum', label: 'report.broker.investorRetrialNum', width: 180 },
    { field: 'investorAmount', label: 'report.broker.investorAmount' },
    { field: 'investorusedmargin', label: 'report.broker.investorusedmargin' },
    { field: 'investorClosePl', label: 'report.broker.investorClosePl' },
    { field: 'investorReckonPl', label: 'report.broker.investorReckonPl' },
    { field: 'investorBuyDeliveryAmount', label: 'report.broker.investorBuyDeliveryAmount' },
    { field: 'investorBuyTradeQty', label: 'report.broker.investorBuyTradeQty' },
    { field: 'investorSellTradeQty', label: 'report.broker.investorSellTradeQty' },
    { field: 'investorBuyHoldQty', label: 'report.broker.investorBuyHoldQty' },
    { field: 'investorSellHoldQty', label: 'report.broker.investorSellHoldQty' }
])

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

// 选择类型
const onTypeChange = (value: number) => {
    if (value === ReportType.Week || value === ReportType.Quarter) {
        dateValue.value = ''
    } else {
        dateValue.value = []
    }
    queryParams.value.reckondate = undefined
    queryParams.value.reckondateend = undefined
    queryParams.value.quarter = undefined
    queryParams.value.quarterend = undefined
}

// 选择日期
const onDateChange = () => {
    if (Array.isArray(dateValue.value)) {
        const [startDate, endDate] = dateValue.value
        queryParams.value.reckondate = startDate
        queryParams.value.reckondateend = endDate
    } else {
        queryParams.value.reckondate = dateValue.value
        queryParams.value.reckondateend = dateValue.value
    }
}

// 选择季度
const onQuarterChange = (value: number) => {
    queryParams.value.quarterend = value
}

const onSearch = (clear = false) => {
    if (clear) {
        queryParams.value = {
            cycletype: 0,
            type: 0
        }
        dateValue.value = []
    } else {
        formRef.value?.validate((valid) => {
            if (valid) {
                run(queryParams.value)
            }
        })
    }
}

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