<template>
  <el-menu
    :default-active="activeIndex"
    mode="horizontal"
    background-color=""
    text-color="#000"
    active-text-color="#4392f7"
    @select="handleSelect"
  >
    <!-- <el-icon
      :size="20"
      v-show="!menuclose"
      class="fold"
      @click="SpreadMenu(true)"
      ><Fold
    /></el-icon>
    <el-icon
      :size="20"
      v-show="menuclose"
      class="fold"
      @click="SpreadMenu(false)"
      ><Expand
    /></el-icon> -->

    <div class="logo">
      <!-- <img src="@/assets/nanchang.png" style="width: 30px; height: 30px" /> -->
      <span>江西装配建管云平台</span>
    </div>
    <div class="login">
      <div class="dateTime">
        <sapn class="dateTime">{{ dateTime }}</sapn>
        <sapn class="time">{{ time }}</sapn>
      </div>
      <!-- <el-badge :value="titlenumber" class="item" v-if="titlenumber">
        <img
          src="@/assets/news.png"
          @click="newsClick"
          style="width: 20px; height: 20px; cursor: pointer"
          alt=""
        />
      </el-badge> -->

      <!-- <el-tooltip content="操作手册" placement="bottom" effect="light">
        <a
          target="_Blank"
          class="link"
          href="https://www.jinganrenjiams.com/illustrate/"
        >
          <img
            src="@/assets/operation.png"
            @click="newsClick"
            style="width: 25px; height: 25px; cursor: pointer"
            alt=""
          />
        </a>
      </el-tooltip> -->
      <!-- <div class="flex flex-wrap items-center" style="cursor: pointer">
        <el-dropdown :hide-on-click="false" trigger="click">
          <el-avatar :size="30">
            <img
              src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
            />
          </el-avatar>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="uppassword">
                <el-icon class="switchButton" :size="20" color="#000">
                  <Lock />
                </el-icon>
                <span>修改密码</span>
              </el-dropdown-item>
              <el-dialog
                :append-to-body="true"
                custom-class="pw"
                v-model="uppasswordShow"
                title="修改密码"
                width="500"
                :before-close="uppasswordClose"
                align-center
                :close-on-click-modal="false"
              >
                <el-form
                  ref="ruleFormRef"
                  :model="ruleForm"
                  :rules="rules"
                  label-width="100px"
                  class="demo-ruleForm"
                  :size="formSize"
                  label-position="left"
                  status-icon
                >
                  <el-form-item label="原密码" prop="oldpass">
                    <el-input
                      v-model="ruleForm.oldpass"
                      placeholder="请输入原密码"
                      clearable
                    />
                  </el-form-item>
                  <el-form-item label="新密码" prop="nowpass">
                    <el-input
                      v-model="ruleForm.nowpass"
                      placeholder="请输入新密码"
                      clearable
                    />
                  </el-form-item>
                  <el-form-item label="确定新密码" prop="correctpass">
                    <el-input
                      v-model="ruleForm.correctpass"
                      placeholder="请再次输入新密码"
                      clearable
                    />
                  </el-form-item>
                  <p
                    style="
                      padding-bottom: 20px;
                      font-size: 12px;
                      color: rgb(43, 151, 252);
                      border-bottom: 1px solid rgba(230, 230, 230, 1);
                    "
                  >
                    注：密码格式为8-20位正确密码（大小写字母·字符·数字）
                  </p>
                  <el-form-item class="options">
                    <el-button class="queding" @click="submitForm(ruleFormRef)">
                      确定
                    </el-button>
                    <el-button class="congzhi" @click="resetForm(ruleFormRef)"
                      >重置</el-button
                    >
                  </el-form-item>
                </el-form>
              </el-dialog>
              <el-dropdown-item @click="loginOut">
                <el-icon class="switchButton" :size="20" color="#000">
                  <SwitchButton />
                </el-icon>
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
      <span class="name">{{ username }}</span> -->
    </div>
  </el-menu>
</template>

<script setup>
import { ref, reactive, onBeforeMount, onMounted, watch } from "vue";
import { ElMessage, ElMessageBox, ElNotification } from "element-plus";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import { dayjs } from "element-plus";
import axios from "axios";

const api = ref();
const store = useStore();
const router = useRouter();
const username = ref(); // 账号名称
const activeIndex = ref(); // 路由路径
const loginOutDialogVisible = ref(false); // 退出按钮
const menuclose = ref(false); // 左边状态栏展开按钮状态
const uppasswordShow = ref(false); // 修改密码展示框
const ruleForm = reactive({
  oldpass: "",
  nowpass: "",
  correctpass: "",
});
const ruleFormRef = ref();
// 表单验证
const rules = reactive({
  oldpass: [
    {
      required: true,
      message: "密码不能为空",
      trigger: "blur",
    },
  ],
  nowpass: [
    {
      required: true,
      message: "确认密码不能为空",
      trigger: "blur",
    },
    {
      min: 8,
      max: 20,
      pattern:
        /^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*,\._\+(){}])[0-9a-zA-Z!@#$%^&*,\\._\+(){}]{8,20}$/,
      message: "请输入8-20位正确密码（大小写字母·字符·数字）",
      trigger: "blur",
    },
  ],
  correctpass: [
    {
      required: true,
      message: "确认密码不能为空",
      trigger: "blur",
    },
    {
      min: 8,
      max: 20,
      pattern:
        /^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*,\._\+(){}])[0-9a-zA-Z!@#$%^&*,\\._\+(){}]{8,20}$/,
      message: "请输入8-20位正确密码（大小写字母·字符·数字）",
      trigger: "blur",
    },
  ],
  // desc: [{ required: true, message: "Please input activity form", trigger: "blur" }],
});

// 监控消息通知数量
// watch(
//   () => store.state.user.activeIndex,
//   (newValue, oldValue) => {
//     titlenumber.value = newValue;
//     console.log(newValue, "路由地址");
//   }
// );

const dateTime = ref(dayjs().format("YYYY-MM-DD") + ` 星期四 `); // 时间
const time = ref(dayjs().format("HH:mm:ss"));

// 选择菜单
// const handleSelect = (key, keyPath) => {
// store.commit("navbarUpdata", key);
// activeIndex.value = key;
// };

// 展开按钮
const SpreadMenu = (flag) => {
  store.commit("menuClose", flag);
  menuclose.value = flag;
};

// 修改密码
const uppassword = () => {
  uppasswordShow.value = true;
};
const uppasswordClose = () => {
  uppasswordShow.value = false;
  ruleFormRef.value.resetFields();
};
// 提交修改密码表单
const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      let data = {
        oldPassword: ruleForm.oldpass,
        newPassword: ruleForm.nowpass,
        confirmPassword: ruleForm.correctpass,
      };
      let resUpdata = await axios({
        method: "post",
        url: api.value + "/login/ChangePassword",
        headers: {
          tokenP: sessionStorage.getItem("tokenP"),
          user_head: sessionStorage.getItem("userhead"),
        },
        data: data,
      });
      if (resUpdata.data.code == 200) {
        ElMessage({
          type: "success",
          showClose: true,
          message: resUpdata.data.message,
          center: true,
        });
        sessionStorage.removeItem("tokenP");
        sessionStorage.removeItem("userName");
        sessionStorage.removeItem("tokenP");
        sessionStorage.removeItem("roleId");
        sessionStorage.removeItem("userhead");
        sessionStorage.removeItem("provinceId");
        sessionStorage.removeItem("cityId");
        sessionStorage.removeItem("districtId");
        sessionStorage.removeItem("address");
        sessionStorage.removeItem("addressId");
        uppasswordShow.value = false;
        ruleFormRef.value.resetFields();
        router.removeRoute("jianguanCloud");
        ElNotification({
          type: "warning",
          title: "提示!!!",
          message: "密码已修改,2s后退出到登录页面",
          // duration: 0,
          // position: "top-right",
        });
        setTimeout(() => {
          router.push({ path: "/" });
        }, 2000);
      } else {
        ElMessage({
          type: "error",
          showClose: true,
          message: resUpdata.data.message,
          center: true,
        });
      }
    } else {
      console.log("error submit!", fields);
    }
  });
};
// 重置表单
const resetForm = (formEl) => {
  // console.log("重置表单");
  if (!formEl) return;
  formEl.resetFields();
};

const loginOut = () => {
  ElMessageBox.confirm("是否退出登录?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      sessionStorage.removeItem("tokenP");
      sessionStorage.removeItem("userName");
      sessionStorage.removeItem("tokenP");
      sessionStorage.removeItem("roleId");
      sessionStorage.removeItem("userhead");
      sessionStorage.removeItem("provinceId");
      sessionStorage.removeItem("cityId");
      sessionStorage.removeItem("districtId");
      sessionStorage.removeItem("address");
      sessionStorage.removeItem("addressId");
      router.push({
        path: `/login`,
      });
      router.removeRoute("jianguanCloud");
      ElMessage({
        type: "success",
        message: "退出成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消登录",
      });
    });
};

onMounted(() => {
  api.value = store.state.user.api;
  activeIndex.value = store.state.user.navbar;
  username.value = sessionStorage.getItem("userName");
  setInterval(() => {
    var week = ["日", "一", "二", "三", "四", "五", "六"];
    var datas = dayjs().day();
    dateTime.value = dayjs().format("YYYY-MM-DD") + ` 星期${week[datas]} `;
    time.value = dayjs().format("HH:mm:ss");
  }, 1000);
});
</script>

<style scoped lang="scss">
.el-menu--horizontal {
  border-bottom: none;
  display: flex;
  align-items: center;
  width: 100%;
  height: 64px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  // background-color: rgba(16, 46, 93, 0.8);
  background-color: transparent;
  background-image: url("../../assets/navber.png");
  background-size: 100% 140%;
  background-position: center;
  background-repeat: no-repeat;
  // box-shadow: 5px 5px 10px 0px rgba(213, 228, 252, 1);
  .logo {
    left: 280px;
    bottom: 12px;
    // display: flex;
    // align-items: center;
    position: absolute;
    span {
      // margin-left: ;
      // transform: translateX(280px);
      // margin-bottom: 12px;
      font-size: 33px;
      font-weight: 600;
      color: #fff;
    }
  }
  .login {
    color: #fff;
    display: flex;
    align-items: center;
    position: absolute;
    right: 10px;
    top: 17px;

    .dateTime {
      margin-right: 0px;
      color: rgba(0, 233, 254, 1);
      text-shadow: 0 0 1px rgba(0, 233, 254, 0.8),
        0 0 2px rgba(0, 233, 254, 0.8), 0 0 6px rgba(0, 233, 254, 0.8);
    }
    .time {
      margin: 0 10px;
      color: rgba(255, 255, 255, 1);
      font-size: 18px;
      text-shadow: 0 0 1px rgba(255, 255, 255, 0.8),
        0 0 2px rgba(255, 255, 255, 0.8), 0 0 6px rgba(255, 255, 255, 0.8);
    }
    .el-badge {
      display: flex;
      align-items: center;
      // :deep(.el-badge__content) {
      //   width: 2px;
      //   height: 14px;
      // }
    }
    .items-center {
      margin-left: 20px;
    }
    .name {
      height: 20px;
      line-height: 20px;
      text-decoration: underline;
      padding: 0 10px;
      margin: 0 10px;
      border-left: 2px solid #ccc;
    }
    .link {
      display: flex;
      align-items: center;
    }
    .switchButton {
      cursor: pointer;
    }
  }
  .login:focus {
    outline: none;
  }
  .fold {
    width: 46px;
    height: 65px;
  }
  .fold:hover {
    cursor: pointer;
    background-color: rgba(67, 146, 249, 0.1);
  }
}
.cockpit {
  background-color: #000;
}
.el-dialog__body {
  .options {
    display: flex;
    flex-direction: row-reverse;
    :deep(.el-form-item__content) {
      margin-left: 0 !important;
      flex: none;
    }
    :deep(.queding) {
      background: linear-gradient(
        90deg,
        rgb(38, 151, 255) 0%,
        rgb(102, 181, 255) 100%
      );
      border: none !important;
      color: #fff;
    }
  }
}
</style>
<style lang="scss">
.el-overlay {
  .el-message-box__btns {
    .el-button--primary {
      background: linear-gradient(
        90deg,
        rgb(38, 151, 255) 0%,
        rgb(102, 181, 255) 100%
      );
    }
  }
  .el-overlay-dialog {
    .pw {
      border-radius: 11px;

      .el-dialog__header {
        border-radius: 11px 11px 0 0;
        background: rgba(237, 241, 245, 1);
        font-weight: 600;
        margin: 0;
        .el-dialog__headerbtn {
          outline: none;
        }
      }
      .el-dialog__body {
        padding: 30px 20px 10px 20px;
        .el-input {
          width: 200px;
        }
        .options {
          margin-left: 170px;
        }
      }
    }
  }
}
</style>
