<!-- 报表查询-推荐成交报表 -->
<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.trade.cycletype')" prop="cycletype">
                    <el-select v-model="queryParams.cycletype">
                        <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.trade.cycletime')" prop="cycletime">
                    <el-date-picker :type="dateType" v-model="queryParams.cycletime" :format="dateFormat"
                    :placeholder="t('common.pleasechoice')" />
                </el-form-item>
                <el-form-item :label="t('report.trade.quarter')" prop="quarter" v-if="queryParams.cycletype === ReportType.Quarter">
                    <el-select v-model="queryParams.quarter">
                        <el-option v-for="item in getQuarterList()" :key="item.value" :label="item.label"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item :label="t('report.trade.userid')" prop="userid">
                    <el-input v-model="queryParams.userid" :placeholder="t('report.trade.tips1')" />
                </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_trade_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, ref, computed } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { formatDate } from '@/filters'
import { useRequest } from '@/hooks/request'
import { queryTrade } 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 = shallowRef<FormInstance>()

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

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

const { dataList, total, pageSize, pageIndex, loading, run } = useRequest(queryTrade, {
    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 'month'
        case ReportType.Quarter:
        case ReportType.Year:
            return 'year'
        default:
            return 'date'
    }
})


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

const tableColumns = shallowRef<Model.TableColumn[]>([
    { field: 'goodscode', label: 'report.trade.goodsname' },
    { field: 'marketname', label: 'report.trade.marketname' },
    { field: 'tradeqty', label: 'report.trade.tradeqty' },
    { field: 'openqty', label: 'report.trade.openqty' },
    { field: 'closeqty', label: 'report.trade.closeqty' },
    { field: 'tradeamount', label: 'report.trade.tradeamount' },
    { field: 'opencharge', label: 'report.trade.opencharge' },
    { field: 'closecharge', label: 'report.trade.closecharge' }
])

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

// 处理日期格式
const formatDateByCycleType = () => {
    const dateValue = queryParams.value.cycletime

    if (queryParams.value.cycletype !== ReportType.Quarter) {
        queryParams.value.quarter = undefined
    }

    queryParams.value.cycletime = formatDate(dateValue, dateFormat.value)
}

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

const openComponentOnClick = (code: string) => {
    formRef.value?.validate((valid) => {
        if (valid) {
            formatDateByCycleType()
            openComponent(code)
        }
    })
}
</script>