import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios'
import router from '../router';
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:{},
    activeApp:null,
    appname:'',
    menuApp:[],
    sys_modules:[],
    searchValue:'',
    closeDrawerTemp:false,
    DrawerShow:false,
    historyRouter:null,
    pageCache:[],
    svlistData:[]
  },
  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,
    activeApp:state => state.activeApp,
    appname:state => state.appname,
    menuApp:state => state.menuApp,
    searchValue:state => state.searchValue,
    closeDrawerTemp:state => state.closeDrawerTemp,
    DrawerShow:state => state.DrawerShow,
    sys_modules:state => state.sys_modules,
    historyRouter:state => state.historyRouter,
    isRouterAlive:state => state.isRouterAlive,
    pageCache: state => state.pageCache,
    svlistData:state => state.svlistData,
    
  },
  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,data) {
      //头部区域搜索所传入的数值
      state.searchValue = data.val

      
      data.app?sessionStorage.setItem('activeApp',JSON.stringify(data.app)):''
      state.appname = data.name
      state.activeApp = JSON.parse(sessionStorage.getItem('activeApp'))
      
    },
    systemModules (state,data) {
      state.sys_modules = data
    },
    setAppMenu (state,data) {
      state.menuApp = data
    },
    changeDetailDrawer (state,bool) {
      state.closeDrawerTemp = bool
    },
    DrawerShowChange (state,bool) {
      state.DrawerShow = bool
    },
    setHistoryRouter (state,route) {
      state.historyRouter = route
    },
    detailDeleteRouterBack (state) {
      if (state.historyRouter) {
        router.replace(state.historyRouter)
        state.historyRouter = null
      } else {
        state.closeDrawerTemp = false
      }
    },
    clearSearchValue (state) {
      state.searchValue = ''
    },
    setPageCache(state,res) {
      state.pageCache = res
    },
    saveListData (state,data) {
      state.svlistData = 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"
        }
      })
      console.log(res.data)
      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)
    },
    getModules ({commit}, data) {
      if (data.type === 'system') {
        commit('setActiveApp',{name:data.mod[0].apps[0].systemappname,app:data.mod[0].apps[0],val:''})
        
        sessionStorage.setItem('active_modules',JSON.stringify(data.mod[0]))

        let inactivePath = data.mod[0].apps[0].path

        router.push({path:inactivePath})

      }
      commit('systemModules',data.mod)
    },
    setSSystemModules ({commit}, data) {
      commit('systemModules',data)
    },
    // 设置活动中的应用
    setActiveApp ({commit}, data) {
      commit('setActiveApp',data)
    },
    changeDetailDrawer ({commit}, bool) {
      commit('changeDetailDrawer',bool)
    },
    DrawerShowChange ({commit},bool) {
      commit('DrawerShowChange',bool)
    },

    //数据绑定附件媒体
    bindFileToData ({commit},data) {
      return new Promise(async (reslove,reject)=>{
        let obj = {
          "classname": "system.attachment.Attachment",
          "method": "createFileLink",
          "content": {
            "ownertable": data.ownertable,
            "ownerid": data.ownerid,
            "usetype": data.usetype,
            "attachmentids": data.attachmentids
          }
        }
        const res = await api.requested(obj)
        reslove(res)
      })
    },

    // 设置历史路由用于返回记录的地址
    setHistoryRouter ({commit},route) {
      commit('setHistoryRouter',route)
    },

    // 历史路由删除后返回上一页操作
    detailDeleteRouterBack ({commit},route) {
      commit('detailDeleteRouterBack',route)
    },

    clearSearchValue ({commit},data) {
      commit('clearSearchValue')
    },

    saveListData ({commit},data) {
      commit('saveListData',data)
    },

  },
  modules: {
  },
});
