<!-- 履约管理-履约计划管理 -->
<template>
    <app-view>
        <template #header>
            <app-filter :option="filterOption"  :rules="filterRules">
                <!-- <template #startDate>
                    <el-form-item :label="t('report.fee.date')" prop="date">
                        <el-date-picker type="daterange" v-model="dateValue" value-format="YYYYMMDD"
                            :start-placeholder="t('report.fee.startdate')" :end-placeholder="t('report.fee.enddate')" />
                    </el-form-item>
                </template> -->
            </app-filter>
        </template>
        <app-table :data="dataList" :columns="tableColumns" :loading="loading">
            <template #headerLeft>
                <app-operation :data-list="getActionButtons(['performance_plan_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, shallowRef } from 'vue'
import { ElMessage, FormRules } from 'element-plus'
import { useDataFilter } from '@/hooks/datatable-v2'
import { useRequest } from '@/hooks/request'
import { useOperation } from '@/hooks/operation'
import { i18n } from '@/stores'
import { queryPerformanceList } from '@/services/api/performance'
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 { global: { t } } = i18n
const dateValue = shallowRef<string[] | null>([])

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

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

const tableColumns = ref<Model.TableColumn[]>([
    // { field: 'tradedate', label: '履约计划ID' },
    // { field: 'areaname', label: '交易日' },
    // { field: 'areatype', label: '市场ID' },
    { field: 'relatedorderid', label: '关联单号' },
    // { field: 'totalcount', label: '金额' },
    // { field: 'totalfee', label: '买方' },
    // { field: 'tradedate', label: '付款方式' },
    // { field: 'areaname', label: '卖方' },
    // { field: 'areatype', label: '卖方已收金额' },
    // { field: 'bankname', label: '履约状态' },
    // { field: 'totalcount', label: '当前步骤' },
    // { field: 'totalcount', label: '操作' },
])

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

const { queryParams, filterOption, getQueryParams, resetFilters } = useDataFilter<Model.PerformanceReq>({
    filters: [
        {
            field: 'relatedorderid',
            label: '关联单号',
            placeholder: '输入关联单号模糊匹配',
        }
    ],
    buttons: [
        { label: t('operation.search'), className: 'el-button--primary', onClick: () => onSearch() },
        { label: t('operation.reset'), className: 'el-button--primary', validateEvent: false, onClick: () => onReset() }
    ]
})

// 处理请求参数
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)
}


const onReset = () => {
    dateValue.value = []
    resetFilters()
}

const openComponentOnClick = (code: string) => {
    queryParams.value = processRequiredParams()
    openComponent(code)
}
</script>