<template>
  <div class="login-container" ref="login" @keyup.enter="handleLogin">
    <div class="login-wrapper animated bounceInDown">
      <!-- 左侧图形装饰区 -->
      <div class="login-left">
        <div class="decoration-bg">
          <div class="circle circle-1"></div>
          <div class="circle circle-2"></div>
          <div class="circle circle-3"></div>
        </div>

        <div class="content-wrapper">
          <div class="logo-area">
            <div class="logo-circle">
              <div class="logo-icon">武</div>
            </div>
            <h1 class="brand-title">武术赛事通</h1>
            <p class="brand-subtitle">专业的武术赛事管理平台</p>
          </div>

          <div class="feature-points">
            <div class="point-item">
              <i class="el-icon-trophy"></i>
              <span>专业赛事管理</span>
            </div>
            <div class="point-item">
              <i class="el-icon-s-flag"></i>
              <span>智能编排调度</span>
            </div>
            <div class="point-item">
              <i class="el-icon-medal"></i>
              <span>权威评分系统</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 右侧登录表单区 -->
      <div class="login-right">
        <div class="login-form-container">
          <h2 class="login-title">
            系统登录
            <top-lang></top-lang>
          </h2>
          <userLogin></userLogin>
        </div>

        <div class="copyright">
          © 2025 武术赛事通 All Rights Reserved
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import userLogin from './userlogin.vue';
import codeLogin from './codelogin.vue';
import thirdLogin from './thirdlogin.vue';
import { mapGetters } from 'vuex';
import { validatenull } from '@/utils/validate';
import topLang from '@/page/index/top/top-lang.vue';
import { getQueryString, getTopUrl } from '@/utils/util';
import website from '@/config/website';

export default {
  name: 'login',
  components: {
    userLogin,
    codeLogin,
    thirdLogin,
    topLang,
  },
  data() {
    return {
      website: website,
      time: '',
      activeName: 'user',
      socialForm: {
        tenantId: '000000',
        source: '',
        code: '',
        state: '',
      },
    };
  },
  watch: {
    $route() {
      this.handleLogin();
    },
  },
  created() {
    this.handleLogin();
    this.getTime();
  },
  mounted() {},
  computed: {
    ...mapGetters(['tagWel']),
  },
  props: [],
  methods: {
    getTime() {
      setInterval(() => {
        this.time = this.$dayjs().format('YYYY-MM-DD HH:mm:ss');
      }, 1000);
    },
    handleLogin() {
      const topUrl = getTopUrl();
      const redirectUrl = '/oauth/redirect/';
      const ssoCode = '?code=';
      this.socialForm.source = getQueryString('source');
      this.socialForm.code = getQueryString('code');
      this.socialForm.state = getQueryString('state');
      if (validatenull(this.socialForm.source) && topUrl.includes(redirectUrl)) {
        let source = topUrl.split('?')[0];
        source = source.split(redirectUrl)[1];
        this.socialForm.source = source;
      }
      if (
        topUrl.includes(redirectUrl) &&
        !validatenull(this.socialForm.source) &&
        !validatenull(this.socialForm.code) &&
        !validatenull(this.socialForm.state)
      ) {
        const loading = this.$loading({
          lock: true,
          text: '第三方系统登录中,请稍后',
          background: 'rgba(0, 0, 0, 0.7)',
        });
        this.$store
          .dispatch('LoginBySocial', this.socialForm)
          .then(() => {
            window.location.href = topUrl.split(redirectUrl)[0];
            this.loadFlowRoutes();
            this.$router.push(this.tagWel);
            loading.close();
          })
          .catch(() => {
            loading.close();
          });
      } else if (
        !topUrl.includes(redirectUrl) &&
        !validatenull(this.socialForm.code) &&
        !validatenull(this.socialForm.state)
      ) {
        const loading = this.$loading({
          lock: true,
          text: '单点系统登录中,请稍后',
          background: 'rgba(0, 0, 0, 0.7)',
        });
        this.$store
          .dispatch('LoginBySso', this.socialForm)
          .then(() => {
            window.location.href = topUrl.split(ssoCode)[0];
            this.loadFlowRoutes();
            this.$router.push(this.tagWel);
            loading.close();
          })
          .catch(() => {
            loading.close();
          });
      }
    },
    loadFlowRoutes() {
      this.$store.dispatch('FlowRoutes').then(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: #f5f5f5;
  padding: 20px;
}

.login-wrapper {
  display: flex;
  width: 1000px;
  max-width: 100%;
  min-height: 600px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.12);
  overflow: hidden;
}

// 左侧图形装饰区
.login-left {
  position: relative;
  width: 50%;
  background: linear-gradient(135deg, #dc2626 0%, #991b1b 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px 40px;
  overflow: hidden;
}

.decoration-bg {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;

  .circle {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.08);

    &.circle-1 {
      width: 400px;
      height: 400px;
      top: -100px;
      right: -100px;
      animation: float 8s ease-in-out infinite;
    }

    &.circle-2 {
      width: 300px;
      height: 300px;
      bottom: -80px;
      left: -80px;
      animation: float 10s ease-in-out infinite reverse;
    }

    &.circle-3 {
      width: 200px;
      height: 200px;
      top: 50%;
      left: 20%;
      transform: translate(-50%, -50%);
      animation: float 6s ease-in-out infinite;
    }
  }
}

@keyframes float {
  0%, 100% {
    transform: translateY(0) scale(1);
  }
  50% {
    transform: translateY(-20px) scale(1.05);
  }
}

.content-wrapper {
  position: relative;
  z-index: 1;
  color: #fff;
  text-align: center;
  width: 100%;
}

.logo-area {
  margin-bottom: 50px;
}

.logo-circle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 120px;
  height: 120px;
  background: rgba(255, 255, 255, 0.15);
  border: 3px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  margin-bottom: 30px;
  backdrop-filter: blur(10px);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.logo-icon {
  font-size: 56px;
  font-weight: 900;
  color: #fff;
}

.brand-title {
  font-size: 36px;
  font-weight: 700;
  margin: 0 0 12px 0;
  letter-spacing: 4px;
}

.brand-subtitle {
  font-size: 14px;
  margin: 0;
  opacity: 0.9;
  font-weight: 300;
  letter-spacing: 1px;
}

.feature-points {
  display: flex;
  flex-direction: column;
  gap: 20px;
  max-width: 280px;
  margin: 0 auto;
}

.point-item {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 16px 20px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.15);
  transition: all 0.3s ease;
  font-size: 15px;

  i {
    font-size: 22px;
  }

  &:hover {
    background: rgba(255, 255, 255, 0.15);
    transform: translateX(8px);
  }
}

// 右侧登录表单区
.login-right {
  width: 50%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 60px 50px 30px;
  background: #fff;
}

.login-form-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-title {
  font-size: 28px;
  color: #333;
  font-weight: 700;
  margin: 0 0 40px 0;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  position: relative;

  &::after {
    content: '';
    position: absolute;
    bottom: -15px;
    left: 50%;
    transform: translateX(-50%);
    width: 60px;
    height: 3px;
    background: linear-gradient(90deg, #dc2626 0%, #991b1b 100%);
    border-radius: 2px;
  }
}

.copyright {
  text-align: center;
  font-size: 12px;
  color: #999;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
  margin-top: 30px;
}

// 响应式设计
@media (max-width: 768px) {
  .login-wrapper {
    flex-direction: column;
    min-height: auto;
  }

  .login-left,
  .login-right {
    width: 100%;
  }

  .login-left {
    padding: 40px 30px;
  }

  .login-right {
    padding: 40px 30px 25px;
  }

  .logo-circle {
    width: 90px;
    height: 90px;
    margin-bottom: 20px;
  }

  .logo-icon {
    font-size: 42px;
  }

  .brand-title {
    font-size: 28px;
  }

  .logo-area {
    margin-bottom: 30px;
  }

  .feature-points {
    max-width: 100%;
  }
}
</style>
