<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="#57ACBB"
            text-color="#fff"
            active-text-color="#fff"
          >
            <el-submenu index="2" class="rightExit">
              <template slot="title" :size="size" :src="circleUrl">{{
                userInfo.name
              }}</template>
              <el-menu-item v-if="userType == 2" index="2-1" @click="downCode(userInfo.name+'-'+userInfo.userNumber)">下载专属邀请码</el-menu-item>
              <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>
<!--          <vue-qr v-show='false' :ref="'Qrcode'+userInfo.id" :text='urlJump+userInfo.invitationCode' :size="500" :width='1' ></vue-qr>-->
          <vue-qr v-show='false' :ref="userInfo.name+'-'+userInfo.userNumber" :text='urlJump+userInfo.invitationCode' :size="500" :width='1' ></vue-qr>
        </el-col>
      </el-row>
    </el-header>
    <el-container>
      <el-aside style="width: auto">
    
        <el-menu
         ref='menu'
          class="el-menu-vertical-demo shadow-menu aside-menu"
          :router="true"
          :default-active="active"
          :collapse="isCollapse"
          text-color="#333"
          active-text-color="#fff"
          @select="selectMenu"
        >
          <el-menu-item
            v-for="item in menuList"
            :key="item.id"
            :index="item.path"
            v-show="item.showItem"
          >
            <i :class="item.icon"></i>
            <span slot="title">{{ item.name }}</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
    
      <el-container>
        <el-header class="main-header">
          <el-row>
            <el-col :span="1">
              <div class="menu-hide" @click="changeMenu">
                <i class="el-icon-s-fold" style="color: #000"></i>
              </div>
            </el-col>
            <el-col :span="10">
              <el-breadcrumb class="admin-breadcrumb" separator="/">
                <el-breadcrumb-item>{{ crumbParent }}</el-breadcrumb-item>
                <el-breadcrumb-item v-if="crumbChild && crumbChild != null">{{
                  crumbChild
                }}</el-breadcrumb-item>
              </el-breadcrumb>
            </el-col>
          </el-row>
        </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>
              <!-- <Footer
    /> -->
    <el-footer style="padding-left: 0px; padding-right: 0px"
      ><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-input>
          </el-form-item>
          <el-form-item label="请输入新密码" prop="pass">
            <el-input
              type="password"
              v-model="ruleForm.pass"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="checkPass">
            <el-input
              type="password"
              v-model="ruleForm.checkPass"
              autocomplete="off"
            ></el-input>
          </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 md5 from "md5";
import vueQr from 'vue-qr'
import { mapGetters, mapActions } from "vuex";
import { oSessionStorage } from "@/utils/utils";
import register from "@/components/Register";
import Footer from "@/components/Footer";
export default {
  name: "manageMain",
  components: {
    vueQr,
    Footer
  },
  computed: {
    // ...mapGetters(['sacleListName']),
    // ...mapGetters(['menuActive']),
  },
  data() {
    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();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else if (value == this.ruleForm.oldpass) {
        callback(new Error("旧密码和新密码相同!"));
      } else {
        callback();
      }
    };

    return {
      userInfo: {}, //用户信息
      dialogVisible: false, //弹出窗是否可见
      size: "small",
      circleUrl:
        "https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png",
      activeIndex: "10",
      activeIndex2: "1",
      isCollapse: false,
      userType: "",
      menuList: [
        {
          id: 0,
          name: "首页",
          icon: "el-icon-s-home",
          path: "/manage/adminHome",
          showItem: true,
        },
        {
          id: 1,
          name: "代理管理",
          icon: "el-icon-user-solid",
          path: "/manage/agencyUserManage",
          showItem: true,
        },
        {
          id: 2,
          name: "医生管理",
          icon: "el-icon-user-solid",
          path: "/manage/doctorUserManage",
          showItem: true,
        },
        // {
        //   id: 2,
        //   name: "医生用户管理",
        //   icon: "el-icon-user-solid",
        //   path: "2",
        //   showItem: true,
        // },
        {
          id: 3,
          name: "用户管理",
          icon: "el-icon-user-solid",
          path: "/manage/userManage",
          showItem: true,
        },
        // {
        //   id: 4,
        //   name: "用户记录",
        //   icon: "el-icon-s-order",
        //   path: "/manage/userRecord",
        //   showItem: true,
        // },
        {
          id: 5,
          name: "游戏记录",
          icon: "el-icon-s-order",
          path: "/manage/gameRecord",
          showItem: true,
        },
      ],
      //面包屑页签
      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" },
        ],
      },
      ifAdmin: false,
      ifSupAdmin: false,
      active: "/manage/adminHome",
      urlJump:'http://152.136.24.101:8997?invitationCode=', //二维码跳转链接
    };
  },
  created() {
    if (oSessionStorage.getItem("userInfo")) {
      this.userInfo = JSON.parse(oSessionStorage.getItem("userInfo"));
      this.userType = this.userInfo.roleType;
      console.log(this.userInfo, "有用户信息吗");
      //假如是代理用户则 //代理用户菜单不显示
      if (this.userType == "3") {
        for (let i = 0; i < this.menuList.length; i++) {
          if (this.menuList[i].id == "1") {
            this.menuList[i].showItem = false;
          }
        }
      } else if (this.userType == "2") {
        for (let i = 0; i < this.menuList.length; i++) {
          if (this.menuList[i].id == "2") {
            this.menuList[i].showItem = false;
          }
          if (this.menuList[i].id == "1") {
            this.menuList[i].showItem = false;
          }
        }
      }
    }
  },
  mounted() {
    let codeImage = sessionStorage.getItem('codeImage');
    this.urlJump=codeImage+'?invitationCode='
    let that =this;
    // 页面初始化或者刷新页面初始化页签及激活菜单-2020-03-17 lwl
    this.active = this.$route.path;
    // debugger;
    this.selectMenu(this.$route.path);
    this.bus.$on("menuStatusUpdate", (e) => {
      console.log(that.$refs.menu.activeIndex)
      that.$refs.menu.activeIndex=e
      that.active = e;
      //  that.$forceUpdate()
      that.selectMenu(e);
    });
    //得到用户信息，根据角色信息判断哪个显示哪个不显示
  },
  methods: {
    ...mapActions({
      // setSacleListName:"setSacleListName",
      // setMenuActive:"setMenuActive"
    }),

    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;
        }
      });
    },
    //下载专属二维码
    downCode(linkId) {
      const iconUrl =this.$refs[linkId].$el.src;
      const a = document.createElement('a');
      const event =new MouseEvent('click');
      a.download=linkId;
      a.href=iconUrl;
      a.dispatchEvent(event)
    },
    // 退出
    escape() {
      // localStorage.clear();
      // sessionStorage.clear();
      //
      // this.$router.push({
      //   path: "/",
      // });

      oSessionStorage.removeItem("userInfo");
      oSessionStorage.removeItem("token");
      this.$router.push({ path: "/" });
    },

    /*lxh-修改密码*/
    submitForm(formName) {
      console.log(this.userInfo)
      this.$refs[formName].validate((valid) => {
        if (valid) {
            this.$http.post(
              "/v1/system/updatePassword",
              {
                id: this.userInfo.id,
                password: md5(this.ruleForm.pass),
                oldPassword: 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="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

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

#app {
  margin-top: 0px;
}

.el-header {
  padding: 0 0px !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: 98vh;*/
  height: 92vh;
  text-align: left;
  /*padding-top: 60px;*/
}
.aside-menu .el-menu-item.is-active {
  background: #57acbb !important;
}
.el-menu-item-group .el-menu-item.is-active {
  background: #57acbb !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: 20px 0 0 20px !important;

  .menu-hide {
    height: 20px !important;
    text-align: left;
    line-height: 20px !important;
  }
}

.admin-header-content {
  background: #57acbb;
  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;
  font-size: 18px;
  color: #fff;
  text-align: left;
  line-height: 45px;
}
.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;
}
.admin-breadcrumb {
  padding-top: 3px;
  margin-left: -18px;
}
.just-right {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
}
</style>
