<template>
  <div class="header">
    <!-- 折叠按钮 -->
    <div class="collapse-btn" @click="collapseChage">
      <i v-if="!collapse" class="el-icon-s-fold"></i>
      <i v-else class="el-icon-s-unfold"></i>
    </div>
    <div class="logo">智能报警及预警系统</div>
    <div class="header-right">
      <div class="header-user-con">
        <el-dropdown class="user-name" trigger="click" @command="handleCommand">
          <span class="el-dropdown-link">
            {{ username }}
            <i class="el-icon-caret-bottom"></i>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="editpassword"
                >修改密码</el-dropdown-item
              >
              <el-dropdown-item command="loginout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <i
          class="el-icon-full-screen"
          style="margin-left: 30px"
          @click="toggleFullScreen"
        ></i>
      </div>
    </div>
  </div>
  <el-dialog v-model="dialogVisible" width="400px">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="refruleForm"
      label-width="80px"
      class="demo-ruleForm"
    >
      <el-form-item label="账号" prop="userName">
        <el-input v-model="ruleForm.userName"></el-input>
      </el-form-item>
      <el-form-item label="中文名称" prop="chineseName">
        <el-input v-model="ruleForm.chineseName"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassWord">
        <el-input type="password" v-model="ruleForm.newPassWord"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(refruleForm)"
          >确认</el-button
        >
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script setup>
import { ref, computed, onMounted, reactive } from "vue";
import { useRouter } from "vue-router";
import { editRequest } from "/@/api/api.js";
import { ElMessage } from "element-plus";
import { useStore } from "vuex";
import { JSEncrypt } from "jsencrypt";
import BASE from "@tools/basicTool";
import screenfull from "screenfull";
const router = useRouter();
const store = useStore();
const dialogVisible = ref(false);
const refruleForm = ref(null);
const rules = reactive({
  userName: [{ required: true, message: "请输入账号", trigger: "blur" }],
  chineseName: [{ required: true, message: "请输入中文名称", trigger: "blur" }],
  newPassWord: [{ required: true, validator: BASE.elCkeck.validatePass, trigger: "blur" }],
});
const ruleForm = reactive({
  id: sessionStorage.getItem("ms_id"),
  userName: sessionStorage.getItem("ms_username"),
  chineseName: sessionStorage.getItem("ms_chinesename"),
  newPassWord: "",
});

const username = computed(() => {
  return sessionStorage.getItem("ms_chinesename");
});

const collapse = computed(() => store.state.collapse);

// 用户名下拉菜单选择事件
const handleCommand = (command) => {
  if (command == "loginout") {
    sessionStorage.clear();
    router.push("/login");
  } else if (command == "editpassword") {
    dialogVisible.value = true;
  }
};
// 利用jsencrypt.js进行RSA加密
const encryptionByPublickey = (formData) => {
  let key = sessionStorage.getItem("publicKey");
  var encrypt = new JSEncrypt();
  encrypt.setPublicKey(key);
  var result = encrypt.encrypt(JSON.stringify(formData));
  return result;
};
// edit
const edit = async () => {
  let requestWrapper = {
    publicKey: sessionStorage.getItem("publicKey"),
    token: sessionStorage.getItem("token"),
    data: encryptionByPublickey(ruleForm),
  };
  const res = await editRequest(requestWrapper);
  dialogVisible.value = false;
  if (res.success){
    ElMessage.success(res.msg);
  } else {
    ElMessage.error(res.msg);
  }
};
//表单提交
const submitForm = (formEl) => {
  formEl.validate((valid) => {
    if (valid) {
      edit();
    } else {
      console.log("error submit!!");
      return false;
    }
  });
};
// 侧边栏折叠
const collapseChage = () => {
  // store.commit("hadndleCollapse", true);
  store.commit("hadndleCollapse", !collapse.value);
};
// 全屏
const toggleFullScreen = () => {
  if (screenfull.isEnabled) {
    screenfull.toggle();
  }
};
onMounted(() => {
  if (document.body.clientWidth < 1500) {
    store.commit("hadndleCollapse", true);
  }
});
</script>
<style scoped>
.header {
  position: relative;
  box-sizing: border-box;
  width: 100%;
  height: 70px;
  font-size: 22px;
  color: #fff;
}
.collapse-btn {
  float: left;
  padding: 0 21px;
  cursor: pointer;
  line-height: 70px;
}
.header .logo {
  float: left;
  width: 250px;
  line-height: 70px;
}
.header-right {
  float: right;
  padding-right: 30px;
}
.header-user-con {
  display: flex;
  height: 70px;
  align-items: center;
}
.btn-fullscreen {
  transform: rotate(45deg);
  margin-right: 5px;
  font-size: 24px;
}
.btn-bell,
.btn-fullscreen {
  position: relative;
  width: 30px;
  height: 30px;
  text-align: center;
  border-radius: 15px;
  cursor: pointer;
}
.btn-bell-badge {
  position: absolute;
  right: 0;
  top: -2px;
  width: 8px;
  height: 8px;
  border-radius: 4px;
  background: #f56c6c;
  color: #fff;
}
.btn-bell .el-icon-bell {
  color: #fff;
}
.user-name {
  margin-left: 10px;
}
.user-avator {
  margin-left: 20px;
  margin-right: 10px;
}
.user-avator img {
  display: block;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.el-dropdown-link {
  color: #fff;
  cursor: pointer;
}
.el-dropdown-menu__item {
  text-align: center;
}
</style>
