import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios'
import api from '../api/api'

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    pageOnlyRead:false,
    loading:false,
    siteinfo:{
      attinfos:[]
    },
    deplist:[],
    sys_options:[],
    shortcutlist:[],
    active_modules:{}
  },
  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
  },
  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
    }
  },
  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.userauth.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)
    },

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