<!-- 报表查询-分润报表 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption">
                <template #tradedate="{ item }">
                    <el-form-item :label="item.label" prop="tradedate">
                        <el-date-picker :type="dateType" v-model="item.value" :format="dateFormat"
                            :value-format="dateFormat" :placeholder="t('common.pleasechoice')" />
                    </el-form-item>
                </template>
                <template #quarter="{ item }">
                    <el-form-item :label="item.label" prop="quarter"
                        v-if="queryParams.cycletype === ReportType.Quarter">
                        <el-select v-model="item.value">
                            <el-option v-for="option in getQuarterList()" :key="option.value" :label="option.label"
                                :value="option.value" />
                        </el-select>
                    </el-form-item>
                    <template v-else>
                        {{ (item.value = undefined) }}
                    </template>
                </template>
                <template #isson="{ item }">
                    <el-form-item :label="item.label" prop="isson">
                        <el-switch v-model="item.value" active-value="1" inactive-value="0" />
                    </el-form-item>
                </template>
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['query_internal_profitshare_export'])"
                    @click="(code: string) => openComponent(code)" />
            </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 { computed, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useEnum } from '@/hooks/enum'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useRequest } from '@/hooks/request'
import { formatDate } from '@/filters'
import { shareAmountQuery } from '@/services/api/report'
import { useOperation } from '@/hooks/operation'
import { i18n } from '@/stores'
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 AppFilter from '@pc/components/base/table-filter-v2/index.vue'

const roleTypeEnum = useEnum('roleType') // 角色

const { global: { t } } = i18n

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

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

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.ShareAmountQueryReq>({
    filters: [
        {
            field: 'cycletype',
            label: t('report.profitshare.cycletype'),
            value: 0,
            options: () => getReportTypeList()
        },
        {
            field: 'tradedate',
            label: t('report.profitshare.tradedate'),
            required: true
        },
        {
            field: 'quarter',
            label: t('report.profitshare.quarter'),
            required: true
        },
        {
            field: 'isson',
            label: t('report.profitshare.isson'),
            value: 0
        }
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => resetFilters() }
    ]
})

// 日期类型
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 'YYYYMM'
        case ReportType.Quarter:
        case ReportType.Year:
            return 'YYYY'
        default:
            return 'YYYYMMDD'
    }
})

const tableColumns = ref<Model.TableColumn[]>([
    { field: 'areatype', label: 'report.profitshare.areatype', formatValue: (val) => roleTypeEnum.getEnumTypeName(val) },
    { field: 'areaaccountid', label: 'report.profitshare.areaaccountid' },
    { field: 'goodsname', label: 'report.profitshare.goodsname' },
    { field: 'searchtype', label: 'report.profitshare.searchtype' },
    { field: 'totalcount', label: 'report.profitshare.totalcount' },
    { field: 'totalfee', label: 'report.profitshare.totalfee' },
    { field: 'divideamount', label: 'report.profitshare.divideamount' }
])

const onSearch = () => {
    const qs = getQueryParams()
    qs.tradedate = formatDate(qs.tradedate, dateFormat.value) // 处理日期格式
    run(qs)
}
</script>