<template>
  <div class="globalheader" ref="header">
    <div class="globalheader-main">
      <div class="globalheader-main-left">
        <div><img src="" alt="" /></div>
        <h3>启服通</h3>
        <p>{{ title }}</p>
      </div>
      <div
        v-if="!userpin"
        class="globalheader-main-right"
        @click="$router.push(url)"
      >
        {{ name }}
      </div>
      <div v-else class="globalheader-main-right">
        <el-dropdown @command="handleCommand">
          <span class="el-dropdown-link">
            <i class="el-icon-s-custom"></i>
            {{ username }}<i class="el-icon-caret-bottom"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="myOrder">我的订单</el-dropdown-item>
            <el-dropdown-item command="myMsg">个人信息</el-dropdown-item>
            <el-dropdown-item command="exit">退出</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script>
import { getCookie, removeCookie } from "@/utils/getToken.js";
import { logout } from "@/api/user.js";
export default {
  props: ["title", "url", "name"],
  data() {
    return {
      windowFirstUrl: "",
      userpin: "",
      username: ""
    };
  },
  created() {
    this.windowFirstUrl = window.location.host.split(".")[0];
    this.userpin = getCookie("userpin");
    this.username = getCookie("username");
  },
  mounted() {
    switch (this.windowFirstUrl) {
      case "gsbweb":
        this.$refs.header.classList.add("globalheader-red");
        break;
      default:
        break;
    }
  },
  methods: {
    handleCommand(command) {
      switch (command) {
        case "myOrder":
          this.$router.push("/jdindentlist");
          break;
        case "myMsg":
          break;
        case "exit":
          console.log(this.userpin);
          logout(this.userpin).then(res => {
            console.log(res);
            if (res.status == 0) {
              removeCookie("userpin");
              removeCookie("username");
              this.$message({
                message: "已退出登录！",
                type: "success"
              });
              this.$router.push("/user");
            } else {
              this.$message.error("退出登录失败！");
            }
          });
          break;
      }
    }
  }
};
</script>

<style lang="scss">
.globalheader-red {
  // background: rgb(226, 87, 87) !important;
}
.globalheader {
  width: 100%;
  height: 60px;
  background: #222;
  .globalheader-main {
    max-width: 1240px;
    height: 100%;
    padding: 0 20px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
  }
}
.globalheader-main-left {
  height: 100%;
  line-height: 60px;
  display: flex;
  align-items: center;
  div {
    width: 28px;
    height: 28px;
    background: #e1e1e1;
    img {
      width: 100%;
      height: 100%;
    }
  }
  h3 {
    padding: 0 23px 0 15px;
    font-size: 16px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #fff;
    line-height: 16px;
    border-right: 2px solid #fff;
  }
  p {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #fff;
    line-height: 22px;
    padding: 0 0 0 15px;
  }
}
.globalheader-main-right {
  font-size: 15px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: #fff;
  line-height: 60px;
  cursor: pointer;
  .el-dropdown {
    color: #fff;
    .el-dropdown-link {
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      line-height: 20px;
      i {
        font-size: 17px;
        margin-left: 15px;
        margin-right: 5px;
      }
    }
  }
}
</style>
