<template>
  <div>
    <div class="header">
      <div class="left">
        <i v-if="hasBack" class="el-icon-back" @click="back"></i>
        <span style="font-size: 20px">{{ name }}</span>
      </div>
      <div class="right">
        <el-popover
          placement="bottom"
          :width="350"
          trigger="click"
          popper-class="popper-user-box"
        >
          <template #reference>
            <div class="author">
              <el-icon><User /></el-icon>
              {{ (userInfo && userInfo.nickName) || "" }}
              <el-icon><CaretBottom /></el-icon>
            </div>
          </template>
          <div class="nickname">
            <p>登录名：{{ (userInfo && userInfo.loginUserName) || "" }}</p>
            <p>昵称：{{ (userInfo && userInfo.nickName) || "" }}</p>
            <el-tag effect="dark" class="reStart" @click="reStart"
              >重启设备</el-tag
            >
            <el-tag effect="dark" class="editPass" @click="editPass"
              >修改密码</el-tag
            >
            <el-tag effect="dark" class="logout" @click="logout"
              >退出登录</el-tag
            >
          </div>
        </el-popover>
      </div>
    </div>
    <EditPass
      :dialogVisible="dialogVisible"
      @dialogClose="dialogClose"
      @dialogSuccess="dialogSuccess"
    ></EditPass>
  </div>
</template>

<script>
import { onMounted, reactive, toRefs } from "vue";
import { useRouter } from "vue-router";
import { localRemove, pathMap } from "@/utils";
import infoApi from "@/api/infoApi.js";
import EditPass from "@/components/EditPass.vue";
import { ElMessage } from "element-plus";
import { useStore } from "vuex";
import myApi from "@/api/myApi.js";
import { response } from '../proto/data/pd'
export default {
  name: "Header",
  components: {
    EditPass,
  },
  setup() {
    const router = useRouter();
    const store = useStore();
    const state = reactive({
      name: "dashboard",
      userInfo: null,
      hasBack: false,
      dialogVisible: false,
    });
    onMounted(() => {
      const pathname = window.location.hash.split("/")[1] || "";
      if (!["login"].includes(pathname)) {
        getUserInfo();
      }
    });
    const dialogClose = () => {
      state.dialogVisible = false;
    };

    const dialogSuccess = () => {
      state.dialogVisible = false;
    };
    const getUserInfo = async () => {
      // const userInfo = await myApi.getUserInfo();
      // state.userInfo = userInfo
      state.userInfo = {
        adminUserId: 1,
        locked: 0,
        loginPassword: "******",
        loginUserName: "admin",
        nickName: "admin",
      };
      store.commit("setUserInfo", state.userInfo);
    };

    const reStart = async () => {
      const res = await infoApi.reboot();
      if (res.code == 0) {
        ElMessage.success(res.msg || "请求成功");
      } else {
        ElMessage.error(res.msg || "请求失败");
      }
    };
    const logout = async () => { 
      const res = await myApi.logOut();
      // 解析返回数据
      const ret = response.decode(new Uint8Array(res));  
      if (ret.code == 0) {
          // 获取数据
          console.log(new TextDecoder().decode(ret.data));
          localRemove("token");
          window.location.reload();
      } else {
          console.log(ret);
      }
    };

    const editPass = () => {
      state.dialogVisible = true;
    };
    const back = () => {
      router.back();
    };
    router.afterEach((to) => {
      console.log("to", to);
      const { id } = to.query;
      state.name = pathMap[to.name];
      // state.hasBack = ['level2', 'level3', 'order_detail'].includes(to.name)
    });
    return {
      ...toRefs(state),
      logout,
      back,
      editPass,
      dialogClose,
      dialogSuccess,
      reStart,
    };
  },
};
</script>

<style scoped>
.header {
  height: 50px;
  border-bottom: 1px solid #e9e9e9;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}
.el-icon-back {
  border: 1px solid #e9e9e9;
  padding: 4px;
  border-radius: 50px;
  margin-right: 10px;
}
.right > div > .icon {
  font-size: 18px;
  margin-right: 6px;
}
.author {
  margin-left: 10px;
  cursor: pointer;
}
</style>
<style>
.popper-user-box {
  background: url("https://s.yezgea02.com/lingling-h5/static/account-banner-bg.png")
    50% 50% no-repeat !important;
  background-size: cover !important;
  border-radius: 0 !important;
}
.popper-user-box .nickname {
  position: relative;
  color: #ffffff;
}
.popper-user-box .nickname .logout {
  position: absolute;
  right: 0;
  top: 0;
  cursor: pointer;
}
.editPass {
  position: absolute;
  right: 75px;
  top: 0px;
  cursor: pointer;
}
.reStart {
  position: absolute;
  right: 150px;
  top: 0px;
  cursor: pointer;
}
</style>
