import { dataBaseTypeListApi, dataPermissionListApi, dataPermissionTypeListApi } from '@/api/base/basedata';
import type { ListIitem } from '@/api/base/BaseDataModel';
import { getLeftTreeApi } from '@/api/user/user';
import { isArrayLength } from '@/utils/is';
import { resCodeSuccess, useReturnData } from '@/utils/useReturnData';
import { defineStore } from 'pinia'
//定义state类型
export type SystemTypeState<T = ListIitem> = {
    dataPermissionTypeList: T[]//数据权限类型
    dataBaseTypeList: T[]//数据库类型
    dataPermissionList: T[]//数据权限
    leftTreeData: Recordable[]
}
const { resData } = useReturnData()
//定义` 状态 `store
export const useStateSystemType = defineStore('list_systemType', {
    state: (): SystemTypeState => {
        return {
            dataPermissionTypeList: [],//数据权限类型
            dataBaseTypeList: [],//数据库类型
            dataPermissionList: [],//数据权限
            leftTreeData: [],//部门树数据
        }
    },
    actions: {
        /**
         * 数据库类型
         */
        async addDataBaseTypeList() {
            if (this.dataBaseTypeList.length > 0) return;
            const res = await dataBaseTypeListApi()
            this.dataBaseTypeList = resData(res)
        },
        /**
         * 数据权限类型
         */
        async addDataPermissionTypeList() {
            if (this.dataPermissionTypeList.length > 0) return;
            const res = await dataPermissionTypeListApi()
            this.dataPermissionTypeList = resData(res)
        },
        /**
         * 数据权限
         */
        async addDataPermissionList() {
            if (this.dataPermissionList.length > 0) return;
            const res = await dataPermissionListApi()
            this.dataPermissionList = resData(res)
        },
        /**
         * 存储部门树数据
         * @param list 
         */
        async addLeftTreeData(list = []) {
            if(!isArrayLength(list)) {
                if(this.leftTreeData.length > 0) return
                const res = await getLeftTreeApi();
                if(resCodeSuccess(res)) {
                    this.leftTreeData = res.data
                }
                return
            }
            this.leftTreeData = list
        }
    },
    getters: {
        /**
         * 数据库类型
         * @returns 
         */
        getDataBaseTypeList(state) {
            return state.dataBaseTypeList;
        },
        /**
         * 数据权限类型
         * @returns 
         */
        getDataPermissionTypeList(state) {
            return state.dataPermissionTypeList;
        },
        /**
         * 数据权限
         * @returns 
         */
        getDataPermissionList(state) {
            return state.dataPermissionList;
        },
        /**
         * 部门树数据
         */
        getLeftTreeData(state) {
            return state.leftTreeData;
        }
    }
})