import { action, makeAutoObservable } from 'mobx';
import { message } from 'antd';
import LocalStorage from '@/LocalStorage';
import { fetchReviseToolList } 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 onFetchReviseToolList = async () => {
        try {
            const data = {
                pageNum: state.page.page,
                pageSize: state.page.size,
                knowledgeName: state.knowledgeName || '',
            }
            const res:any = await fetchReviseToolList(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 onFetchReviseToolList();
    }

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


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