import Router from "vue-router";
import Home from "../views/Home.vue";
import Vue from 'vue'
import store from "../store/index.js"
Vue.use(Router)
var HOME = "/home"
var userInfo = localStorage.getItem('info')
import i18n from "../utils/i18n.js"
function getChidlren(data, key, value) {
    var hasFound = false, // 表示是否有找到id值
      result = null;
    var fn = function(data) {
      if (!hasFound) { // 判断是否是数组并且没有的情况下，
        data.forEach(item => {
          if (item[key] === value) { // 数据循环每个子项，并且判断子项下边是否有id值
            result = item; // 返回的结果等于每一项
            hasFound = true; // 并且找到id值
          } else if (item.subs) {
            fn(item.subs); // 递归调用下边的子项
          }
        })
      }
    }
    fn(data); // 调用一下
    return result;
  }
  if (userInfo) {
    if(JSON.parse(userInfo).type!=0){
		let authorities=JSON.parse(userInfo)
      let authoritiesList = authorities.authorities.filter(item => (item!='BLUETOOTH_INPUT' && item!='NORMALLY_OPEN_PERMISSION' && item!='FREEZE_PERMISSION' && item!='REMOTE_CONTROL_PERMISSION'))

      let data = ''
      if(authoritiesList[0]=='BLUETOOTH_INPUT'){
        data=getChidlren(i18n.cn.i18n.items, 'key', authoritiesList[1])
      }else{
        data=getChidlren(i18n.cn.i18n.items, 'key', authoritiesList[0])
      }
      HOME=data.index
    }
  }
const routes = [
  {
    path: '/',
    redirect:HOME
  },
  {
    path: '/screen',
    name: 'Screen',
    meta: {
      title: '数据大屏'
    },
    component: () => import('../views/Screen.vue')
  },
  {
    path: "/login",
    name: "Login",
    meta: {
      title: '登录'
    },
    component: () =>
      import("../views/Login.vue")
  }, {
    path: "/",
    name: "Home",
    component: Home,
    children: [
      // {
      //   path: "/home",
      //   name: "home",
      //   meta: {
      //     title: '首页'
      //   },
      //   component: () => import("../views/visualization/home.vue")
      // },
      {
        path: "/home",
        name: "home",
        meta: {
          title: '首页'
        },
        component: () => import("../views/visualization/flatsIndex.vue")
      },
      {
        path: "/getFinger",
        name: "getFinger",
        meta: {
          title: '首页'
        },
        component: () => import("../views/getFinger.vue")
      },
      {
        path: '/datascreen',
        name: 'Datascreen',
        meta: {
          title: '数据大屏'
        },
        component: () => import('../views/Datascreen.vue')
      },
      // {
      //   path: "/homepage",//"/homepage",
      //   name: "homepage",//"homepage",
      //   meta: {
      //     title: '门锁首页'
      //   },
      //   component: () => import("../views/visualization/flatsIndex.vue")
      // },
      {
        path: "/dashboard",
        name: "dashboard",
        meta: {
          title: '宿舍管理'
        },
        component: () =>
          import("../views/Dashboard.vue")
      }, {
        path: "/studentsRoom",
        name: "studentsRoom",
        meta: {
          title: '教室管理'
        },
        component: () =>
          import("../views/studentsRoom.vue")
      }, {
        path: "/books",
        name: "books",
        meta: {
          title: '图书馆'
        },
        component: () =>
          import("../views/books.vue")
      }, {
        path: "/academicBuilding",
        name: "academicBuilding",
        meta: {
          title: '教学楼'
        },
        component: () =>
          import("../views/academicBuilding.vue")
      }, {
        path: "/tabs",
        name: "basetable",
        meta: {
          title: '设备管理'
        },
        component: () =>
          import("../views/BaseCharts.vue")
      }, {
        path: "/doorLock",
        name: "doorLock",
        meta: {
          title: '门锁管理'
        },
        component: () =>
          import("../views/doorLock.vue")
      }, {
        path: "/aNormEquip",
        name: "aNormEquip",
        meta: {
          title: '异常设备'
        },
        component: () =>
          import("../views/aNormEquip.vue")
      }, {
        path: "/airSwitchDevice",
        name: "airSwitchDevice",
        meta: {
          title: '空开设备'
        },
        component: () =>
          import("../views/device/airSwitchDevice.vue")
      }, {
        path: "/smokeSensationDevice",
        name: "smokeSensationDevice",
        meta: {
          title: '烟感设备'
        },
        component: () =>
          import("../views/device/smokeSensationDevice.vue")
      },{
        path: "/AirCleaningDevice",
        name: "AirCleaningDevice",
        meta: {
          title: '空气清洁'
        },
        component: () =>
          import("../views/device/AirCleaningDevice.vue")
      },{
        path: "/humanMotionSensorDevice",
        name: "humanMotionSensorDevice",
        meta: {
          title: '红外传感器'
        },
        component: () =>
          import("../views/device/humanMotionSensorDevice.vue")
      }, {
        path: "/smartButtonDevice",
        name: "smartButtonDevice",
        meta: {
          title: '智能按钮'
        },
        component: () =>
          import("../views/device/smartButtonDevice.vue")
      }, {
        path: "/manholeCoverDevice",
        name: "manholeCoverDevice",
        meta: {
          title: '井盖'
        },
        component: () =>
          import("../views/device/manholeCoverDevice.vue")
      },  {
        path: "/trashCanDevice",
        name: "trashCanDevice",
        meta: {
          title: '垃圾桶'
        },
        component: () =>
          import("../views/device/trashCanDevice.vue")
      },  {
        path: "/airDetectionDevice",
        name: "airDetectionDevice",
        meta: {
          title: '空气检测传感器'
        },
        component: () =>
          import("../views/device/airDetectionDevice.vue")
      },  {
        path: "/gatewayDevice",
        name: "gatewayDevice",
        meta: {
          title: '网关'
        },
        component: () =>
          import("../views/device/gatewayDevice.vue")
      },{
        path: "/acoustoopticAlarm",
        name: "acoustoopticAlarm",
        meta: {
          title: '声光报警'
        },
        component: () =>
          import("../views/device/acoustoopticAlarm.vue")
      }, {
        path: "/userManagement",
        name: "userManagement",
        meta: {
          title: '租客信息'
        },
        component: () =>
          import("../views/userManagement.vue")
      }, {
        path: "/voucher",
        name: "voucher",
        meta: {
          title: '教师信息'
        },
        component: () =>
          import("../views/voucher.vue")
      }, {
        path: "/agreement",
        name: "agreement",
        meta: {
          title: '合同管理'
        },
        component: () =>
          import("../views/agreement.vue")
      },
      // {
      //   path: "/tenantExportTemplate",
      //   name: "tenantExportTemplate",
      //   meta: {
      //     title: '租客导出模板'
      //   },
      //   component: () =>
      //     import("../views/template/tenantExportTemplate.vue")
      // },
      {
        path: "/contractExportTemplate",
        name: "contractExportTemplate",
        meta: {
          title: '合同导出模板'
        },
        component: () =>
          import("../views/template/contractExportTemplate.vue")
      },{
        path: "/CheckIn",
        name: "CheckIn",
        meta: {
          title: '入住办理'
        },
        component: () =>
          import("../views/CheckIn.vue")
      },  {
        path: "/concat",
        name: "concat",
        meta: {
          title: '报警联系人'
        },
        component: () =>
          import("../views/concat.vue")
      }, {
        path: "/offInCardOver",
        name: "offInCardOver",
        meta: {
          title: '离线卡操作'
        },
        component: () =>
          import("../views/offInCard/offInCardOver.vue")
      }, {
        path: "/offInCardList",
        name: "offInCardList",
        meta: {
          title: '离线卡列表'
        },
        component: () =>
          import("../views/offInCard/offInCardList.vue")
      }, {
        path: "/RoomInquiry",
        name: "RoomInquiry",
        meta: {
          title: '房间查询'
        },
        component: () =>
          import("../views/RoomInquiry.vue")
      }, {
        path: "/CheckInStaff",
        name: "CheckInStaff",
        meta: {
          title: '入住租客'
        },
        component: () =>
          import("../views/CheckInStaff.vue")
      }, {
        path: "/Refund",
        name: "Refund",
        meta: {
          title: '退住办理'
        },
        component: () =>
          import("../views/Refund.vue")
      },{
        path: "/Freeze",
        name: "Freeze",
        meta: {
          title: '冻结管理'
        },
        component: () =>
          import("../views/FreezeManagement.vue")
      }, {
        path: "/renewalHandling",
        name: "renewalHandling",
        meta: {
          title: '续住办理'
        },
        component: () =>
          import("../views/renewalHandling.vue")
      }, {
        path: "/VoucherList",
        name: "VoucherList",
        meta: {
          title: '租约列表'
        },
        component: () =>
          import("../views/VoucherList.vue")
      }, {
        path: "/InformationBase",
        name: "InformationBase",
        meta: {
          title: '信息库'
        },
        component: () =>
          import("../views/InformationBase.vue")
      }, {
        path: "/dueRoom",
        name: "dueRoom",
        meta: {
          title: '到期房间'
        },
        component: () =>
          import("../views/dueRoom.vue")
      }, {
        path: "/duePerson",
        name: "duePerson",
        meta: {
          title: '到期租客'
        },
        component: () =>
          import("../views/duePerson.vue")
      }, {
        path: "/loginAccount",
        name: "loginAccount",
        meta: {
          title: '登录账号'
        },
        component: () =>
          import("../views/loginAccount.vue")
      }, {
        path: "/rolePermissions",
        name: "rolePermissions",
        meta: {
          title: '角色权限'
        },
        component: () =>
          import("../views/rolePermissions.vue")
      }, {
        path: "/bindingRoles",
        name: "bindingRoles",
        meta: {
          title: '绑定角色'
        },
        component: () =>
          import("../views/bindingRoles.vue")
      }, {
        path: "/administrativeDepartment",
        name: "administrativeDepartment",
        meta: {
          title: '部门管理'
        },
        component: () =>
          import("../views/administrativeDepartment.vue")
      }, {
        path: "/campus",
        name: "campus",
        meta: {
          title: '办公楼'
        },
        component: () =>
          import("../views/campus.vue")
      }, {
        path: "/order",
        name: "order",
        meta: {
          title: '工单管理'
        },
        component: () =>
          import("../views/order.vue")
      }, {
        path: "/permission",
        name: "permission",
        meta: {
          title: '权限管理',
          permission: true
        },
        component: () =>
          import("../views/Permission.vue")
      },{
        path: "/systemConfiguration",
        name: "systemConfiguration",
        meta: {
          title: '系统配置',
          permission: true
        },
        component: () =>
          import("../views/systemConfiguration.vue")
      }, {
        path: "/room",
        name: "room",
        meta: {
          title: '房态图'
        },
        component: () =>
          import("../views/room.vue")
      }, {
        path: '/404',
        name: '404',
        meta: {
          title: '找不到页面'
        },
        component: () =>
          import('../views/404.vue')
      }, {
        path: '/403',
        name: '403',
        meta: {
          title: '没有权限'
        },
        component: () =>
          import('../views/403.vue')
      }, {
        path: '/Upload',
        name: 'upload',
        meta: {
          title: '管理logo'
        },
        component: () =>
          import('../views/Upload.vue')
      }, {
        path: '/journal',
        name: 'journal',
        meta: {
          title: '系统日志'
        },
        component: () =>
          import('../views/journal.vue')
      }, {
        path: '/warningLog',
        name: 'warningLog',
        meta: {
          title: '警告日志'
        },
        component: () =>
          import('../views/warningLog.vue')
      }, {
        path: '/UnlockRecord',
        name: 'UnlockRecord',
        meta: {
          title: '开锁记录'
        },
        component: () =>
          import('../views/UnlockRecord.vue')
      },{
        path: '/bluetoothUnlockRecord',
        name: 'bluetoothUnlockRecord',
        meta: {
          title: '蓝牙开锁记录'
        },
        component: () =>
          import('../views/bluetoothUnlockRecord.vue')
      },
      {
        path: '/configPage',
        name: 'configPage',
        meta: {
          title: '配置'
        },
        component: () => import('../views/configPage.vue')
      },
      {
        path: '/quantityConfiguration',
        name: 'quantityConfiguration',
        meta: {
          title: '设备数量配置'
        },
        component: () => import('../views/quantityConfiguration.vue')
      },
      {
        path: '/allDevice',
        name: 'allDevice',
        meta: {
          title: '设备总列表'
        },
        component: () => import('../views/allDevice.vue')
      },
      {
        path: '/DeviceFeedbackLog',
        name: 'DeviceFeedbackLog',
        meta: {
          title: '操作日志'
        },
        component: () =>
          import('../views/DeviceFeedbackLog.vue')
      },
      // 设备日志
      {
        path: '/humanMotionSensorLog',
        name: 'humanMotionSensorLog',
        meta: {
          title: '红外传感器日志'
        },
        component: () =>
          import('../views/log/humanMotionSensorLog.vue')
      },
      {
        path: '/airDetectionSensorLog',
        name: 'airDetectionSensorLog',
        meta: {
          title: '空气检测传感器日志'
        },
        component: () =>
          import('../views/log/airDetectionSensorLog.vue')
      },
      {
        path: '/gatewayLog',
        name: 'gatewayLog',
        meta: {
          title: '网关日志'
        },
        component: () =>
          import('../views/log/gatewayLog.vue')
      },
      {
        path: '/trashLog',
        name: 'trashLog',
        meta: {
          title: '垃圾桶日志'
        },
        component: () =>
          import('../views/log/trashLog.vue')
      },
      {
        path: '/manholeCoverLog',
        name: 'manholeCoverLog',
        meta: {
          title: '井盖日志'
        },
        component: () =>
          import('../views/log/manholeCoverLog.vue')
      },
      {
        path: '/smartButtonLog',
        name: 'smartButtonLog',
        meta: {
          title: '智能按钮日志'
        },
        component: () =>
          import('../views/log/smartButtonLog.vue')
      },
      {
        path: '/airCleaningLog',
        name: 'airCleaningLog',
        meta: {
          title: '空气清洁日志'
        },
        component: () =>
          import('../views/log/airCleaningLog.vue')
      },
      {
        path: '/smokeLog',
        name: 'smokeLog',
        meta: {
          title: '烟感日志'
        },
        component: () =>
          import('../views/log/smokeLog.vue')
      },
      {
        path: '/emptyLog',
        name: 'emptyLog',
        meta: {
          title: '空开日志'
        },
        component: () =>
          import('../views/log/emptyLog.vue')
      },
      {
        path: '/acoustoopticAlarmLog',
        name: 'acoustoopticAlarmLog',
        meta: {
          title: '声光报警日志'
        },
        component: () =>
          import('../views/log/acoustoopticAlarmLog.vue')
      },

      // 智能家具首页页面
      {
        path: '/trashMonitorHomePage',
        component: () => import('../views/presentationPage/trashMonitorHomePage.vue'),
        meta: {
          title: '垃圾桶监测器首页'
        }
      },
      // 空气检测传感器首页
      {
        path: '/airQualityMonitorHomePage',
        component: () => import('../views/presentationPage/airQualityMonitorHomePage.vue'),
        meta: {
          title: '空气检测传感器首页'
        }
      },
      // 环境质量监测器首页
      {
        path: '/homePageEnvironmentalQualityMonitor',
        component: () => import('../views/presentationPage/homePageEnvironmentalQualityMonitor.vue'),
        meta: {
          title: '环境质量监测器首页'
        }
      },
      // 井盖监测器首页
      {
        path: '/manholeCoverMonitorHomePage',
        component: () => import('../views/presentationPage/manholeCoverMonitorHomePage.vue'),
        meta: {
          title: '井盖监测器首页'
        }
      },
      // 智能按钮首页
      {
        path: '/smartButtonHomePage',
        component: () => import('../views/presentationPage/smartButtonHomePage.vue'),
        meta: {
          title: '智能按钮首页'
        }
      },
      // 声光报警器首页
      {
        path: '/acoustoopticAlarmHomePage',
        component: () => import('../views/presentationPage/acoustoopticAlarmHomePage.vue'),
        meta: {
          title: '声光报警器首页'
        }
      },
      // 红外报警首页
      {
        path: '/infraredAlarmHomePage',
        component: () => import('../views/presentationPage/infraredAlarmHomePage.vue'),
        meta: {
          title: '红外报警首页'
        }
      },
      // 配电柜空开首页
      {
        path: '/homePageDistributionCabinetIsEmpty',
        component: () => import('../views/presentationPage/homePageDistributionCabinetIsEmpty.vue'),
        meta: {
          title: '配电柜空开首页'
        }
      },
      // 水电表首页
      {
        path: '/homePageWaterAndElectricityMeter',
        component: () => import('../views/presentationPage/homePageWaterAndElectricityMeter.vue'),
        meta: {
          title: '水电表首页'
        }
      },
      // 共享热水首页
      {
        path: '/sharedHotWaterHomePage',
        component: () => import('../views/presentationPage/sharedHotWaterHomePage.vue'),
        meta: {
          title: '共享热水首页'
        }
      },
      // 烟感首页
      {
        path: '/smokeSensationDeviceHomePage',
        component: () => import('../views/presentationPage/smokeSensationDeviceHomePage.vue'),
        meta: {
          title: '烟感首页'
        }
      },

      // 门磁页面
      {
        path: '/index',
        component: () => import('../views/doorMagnetism/home.vue'),
        meta: {
          title: '门磁首页'
        }
      },
      {
        path: '/alarmRecord',
        component: () => import('../views/doorMagnetism/alarmRecord.vue'),
        meta: {
          title: '报警记录'
        }
      },
      {
        path: '/Architecture',
        component: () => import('../views/doorMagnetism/Architecture.vue'),
        meta: {
          title: '建筑物管理'
        }
      },
      {
        path: '/Linkman',
        component: () => import('../views/doorMagnetism/Linkman.vue'),
        meta: {
          title: '通知管理'
        }
      },
      {
        path: '/EquipmentList',
        component: () => import('../views/doorMagnetism/EquipmentList.vue'),
        meta: {
          title: '门磁列表'
        }
      },
      {
        path: '/subordinateUnit',
        component: () => import('../views/doorMagnetism/subordinateUnit.vue'),
        meta: {
          title: '下级单位管理'
        }
      },
      {
        path: '/userManagementHouse',
        component: () => import('../views/doorMagnetism/userManagementHouse.vue'),
        meta: {
          title: '用户管理'
        }
      },
      {
        path: '/bluetoothButtonManage',
        component: () => import('../views/bluetoothButtonManage.vue'),
        meta: {
          title: '蓝牙扣管理'
        }
      },
      {
        path: '/rechargeManagement',
        component: () => import('../views/rechargeManagement/rechargeManagement.vue'),
        meta: {
          title: '充值'
        }
      },
      {
        path: '/bill',
        component: () => import('../views/rechargeManagement/bill.vue'),
        meta: {
          title: '账单'
        }
      },
      {
        path: '/linkperson',
        component: () => import('../views/linkman/linkperson.vue'),
        meta: {
          title: '通知管理'
        }
      },
      {
        path: '/byRoom',
        component: () => import('../views/boYu/byRoom.vue'),
        meta: {
          title: '泊寓房源'
        }
      },

    ]
  }, {
    path: "/Datavisualization",
    name: "Datavisualization",
    meta: {
      title: '数据可视化'
    },
    // component: () => import(
    //     /* webpackChunkName: "login" */
    //     "../views/LargeScreen.vue")
    component: () =>
      import(
        /* webpackChunkName: "login" */
        "../views/echart1/index.vue")
  },
  {
    path: '/visualization',
    name: "visualization",
    meta: {
      title: '全屏'
    },
    component: () => import('../views/visualization/index.vue'),
  },
];

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const router = new Router({
  mode: 'hash',
  // base:"/iot",
  routes
});

router.beforeEach((to, from, next) => {
  document.title = `智慧公寓管理系统`; //${to.meta.title} |
  const role = localStorage.getItem('ms_username');
  const info = JSON.parse(localStorage.getItem('info'))
  if (!role && to.path !== '/login') {
    next('/login');
  } else {
    next();
  }
});

export default router;
