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

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

// 修改状态
const stateActionsGenerator = (state: State): StateAction => {
    return {
        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 useKnowledgeLibListStore = (): DocumentLibListStore => {
    const state = makeAutoObservable(stateGenerator());
    const actions = stateActionsGenerator(state);

    const api = {
        // 获取审核列表
        fetchApplicationLibList: async (userId: string) => {
            actions.setListLoading(true);
            try {
                const data = {
                    pageNumber: state.page.pageNumber,
                    pageSize: state.page.pageSize,
                    approver: userId,
                };
                const res = await apis.fetchTakaiAuditConfigLibList(data);
                console.log(res.rows, 'rew.rows');
                actions.setList(res.rows);
                actions.setPage({
                    ...state.page,
                    total: res.total,
                });
            } catch (error: any) {
                console.error(error);
            } finally {
                actions.setListLoading(false);
            }
        },
        
        // 应用审核
        modifyDocumentLib: async (documentId: string, userId: string, data: ModifyDocumentApiParams) => {
            try {
                const res = await apis.modifyTakaiAuditDocumentLibApi(documentId, data);
                // 获取审核列表
                api.fetchApplicationLibList(userId);
                if(res.data === 1 && res.code === 200){
                    message.success('修改成功');
                }else{
                    message.error('修改失败');
                }
            } catch (error: any) {
                message.error(error.msg);
            }
        },
        // 获取详情
        fetchTakaiApplicationDetail: async (appId: string) => {
            try {
                const res = await apis.fetchTakaiApplicationDetail(appId);
                console.log(res, 'resresres');
                return res;
            } catch (error: any) {
                message.error(error.msg);
            }
        },
    }
    // 点击查看出现弹窗
    const onClickfetchTakaiApplicationDetail = async (appId: string) => {
        if(appId){
            await api.fetchTakaiApplicationDetail(appId);
        }
    }
    // 更改分页
    const onChangePagination: DocumentLibListStore['onChangePagination'] = async (pageNumber, pageSize) => {
        actions.setPage({
            ...state.page,
            pageNumber: pageNumber,
            pageSize: pageSize,
        });
        // 获取知识库列表
        await api.fetchApplicationLibList('');
    }

    // 点击创建
    const onClickCreate: DocumentLibListStore['onClickCreate'] = () => {
        const initialInfoModalId = stateGenerator().infoModalId;

        actions.setInfoModalId(initialInfoModalId);
        actions.setInfoModalOpen(true);
    }

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

    // 信息弹出层-点击通过
    const infoModalOnClickConfirm: DocumentLibListStore['infoModalOnClickConfirm'] = async (documentId, userId, data) => {
        const initialInfoModalOpen = stateGenerator().infoModalOpen;

        actions.setInfoModalOpen(initialInfoModalOpen);

        if (documentId) {
            // 审核通过
            console.log(data, 'datadata');
            await api.modifyDocumentLib(documentId, userId, data);
        } 
    }

    // 信息弹出层-点击拒绝
    const infoModalOnClickCancel: DocumentLibListStore['infoModalOnClickCancel'] = async(documentId, userId, data) => {
        const initialInfoModalOpen = stateGenerator().infoModalOpen;

        actions.setInfoModalOpen(initialInfoModalOpen);

        if (documentId) {
            // 审核拒绝
            await api.modifyDocumentLib(documentId, userId, data);
        } 
    }

    const infoModalOnClickClose: DocumentLibListStore['infoModalOnClickClose'] = () => {
        const initialInfoModalOpen = stateGenerator().infoModalOpen;
        actions.setInfoModalOpen(initialInfoModalOpen);

    }

    
    // 初始渲染
    const init: DocumentLibListStore['init'] = async (userId) => {
        // 获取知识库列表
        await api.fetchApplicationLibList(userId);
    }

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

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

    return {
        state,
        onChangePagination,
        onClickCreate,
        onClickModify,
        onClickfetchTakaiApplicationDetail,
        infoModalOnClickConfirm,
        infoModalOnClickCancel,
        infoModalOnClickClose,
        init,
        reset
    };
};

export default useKnowledgeLibListStore();