// pageList 列表数据
// pageTotalCount 列表数据总数
// pageDetailInfo 详情页数据
// pageOperateType 详情页类型 add新增 edit修改 detail查看
// paramsType 增删改页面新增项，用来给（特殊）的列表接口刷新页面使用

import {
    deletePageData,
    editPageData,
    getPageListData,
    newPageData,
    getPageDetail,
    getCommon,
} from '@/api/main';
import { defineStore } from 'pinia';
import cache from '@/plugins/cache';
import { ElMessage } from 'element-plus';
import { log } from 'console';

const useSystemStore = defineStore('system', {
    state: () => ({
        pageList: [],
        pageTotalCount: 0,
        pageDetailInfo: {},
        pageOperateType: 'add',
        deletSign: true,
        pageInfo: {
            pageNum: 1,
            pageSize: 10,
        },
        resDate: {} as any,
        searchObj: {} as any,
    }),
    actions: {
        // 页面的网络请求
        async getPageListDataAction(pageName: string, queryInfo?: any) {
            await getPageListData(
                pageName,
                queryInfo
                    ? { ...queryInfo, ...this.pageInfo, ...this.searchObj }
                    : { ...this.pageInfo, ...this.searchObj }
            ).then((res: any) => {
                this.pageList = res.rows;
                this.pageTotalCount = res.total;
                this.resDate = res;
            });
        },
        async deletePageDataAction(pageName: string, urlId: any, paramsType?: object) {
            // 2.删除用户列表数据
            await deletePageData(pageName, urlId).then((res: any) => {
                ElMessage.success(res.msg);
            });
            this.deletSign = !this.deletSign;
            await this.getPageListDataAction(pageName, { pageSize: 10, ...paramsType });
        },
        async newPageDataAction(pageName: string, pageData: any, paramsType?: object) {
            let flag = reactive({});
            // 3.新增用户数据
            // const res = await newPageData(pageName, pageData);
            await newPageData(pageName, { ...pageData, ...paramsType }).then((res: any) => {
                if (res.code === 200) {
                    ElMessage.success('新增成功');
                    flag = res;
                } else {
                    ElMessage.error(res.msg);
                    flag = res;
                }
            });
            await this.getPageListDataAction(pageName, { ...this.pageInfo, ...paramsType });
            return flag;
        },
        async editPageDataAction(pageName: string, pageData: any, paramsType?: object) {
            // 4.修改用户数据
            await editPageData(pageName, { ...pageData, ...paramsType }).then((res: any) => {
                if (res.code === 200) {
                    ElMessage.success('更新成功');
                } else {
                    ElMessage.error(res.msg);
                }
            });
            await this.getPageListDataAction(pageName, { ...this.pageInfo, ...paramsType });
        },
        async detailPageDataAction(pageName: string, urlId: any) {
            // 5.获取用户详情数据
            const res = await getPageDetail(pageName, urlId);
            this.pageDetailInfo = res.data;
            cache.local.setJSON('pageDetailInfo', this.pageDetailInfo);
            return res.data;
        },
        // 获取页面详情, 应对特殊情况的详情接口
        detailPageEval(data: any) {
            this.pageDetailInfo = data;
        },
        // 获取页面操作类型
        getDetailType(data: string) {
            this.pageOperateType = data;
        },
    },
});

export default useSystemStore;
