<script setup>
import { login, message } from "./api.js";
import { user, refresh } from '~/store/user';
import { setCookie } from "~/utils/common-util.js"
// 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()

// console.log('local :>> ', localStorage.getItem('userInfo'));
// console.log('user :>> ', user);
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));
      setCookie('token', res.data.token, 30, '/','.txhlwxx.com');
      refresh()
      loginShow = false;
      if (tempRoute) {
        routerReplace(tempRoute)
      } else {
        location.reload();
      }
    }
  })
}

function handleCommand(command) {
  switch (command) {
    case 'loginOut':
      if (localStorage.getItem('userInfo')) {
        localStorage.removeItem('userInfo');

        router.replace({ name: 'home' });
        setTimeout(() => {
          location.reload();
        }, 300);
        // 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;
    case 'back_manager':
      window.open(window.GLOBAL_CONFIG.origin + '/manage/', '_self');
  }
}

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(window.GLOBAL_CONFIG.api +'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();
}

let passwordType = $ref('password')

function partyLogin(from) {
  switch (from) {
    case 'jyej':
      location.replace(`https://jyyj.zjjxedu.gov.cn:86/edu-manager/login?redirectTo=${window.location.origin}/pending.html?fromType=${from}_login`)
      break;
    case 'zlb':
      location.replace(`https://puser.zjzwfw.gov.cn/sso/newusp.do?action=ssoLogin&servicecode=beb8fa1fd85f4963bebf839f0d304d63&redirectUrl=`
        + encodeURIComponent(
          `${window.GLOBAL_CONFIG.pend}/openapi/oauth/zlb/checkticket.php?goto=${window.location.origin}/pending.html?fromType=${from}_login`
        ))
      break;
    default:
      break;
  }
}
</script>

<template>
  <header class="header">
    <div class="main">
      <h3 class="logo" @click="routerReplace('home')"></h3>
      <div>
        <h3 v-if="!user" class="title">欢迎来到桐乡市互联网学校 请登录</h3>
        <button v-if="!user" type="button" class="login-btn" @click="userLogin">登录</button>
        <div v-if="user" style="display: inline-block;vertical-align: middle">
          <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" v-if="user.unread_message && user.unread_message == 1"></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 v-if="user.user_role_id < 76" command="back_manager">管理后台</el-dropdown-item>
                <el-dropdown-item command="loginOut">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>

      </div>
    </div>
    <div class="nav-list">
      <ul>
        <li v-for="item in navList" :class="{ 'selected': currentRootRoute === item.name }"
          @click="handleSwitchRoute(item.name)">
          {{ item.title }}
        </li>
      </ul>

    </div>
  </header>

  <!-- <div class="login-mask" v-if="loginShow">
    <div class="login-box">
      <div class="login-close" @click="loginShow = false"></div>
      <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> -->

  <div class="z-2000 fixed top-0 left-0 w-screen h-screen bg-hex-000 bg-opacity-80 flex_center" v-if="loginShow">
    <div class="w-1065px h-730px relative bg-img-login ">
      <div class="absolute -right-40 -top-40 login-close" @click="loginShow = false"></div>
      <div class="box-border py-12 mx-16 w-420px flex flex-col h-full items-center">
        <div class="w-310px h-104px">
          <img class="w-full h-full" src="/images/header/login-logo.png" alt="">
        </div>
        <div class="w-full flex-auto flex-auto mt-8 flex flex-col justify-between">
          <el-input v-model="loginForm.user_name" class="h-70px" size="large" placeholder="账号">
            <template #suffix>
              <i:ant-design:user-outlined class="w-23px h-23px" />
            </template>
          </el-input>
          <el-input v-model="loginForm.user_password" class="h-70px mt-4" size="large" :type="passwordType"
            placeholder="数字、字母或符号组成的密码">
            <template #suffix>
              <i:bi:eye-slash class="w-23px h-23px cursor-pointer" v-show="passwordType === 'password'"
                @click="passwordType = 'text'" />
              <i:bi:eye class="w-23px h-23px cursor-pointer" v-show="passwordType === 'text'"
                @click="passwordType = 'password'" />
              <!-- <i:akar-icons:lock-off class="w-23px h-23px" /> -->
            </template>
          </el-input>
          <button type="button" :disabled="!(loginForm.user_name && loginForm.user_password)" @click="userLoginSubmit"
            class="w-full mt-12 h-56px bg-hex-409EFF rounded text-white tracking-0.5rem "
            :class="!(loginForm.user_name && loginForm.user_password) && 'cursor-not-allowed bg-hex-79BBFF'">登录
          </button>
          <div class="mt-12 w-full">
            <el-divider>其他登录方式</el-divider>
          </div>
          <div class="w-full flex items-center justify-around">
            <el-link type="primary" @click="partyLogin('zlb')">浙里办登录</el-link>
            <el-link type="primary" @click="partyLogin('jyej')">教育E家登录</el-link>
          </div>
          <div class="flex-grow w-full flex items-end justify-around">
            <!-- <el-link type="primary" class="text-xs">忘记密码</el-link> -->
          </div>
        </div>

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

<style>
.bg-img-login {
  background: url("/images/header/login-bg.png");
  background-size: 100% 100%;
}
</style>
<style lang="scss" scoped>
.header {
  width: 100%;
  height: 204px;
  background: #f6f8fa;
  padding-top: 8px;

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

  .logo {
    cursor: pointer;
    width: 410px;
    height: 116px;
    background: url("/images/header/logo.png") center no-repeat;
  }

  .title {
    margin-right: 15px;
    display: inline-block;
    vertical-align: middle;
    font-size: 22px;
    color: #423F72;
  }

  .nav-list {
    width: 100%;
    height: 80px;
    background: #423F72;

    ul {
      width: 1200px;
      margin: 0 auto;
      display: flex;
    }


    li {
      width: 170px;
      position: relative;
      font-size: 24px;
      color: #fff;
      text-align: center;
      cursor: pointer;
      line-height: 80px;

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

      &.selected {
        background: rgba(255, 255, 255, .2);
      }
    }
  }

  .login-btn {
    display: inline-block;
    vertical-align: middle;
    width: 140px;
    height: 40px;
    background: #423F72;
    border-radius: 20px;
    font-size: 22px;
    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: 22px;
      height: 22px;
      background: url("/images/header/icon-message.png") center no-repeat;
      background-size: 22px 22px;
    }

    .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(66, 63, 114, 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: #423F72;
        }
      }
    }
  }


  .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%;
      height: 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-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-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: 730px;
    background: url("/images/header/login-bg.png");
    background-size: 100% 100%;
    // padding: 80px 0 0 110px;

    .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>
