import { Session } from "@/utils/storage"
import { API } from "@/axios/api/index.js"
import Vue from "vue"
import Router from "vue-router"
import { routesFormat } from "@/utils/handleRoutes"
Vue.use(Router)


import { formatMenulist } from "@/utils/handleRoutes"
const user = {
  state: {
    permissionList: Session.get("per-list") || [],
    menuList: [],
  },

  mutations: {
    SET_PERMISSION: (state, list) => {
      const permissionList = list.map((item) => {
        return {
          route: item.route,
        }
      })
      Session.set("per-list", permissionList)
      state.permissionList = permissionList
    },
    SET_MENU: (state, list) => {
      state.menuList = list
    },
  },
  actions: {
    set_menu: ({ state, commit }) => {
      return new Promise((resolve, reject) => {
        API.getNavigation().then(
          (res) => {
           // res=[{"code":1,"object":[{"id":"5e9fa5862b85a707092aaddf","parentId":"root","menuType":1,"route":"workbench","index":"00","component":"workbench/index","path":"/workbench","name":"工作台","icon":"_workbench","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e167f01eb67ee5dc2318cce","parentId":"root","menuType":1,"route":"datareport","index":"01","component":"data-report/DataReport","path":"/datareport","name":"数据录入","icon":"md-cloud-upload","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5fac9c808447ec8e3b681e96","parentId":"root","menuType":1,"route":"disease-class","index":"02","component":"data-manage/DiseaseClass","path":"/datamanage/disease-class","name":"疾病分类管理","icon":"md-stats","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5fac9c808447ec8e3b681e96","parentId":"root","menuType":1,"route":"disease-manage","index":"02","component":"data-manage/DiseaseManage","path":"/datamanage/disease-manage","name":"肠道疾病管理","icon":"md-stats","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e167f43eb67ee5dc2318cd2","parentId":"root","menuType":1,"route":"systemreport","index":"07","component":"system-report/SystemReport","path":"/systemreport","name":"系统公告","icon":"md-megaphone","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5f3a37ca2b85a745c6364c17","parentId":"root","menuType":1,"route":"screening-communication","index":"19","component":"screening-communication/index","path":"/screening-communication","name":"筛查沟通","icon":"ios-text","isShow":0,"isCharge":0,"subPermissions":null},{"id":"60aef9712b85a74babe03fb7","parentId":"root","menuType":1,"route":"/tem-manange","index":"49","component":"","path":"/tem-manange","name":"模板管理","icon":"ios-albums","isShow":1,"isCharge":0,"subPermissions":[{"id":"5e167f31eb67ee5dc2318cd1","parentId":"60aef9712b85a74babe03fb7","menuType":1,"route":"templatmanage","index":"06","component":"/templat-manage/TemplatManage","path":"/templatmanage","name":"模板管理","icon":"_mouldboard","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e1c29f2eb67ee2d893b89b5","parentId":"60aef9712b85a74babe03fb7","menuType":1,"route":"sms-tpl","index":"11","component":"/sms-tpl/index","path":"/sms-tpl","name":"通知短信模板","icon":"md-mail","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e69bd942b85a77983e11d55","parentId":"60aef9712b85a74babe03fb7","menuType":1,"route":"notice-manage","index":"12","component":"notice-manage/index","path":"/notice-manage","name":"通知管理","icon":"md-chatboxes","isShow":1,"isCharge":0,"subPermissions":null},{"id":"6084c71d2b85a72252ae8471","parentId":"60aef9712b85a74babe03fb7","menuType":1,"route":"sms-follow","index":"12","component":"/sms-follow/index","path":"/sms-follow","name":"随访短信模板","icon":"md-mail","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5eb3ad1a2b85a76c83388952","parentId":"60aef9712b85a74babe03fb7","menuType":1,"route":"consent","index":"18","component":"/consent/index","path":"/consent","name":"知情同意书管理","icon":"ios-albums","isShow":0,"isCharge":0,"subPermissions":null}]},{"id":"60aef8e22b85a74babe03fb2","parentId":"root","menuType":1,"route":"/systems","index":"50","component":"","path":"/systems","name":"系统管理","icon":"ios-construct","isShow":1,"isCharge":0,"subPermissions":[{"id":"5e1686cbeb67ee5dc2318cd6","parentId":"60aef8e22b85a74babe03fb2","menuType":1,"route":"accountinfo","index":"05","component":"account-information/AccountInfo","path":"/accountinfo","name":"用户管理","icon":"md-person","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e1d4d80eb67ee5952fda874","parentId":"60aef8e22b85a74babe03fb2","menuType":1,"route":"wechat","index":"13","component":"/wechat/index","path":"/wechat","name":"小程序用户管理","icon":"ios-contact","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e4276eceb67ee56491680cd","parentId":"60aef8e22b85a74babe03fb2","menuType":1,"route":"role","index":"30","component":"role/index","path":"/role","name":"角色管理","icon":"md-person","isShow":1,"isCharge":0,"subPermissions":null}]},{"id":"5e6887dd2b85a758e32f214b","parentId":"root","menuType":2,"route":"button","index":"99","component":null,"path":null,"name":"特殊权限","icon":"","isShow":1,"isCharge":0,"subPermissions":[{"id":"5e6888082b85a758e32f214c","parentId":"5e6887dd2b85a758e32f214b","menuType":2,"route":"BULKNOTICE","index":"01","component":null,"path":null,"name":"批量通知","icon":"","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e6888402b85a758e32f214d","parentId":"5e6887dd2b85a758e32f214b","menuType":2,"route":"download","index":"02","component":null,"path":null,"name":"数据-下载","icon":"","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e6888792b85a758e32f214f","parentId":"5e6887dd2b85a758e32f214b","menuType":2,"route":"SMSAUDIT","index":"04","component":null,"path":null,"name":"短信审核","icon":"","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e688b942b85a758e32f2150","parentId":"5e6887dd2b85a758e32f214b","menuType":2,"route":"FILLIN","index":"06","component":null,"path":null,"name":"填报权限","icon":"","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e869be3eb67ee171a3323f1","parentId":"5e6887dd2b85a758e32f214b","menuType":2,"route":"sysc","index":"07","component":null,"path":null,"name":"实验室质控","icon":"","isShow":0,"isCharge":0,"subPermissions":null},{"id":"5e869c0aeb67ee171a3323f2","parentId":"5e6887dd2b85a758e32f214b","menuType":2,"route":"EQC","index":"08","component":null,"path":null,"name":"内镜质控","icon":"","isShow":0,"isCharge":0,"subPermissions":null}]}],"error":null,"message":null,"exception":null,"result":null}];
            if (res.code === 1) {
              const d = res.object
            // const d = [{"id":"5e9fa5862b85a707092aaddf","parentId":"root","menuType":1,"route":"workbench","index":"00","component":"workbench/index","path":"/workbench","name":"工作台","icon":"_workbench","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e167f01eb67ee5dc2318cce","parentId":"root","menuType":1,"route":"datareport","index":"01","component":"data-report/DataReport","path":"/datareport","name":"数据录入","icon":"md-cloud-upload","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5fac9c808447ec8e3b681e96","parentId":"root","menuType":1,"route":"disease-class","index":"02","component":"data-manage/DiseaseClass","path":"/datamanage/disease-class","name":"疾病分类管理","icon":"md-stats","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5fac9c808447ec8e3b681e97","parentId":"root","menuType":1,"route":"disease-manage","index":"02","component":"data-manage/DiseaseManage","path":"/datamanage/disease-manage","name":"肠道疾病管理","icon":"ios-funnel","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e167f43eb67ee5dc2318cd2","parentId":"root","menuType":1,"route":"systemreport","index":"07","component":"system-report/SystemReport","path":"/systemreport","name":"系统公告","icon":"md-megaphone","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5f3a37ca2b85a745c6364c17","parentId":"root","menuType":1,"route":"screening-communication","index":"19","component":"screening-communication/index","path":"/screening-communication","name":"筛查沟通","icon":"ios-text","isShow":0,"isCharge":0,"subPermissions":null},{"id":"60aef9712b85a74babe03fb7","parentId":"root","menuType":1,"route":"/tem-manange","index":"49","component":"","path":"/tem-manange","name":"模板管理","icon":"ios-albums","isShow":1,"isCharge":0,"subPermissions":[{"id":"5e167f31eb67ee5dc2318cd1","parentId":"60aef9712b85a74babe03fb7","menuType":1,"route":"templatmanage","index":"06","component":"/templat-manage/TemplatManage","path":"/templatmanage","name":"模板管理","icon":"_mouldboard","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e1c29f2eb67ee2d893b89b5","parentId":"60aef9712b85a74babe03fb7","menuType":1,"route":"sms-tpl","index":"11","component":"/sms-tpl/index","path":"/sms-tpl","name":"通知短信模板","icon":"md-mail","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e69bd942b85a77983e11d55","parentId":"60aef9712b85a74babe03fb7","menuType":1,"route":"notice-manage","index":"12","component":"notice-manage/index","path":"/notice-manage","name":"通知管理","icon":"md-chatboxes","isShow":1,"isCharge":0,"subPermissions":null},{"id":"6084c71d2b85a72252ae8471","parentId":"60aef9712b85a74babe03fb7","menuType":1,"route":"sms-follow","index":"12","component":"/sms-follow/index","path":"/sms-follow","name":"随访短信模板","icon":"md-mail","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5eb3ad1a2b85a76c83388952","parentId":"60aef9712b85a74babe03fb7","menuType":1,"route":"consent","index":"18","component":"/consent/index","path":"/consent","name":"知情同意书管理","icon":"ios-albums","isShow":0,"isCharge":0,"subPermissions":null}]},{"id":"60aef8e22b85a74babe03fb2","parentId":"root","menuType":1,"route":"/systems","index":"50","component":"","path":"/systems","name":"系统管理","icon":"ios-construct","isShow":1,"isCharge":0,"subPermissions":[{"id":"5e1686cbeb67ee5dc2318cd6","parentId":"60aef8e22b85a74babe03fb2","menuType":1,"route":"accountinfo","index":"05","component":"account-information/AccountInfo","path":"/accountinfo","name":"用户管理","icon":"md-person","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e1d4d80eb67ee5952fda874","parentId":"60aef8e22b85a74babe03fb2","menuType":1,"route":"wechat","index":"13","component":"/wechat/index","path":"/wechat","name":"小程序用户管理","icon":"ios-contact","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e4276eceb67ee56491680cd","parentId":"60aef8e22b85a74babe03fb2","menuType":1,"route":"role","index":"30","component":"role/index","path":"/role","name":"角色管理","icon":"md-person","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e4276eceb67ee56491680ca","parentId":"60aef8e22b85a74babe03fb2","menuType":1,"route":"followup-rule","index":"30","component":"followup-rule/index","path":"/followup-rule","name":"随访规则维护","icon":"md-funnel","isShow":1,"isCharge":0,"subPermissions":null}]},{"id":"5e6887dd2b85a758e32f214b","parentId":"root","menuType":2,"route":"button","index":"99","component":null,"path":null,"name":"特殊权限","icon":"","isShow":1,"isCharge":0,"subPermissions":[{"id":"5e6888082b85a758e32f214c","parentId":"5e6887dd2b85a758e32f214b","menuType":2,"route":"BULKNOTICE","index":"01","component":null,"path":null,"name":"批量通知","icon":"","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e6888402b85a758e32f214d","parentId":"5e6887dd2b85a758e32f214b","menuType":2,"route":"download","index":"02","component":null,"path":null,"name":"数据-下载","icon":"","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e6888792b85a758e32f214f","parentId":"5e6887dd2b85a758e32f214b","menuType":2,"route":"SMSAUDIT","index":"04","component":null,"path":null,"name":"短信审核","icon":"","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e688b942b85a758e32f2150","parentId":"5e6887dd2b85a758e32f214b","menuType":2,"route":"FILLIN","index":"06","component":null,"path":null,"name":"填报权限","icon":"","isShow":1,"isCharge":0,"subPermissions":null},{"id":"5e869be3eb67ee171a3323f1","parentId":"5e6887dd2b85a758e32f214b","menuType":2,"route":"sysc","index":"07","component":null,"path":null,"name":"实验室质控","icon":"","isShow":0,"isCharge":0,"subPermissions":null},{"id":"5e869c0aeb67ee171a3323f2","parentId":"5e6887dd2b85a758e32f214b","menuType":2,"route":"EQC","index":"08","component":null,"path":null,"name":"内镜质控","icon":"","isShow":0,"isCharge":0,"subPermissions":null}]}]
              const menulist = formatMenulist(d)
              commit("SET_PERMISSION", formatMenulist(d, 2))
              commit("SET_MENU", menulist)
              resolve(menulist)
            }
            else{
              // next(`/login`)
              const menulist = []
              commit("SET_MENU", menulist)
              resolve(menulist)
              // return 0
            }
          },
          (err) => {
            console.log("menu_err")
            reject(err)
          }
        )
      })
    },
    logout({ commit, state, dispatch }) {
      return new Promise((resolve, reject) => {
        commit("SET_PERMISSION", [])
        commit("SET_MENU", [])
        localStorage.setItem("token", "")
        localStorage.setItem("etoken", '')
        localStorage.setItem("diseaseType", '')
        commit("DEL_DICT")
        resolve()
      })
    },
  },
}

export default user
