import { action, makeAutoObservable } from 'mobx';
import { message } from 'antd';
import LocalStorage from '@/LocalStorage';
import { fetchEvaluationList } from '@/apis';
import { downloadFile } from '@/utils/index'
import { data } from 'react-router-dom';

// 定义状态
const stateRevise = (): any => ({
    reviseList: [] as any[],
    knowledgeName:'',
    page: {
        page: 1,
        size: 10,
        total: 0,
    },
})
// 修改状态
const stateActionRevise = (state: any) => {

    return{
        setPage: (page: { page: number; size: number; total: number }) => {
            state.page = page;
        },
        setKnowledgeName: (knowledgeName: string) => {
            state.knowledgeName = knowledgeName;
        },
    }
}
// 使用仓库
const useRevisionToolListStore = () => {
    const state = makeAutoObservable(stateRevise());
    const actions = stateActionRevise(state);

    const onFetchEvaluationList = async () => {
        try {
            const data = {
                pageNum: state.page.page,
                pageSize: state.page.size,
                name: state.knowledgeName || '',
            }
            const res:any = await fetchEvaluationList(data);
            if (res && res.code === 200) {
                state.reviseList = res.rows;
                state.page.total = res.total;
            } else {
                message.error(res.message || '获取评估任务列表失败');
            }
        } catch (error) {
            message.error('获取评估任务列表异常');
        }
    }
    const handleNameChange = (name: string) => {
        actions.setKnowledgeName(name);
    };
    const onChangePagination = async (page: number, pageSize: number) => {
        actions.setPage({
            ...state.page,
            page: page,
            size: pageSize,
        });
        await onFetchEvaluationList();
    }

    const init = ()=>{
        onFetchEvaluationList()
    }


    return {
        state,
        init,
        handleNameChange,
        onChangePagination
    };
}
export default useRevisionToolListStore();