<script setup>
import {login,message} from "./api.js";
import {user, refresh} from '~/store/user';

// console.log('user :>> ', user);
const 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'
  },
  {
    id: '7',
    title: '同步课堂',
    name: 'tbkt'
  }
]

const route = useRoute()
const currentRootRoute = $computed(() => route.matched[0].path.substring(1))

const router = useRouter()


let loginShow = $ref(currentRootRoute !== 'home' && !user);
let loginForm = reactive({
  user_name: '',
  user_password: '',
})

function userLogin() {
  loginForm.user_name = '';
  loginForm.user_password = '';
  loginShow = true
}

function userLoginSubmit() {
  login(loginForm).then(res => {
    if (res.code == 1) {
      ElMessage({
        message: '登录成功！',
        type: 'success',
        showClose: true,
      });
      // user = res.data

      localStorage.setItem('userInfo', JSON.stringify(res.data));
      refresh()
      loginShow = false;
      if (tempRoute) {
        routerReplace(tempRoute)
      } else {
        location.reload();
      }
    }
  })
}

function handleCommand(command) {
  switch (command) {
    case 'loginOut':
      if (localStorage.getItem('userInfo')) {
        localStorage.removeItem('userInfo');
        refresh()
        router.replace({name: 'home'});
        // loginShow = true;
      }
      break;
    case 'manage':
      let userInfo = localStorage.getItem("userInfo");
      let userData = JSON.parse(userInfo);
      let isStudent = userData.user_role_id == 76 ? true : false;
      if(!isStudent) {
        router.replace({ name: 'personal' });
      } else {
        router.replace({ name: 'personalStudent' });
      }
      break;
  }
}

let tempRoute = undefined

function handleSwitchRoute(name) {
  if (name === 'home') return routerReplace(name);
  if (user) {
    routerReplace(name)
  } else {
    tempRoute = name
    userLogin()
  }
}

function routerReplace(name) {
  if (name === 'tbkt') {
    window.open('https://tyyxopenapi.bozedu.top/tbkt.php?token=' + JSON.parse(localStorage.getItem('userInfo')).token, '_self');
  } else {
    router.replace({name})
  }

}
function toMessage() {
  router.replace({name: 'message'});
}
let messageList = $ref([]);
function getMes() {
  let data = {
    page:'1',
    limit:'3'
  }
  message(data).then(res =>{
    if(res.code === '1') {
      messageList = res.data.page_data;
    }
  })
}
if(currentRootRoute !== 'message') {
  getMes();
}

</script>

<template>
  <header class="header">
    <div class="main">
      <h3 class="logo" @click="toHome">桐艺云校</h3>
      <ul class="nav-list">
        <li v-for="item in navList" :class="{'selected':currentRootRoute===item.name}"
            @click="handleSwitchRoute(item.name)">
          {{ item.title }}<span></span>
        </li>
      </ul>
      <div>
        <button v-if="!user" type="button" class="login-btn" @click="userLogin">登录</button>
        <div v-else>
          <div class="message-box" v-if="currentRootRoute !== 'message'">
            <span class="icon-message d-in-block v-mid hand"></span>
            <div class="message-pop">
              <div class="pop-content">
                <ul class="message-list">
                  <li v-for="item in messageList" @click="toMessage">
                    <div class="m-con">
                      <div class="m-title">
                        <span class="dot"></span>
                        <span class="title-text">{{item.mc_title}}</span>
                      </div>
                      <h4 class="m-brief">{{item.mc_content}}</h4>
                      <h5 class="m-time">{{item.create_time}}</h5>
                    </div>

                  </li>
                </ul>
                <div class="mt10 text-center message-more">
                  <a href="javascript:;" @click="toMessage">更多></a>
                </div>
              </div>
            </div>
          </div>


          <el-dropdown @command="handleCommand">
            <div class="ml10 d-in-block v-mid shot pr hand">
              <img :src="user.user_avatar" alt="" :key="user?.user_avatar">
              <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" @click="loginShow = false"></div>
      <div class="login-logo"><img src="/images/header/login-logo.png" alt=""></div>
      <div class="form-box">

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


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


<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 {
    cursor: pointer;
    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 {
        color: #00A3FF;

        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;
  }

  .message-box {
    position: relative;
    display: inline-block;
    vertical-align: middle;
    cursor: pointer;
    margin-right: 10px;
    margin-top: 10px;
    &:hover{
      .message-pop{
        display: block;
      }
    }

    .icon-message {
      display: inline-block;
      vertical-align: middle;
      width: 20px;
      height: 20px;
      background: url("/images/header/icon-message.png") center no-repeat;
      background-size: 20px 20px;
    }

    .message-pop {
      display: none;
      position: absolute;
      left: 50%;
      z-index: 100;
      margin-left: -165px;
      top: 20px;
      width: 330px;
      padding-top: 10px;

      .arrow {
        margin-left: 150px;
        width: 30px;
        height: 13px;
        background: url("/images/header/icon-arrow.png") center no-repeat;
      }

      .pop-content {
        width: 100%;
        border-radius: 14px;
        background: #fff;
        box-shadow: 0 10px 24px rgba(161, 153, 168, 0.54);
        padding: 0 10px 15px 10px;
      }

      .message-list {
        li {
          padding: 10px 0;
          cursor: pointer;
          &:hover{
            .m-con{
              background: rgba(0,163,255,0.06);
            }
            .dot{
              visibility: hidden;
            }
          }
          & + li {
            border-top: 1px solid #EFF0F1;
          }
          .m-con{
            border-radius: 14px;
            padding: 15px 10px;
          }
          .m-title {
            display: flex;

            .dot {
              margin: 8px 5px 0 0;
              width: 6px;
              height: 6px;
              background: #f35421;
              border-radius: 50%;
            }
            .title-text{
              flex: 1;
              font-size: 14px;
              color: #171721;
              white-space: nowrap;
              text-overflow: ellipsis;
              overflow: hidden;
            }
          }
          .m-brief{
            margin-top: 8px;
            font-size: 14px;
            color: #8083A3;
            white-space: nowrap;
            text-overflow: ellipsis;
            overflow: hidden;
          }
          .m-time{
            margin-top: 5px;
            font-size: 12px;
            color: #8083A3;
          }
        }
      }
      .message-more{
        text-align: center;
        a{
          display: inline-block;
          font-size: 13px;
          color: #00A3FF;
        }
      }
    }
  }


  .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("/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("/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("/images/header/icon-user.png") center no-repeat;
        background-size: 21px 23px;
      }

      .icon-pwd {
        width: 21px;
        height: 23px;
        background: url("/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>
