import storage from 'store'
import {
  login,
  UserInfo,
  logout
} from '@/api/login'
import {
  ACCESS_TOKEN
} from '@/store/mutation-types'
import {
  welcome
} from '@/utils/util'

const user = {
  state: {
    token: '',
    name: '',
    welcome: '',
    avatar: '',
    roles: [],
    info: {}
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, {
      name,
      welcome
    }) => {
      state.name = name
      state.welcome = welcome
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_INFO: (state, info) => {
      state.info = info
    }
  },

  actions: {
    // 登录
    Login({
      commit
    }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          if (response.code === 1) {
            const result = response.data
            storage.set(ACCESS_TOKEN, result, 7 * 24 * 60 * 60 * 1000)
            commit('SET_TOKEN', result)
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({
      commit
    }) {
      return new Promise((resolve, reject) => {
        UserInfo().then(response => {
          if (response.code === 1) {
            // console.log(response)
            const data123 = {
              menulist: [{
                  'name': 'home',
                  'parentid': '00000000-0000-0000-0000-000000000000',
                  'id': 1,
                  'icon': 'home',
                  'title': '首页',
                  'show': true,
                  'component': 'home',
                  'redirect': '',
                  'sortcode': 1,
                  'path': '/home',
                  'createtime': '2020-09-10 16:14:59',
                  'id': 'bf681493-ebbb-41f4-ac44-be02e1a6bd97',
                },
                {
                  'name': 'device',
                  'parentid': '00000000-0000-0000-0000-000000000000',
                  'id': 'cb242636-1645-4b8b-b23c-6b81df0edf82',
                  'createtime': '2020-09-10 17:47:52',
                  'title': '设备',
                  'icon': 'api',
                  'show': true,
                  'component': 'RouteView',
                  'redirect': '/device/list',
                  'path': '/device',
                  'sortcode': 2
                },
                {
                  'name': 'devicelist',
                  'parentid': 'cb242636-1645-4b8b-b23c-6b81df0edf82',
                  'id': '8f566a86-9859-4f2c-ba9b-98d620afeb5c',
                  'title': '设备管理',
                  'show': true,
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'device/list',
                  'path': '/device/list',
                  'redirect': '/device/list',
                  'sortcode': 1,
                },
                {
                  'name': 'resultlist',
                  'parentid': 'cb242636-1645-4b8b-b23c-6b81df0edf82',
                  'id': 'e11a4c02-532d-4937-9579-b36902367d0c',
                  'title': '测量记录',
                  'show': true,
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'device/result',
                  'path': '/device/result',
                  'sortcode': 1,
                },
                {
                  'name': 'Bindcountlist',
                  'parentid': 'cb242636-1645-4b8b-b23c-6b81df0edf82',
                  'id': '8f566a86-9859-4f2c-ba9b-98d620afeb452',
                  'title': '绑定用户关系管理',
                  'show': false,
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'device/Bindcount',
                  'path': '/device/Bindcount',
                  'sortcode': 1,
                },
                //
                {
                  'name': 'official',
                  'parentid': '00000000-0000-0000-0000-000000000000',
                  'id': '09036169-x',
                  'createtime': '2020-09-10 17:47:52',
                  'title': '公众号设置',
                  'icon': 'global',
                  'show': true,
                  'component': 'RouteView',
                  'redirect': '/official/List',
                  'path': '/official',
                  'sortcode': 2
                },
                {
                  'name': 'officiallist',
                  'parentid': '09036169-x',
                  'id': '637e37c1-22fd-4a20-9768-8f1cb6ad085f',
                  'title': '公众号管理',
                  'show': true,
                  'redirect': '',
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'official/List',
                  'path': '/official/List',
                  'sortcode': 1,
                },
                {
                  'name': 'officialempower',
                  'parentid': '09036169-x',
                  'id': '637e37c1-22fd-4a20-9768-8f1cb6ad0852',
                  'title': '公众号授权',
                  'show': false,
                  'redirect': '',
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'official/empower',
                  'path': '/official/empower',
                  'sortcode': 1,
                },
                {
                  'name': 'officialtemplate',
                  'parentid': '09036169-x',
                  'id': '4544dfe7-6d27-43a0-87bb-ce4f21084a6b',
                  'title': '模板管理',
                  'show': true,
                  'redirect': '',
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'official/template',
                  'path': '/official/template',
                  'sortcode': 1,
                },
                // 营销管理
                {
                  'name': 'market',
                  'parentid': '00000000-0000-0000-0000-000000000000',
                  'id': '09036169-y',
                  'createtime': '2020-09-10 17:47:52',
                  'title': '营销管理',
                  'icon': 'tags',
                  'show': true,
                  'component': 'RouteView',
                  'redirect': '/market/tag',
                  'path': '/market',
                  'sortcode': 2
                },
                {
                  'name': 'markettag',
                  'parentid': '09036169-y',
                  'id': '637e37c1-22fd-4a20-9768-8f1cb6ad085a',
                  'title': '标签管理',
                  'show': true,
                  'redirect': '',
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'market/tag',
                  'path': '/market/tag',
                  'sortcode': 1,
                },
                {
                  'name': 'marketmessage',
                  'parentid': '09036169-y',
                  'id': '4544dfe7-6d27-43a0-87bb-ce4f21084a6c',
                  'title': '消息群发',
                  'show': true,
                  'redirect': '',
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'market/message',
                  'path': '/market/message',
                  'sortcode': 1,
                },
                // 用户管理
                {
                  'name': 'reguser',
                  'parentid': '00000000-0000-0000-0000-000000000000',
                  'id': '09036169-u',
                  'createtime': '2020-09-10 17:47:52',
                  'title': '用户管理',
                  'icon': 'team',
                  'show': true,
                  'component': 'RouteView',
                  'redirect': '/reguser/list',
                  'path': '/reguser',
                  'sortcode': 2
                },
                {
                  'name': 'reguserlist',
                  'parentid': '09036169-u',
                  'id': '637e37c1-22fd-4a20-9768-8f1cb6ad085z',
                  'title': '注册用户管理',
                  'show': true,
                  'redirect': '',
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'reguser/list',
                  'path': '/reguser/list',
                  'sortcode': 1,
                },
                {
                  'name': 'reguserbind',
                  'parentid': '09036169-u',
                  'id': '4544dfe7-6d27-43a0-87bb-ce4f21084a6zz',
                  'title': '绑定用户管理',
                  'show': true,
                  'redirect': '',
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'reguser/bind',
                  'path': '/reguser/bind',
                  'sortcode': 1,
                },
                {
                  'name': 'reguserresultlist',
                  'parentid': '09036169-u',
                  'id': '4544dfe7-6d27-43a0-87bb-ce4f21084a6cl',
                  'title': '用户测量记录',
                  'show': false,
                  'redirect': '',
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'reguser/resultlist',
                  'path': '/reguser/resultlist',
                  'sortcode': 1,
                },
                // 渠道管理
                {
                  'name': 'business',
                  'parentid': '00000000-0000-0000-0000-000000000000',
                  'id': 1,
                  'icon': 'apartment',
                  'title': '渠道管理',
                  'show': true,
                  'component': 'business',
                  'redirect': '',
                  'sortcode': 1,
                  'path': '/business',
                  'createtime': '2020-09-10 16:14:59',
                  'id': 'bf681493-ebbb-41f4-ac44-be02e1a6bd97',
                },
                //
                {
                  'name': 'report',
                  'parentid': '00000000-0000-0000-0000-000000000000',
                  'id': '09036169-s',
                  'createtime': '2020-09-10 17:47:52',
                  'title': '数据分析',
                  'icon': 'project',
                  'show': true,
                  'component': 'RouteView',
                  'redirect': '/report/tag',
                  'path': '/report',
                  'sortcode': 4
                },
                {
                  'name': 'reporttag',
                  'parentid': '09036169-s',
                  'id': '637e37c1-22fd-4a20-9768-8f1cb6ad085sj',
                  'title': '标签数据分析',
                  'show': true,
                  'redirect': '',
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'report/tag',
                  'path': '/report/tag',
                  'sortcode': 1,
                },
                {
                  'name': 'reportuser',
                  'parentid': '09036169-s',
                  'id': '4544dfe7-6d27-43a0-87bb-ce4f21084a6yh',
                  'title': '用户画像',
                  'show': true,
                  'redirect': '',
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'report/user',
                  'path': '/report/user',
                  'sortcode': 1,
                },
                {
                  'name': 'reporthealth',
                  'parentid': '09036169-s',
                  'id': '637e37c1-22fd-4a20-9768-8f1cb6ad085jk',
                  'title': '健康数据分析',
                  'show': true,
                  'redirect': '',
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'report/health',
                  'path': '/report/health',
                  'sortcode': 1,
                },
                {
                  'name': 'reportmeasure',
                  'parentid': '09036169-s',
                  'id': '4544dfe7-6d27-43a0-87bb-ce4f21084a6tj',
                  'title': '测量统计',
                  'show': true,
                  'redirect': '',
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'report/measure',
                  'path': '/report/measure',
                  'sortcode': 1,
                },
                // 个人中心
                {
                  'name': 'user',
                  'parentid': '00000000-0000-0000-0000-000000000000',
                  'id': '09036169-g',
                  'createtime': '2020-09-10 17:47:52',
                  'title': '个人中心',
                  'icon': 'setting',
                  'show': true,
                  'component': 'RouteView',
                  'redirect': '/user/base',
                  'path': '/user',
                  'sortcode': 4
                },
                {
                  'name': 'userpassword',
                  'parentid': '09036169-g',
                  'id': '4544dfe7-6d27-43a0-87bb-ce4f21084a6mm',
                  'title': '修改密码',
                  'show': true,
                  'redirect': '',
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'user/password',
                  'path': '/user/password',
                  'sortcode': 1,
                },
                {
                  'name': 'userchat',
                  'parentid': '09036169-g',
                  'id': '637e37c1-22fd-4a20-9768-8f1cb6ad085kf',
                  'title': '微信客服',
                  'show': true,
                  'createtime': '2020-09-10 17:48:40',
                  'path': 'https://www.baidu.com/',
                  'sortcode': 1,
                  'target': '_blank',
                },
                {
                  'name': 'userbase',
                  'parentid': '09036169-g',
                  'id': '637e37c1-22fd-4a20-9768-8f1cb6ad085jb',
                  'title': 'QQ客服',
                  'show': true,
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'http://wpa.qq.com/msgrd?v=3&uin=494814623&site=qq&menu=yes',
                  // 'redirect': '',
                  'target': '_blank',
                  'path': 'http://wpa.qq.com/msgrd?v=3&uin=494814623&site=qq&menu=yes',
                  'sortcode': 1,
                },
                // 三级addList
                {
                  'name': 'addList',
                  'parentid': '09036169-x',
                  'id': '637e37c1-22fd-4a20-9768-8f1cb6ad084gh',
                  'title': '公众号管理',
                  'show': false,
                  'redirect': '',
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'official/components/addList',
                  'path': '/official/components/addList',
                  'sortcode': 1,
                },
                {
                  'name': 'officialdeviceList',
                  'parentid': '09036169-x',
                  'id': '637e37c1-22fd-4a20-9768-8f1cb6ad084sbbd',
                  'title': '设备绑定',
                  'show': false,
                  'redirect': '',
                  'createtime': '2020-09-10 17:48:40',
                  'component': 'official/deviceList',
                  'path': '/official/deviceList',
                  'sortcode': 1,
                },

              ],
              realname: '管理员'
            }
            const result = data123
            var list = []
            if (result.menulist && result.menulist.length > 0) {
              // 遍历菜单进行拼接
              result.menulist.map((per) => {
                if (per.parentid === '00000000-0000-0000-0000-000000000000') {
                  var json = {
                    name: per.name,
                    key: per.id,
                    path: per.path,
                    parentid: per.parentid,
                    component: per.component,
                    children: [],
                    meta: {
                      title: per.title,
                      icon: per.icon,
                      show: per.show,
                    }
                  }
                  if (per.redirect && per.redirect.length > 0) {
                    json['redirect'] = per.redirect
                  }
                  list.push(json)
                }
              })
              result.menulist.map((per) => {
                for (var i = 0; i < list.length; i++) {
                  if (per.parentid === list[i].key) {
                    list[i].children.push({
                      key: per.id,
                      path: per.path,
                      component: per.component,
                      children: [],
                      parentid: per.parentid,
                      meta: {
                        title: per.title,
                        icon: per.icon,
                        show: per.show,
                        target: per.target,
                        hideChildren: true,
                      }
                    })
                  }
                  // 三级隐藏菜单
                  // for (var j = 0; i < list[i].children.length; i++) {
                  //   if (list[i].children[j].key === per.id) {
                  //     list[i].children[j].children.push({
                  //       key: per.id,
                  //       path: per.path,
                  //       component: per.component,
                  //       children: [],
                  //       parentid: per.parentid,
                  //       meta: {
                  //         title: per.title,
                  //         // show: per.show,
                  //         target: per.target,
                  //       }
                  //     })
                  //   }
                  // }
                }
              })
              commit('SET_ROLES', list)
              commit('SET_INFO', result)
              commit('SET_NAME', {
                name: result.realname,
                welcome: welcome()
              })
              commit('SET_AVATAR', result.avatar)
              resolve(list)
            } else {
              reject(new Error('getInfo: roles must be a non-null array !'))
            }
          }
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 登出
    Logout({
      commit,
      state
    }) {
      return new Promise((resolve) => {
        logout(state.token).then(() => {
          resolve()
        }).catch(() => {
          resolve()
        }).finally(() => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          storage.remove(ACCESS_TOKEN)
        })
      })
    }

  }
}

export default user
