import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios'
import api from '../api/api'
import tool from '../utils/tool'
Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    pageOnlyRead:false,
    loading:false,
    siteinfo:{
      attinfos:[]
    },
    deplist:[],
    sys_options:[],
    shortcutlist:[],
    active_modules:{},
    checks:[],
    dataToForm:{},
    acitveApp:{},
    menuApp:[]
  },
  getters: {
    siteinfo:state => state.siteinfo,
    loading:state => state.loading,
    deplist:state =>  state.deplist,
    sys_options:state => state.sys_options,
    pageOnlyRead:state => state.pageOnlyRead,
    shortcutlist:state => state.shortcutlist,
    active_modules:state => state.active_modules,
    checks:state => state.checks,
    dataToForm:state => state.dataToForm,
    acitveApp:state => state.acitveApp,
    menuApp:state => state.menuApp,
  },
  mutations: {
    setSiteInfo(state,res) {
      state.siteinfo = res.data
    },
    setDeplist (state,res) {
      state.deplist = res
    },
    setSelectList (state,res) {
      state.sys_options = res.data
    },
    setUsershortcuts (state,param) {
      state.shortcutlist = [...param]
    },
    pageOnlyRead (state,bool) {
      state.pageOnlyRead = bool
    },
    checkClass (state,id) {
      state.checks.push(id)
      state.checks = [...new Set(state.checks)]
    },
    uncheckClass (state,id) {
      state.checks =  state.checks.filter(e=>{
        return e !== id
      })
    },
    sendDataToForm (state,data) {
      state.dataToForm = data
    },
    setActiveApp (state,app) {
      state.acitveApp = app
    },
    setAppMenu (state,data) {
      state.menuApp = data
    },
  },
  actions: {
    // 系统选项分类查询
    optiontypeselect ({commit}, param) {
      return new Promise(async (reslove,reject)=>{
        let obj = {
          "classname": "sysmanage.develop.optiontype.optiontype",
          "method": "optiontypeselect",
          "content": {
              "pageNumber": 1,
              "pageSize": 1000,
              "typename": param,
              "parameter": {
              }
          }
        }
        const res = await api.requested(obj)

        commit('setSelectList',res)
        
        reslove(res)
      })
      
    },
    // 查询站点信息
    async querySiteInfo ({commit}, param) {
      const res = await api.requested(param)
      commit('setSiteInfo',res)
    },
    // 创建下载记录
    async createdownlog ({commit}, param) {
      let obj = {
        "classname": "system.attachment.Attachment",
        "method": "createdownlog",
        "content": {
            "linksid": param.linksid,
            "attachmentid":param.attachmentid
        }
      }
      const res = await api.requested(obj)
    },


    // 存储部门列表数据
    setDeplistData ({commit}, param) {
      commit('setDeplist',param)
    },

    // 设置快捷栏
    async setUsershortcuts ({commit}, param) {
      const res = await api.requested({
        "classname": "sysmanage.develop.userauthforweb.userauth",
        "method": "query_usershortcuts",
        "content": {
        }
      })
      let data = JSON.parse(sessionStorage.getItem('module_info'))
      let modules = []
      data.forEach(mod => {
        modules = modules.concat(mod.modules)
      });
      let arr = []
      res.data.forEach(e=>{
        modules.forEach(mod=>{
          if (mod.systemmoduleid === e.systemmoduleid) {
            mod.issystem = e.issystem
            arr.push(mod)
          }
        })
      })
      commit('setUsershortcuts',arr)
    },
    // 设置应用快捷栏 
    async setAppMenu ({commit}, param) {
      const res = await api.requested({
        "classname": "sysmanage.develop.userauthforweb.userauth",
        "method": "query_usershortcuts",
        "content": {
          "shortcuttype":"systemapp"
        }
      })
      commit('setAppMenu',res.data)
    },
    // 查询标签数据
    async queryTagList ({commit}, param) {
      return new Promise(async (reslove,reject)=>{
        let obj = {
          "id": 20220929085401,
          "content": {
              "ownertable":param.table,
              "ownerid":param.id
          }
        }
        const res = await api.requested(obj)
        reslove(res.data)
      })
    },

    // 设置权限状态
    pageOnlyRead ({commit}, bool) {
      commit('pageOnlyRead',bool)
    },

    checkClass ({commit}, id) {
      commit('checkClass',id)
    },
    uncheckClass ({commit}, id) {
      commit('uncheckClass',id)
    },
    // 主文件向Form中的组件传值
    sendDataToForm ({commit}, data) {
      commit('sendDataToForm',data)
    },
    // 设置活动中的应用
    setActiveApp ({commit}, app) {
      commit('setActiveApp',app)
    },
  },
  modules: {
  },
});
