<template>
  <div class="navbar">
    <hamburger :toggle-click="toggleSideBar" :is-active="sidebar.opened" class="hamburger-container" />

    <!-- <breadcrumb class="breadcrumb-container" /> -->
    <div style="display:inline-block;">
      <span @click="pushView('/report', 2)" :class="[activeSys == 2 ? 'active-sys' : '', 'system-title']">报表系统</span>
      <span @click="pushView('/jzmall', 6)" :class="[activeSys == 6 ? 'active-sys' : '', 'system-title']">商城系统</span>
      <span @click="pushView('/worker', 3)" :class="[activeSys == 3 ? 'active-sys' : '', 'system-title']">服务者系统</span>
      <span @click="pushView('/customer', 5)" :class="[activeSys == 5 ? 'active-sys' : '', 'system-title']">客户CRM</span>
    </div>
    <div class="right-menu">
      <template v-if="device !== 'mobile'">
        <el-tooltip content="全屏" effect="dark" placement="bottom">
          <screenfull class="right-menu-item" />
        </el-tooltip>

        <el-tooltip content="布局大小" effect="dark" placement="bottom">
          <size-select class="right-menu-item" />
        </el-tooltip>

        <el-tooltip content="通知中心" effect="dark" placement="bottom">
          <notice ref="noticeIndexRef" class="right-menu-item" />
        </el-tooltip>
      </template>

      <el-dropdown class="avatar-container right-menu-item" trigger="click">
        <div class="avatar-wrapper">
          <!-- <img
            :src="'https://jzmall.lifejingzhi.com/file/jzlife-jms/user-avatar.png'"
            class="user-avatar"
            alt="avatar"
          /> -->
          <img :src="avatar + '?imageView2/1/w/80/h/80'" class="user-avatar">
          <i class="el-icon-caret-bottom" />
        </div>
        <el-dropdown-menu slot="dropdown">
          <router-link to="/dashboard">
            <el-dropdown-item>首页</el-dropdown-item>
          </router-link>
          <el-dropdown-item divided v-if="this.$store.state.user.name == '管理员'">
            <span style="display:block;" @click="handleJob">定时任务</span>
          </el-dropdown-item> 
          <el-dropdown-item divided v-if="this.$store.state.user.name == '管理员'">
            <span style="display:block;" @click="handleVersion">发布日志</span>
          </el-dropdown-item>

          <el-dropdown-item divided>
            <router-link to="/profile/password">密码修改</router-link>
          </el-dropdown-item>
          <el-dropdown-item divided>
            <span style="display:block;" @click="logout">退出</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>

    <jobInfo ref="job"></jobInfo>
    <version ref="version"></version>

  </div>
</template>

<script>
import { mapGetters } from "vuex";
import Breadcrumb from "@/components/Breadcrumb";
import Hamburger from "@/components/Hamburger";
import Screenfull from "@/components/Screenfull";
import SizeSelect from "@/components/SizeSelect";
import Notice from "@/components/Notice";
import Layout from "@/views/layout/Layout";
import jobInfo from "@/views/job/job.vue"
import version from "@/views/version/version.vue"
//  import router from '../../../router'

export default {
  components: {
    Breadcrumb,
    Hamburger,
    Screenfull,
    SizeSelect,
    Notice,
    jobInfo,version
  },
  computed: {
    ...mapGetters(["sidebar", "name", "avatar", "device", "activeSys"])
  },
  data() {
    return {
      // activeSys: this.$store.state.user.activeSys
    };
  },
  created() {

    // console.log(this.$store.state.user);
    // let reportRoute=[];
    // let list=this.$store.state.user.user.report;
    // list.forEach(item=>{
    //    let obj={
    //      path: item.path,
    //      component: Layout,
    //      meta:item.meta,
    //      children:item.children
    //    }
    //    reportRoute.push(obj);
    // });
    // this.$store.state.permission.routers=reportRoute;

    //2报表系统，3服务者crm；5客户crm，6商城
    if (this.$store.state.user.activeSys == 2 || this.$route.path.indexOf('/report') > -1 || this.$route.path.indexOf('/dashboard') > -1) {
      this.pushView("/report", 2);
    }
    if (this.$store.state.user.activeSys == 3 || this.$route.path.indexOf('/worker') > -1) {
      this.pushView("/worker", 3);
    }
    if (this.$store.state.user.activeSys == 5 || this.$route.path.indexOf('/customer') > -1) {
      this.pushView("/customer", 5);
    }
    if (this.$store.state.user.activeSys == 6 || this.$route.path.indexOf('/jzmall') > -1) {
      this.pushView("/jzmall", 6);
    }


  },
  watch: {
    activeSys(b) {
      //反向监听tagsview切换，还要处理最后一次navbar切换
      console.log("activeSys----:" + b);
      // if (b) {
      //   if (this.$store.state.user.activeSys == 2) {
      //     this.pushView("/report", 2);
      //   }
      //   if (this.$store.state.user.activeSys == 3) {
      //     this.pushView("/worker", 3);
      //   }
      //   if (this.$store.state.user.activeSys == 5) {
      //     this.pushView("/customer", 5);
      //   }
      //   if (this.$store.state.user.activeSys == 6) {
      //     this.pushView("/jzmall", 6);
      //   }
      // }
    }
  },
  methods: {
    toggleSideBar() {
      this.$store.dispatch("toggleSideBar");
    },
    logout() {
      this.$store.dispatch("LogOut").then(() => {
        // this.$router.push('/login');
        setTimeout(() => {
          this.$router.push('/login');
          location.reload();
        }, 300);
        // this.$router.push(`/login?redirect=${this.$route.fullPath}`)
      }).catch(err => {
        console.log(err);
        this.$router.push('/login');
        location.reload();
      });
    },
    handleJob() {
      this.$refs.job.showForm();
    },
    handleVersion(){
      this.$refs.version.showForm(); 
    },
    pushView(subapp, type) {
      // this.activeSys = type;
      this.$store.commit("SET_ACTIVESYS", type);
      // console.log(subapp);
      //console.log("this.$store--->" + JSON.stringify(this.$store.state.user));

      let user = this.$store.state.user;
      if (user.user === '' || user.user == null || user.user == undefined) {
        this.$router.push('/login');
        return;
      }



      if (subapp == "/report") {
        // console.log(this.$store.state.user);
        let reportRoute = [];
        let list = this.$store.state.user.user.report;
        list.forEach(item => {
          let obj = {
            path: item.path,
            component: Layout,
            meta: item.meta,
            children: item.children
          }
          reportRoute.push(obj);
        });

        this.$store.state.permission.routers = reportRoute;
        console.log(this.$store.state.permission.routers);

      } else if (subapp == "/jzmall") {
        // console.log(this.$store.state.user);
        let jzmallRoute = [];
        let list = this.$store.state.user.user.jzmall;
        list.forEach(item => {
          let obj = {
            path: item.path,
            component: Layout,
            meta: item.meta,
            children: item.children
          }
          jzmallRoute.push(obj);
        });

        this.$store.state.permission.routers = jzmallRoute;
        // console.log(this.$store.state.permission.routers);
      } else if (subapp == "/worker") {
        // console.log(this.$store.state.user);
        let workerRoute = [];
        console.log(5)
        console.log(this.$store.state.user.user)
        let list = this.$store.state.user.user.worker;
        list.forEach(item => {
          let obj = {
            path: item.path,
            component: Layout,
            meta: item.meta,
            children: item.children
          }
          workerRoute.push(obj);
        });

        this.$store.state.permission.routers = workerRoute;
        // console.log(this.$store.state.permission.routers);
      }
      else if (subapp == "/customer") {
        // console.log(this.$store.state.user);
        let customerRoute = [];
        console.log('customer')
        console.log(this.$store.state.user.user)
        let list = this.$store.state.user.user.customer;
        list.forEach(item => {
          let obj = {
            path: item.path,
            component: Layout,
            meta: item.meta,
            children: item.children
          }
          customerRoute.push(obj);
        });

        this.$store.state.permission.routers = customerRoute;
        // console.log(this.$store.state.permission.routers);
      } else {
        this.$store.state.permission.routers = [

        ];
      }

    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.active-sys {
  color: #09afff;
}

.system-title {
  display: inline-block;
  line-height: 50px;
  padding: 0 10px;
  cursor: pointer;
}

.navbar {
  height: 50px;
  line-height: 50px;
  border-radius: 0px !important;

  .hamburger-container {
    line-height: 58px;
    height: 50px;
    float: left;
    padding: 0 10px;
  }

  .breadcrumb-container {
    float: left;
  }

  .errLog-container {
    display: inline-block;
    vertical-align: top;
  }

  .right-menu {
    float: right;
    height: 100%;

    &:focus {
      outline: none;
    }

    .right-menu-item {
      display: inline-block;
      margin: 0 8px;
      vertical-align: top;
    }

    .avatar-container {
      height: 50px;
      margin-right: 30px;

      .avatar-wrapper {
        cursor: pointer;
        margin-top: 5px;
        position: relative;

        .user-avatar {
          width: 40px;
          height: 40px;
          border-radius: 10px;
        }

        .el-icon-caret-bottom {
          position: absolute;
          right: -20px;
          top: 25px;
          font-size: 12px;
        }
      }
    }
  }
}
</style>
