<!-- 报表查询-报表重算 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption"  :rules="filterRules">
                <template #startDate>
                    <el-form-item :label="t('report.recalculate.date')" prop="date">
                        <el-date-picker type="daterange" v-model="dateValue" value-format="YYYYMMDD"
                            :start-placeholder="t('report.recalculate.startdate')" :end-placeholder="t('report.recalculate.enddate')" />
                    </el-form-item>
                </template>
                <fieldset class="g-fieldset el-form--vertical">
                    <legend class="g-fieldset__legend">{{ t('report.recalculate.tips1') }}</legend>
                    <el-form-item>
                        <span>{{ t('report.recalculate.tips2') }}</span>
                    </el-form-item>
                    <el-form-item>
                        <span>{{ t('report.recalculate.tips3') }}</span>
                    </el-form-item>
                </fieldset>
            </app-filter>
        </template>
    </app-view>
</template>

<script lang="ts" setup>
import { shallowRef } from 'vue'
import { ElMessage, FormRules } from 'element-plus'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useOperation } from '@/hooks/operation'
import { rerun } from '@/services/api/report'
import { useRequest } from '@/hooks/request'
import AppFilter from '@pc/components/base/table-filter-v2/index.vue'
import { i18n } from '@/stores'

const { global: { t } } = i18n
const dateValue = shallowRef<string[] | null>([])

const { run } = useRequest(rerun, {
    manual: true,
    params: {
        pageNum: 1,
        pageSize: 20
    },
    onSuccess: () => {
        ElMessage.success(t('common.tips17'))
    },
    onError: (err) => {
        ElMessage.error(err)
    }
})

useOperation<Model.CommissionSheetRsp>({
    onClose: () => onSearch()
})

// 表单验证规则
const filterRules: FormRules = {
    date: [{
        required: true,
        validator: () => dateValue.value?.length === 2
    }]
}

const { filterOption, getQueryParams } = useDataFilter<Model.ReturnReq>({
    filters: [
        {
            field: 'startDate',
            label: t('report.recalculate.startDate'),
        },
    ],
    buttons: [
        { label: t('report.recalculate.tips4'), className: 'el-button--primary', onClick: () => onSearch() }
    ]
})

// 处理请求参数
const processRequiredParams = () => {
    const qs = getQueryParams()
    const [startDate, endDate] = dateValue.value || []
    qs.startDate = startDate
    qs.endDate = endDate
    return qs
}

const onSearch = () => {
    const qs = processRequiredParams()
    run(qs)
}

</script>