import { action, makeAutoObservable } from 'mobx';
import { message } from 'antd';
import { apis, ModifyDocumentApiParams } from '@/apis';
import { State, ReadonlyState, StateAction, DocumentLibInfoStore } from './types';

// 定义状态
const stateGenerator = (): ReadonlyState => ({
    knowledge_id: '',
    listLoading: false,
    list: [],
    infoModalId: '',
    infoModalOpen: false,
    page: {
        page: 1,
        size: 10,
        total: 0,
    },
});

// 修改状态
const stateActionsGenerator = (state: State): StateAction => {
    return {
        setknowledge_id(knowledge_id) {
            state.knowledge_id = knowledge_id;
        },
        setListLoading: (loading) => {
            state.listLoading = loading;
        },
        setList: (list) => {
            state.list = list;
        },
        setInfoModalId: (id) => {
            state.infoModalId = id;
        },
        setInfoModalOpen: (open) => {
            state.infoModalOpen = open;
        },
        setPage: (page) => {
            state.page = page;
        },
    };
};

// 使用仓库
const useKnowledgeLibInfoStore = (): DocumentLibInfoStore => {
    const state = makeAutoObservable(stateGenerator());
    const actions = stateActionsGenerator(state);

    const api = {
        // 获取知识列表
        fetchDocumentLibList: async () => {
            actions.setListLoading(true);
            try {
                console.log('state.knowledge_id', state.knowledge_id);
                const data = {
                    knowledge_id: state.knowledge_id,
                    page: state.page.page,
                    size: state.page.size,
                };
                const res = await apis.fetchDocumentLibListApi(data);
                actions.setList(res.data.list);
                actions.setPage({
                    ...state.page,
                    total: res.data.total,
                });
            } catch (error: any) {
                console.error(error);
            } finally {
                actions.setListLoading(false);
            }
        },
        // 修改知识
        modifyDocumentLib: async (id: string, data: ModifyDocumentApiParams) => {
            try {
                await apis.modifyDocumentLibApi(id, data);
                // 获取知识列表
                api.fetchDocumentLibList();
                message.success('修改成功');
            } catch (error: any) {
                message.error(error.msg);
            }
        },
        // 删除知识
        deleteDocumentLib: async (id: string) => {
            try {
                await apis.deleteDocumentLibApi(id);
                // 获取知识列表
                api.fetchDocumentLibList();
                message.success('删除成功');
            } catch (error: any) {
                message.error(error.msg);
            }
        },
    }

    // 更改分页
    const onChangePagination: DocumentLibInfoStore['onChangePagination'] = async (page, size) => {
        actions.setPage({
            ...state.page,
            page: page,
            size: size,
        });
        // 获取知识列表
        api.fetchDocumentLibList();
    }

    // 点击上传文件
    const onClickDocumentDetail: DocumentLibInfoStore['onClickDocumentDetail'] = () => {
        api.fetchDocumentLibList();
    }

    // 点击修改
    const onClickModify: DocumentLibInfoStore['onClickModify'] = (id) => {
        actions.setInfoModalId(id);
        actions.setInfoModalOpen(true);
    }

    // 信息弹出层-点击确定
    const infoModalOnClickConfirm: DocumentLibInfoStore['infoModalOnClickConfirm'] = async (id, data) => {
        const initialInfoModalOpen = stateGenerator().infoModalOpen;

        actions.setInfoModalOpen(initialInfoModalOpen);

        if (id) {
            // 修改知识数据
            api.modifyDocumentLib(id, data);
        }
    }

    // 信息弹出层-点击取消
    const infoModalOnClickCancel: DocumentLibInfoStore['infoModalOnClickCancel'] = () => {
        const initialInfoModalOpen = stateGenerator().infoModalOpen;

        actions.setInfoModalOpen(initialInfoModalOpen);
    }

    // 点击删除
    const onClickDelete: DocumentLibInfoStore['onClickDelete'] = async (id) => {
        // 删除知识文件
        await api.deleteDocumentLib(id);
    }


    // 初始渲染
    const init: DocumentLibInfoStore['init'] = (id) => {
        // 获取知识列表
        if(id){
            actions.setknowledge_id(id);
            api.fetchDocumentLibList();
        }
    }

    // 状态重置
    const reset: DocumentLibInfoStore['reset'] = () => {
        const initialListLoading = stateGenerator().listLoading;
        const initialList = stateGenerator().list;
        const initialInfoModalId = stateGenerator().infoModalId;
        const initialInfoModalOpen = stateGenerator().infoModalOpen;
        const initialPage = stateGenerator().page;
        const knowledge_id = stateGenerator().knowledge_id;

        actions.setListLoading(initialListLoading);
        actions.setList(initialList);
        actions.setInfoModalId(initialInfoModalId);
        actions.setInfoModalOpen(initialInfoModalOpen);
        actions.setPage(initialPage);
        actions.setknowledge_id(knowledge_id);
    }

    return {
        state,
        onChangePagination,
        onClickDocumentDetail,
        onClickModify,
        infoModalOnClickConfirm,
        infoModalOnClickCancel,
        onClickDelete,
        init,
        reset
    };
};

export default useKnowledgeLibInfoStore();