<template>
  <header class="header">
    <div class="main">
      <h3 class="logo">桐乡艺校</h3>
      <ul class="nav-list">
        <li v-for="item in navList" :class="curNavName==item.name?'selected':''" @click="switchNav(item)">
          {{ item.title }}<span></span></li>
      </ul>
      <div>
        <button v-if="!userInfo" type="button" class="login-btn" @click="userLogin">登录</button>
        <div v-else>
          <span class="icon-message d-in-block v-mid hand"></span>

          <el-dropdown srccommand="handleCommand">
            <div class="ml10 d-in-block v-mid shot pr hand">
              <img :src="userInfo.user_avatar" alt="">
              <span class="dot"></span>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="manage">个人中心</el-dropdown-item>
                <el-dropdown-item command="loginOut">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>

      </div>
    </div>
  </header>
  <div class="login-mask" v-if="loginShow">
    <div class="login-box">
      <div class="login-close"></div>
      <div class="login-logo"><img src="/src/assets/images/header/login-logo.png" alt=""></div>
      <div class="form-box">

        <div class="form-line">
          <input type="text" class="form-in" v-model="user_name" autocomplete="off" placeholder="账号">
          <span class="icon-user"></span>
        </div>
        <div class="form-line">
          <input type="password" class="form-in" v-model="user_password" placeholder="数字、字母或符号组成的密码">
          <span class="icon-pwd"></span>
        </div>
        <button type="button" :disabled="user_name == ''||user_password == ''" class="form-btn"
                @click="userLoginSubmit">登录
        </button>
        <div class="bottom-info">
          <a href="#">关于</a>
          &nbsp;
          <a href="#">支持</a>
        </div>
      </div>


    </div>
  </div>
</template>

<script>
import {login} from "@/components/header/api.js";

export default {
  name: "commonHeader",
  data() {
    return {
      userInfo: null,
      navList: [{
        id: '1',
        title: '首页',
        name: 'home'
      }, {
        id: '2',
        title: '课程资源',
        name: 'resource'
      }, {
        id: '3',
        title: '直播课堂',
        name: 'zbkt'
      }, {
        id: '4',
        title: '智慧教研',
        name: 'zhjy'
      }, {
        id: '5',
        title: '艺术广场',
        name: 'ysgc'
      }, {
        id: '6',
        title: '数字美术馆',
        name: 'szmsg'
      }],
      curNavName: 'home',
      loginShow: false,
      user_name: '',
      user_password: '',
    }
  },
  methods: {
    switchNav(item) {
      this.$router.push({
        name: item.name
      });
      localStorage.setItem('curNav', item.name);
    },
    userLogin() {
      this.user_name = '';
      this.user_password = '';
      this.loginShow = true;
    },
    userLoginSubmit() {
      let data = {
        user_name: this.user_name,
        user_password: this.user_password
      }
      login(data).then(res => {
        if (res.code == 1) {
          console.log(res.data, 88)
          this.$message({
            message: '登录成功！',
            type: 'success',
            showClose: true,
            duration: '1000',
            onClose: () => {
              this.loginShow = false;
              this.userInfo = res.data;
              localStorage.setItem('userInfo', JSON.stringify(res.data));
            }
          });
        }
      })
    },
    handleCommand(command) {
      switch (command) {
        case 'loginOut':
          if (localStorage.getItem('userInfo')) {
            localStorage.removeItem('userInfo');
            this.userInfo = null;
            this.$router.push({name: 'home'});
          }
          break;
        case 'manage':

          break;
      }
    }
  },
  mounted() {
    if (localStorage.getItem('curNav')) {
      this.curNavName = localStorage.getItem('curNav');
    }
    if (localStorage.getItem('userInfo')) {
      this.userInfo = JSON.parse(localStorage.getItem('userInfo'));
    }
  }
}
</script>

<style lang="scss" scoped>
.header {
  width: 100%;
  height: 64px;
  background: #fff;
  box-shadow: 0px 10px 24px 0px rgba(0, 20, 31, 0.08);

  .main {
    width: 1400px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .logo {
    font-size: 36px;
    line-height: 64px;
    color: #000;
  }

  .nav-list {
    margin-left: 226px;
    display: flex;

    li {
      position: relative;
      font-size: 18px;
      color: #000;
      text-align: center;
      cursor: pointer;
      line-height: 64px;

      & + li {
        margin-left: 45px;
      }

      &.selected {
        span {
          display: block;
        }
      }

      span {
        display: none;
        position: absolute;
        left: 0;
        bottom: 0;
        width: 100%;
        height: 3px;
        background: #00A3FF;
      }
    }
  }

  .login-btn {
    width: 100px;
    height: 40px;
    background: #00a3ff;
    border-radius: 20px;
    font-size: 16px;
    color: #fff;
    text-align: center;
  }

  .icon-message {
    cursor: pointer;
    display: inline-block;
    vertical-align: middle;
    position: relative;
    margin-right: 10px;
    margin-top: 10px;
    width: 20px;
    height: 20px;
    background: url("src/assets/images/header/icon-message.png") center no-repeat;
    background-size: 20px 20px;
  }

  .shot {
    position: relative;
    cursor: pointer;
    display: inline-block;
    vertical-align: middle;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    box-shadow: 0 1px 2px rgba(17, 46, 74, 0.10) inset;
    overflow: hidden;

    img {
      width: 100%;
      border-radius: 50%;
    }

    .dot {
      position: absolute;
      right: 3px;
      bottom: 3px;
      width: 14px;
      height: 14px;
      border-radius: 50%;
      background: #3EBB92;
      border: 2px solid #F1FEF6;
    }
  }
}

.login-mask {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 200;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, .8);
  display: flex;
  align-items: center;
  justify-content: center;

  .login-box {
    position: relative;
    width: 1065px;
    height: 624px;
    background: url("src/assets/images/header/login-bg.png") center no-repeat;
    padding: 80px 0 0 110px;

    .login-close {
      position: absolute;
      right: -40px;
      top: -40px;
      width: 30px;
      height: 30px;
      background: url("src/assets/images/header/login-close.png") center no-repeat;
      background-size: 30px 30px;
      cursor: pointer;
    }

    .login-logo {
      margin-left: 55px;
      width: 310px;
      height: 104px;

      img {
        width: 100%;
      }
    }

    .form-box {
      width: 420px;
      margin-top: 60px;

      .form-line {
        width: 100%;
        height: 72px;
        border-radius: 10px;
        border: 1px solid rgba(228, 230, 232, .6);
        background: #fff;
        display: flex;
        align-items: center;
        padding-left: 20px;
        padding-right: 15px;

        & + .form-line {
          margin-top: 8px;
        }
      }

      .form-in {
        flex: 1;
        height: 70px;
        border: 0;
        font-size: 16px;
        color: #666;
        background: #fff;
        outline: none;
      }

      .icon-user {
        width: 21px;
        height: 23px;
        background: url("src/assets/images/header/icon-user.png") center no-repeat;
        background-size: 21px 23px;
      }

      .icon-pwd {
        width: 21px;
        height: 23px;
        background: url("src/assets/images/header/icon-pwd.png") center no-repeat;
        background-size: 17px 20px;
      }

      .form-btn {
        margin-top: 30px;
        width: 100%;
        height: 54px;
        background: #00a3ff;
        border-radius: 10px;
        font-size: 16px;
        color: #fff;
        text-align: center;

        &:disabled {
          opacity: .5;
          cursor: default;
          pointer-events: none;
        }
      }

      .bottom-info {
        margin-top: 100px;
        text-align: center;

        a {
          font-size: 12px;
          color: #666;
        }
      }

    }
  }
}
</style>