<template>
  <div class="header">
    <div class="div30 left" @click="back" style="cursor: pointer">
      <i v-if="hasBack" class="el-icon-back"></i>
      <span style="font-size: 20px">{{ projectName }}</span>
    </div>
    <div class="div30" style="font-size: 24px; text-align: center">
      {{ modelName }}
    </div>
    <div class="right div30">
      <p class="mr10">
        {{ currentTime }}
      </p>
      <p class="mr10">登录名： admin</p>
      <img src="../assets/index.png" alt="" class="mr10" @click="back"/>
      <el-icon size="32" class="mr10" @click="setting"><Setting /></el-icon>
      <el-icon size="32" class="mr10" @click="logout"><SwitchButton /></el-icon>
    </div>
  </div>
</template>

<script>
import { onMounted, reactive, toRefs } from "vue";
import { useRouter } from "vue-router";
import { localRemove, pathMap } from "@/utils";
import myApi from "@/api/myApi.js";
import { useStore } from "vuex";
import dayjs from "dayjs";
import { localSet,localGet } from '@/utils';
export default {
  name: "Header",
  setup() {
    const router = useRouter();
    const store = useStore();
    const state = reactive({
      name: "dashboard",
      userInfo: null,
      hasBack: false,
      modelName: "",
      currentTime: "",
      interval: "",
      projectName: ''
    });
    onMounted(() => {
      // const pathname = window.location.hash.split('/')[1] || ''
      // if (!['login'].includes(pathname)) {

      // }
      state.interval = setInterval(updateTime, 1000);
      state.projectName = localGet('projectName') || "世博浦西区活水公园";
    });
    const getUserInfo = async () => {};
    const logout = async () => {
      router.push("/login");
    };
    const back = () => {
      router.push("/home");
      store.commit("setModelName", "");
    };
    const updateTime = () => {
      state.currentTime = dayjs().format("YYYY年 MM月 DD日 HH:mm:ss");
    };

    const setting = () => {
      router.push("/config");
    }
    router.afterEach((to) => {
      console.log("to", to);
      const { id } = to.query;
      state.name = pathMap[to.name];
      state.modelName = store.state.modelName;
      // state.hasBack = ['level2', 'level3', 'order_detail'].includes(to.name)
    });
    return {
      ...toRefs(state),
      logout,
      back,
      setting
    };
  },
};
</script>

<style lang="scss" scoped>
.header {
  height: 50px;
  border-bottom: 1px solid #e9e9e9;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  .div30 {
    width: 33%;
  }
}
.el-icon-back {
  border: 1px solid #e9e9e9;
  padding: 4px;
  border-radius: 50px;
  margin-right: 10px;
}
.right {
  display: flex;
  justify-content: end;
  align-items: center;
  cursor: pointer;
  img {
    width: 32px;
  }
}
.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: #000;
}
.popper-user-box .nickname .logout {
  position: absolute;
  right: 0;
  top: 0;
  cursor: pointer;
}
</style>