<template>
  <el-container>
    <el-header class="shadow-menu">
      <el-row class="admin-header-content">
        <el-col :span="12" >
          <div class="admin-logo-content">
            <img src="../../assets/manage/manage_logo.png" alt="" />
          </div>
        </el-col>
        <!--<el-col :offset="15" :span="1">-->
        <!--<div class="admin-head-content" key="large">-->
        <!--<el-avatar :size="size" :src="circleUrl"></el-avatar>-->
        <!--</div>-->
        <!--</el-col>-->
        <el-col  :span="12" class="just-right">
          <el-menu
            :default-active="activeIndex"
            class="el-menu-demo"
            mode="horizontal"
            background-color="#00CDDD"
            text-color="#fff"
            active-text-color="#fff"
          >
            <el-submenu index="2" class="rightExit">
              <template slot="title" :size="size" :src="circleUrl">
                {{ Name }}
              </template>
              <el-menu-item index="2-1" @click="escape()">退出</el-menu-item>
              <el-menu-item index="2-1" @click="changePwd()">修改密码</el-menu-item>
            </el-submenu>
          </el-menu>
        </el-col>
      </el-row>
    </el-header>
    <el-container class="main-container">
      <el-aside style="width: auto">

        <el-menu
          class="el-menu-vertical-demo shadow-menu aside-menu"
          :router="true"
          :default-active="  menuActive || active"
          :collapse="isCollapse"
          text-color="#333"
          active-text-color="#fff"
          @select="selectMenu"
        >


          <el-submenu
            v-for="item in menuList"
            :key="item.id"
            :index="item.path"
            v-if="item.child && item.child.length && item.showItem > 0"
          >
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{ item.name }}</span>
            </template>
            <el-menu-item-group>
              <el-menu-item
                v-for="el in item.child"
                :key="el.id"
                :index="el.path"
                v-if="el.showItem"
              >
                <span style="margin-left: 10px">{{ el.name }}</span>
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>

          <el-menu-item v-else-if="item.showItem" :index="item.path">
            <i :class="item.icon"></i>
            <span slot="title">{{ item.name }}</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-container>
        <!--<el-header class="shadow-menu">-->
        <!--<el-menu-->
        <!--:default-active="activeIndex"-->
        <!--class="el-menu-demo"-->
        <!--mode="horizontal"-->
        <!--background-color="#545c64"-->
        <!--text-color="#fff"-->
        <!--active-text-color="#ffd04b"-->
        <!--&gt;-->
        <!--<el-radio-group-->
        <!--v-model="isCollapse"-->
        <!--class="top-menu-control"-->
        <!--v-on:click="changeMenu"-->
        <!--&gt;-->
        <!--<el-menu-item index="1" v-on:click="changeMenu"-->
        <!--&gt;<i class="el-icon-s-fold" style="color: #ffffff"></i-->
        <!--&gt;</el-menu-item>-->
        <!--</el-radio-group>-->
        <!---->
        <!--<el-submenu index="2" class="rightExit">-->
        <!--<template slot="title" :size="size" :src="circleUrl">{{-->
        <!--Name-->
        <!--}}</template>-->
        <!--<el-menu-item index="2-1" @click="escape()">退出</el-menu-item>-->
        <!--<el-menu-item index="2-1" @click="changePwd()"-->
        <!--&gt;修改密码</el-menu-item-->
        <!--&gt;-->
        <!--</el-submenu>-->
        <!--</el-menu>-->
        <!--</el-header>-->
        <el-header class="main-header">
          <div class="menu-hide" @click="changeMenu">
            <i class="el-icon-s-fold" style="color: #000"></i>
          </div>
          <el-breadcrumb class="admin-breadcrumb" separator="/">
            <el-breadcrumb-item>{{ crumbParent }}</el-breadcrumb-item>
            <el-breadcrumb-item v-if="crumbChild">
              {{ crumbChild }}
            </el-breadcrumb-item>
            <el-breadcrumb-item v-if="sacleListName"> {{sacleListName}}</el-breadcrumb-item>
          </el-breadcrumb>
        </el-header>

        <el-main class="el-mains" style="background-color: #f5f5f5">
          <transition name="fade" mode="out-in">
            <router-view></router-view>
          </transition>
        </el-main>

      </el-container>
    </el-container>
     <el-footer style='padding-left:0;padding-right:0'><Footer /></el-footer>

    <!---lxh-修改密码-->
    <el-dialog title="修改密码" :visible.sync="dialogVisible" :close-on-click-modal="false" width="40%">
      <div>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="150px"
          class="demo-ruleForm"
        >
          <el-form-item label="请输入旧密码" prop="oldpass">
            <el-input
              type="password"
              v-model="ruleForm.oldpass"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="请输入新密码" prop="pass">
            <el-input
              type="password"
              v-model="ruleForm.pass"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="确认新密码" prop="checkPass">
            <el-input
              type="password"
              v-model="ruleForm.checkPass"
              autocomplete="off"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </el-container>
</template>
<script>
import {mapGetters,mapActions} from "vuex"
export default {
  name: "ContainerSys",
computed: {
      ...mapGetters(['sacleListName']),
      ...mapGetters(['menuActive']),
    },
  data() {
    var systemVersion = process.env.VUE_APP_VERSION;
    //修改密码***
    var validatePassOld = (rule, value, callback) => {
      // if (value === "") {
      //   callback(new Error("请输入密码"));
      // } else {
      //   let juPassword = /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[~!@#$%^&*])[\da-zA-Z~!@#$%^&*]{8,16}$/;
      //   if (!juPassword.test(this.ruleForm.oldpass)) {
      //     callback(new Error("格式为数字+英文字母+特殊字符(~!@#$%^&*)8-16位"));
      //   } else {
      //     callback();
      //   }
      // }
      if (value === "") {
        callback(new Error("请输入旧密码"));
      } else {
        if (value.length < 6 || value.length > 18) {
          callback(new Error("密码长度需在6~18位之间"));
        } else {
          this.$refs.ruleForm.validateField("pass");
        }
        callback();
      }
    };
    var validatePass = (rule, value, callback) => {
      // if (value === "") {
      //   callback(new Error("请输入密码"));
      // } else {
      //   let juPassword = /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[~!@#$%^&*])[\da-zA-Z~!@#$%^&*]{8,16}$/;
      //   if (!juPassword.test(this.ruleForm.pass)) {
      //     callback(new Error("格式为数字+英文字母+特殊字符(~!@#$%^&*)8-16位"));
      //   } else {
      //     callback();
      //   }
      // }
      if (value === "") {
        callback(new Error("请输入新密码"));
      } else {
        if (value.length < 6 || value.length > 18) {
          callback(new Error("密码长度需在6~18位之间"));
        } else {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    let institutionId = sessionStorage.getItem(
      "f7a42fe7211f98ac7a60a285ac3a9527"
    );
    console.log("============>", institutionId);

    // end-**************
    return {
      systemVersion: systemVersion,
      Name: "",
      dialogVisible: false, //弹出窗是否可见
      size: "small",
      circleUrl:
        "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png",
      institutionNo: "", //学校编号
      institutionName: "", //学校名称
      institutionId: "", //学校id
      activeIndex: "10",
      activeIndex2: "1",
      isCollapse: false,
      menuList: [],
      //面包屑页签
      crumbParent: "首页",
      crumbChild: null,
      //修改密码
      ruleForm: {
        pass: "",
        checkPass: "",
        oldpass: "",
      },
      upPass: {
        institutionNo: "",
        password: "",
      },
      rules: {
        pass: [{ required: true, validator: validatePass, trigger: "blur" }],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
        oldpass: [
          { required: true, validator: validatePassOld, trigger: "blur" },
        ],
      },
      active: "/ContainerSys/AdminHome",
      userId: '',
      phone: '', //用户账号
      userType: '', //角色类型
    };
  },
  created() {
    this.institutionNo = sessionStorage.getItem(
      "f7a42fe7211f98ac7a60a285ac3a9527"
    );
    this.institutionName = sessionStorage.getItem(
      "f7a42fe7211f98ac7a60a285ac3a9529"
    );
    this.userId = sessionStorage.getItem(
      "b80bb7740288fda1f201890375a60c8f"
    );
    this.phone = sessionStorage.getItem(
        "f7a42fe7211f98ac7a60a285ac3a9e87"
    );
    this.userType = sessionStorage.getItem(
        "f7a42fe7211f98ac7a60a285ac3a9528"
    );

    console.log('--------------------------------------------------')
    console.log('this.userType: ', this.userType)
    console.log('--------------------------------------------------')
    this.Name = this.institutionName;
    this.initMenus()
    // if (this.userType == 3) {
    //   this.Name = this.institutionName;
    // } else if (this.userType == 2) {
    //   this.Name = this.institutionNo;
    // }

    if (sessionStorage.getItem("menuActive")) {
      this.active = sessionStorage.getItem("menuActive");
    }
  },
  mounted() {
    // 页面初始化或者刷新页面初始化页签及激活菜单-2020-03-17 lwl
    this.active = this.$route.path;
    this.selectMenu(this.$route.path);
  },
  methods: {
     ...mapActions({
      setSacleListName:"setSacleListName",
      setMenuActive:"setMenuActive"
    }),

    initMenus() {
       this.menuList = [

         {
           id: 0,
           name: "首页",
           icon: "el-icon-s-home",
           path: "/ContainerSys/AdminHome",
           showItem: true,
           child: [],
         },
         {
           id: 1,
           name: "用户管理",
           icon: "el-icon-user-solid",
           path: "1",
           showItem: ['2', '3'].includes(this.userType),
           child: [
             {
               id: "1-1",
               name: "学生管理",
               parentName: "用户管理",
               styleCss: "el-icon-s-custom",
               path: "/ContainerSys/UserManage",
               showItem: true,
             },
             {
               id: "1-2",
               name: "年级管理",
               parentName: "用户管理",
               styleCss: "el-icon-s-custom",
               path: "/ContainerSys/UserGroupManagement",
               showItem: true,
             },
             {
               id: "1-3",
               name: "管理员管理",
               parentName: "用户管理",
               styleCss: "el-icon-s-grid",
               path: "/ContainerSys/AdminManage",
               showItem: this.userType == 3,
             },
           ],
         },
         {
           id: 2,
           name: "测试记录",
           icon: "el-icon-s-order",
           path: "2",
           showItem: ['2', '3'].includes(this.userType),
           child: [
             {
               id: "2-1",
               name: "学生测试记录",
               parentName: "测试记录",
               styleCss: "el-icon-s-custom",
               path: "/ContainerSys/UserRecords",
               showItem: true,
             },
             {
               id: "2-2",
               name: "量表测试记录",
               parentName: "测试记录",
               styleCss: "el-icon-s-custom",
               path: "/ContainerSys/SacleList",
               showItem: true,
             },
             {
               id: "2-3",
               name: "认知任务测试记录",
               parentName: "测试记录",
               styleCss: "el-icon-s-grid",
               path: "/ContainerSys/CognitiveTaskList",
               showItem: true,
             },
           ],
         },
         {
           id: 3,
           name: "学校管理",
           icon: "el-icon-s-order",
           path: "3",
           showItem: this.userType === '4',
           child: [
             {
               id: "3-1",
               name: "学校信息",
               parentName: "学校管理",
               styleCss: "el-icon-s-custom",
               path: "/ContainerSys/InstitutionInfo",
               showItem: this.userType === '4',
             },
           ],
         },
         {
           id: 4,
           name: "系统管理",
           icon: "el-icon-s-order",
           path: "4",
           showItem: ['2', '3'].includes(this.userType),
           child: [
             {
               id: "4-1",
               name: "操作日志管理",
               parentName: "系统管理",
               styleCss: "el-icon-s-grid",
               path: "/ContainerSys/OperationLog",
               showItem: true,
             },
             {
               id: "4-2",
               name: "测试计划",
               parentName: "系统管理",
               styleCss: "el-icon-s-custom",
               path: "/ContainerSys/testPlan",
               showItem: true,
             },
             {
               id: "4-3",
               name: "历史计划",
               parentName: "系统管理",
               styleCss: "el-icon-s-custom",
               path: "/ContainerSys/planHistory",
               showItem: true,
             },
           ],
         },
         {
           id: 5,
           name: "量表管理",
           icon: "el-icon-s-order",
           path: "5",
           showItem: ['2', '3'].includes(this.userType),
           child: [
             {
               id: "5-1",
               name: "自定义量表",
               parentName: "量表管理",
               styleCss: "el-icon-s-custom",
               path: "/ContainerSys/ScaleManagement",
               showItem: true,
             },
             {
               id: "5-2",
               name: "量表预警",
               parentName: "量表管理",
               styleCss: "el-icon-s-custom",
               path: "/ContainerSys/scaleWarn",
               showItem: true,
             },
           ],
         },
       ]
    },

    changeMenu: function() {
      this.isCollapse = !this.isCollapse;
    },
    //匹配获取页签名称-2020-03-17 lwl
    selectMenu(param) {
      console.log(param, "888");
      this.menuList.forEach((item) => {
        if (item.path == param) {
          this.crumbParent = item.name;
          this.crumbChild = null;
          return;
        } else {
          item.child.forEach((i) => {
            if (i.path == param) {
              this.crumbParent = i.parentName;
              this.crumbChild = i.name;
            }
            if( param !== "/ContainerSys/SacleList"){
              this.setSacleListName(null)
            }
          });
        }
      });
    },
    // 退出
    escape() {
      let tempData = localStorage.getItem('topUserInfo')
      if (tempData) {
        this.$alert('即将切换回教育局管理账号！', '页面切换提醒', {
          confirmButtonText: '确定',
          type: 'warning',
          callback: () => {
            let tempInfo = JSON.parse(tempData)
            sessionStorage.setItem("f7a42fe7211f98ac7a60a285ac3a9528", '4')
            sessionStorage.setItem("f7a42fe7211f98ac7a60a285ac3a9527", tempInfo.institutionNo)
            sessionStorage.setItem("f7a42fe7211f98ac7a60a285ac3a9529", tempInfo.institutionName)

            localStorage.removeItem('topUserInfo')

            this.$router.push({
              path: '/ContainerSys/AdminHome'
            })
            location.reload()
          }
        })
      } else {
        let data = {
          phone: this.phone,
          roleType: this.userType,
        }
        if (this.institutionNo && this.institutionNo !== 'null') {
          data.institutionNo = this.institutionNo
        }
        this.$http.post("system/logout", data,(res) => {
          if( res && res.code == 200 ){
            this.$message({
              message: "已成功退出登录",
              type: "success",
            })
            localStorage.clear()
            sessionStorage.clear()
            this.$router.push({ path: "/",})
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            })
          }
        })
      }
    },

    /*lxh-修改密码*/
    submitForm(formName) {
      //console.log(this.userType)
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.systemVersion == "public") {
          }
          if (this.userType == 2) {
            this.$http.post(
              "system/updatePassword",
              {
                id: this.userId,
                password: this.$md5(this.ruleForm.pass),
                oldPassword: this.$md5(this.ruleForm.oldpass),
              },
              (res) => {
                (this.ruleForm.oldpass = ""),
                  (this.ruleForm.pass = ""),
                  (this.ruleForm.checkPass = ""),
                  (this.dialogVisible = false);
                this.$message({
                  message: "密码修改完成！",
                  type: "success",
                });
                sessionStorage.clear();
                const IsPC = (function () {
                  var userAgentInfo = navigator.userAgent;
                  var Agents = ["Android",
                    "iPhone",
                    "SymbianOS",
                    "Windows Phone",
                    "iPad",
                    "iPod"];
                  var flag = true;
                  for (var v = 0; v < Agents.length; v++) {
                    if (userAgentInfo.indexOf(Agents[v]) > 0) {
                      flag = false;
                      break;
                    }
                  }
                  return flag;
                })();
                if (IsPC) {
                  this.$router.push({
                    path: "/",
                  });
                } else {
                  window.location.href = `${systemUrl}/h5/#/login`;
                }
              }
            );
          } else if (this.userType == 3) {
            this.$http.post(
              "/system/updatePassword",
              {
                id: this.userId,
                password: this.$md5(this.ruleForm.pass),
                oldPassword: this.$md5(this.ruleForm.oldpass),
              },
              (res) => {
                (this.ruleForm.oldpass = ""),
                  (this.ruleForm.pass = ""),
                  (this.ruleForm.checkPass = ""),
                  (this.dialogVisible = false);
                this.$message({
                  message: "密码修改完成！",
                  type: "success",
                });
                sessionStorage.clear();
                this.$router.push({
                  path: "/",
                });
              }
            );
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
      console.log(this.userType);
    },
    // 修改密码
    changePwd() {
      this.dialogVisible = true;
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-container {

  &.main-container {
    height: calc(100vh - 96px);
    overflow: hidden;
  }

  .el-menu {
    .el-menu-item-group {
      .el-menu-item-group__title {
        display: none;
      }
    }
  }

  .el-main {
    &.el-mains {
      position: relative;
    }
  }
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
  overflow: hidden;
}

.shadow-menu {
  box-shadow: 0 4px 12px 2px rgba(0, 0, 0, 0.1);
}

#app {
  margin-top: 0;
}

.el-header {
  padding: 0 0 !important;
  box-sizing: border-box;
  flex-shrink: 0;
}

.menu-top-left {
  background-color: black;
  height: 3.75rem;
  display: flex;
  align-items: center;
  font-size: 16px;
  line-height: 2;
}

.menu-top-left i img {
  width: 30px;
  height: 30px;
}

.menu-top-left-title {
  color: #ffffff;
  font-size: 1rem;
}

/*.el-menu--horizontal > .el-submenu {*/
/*float: right;*/
/*}*/

/*.el-menu--horizontal > .el-menu-item {*/
/*float: right;*/
/*height: 60px;*/
/*line-height: 60px;*/
/*margin: 0;*/
/*border-bottom: 2px solid transparent;*/
/*color: #909399;*/
/*}*/

.top-menu-control {
  float: left;
}

.aside-menu {
  height: calc(100vh - 96px);
  text-align: left;
}
.aside-menu .el-menu-item.is-active {
  background: #00CDDD !important;
}
.el-menu-item-group .el-menu-item.is-active {
  background: #00CDDD !important;
}
.fade-enter {
  opacity: 0;
}

.fade-leave {
  opacity: 1;
}

.fade-leave-active,
.fade-enter-active {
  transition: opacity 0s;
}
/*.rightExit {*/
/*float: right !important;*/
/*}*/

.main-header {
  height: 40px !important;
  padding-left: 20px !important;
  display: flex;
  flex-direction: row;
  align-items: center;

  .menu-hide {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 12px;
  }
}

.admin-header-content {
  background: #00CDDD;
  height: 100% !important;
    display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}
.admin-header-content .el-col {
  height: 100% !important;
}
.admin-header-content .admin-logo-content {
  /*box-sizing: border-box;*/
  /*width: 100%;*/
  /*height: 100%;*/
  width: 165px;
  height: 45px;
  padding: 8px 0 8px 20px;
}
.admin-header-content .admin-logo-content img {
  width: 100%;
  height: 100%;
}
.admin-header-content .admin-head-content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  flex: 1;
}
.admin-header-content .el-menu-demo {
  height: 100%;
}
.el-menu--horizontal > .el-submenu .el-submenu__title,
/deep/.el-menu--horizontal > .el-submenu.is-active .el-submenu__title {
  border-bottom: none !important;
}
/deep/.el-menu--horizontal > .el-submenu .el-submenu__icon-arrow {
  color: #fff;
}

.el-menu.el-menu--horizontal {
  border-bottom: none;
}

.just-right{
      display: flex;
    flex-direction: row;
    justify-content: flex-end;
}
</style>
