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

// 定义状态
const stateRevise = (): any => ({
    reviseList: [] as any[],
    name:'',
    createBy: '',
    myCreateBy: LocalStorage.getUserInfo()?.id || '', 
    page: {
        page: 1,
        size: 10,
        total: 0,
    },
})
// 修改状态
const stateActionRevise = (state: any) => {

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

    const onFetchDatasetList = async () => {
        try {
            const data = {
                pageNum: state.page.page,
                pageSize: state.page.size,
                name: state.name || '',
                createBy: state.createBy || '',
            }
            const res:any = await fetchDatasetList(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.setName(name);
    };
    const handleModeChange = (createBy: string) => {
        actions.setCreateBy(createBy);
        onFetchDatasetList()
    }
    const onChangePagination = async (page: number, pageSize: number) => {
        actions.setPage({
            ...state.page,
            page: page,
            size: pageSize,
        });
        await onFetchDatasetList();
    }

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


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