<!-- 报表查询-交易商资金报表 -->
<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.investor.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.investor.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.investor.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.investor.accountid')" prop="accountid">
                    <el-input v-model="queryParams.accountid" :placeholder="t('report.investor.tips2')" />
                </el-form-item>
                <el-form-item :label="t('report.investor.memberid')" prop="memberid">
                    <el-input v-model="queryParams.memberid" :placeholder="t('report.investor.tips3')" />
                </el-form-item>
                <el-form-item :label="t('report.investor.subareaid')" prop="subareaid">
                    <el-input v-model="queryParams.subareaid" :placeholder="t('report.investor.tips4')" />
                </el-form-item>
                <el-form-item :label="t('report.investor.brokerid')" prop="brokerid">
                    <el-input v-model="queryParams.brokerid" :placeholder="t('report.investor.tips5')" />
                </el-form-item>
                <el-form-item :label="t('report.investor.inamount')" prop="inamount">
                    <el-input-number v-model="queryParams.inamount" :min="0" :placeholder="t('report.investor.tips1')"
                        style="width: 240px;" />
                </el-form-item>
                <el-form-item :label="t('report.investor.outamount')" prop="outamount">
                    <el-input-number v-model="queryParams.outamount" :min="0" :placeholder="t('report.investor.tips1')"
                        style="width: 240px;" />
                </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_investor_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 { investorReportQuery } 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 { i18n } from '@/stores'

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

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

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

const { dataList, total, pageSize, pageIndex, loading, run } = useRequest(investorReportQuery, {
    manual: true,
    params: {
        pageNum: 1,
        pageSize: 20,
        flag: 0
    },
    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: 'reckondate', label: 'report.investor.reckondate' },
    { field: 'loginids', label: 'report.investor.loginids' },
    { field: 'accountid', label: 'report.investor.accountid1' },
    { field: 'accountname', label: 'report.investor.accountname' },
    { field: 'memberaccountname', label: 'report.investor.memberaccountname' },
    { field: 'balance', label: 'report.investor.balance' },
    { field: 'currentbalance', label: 'report.investor.currentbalance' },
    { field: 'inamount', label: 'report.investor.inamount' },
    { field: 'outamount', label: 'report.investor.outamount' },
    { field: 'closepl', label: 'report.investor.closepl' },
    { field: 'floatpl', label: 'report.investor.floatpl' },
    { field: 'reckonpl', label: 'report.investor.reckonpl' },
    { field: 'paycharge', label: 'report.investor.paycharge' },
    { field: 'interestCharge', label: 'report.investor.interestCharge' },
    { field: 'usedmargin', label: 'report.investor.usedmargin' },
    { field: 'freezemargin', label: 'report.investor.freezemargin' },
    { field: 'otherfreezemargin', label: 'report.investor.otherfreezemargin' },
    { field: 'outamountfreeze', label: 'report.investor.outamountfreeze' },
    { field: 'avaiablemoney', label: 'report.investor.avaiablemoney' },
    { field: 'avaiableoutmoney', label: 'report.investor.avaiableoutmoney' },
    { field: 'riskrate', label: 'report.investor.riskrate' }
])

// 表单验证规则
const formRules: FormRules = {
    reckondate: [{ required: true }],
    reckondateend: [{ 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
        }
        dateValue.value = []
    } else {
        formRef.value?.validate((valid) => {
            if (valid) {
                run(queryParams.value)
            }
        })
    }
}

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