<template>
  <div style="background: #2B5451;color:#fff;">
    <div class="header">
      <div class="left">
        <i v-if="hasBack" class="el-icon-back" @click="back"></i>
        <span class="name">{{ 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.nickName) || "" }}</p>
            <p>昵称：{{ (userInfo && userInfo.nickName) || "" }}</p>
            <div class="btn-class">
              <el-button type="primary" size="large" @click="reStart">重启设备</el-button>
              <el-button type="primary" size="large" @click="editPass">修改密码</el-button>
              <el-button type="primary" size="large" @click="logout">退出登录</el-button>
            </div>
          </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 { pathMap, localRemove } from "@/utils";
import EditPass from "@/components/EditPass.vue";
import { useStore } from "vuex";
import myApi from "@/api/myApi.js";
import infoApi from "@/api/infoApi.js";
import { ElMessage } from "element-plus";
export default {
  name: "Header",
  components: {
    EditPass,
  },
  setup() {
    const router = useRouter();
    const store = useStore();
    const state = reactive({
      name: "dashboard",
      userInfo: store.state.userInfo,
      hasBack: false,
      dialogVisible: false,
    });
    onMounted(() => {
      const pathname = window.location.hash.split("/")[1] || "";
    });
    const dialogClose = () => {
      state.dialogVisible = false;
    };

    const dialogSuccess = () => {
      state.dialogVisible = false;
    };
    const reStart = async () => {
      const res = await infoApi.reboot();
      if (res.code == 0) {
        ElMessage.success(res.message || "请求成功");
      } else {
        ElMessage.error(res.message || "请求失败");
      }
    };
    const logout = async () => {
      const res = await myApi.logout();
      if (res.code == 0) {
        localRemove("token");
        window.location.reload();
      } else {
      }
    };

    const editPass = () => {
      state.dialogVisible = true;
    };
    const back = () => {
      router.back();
    };
    router.afterEach((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;

  .left {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .name {
      font-size: 20px;
    }
  }

  .right {
    font-size: 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;
}
.btn-class {
  margin-top: 10px;
}
</style>
