<template>
  <div
    :class="{ nav: true, 'header-fixed ': isFixed, 'home-nav': isHome }"
    id="navbar"
  >
    <div class="container">
      <div class="logo-container animate__animated animate__fadeInLeft">
        <img src="@/assets/imgs/home/logo.png" />
      </div>
      <div class="right">
        <div class="tabs animate__animated animate__fadeInUp">
          <div
            class="tab-item"
            :class="{
              activeHome: currentTab.name == item.name && isHome,
              active: currentTab.name == item.name && !isHome,
            }"
            v-for="(item, index) in pages"
            :key="index"
            @click="handleClickTab(item)"
          >
            <!-- <div ></div> -->
            <router-link :to="item.path">{{ item.name }} </router-link>
          </div>
        </div>
        <div class="tools animate__animated animate__fadeInRight">
          <div class="wrapper">
            <el-dropdown trigger="click">
              <el-button round>
                <i class="el-icon-user"></i>
                <i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  ><span
                    v-if="!hasToken"
                    @click="handleToLogin"
                    class="operation-item"
                    >登陆</span
                  ></el-dropdown-item
                >
                <el-dropdown-item
                  ><router-link
                    v-if="hasToken"
                    to="/personal"
                    class="operation-item"
                    >个人中心</router-link
                  ></el-dropdown-item
                >
                <el-dropdown-item>
                  <span v-if="hasToken" @click="handleLogOut">退出</span>
                  <!-- <span class="logout" v-if="hasToken">
                    <el-popover
                      placement="bottom"
                      width="200"
                      trigger="click"
                      v-model="logoutDialogVisible"
                    >
                     
                      <p>确定退出吗？</p>
                      <div style="text-align: right; margin: 0">
                        <el-button
                          size="mini"
                          type="text"
                          @click="logoutDialogVisible = false"
                          >取消</el-button
                        >
                        <el-button
                          type="primary"
                          size="mini"
                         
                          >确定</el-button
                        >
                      </div>
                    </el-popover> -->
                  <!-- </span> -->
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
      </div>
    </div>
    <el-dialog :visible.sync="loginVisible" append-to-body title="账号登陆">
      <div class="login">
        <el-form
          :model="loginForm"
          :label-position="labelPosition"
          ref="loginForm"
        >
          <el-form-item :label-width="formLabelWidth" prop="username">
            <el-input
              v-model="loginForm.username"
              autocomplete="off"
              clearable
              placeholder="用户名/手机号"
            ></el-input>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth" prop="password">
            <el-input
              v-model="loginForm.password"
              autocomplete="off"
              type="password"
              clearable
              show-password
              placeholder="密码"
            ></el-input>
          </el-form-item>
          <el-form-item :label-width="formLabelWidth">
            <el-row :gutter="10">
              <el-col :span="12">
                <el-form-item prop="captcha">
                  <el-input
                    ref="captcha"
                    v-model="loginForm.captcha"
                    placeholder="请输入验证码"
                    name="captcha"
                    tabindex="3"
                    auto-complete="on"
                    @keyup.enter.native="handleLoginSubmit"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12" class="captcha">
                <img
                  v-if="requestCodeSuccess"
                  :src="captchaImgSrc"
                  @click="handleGetCaptcha"
                />
                <img
                  v-else
                  src="@/assets/404_images/checkcode.png"
                  @click="handleGetCaptcha"
                />
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
        <div class="opration">
          <el-button
            round
            @click="handleLoginSubmit"
            size="small"
            :loading="loading"
            class="loginBtn"
            >登 陆</el-button
          >
          <div class="register" @click="handleToRegister">注册</div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { getVerify } from "@/api/user";
export default {
  name: "NavBar",
  computed: {
    ...mapGetters([
      "token",
      "userInfo",
      "curPath",
      "navBarFixed",
      "showLoginDialog",
    ]),
    hasToken() {
      return this.token;
    },
  },
  watch: {
    curPath: {
      handler: function (value) {
        this.isHome = value.name == "home";
        this.currentTab = {
          name: value.name,
          path: value.path,
        };

        if (value.name.indexOf("display") != -1) {
          this.currentTab = {
            name: "展览",
            path: "/display",
          };
        }
        if (value.name.indexOf("culturalRelic") != -1) {
          this.currentTab = {
            name: "文物展",
            path: "/culturalRelic",
          };
        }
        if (value.name.indexOf("museum") != -1) {
          this.currentTab = {
            name: "博物馆",
            path: "/museum",
          };
        }
      },
      immediate: true,
    },
    navBarFixed(value) {
      console.log("navBarFixed", value);
    },
    showLoginDialog(value) {
      this.loginVisible = value;
    },
  },
  data() {
    return {
      pages: [
        {
          name: "首页",
          path: "/",
        },
        {
          name: "精品展",
          path: "/boutique",
        },
        {
          name: "虚拟展厅",
          path: "/virtual",
        },
        {
          name: "展览",
          path: "/display",
        },
        {
          name: "文物展",
          path: "/culturalRelic",
        },
        {
          name: "博物馆",
          path: "/museum",
        },
      ],
      userName: "",
      logoutDialogVisible: false,
      currentTab: {
        name: "首页",
        path: "/",
      },
      isHome: true,
      isFixed: false,
      offsetTop: 0,
      loginVisible: false,
      loginForm: {
        username: "",
        password: "",
        captcha: "",
      },
      requestCodeSuccess: false,
      captchaImgSrc: "",
      formLabelWidth: "1px",
      labelPosition: "right",
      loading: false,
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入用户名" },
        ],
        password: [{ required: true, trigger: "blur", message: "请输入密码" }],
        captcha: [{ required: true, trigger: "blur", message: "请输入验证码" }],
      },
    };
  },
  created() {
    this.handleGetCaptcha();
  },
  mounted() {
    window.addEventListener("scroll", this.initHeight);
    this.$nextTick(() => {
      this.offsetTop = document.querySelector("#navbar").offsetTop;
    });
  },
  methods: {
    initHeight() {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      this.isFixed = scrollTop > this.offsetTop ? true : false;
    },

    handleToRegister() {
      this.$router.push("/register?redirect=" + this.$route.fullPath);
      this.handleCloseLogin();
    },

    handleToLogin() {
      this.loginVisible = true;
    },

    async handleLogOut() {
      await this.$store.dispatch("user/logout");
      this.logoutDialogVisible = false;
      let path = this.$route.fullPath;
      if (path.indexOf("/personal") != -1) {
        this.$router.push("/");
      }
    },

    handleClickTab(tab) {
      this.currentTab = tab;
    },

    handleLoginSubmit() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({ path: this.$route.query.redirect || "/" });
              this.loading = false;
              this.handleCloseLogin();
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    handleCloseLogin() {
      this.loginVisible = false;
      this.$refs["loginForm"].resetFields;
    },

    handleGetCaptcha() {
      this.currdatetime = new Date().getTime();
      getVerify()
        .then((res) => {
          this.requestCodeSuccess = true;
          const imgSrc = window.URL.createObjectURL(res);
          this.captchaImgSrc = imgSrc;
        })
        .catch(() => {
          this.requestCodeSuccess = false;
        });
    },
  },

  destroyed() {
    window.removeEventListener("scroll", this.initHeight);
  },
};
</script>

<style lang="scss" scoped>
.tab-item {
  a {
    color: #fff;
    text-decoration: none;
  }
}

.home-nav {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 9;
  background-color: rgba(#000, 0.25) !important;
}
.header-fixed {
  position: fixed !important;
  background-color: #2069c4 !important;
  z-index: 99;
  top: 0;
}

.nav {
  height: 100px;
  width: 100%;
  transition: all 0.5s ease;
  background-color: #2069c4;
  box-shadow: rgba(0, 0, 0, 0.3) 0 1px 5px 0px;
  .container {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 70px 0 100px;
    overflow: hidden;
    .logo-container {
      width: 380px;
      height: 74px;
    }
    .right {
      display: flex;
      height: 100%;
      align-items: center;

      .tabs {
        display: flex;
        margin-right: 80px;
        min-width: 512px;
        height: 100%;
        .tab-item {
          margin-right: 70px;
          color: #fff;
          display: flex;
          align-items: center;
          font-family: "Alibaba-PuHuiTi";
          position: relative;
          height: 100%;
          text-align: center;

          a {
            font-size: 18px;
            white-space: nowrap;
            transition: all 0.5s ease;
          }

          &:hover {
            a {
              // color: #182f68;
              color: #fff8a3;
              text-shadow: 0 1px 2px #9fafcb, 1px 0px 2px #9fafcb;
            }
          }
          .router-link-exact-active {
            // color: #182f68;
            color: #fff8a3;
            text-shadow: 0 1px 2px #9fafcb, 1px 0px 2px #9fafcb;
          }
        }
      }
    }
  }
}
@keyframes fadeUp {
  from {
    transform: translateY(200);
  }
  to {
    transform: translateY(0);
  }
}

@keyframes fadeLeft {
  from {
    transform: translateX(-1000);
  }
  to {
    transform: translateX(0);
  }
}
::v-deep .tools {
  .el-button {
    background: transparent;
    border-color: #fff;
    color: #fff;
    i {
      font-size: 16px;
    }
  }
}

.el-dropdown-menu {
  background-color: #fff;
  // border: 1px solid #8a919f;
}
.el-dropdown-menu__item {
  background-color: transparent;
  // color: #fff;
  color: #8a919f;
  a {
    text-decoration: none;
    color: #8a919f;
  }
}

.el-dropdown-menu__item:focus,
.el-dropdown-menu__item:not(.is-disabled):hover {
  background-color: #f8f8f8;
  color: #8a919f;
}

.login {
  .title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    .text {
      font-weight: bold;
      font-size: 24px;
      color: #666;
    }
    .close {
      font-size: 34px;
      cursor: pointer;
      color: #ccc;
    }
  }
  .login-box {
    width: 500px;
    margin: auto;
  }
}
.opration {
  display: flex;
  justify-content: space-between;
  flex-direction: column;
  margin-top: 20px;
  .register {
    display: flex;
    justify-content: center;
    margin-top: 20px;
    font-size: 14px;
    cursor: pointer;
    &:hover {
      color: #2069c4;
    }
  }
}

.dialog-footer {
  display: flex;
  justify-content: center;
}

.captcha {
  display: flex;
  justify-content: flex-end;
  img {
    margin-top: 2px;
    height: 40px;
  }
}

::v-deep .loginBtn {
  padding: 16px !important;
  border-radius: 26px !important;
  width: 100%;
  background-color: #2069c4;
  color: #fff;
  font-size: 14px;
  &:hover {
    filter: brightness(0.9) !important;
    background-color: #2069c4;
    color: #fff;
    border: none;
    border-radius: 26px !important;
    padding: 16px !important;
    width: 100%;
    font-size: 14px;
  }
}
::v-deep .el-dialog {
  width: 380px;
  border-radius: 16px;
  .el-dialog__body {
    padding: 20px 30px 60px !important;
  }
}

::v-deep .el-input__inner {
  background: #e8f0fe;
  border: none;
}

::v-deep .el-dialog__header {
  padding: 48px 30px 10px !important;
  color: #666 !important;
  font-weight: bold;
}

::v-deep .el-dialog__headerbtn {
  top: 48px !important;
  font-size: 24px;
  right: 30px;
}
</style>
