<template>
  <div class="elmenu" :class="{'has-logo':showLogo}">
    <logo v-if="showLogo" :collapse="isCollapse"/>
    <el-scrollbar wrap-class="scrollbar-wrapper">

      <el-menu
        :default-active="activeMenu"
        :collapse="isCollapse"
        :background-color="variables.menuBg"
        :text-color="variables.menuText"
        :unique-opened="false"
        :active-text-color="variables.menuActiveText"
        :collapse-transition="false"
        mode="vertical"
      >
        <sidebar-item v-for="route in allRoute" :key="route.path" :item="route" :base-path="route.path"/>
      </el-menu>
    </el-scrollbar>
  </div>
</template>

<script>
  import {mapGetters} from 'vuex'
  import Logo from './Logo'
  import SidebarItem from './SidebarItem'
  import variables from '@/styles/variables.scss'
  export default {
    components: {SidebarItem, Logo},
    data() {
      return {
        electricField: {},
        allRoute: {},
        fileUploadNodeShowSysValue: '1',
        menuList: []
      }
    },
    created() {
      this.getParameterBySysKey()
    },
    destoryed() {
      this.allRoute = {}
    },
    mounted(){
      this.getCookie()
    },
    methods: {
      getCookie() {
        const strCookie = document.cookie
        var arrCookie=strCookie.split(";");
        var user;
        //遍历cookie数组，处理每个cookie对
        for(var i=0;i<arrCookie.length;i++){
          var arr=arrCookie[i].split("=");
          //找到名称为user的cookie，并返回它的值
          if("user"==arr[0].trim()){
            user=arr[1];
            sessionStorage.setItem('user', user)
            this.getMenu()
            break;
          }
        }
      },
      getParameterBySysKey() {
        this.$axios.get('/sysParameter/sysParameterKey/FILE_UPLOAD_NODE_SHOW').then(res => {
          this.fileUploadNodeShowSysValue = res.data.sysValue
          this.routes()
        })
      }
      ,
      getMenu() {
        this.$axios.get('/sysMenu/getMenufindByName/' + sessionStorage.getItem('user') + '/9001').then(res => {
          console.log(res.data)
          this.menuList = res.data
        }).catch(e => {
          this.$message.error('查询菜单异常：' + e)
        })
      }
      ,
      routes() {
        var routesResult = []
        var routes = this.$router.options.routes
        const user = sessionStorage.getItem('user')
        this.$axios.get('/electricField/').then((res) => {
          this.electricField = res.data
          var sign = 'windPower'
          if (this.electricField.electricFieldTypeEnum == 'E2') {
            sign = 'photovoltaic'
          }
          for (var i = 0; i < routes.length; i++) {

            if (routes[i].children != undefined) {

              var routesR = []
              var routeChildren = routes[i].children
              for (var p = 0; p < routeChildren.length; p++) {

                // if (routeChildren[p].sign != sign) {
                //   if (routeChildren[p].path != 'systemuser') {
                //     if (routeChildren[p].path == 'powerPlanSD') {
                //       if (this.electricField.provinceEnum == 'E37') {
                //         routesR.push(routeChildren[p])
                //       }
                //     } else {
                //       if (routeChildren[p].path == 'feedbackFile') {
                //         if (this.electricField.provinceEnum == 'E46' || this.electricField.provinceEnum == 'E52') {
                //           routesR.push(routeChildren[p])
                //         }
                //       } else {
                //         if (routeChildren[p].meta.title == '上传上报文件') {
                //           if (this.fileUploadNodeShowSysValue == '1') {
                //             routesR.push(routeChildren[p])
                //           }
                //         } else {
                //           routesR.push(routeChildren[p])
                //         }
                //       }
                //     }
                //   }
                //
                //   if (routeChildren[p].sign == 'admin') {
                //     routesR.push(routeChildren[p])
                //   }
                //
                //
                // }
                routesR.push(routeChildren[p])
              }

              if (routesR.length > 0) {
                routes[i].children = routesR
                routesResult.push(routes[i])
              }
            } else {

              // if (routes[i].sign != 'photovoltaic') {
                routesResult.push(routes[i])
              // }

            }
          }

          if (this.menuList.length === 0) {
            this.allRoute = routesResult
          } else {
            this.allRoute = this.filterMenu(routesResult)
          }
          // this.allRoute = routesResult
          // console.log(this.allRoute)

        }).catch((error) => {
          this.$message.error('获取场站类型动态路由失败' + error)
          this.allRoute = routes
        })

      }
      ,
      filterMenu(router) {
        var routerList = []
        routerList.push(router[0])
        routerList.push(router[1])
        for (var i = 0; i < this.menuList.length; i++) {
          for (var j = 0; j < router.length; j++) {
            if (this.menuList[i].itemPath == router[j].path && this.menuList[i].children[0].title == router[j].children[0].name) {
              routerList.push(router[j])
            }

            if (this.menuList[i].itemPath == 'ftpSftpTools' && router[j].children != undefined && router[j].children[0].path == 'ftpSftpTools') {
              routerList.push(router[j])
            }
            if (this.menuList[i].itemPath == router[j].name && router[j].name != undefined) {
              if (router[j].children != undefined && router[j].children.length > 0 && this.menuList[i].children.length > 0) {
                router[j].children = this.filterChildrenMenu(router[j].children, this.menuList[i].children)
                routerList.push(router[j])
              } else {
                routerList.push(router[j])
              }
            }
          }
        }
        return routerList
      }
      ,
      filterChildrenMenu(router, menuList) {
        var childrenList = []
        for (var i = 0; i < router.length; i++) {
          for (var j = 0; j < menuList.length; j++) {
            if (router[i].path == menuList[j].itemPath) {
              childrenList.push(router[i])
            }
          }
        }
        return childrenList;
      }
      ,
    },

    computed: {
      ...
        mapGetters([
          'sidebar'
        ]),
      /*routes() {
        var routes = this.$router.options.routes
        this.$axios.get('/electricField/').then((res) => {
          this.electricField = res.data

          if(this.electricField.electricFieldTypeEnum == 'E1'){
              for(var i = 0;i<routes.length;i++){
                if(routes[i].path == '/realTimeQuery'){
                  routes[i].children.splice(1,1)
                }
              }
          }

          if(this.electricField.electricFieldTypeEnum == 'E2'){
            for(var i = 0;i<routes.length;i++){
              if(routes[i].path == '/realTimeQuery'){
                console.log(routes[i])
                routes[i].children.splice(1,1)
                console.log(routes[i])
              }

            }
          }
        }).catch((error) => {
          this.$message.error('查询实时预测短期echarts出错' + error)

        })
        return routes
      },*/
      activeMenu() {
        const route = this.$route
        const {meta, path} = route
        // if set path, the sidebar will highlight the path you set
        if (meta.activeMenu) {
          return meta.activeMenu
        }
        return path
      }
      ,
      showLogo() {
        return this.$store.state.settings.sidebarLogo
      }
      ,
      variables() {
        return variables
      }
      ,
      isCollapse() {
        if (!this.sidebar.opened) {
          this.variables.menuBg = 'rgb(0,0,0)'
        } else {
          this.variables.menuBg = 'rgba(48, 65, 86,0)'
        }
        return !this.sidebar.opened
      }
    }
  }
</script>
<style scoped>
  /*.elmenu {*/
  /*  background: url('../../img/pageBg.png');*/
  /*  border: 1px solid #ffffff;*/
  /*  height: 100%;*/
  /*}*/
</style>
