import {
  getPageListData,
  deletePageData,
  createPageData,
  editPageData,
} from '@/api/main/system/system'

import { listConfig } from '@/api/system/config'

import { addDateRange,handleTree } from '@/utils/ruoyi'
import { arrayToMap } from '@/utils/index'

import { ElMessage } from 'element-plus'

// const { proxy } = getCurrentInstance();
// deletePageData
const useSystemStore = defineStore('system', {
  state: () => ({
    listData: [],
    pageFlag:true,
    dataCount: 0,
    treelistData: [],
    systemConfig: {},
  }),
  actions: {
    setPageFlag(value){
      this.pageFlag = value
    },
    async setSystemConfig() {
      const confs = await listConfig({
        pageNum: 1,
        pageSize: 1000,
      })
      if (confs.data) {
        this.systemConfig = arrayToMap(confs.data, 'configKey')
      }
    },
    async getPageListAction(payload) {
      // 1.获取pageUrl pageName  /system/post/list
      const pageName = payload.pageName
      const pageUrl = `${pageName}`
      // 2.对页面发送请求  /list addDateRange(payload.queryInfo)
      const pageResult = await getPageListData(
        pageUrl,
        addDateRange(payload.queryInfo)
      )
      this.listData = pageResult.data
      this.dataCount = pageResult.total
      // this.treelistData = handleTree(pageResult.data,'deptId')
      if(payload.pageName == '/system/dept'){
        // this.listData = handleTree(pageResult.data,'deptId')
      }
    },
    async deletePageDataAction(payload) {
      // 1.获取pageName和id
      const { pageName, id, subimitLoding = true } = payload
      const pageUrl = `${pageName}/${id}`
      // 2.调用删除网络请求
      await deletePageData(pageUrl)
      ElMessage.success('删除成功')
      // 3.重新请求最新的数据
      // if (subimitLoding) {
      //   this.getPageListAction({
      //     pageName: payload.pageName,
      //     queryInfo: {
      //       pageNum: 1,
      //       pageSize: 10
      //     }
      //   })
      // }

    },
    async createPageDataAction(payload) {
      // 1.创建数据的请求
      const { pageName, newData, subimitLoding = true, pageNum = 1, pageSize = 10 } = payload
      const pageUrl = `${pageName}`
      const res = await createPageData(pageUrl, newData)

      // 2.请求最新的数据
      if (subimitLoding) {
        this.getPageListAction({
          pageName: payload.pageName,
          queryInfo: {
            pageNum,
            pageSize,
          }
        })
      }

      return res;

    },
    async editPageDataAction(payload) {
      // 1.编辑数据的请求
      const { pageName, editData, id, idName, subimitLoding = true, pageNum = 1, pageSize = 10 } = payload
      const pageUrl = `${pageName}`
      editData[idName] = id
      const res = await editPageData(pageUrl, editData)
      // console.log(res, 'res')
      // 2.请求最新的数据
      if (subimitLoding) {
        this.getPageListAction({
          pageName: payload.pageName,
          queryInfo: {
            ...payload.searchInfo,
            pageNum,
            pageSize,
          }
        })
      }

      return res;
      
    },
    async importPageDataAction(payload) {
      const { pageName, newData } = payload
      const pageUrl = `${pageName}`
      await createPageData(pageUrl, newData)

      // // 2.请求最新的数据
      // if (subimitLoding) {
      //   this.getPageListAction({
      //     pageName: payload.pageName,
      //     queryInfo: {
      //       pageNum: 1,
      //       pageSize: 10
      //     }
      //   })
      // }
      
    }
  }
})

export default useSystemStore
