<template>

  <div class="login-container">
      <p class="login-form-title"><h2>统一登录</h2></p>
      <p class="login-form-title-l2"><h4>基于分布式WebSocket的在线IM对话软件</h4></p>
      <a-input class="oauth-input" v-model:value="mobile" placeholder="请输入账号" />
      <a-input class="oauth-input" v-model:value="password" placeholder="请输入密码" type="password" autocomplete="new-password" :style="{ backgroundColor: 'transparent' }"/>
      <p :style="{ marginBottom: '20px' }">
        <a-checkbox v-model:checked="checked" :disabled="disabled">
          30天内免登录
        </a-checkbox>
      </p>
      <a-button type="primary" class="login-btn" @click="login">立即登录</a-button>
      <router-link :to="{ path: '/register', query: { call_back: callBackUrl } }"  tag="a">
        <a-button type="link" style="line-height: 40px; color: black;">注册新账号</a-button>
      </router-link>
  </div>
</template>

<script setup>
import { Button, Input, InputPassword, message } from 'ant-design-vue';
import axios from 'axios';
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';

const router = useRouter();
const mobile = ref('');
const password = ref('');
const route = useRoute();
const callBackUrl = ref(route.query.call_back || new URLSearchParams(window.location.search).get('call_back'));
const login = async () => {
  try {
    // 调用接口发送登录请求
    const response = await axios.post(`/sso-service/oauth/login`, {
      mobile: mobile.value,
      password: password.value
    });
    if (response.data.code == 200) {
      if(callBackUrl.value == null) {
        router.push({ name: '302' });
      }else if(callBackUrl.value.indexOf("?") > -1) {
        window.location.href = callBackUrl.value+'&oauthCode='+response.data.data;
      } else {
        window.location.href = callBackUrl.value+'?oauthCode='+response.data.data;
      }
    } else {
      message.error(response.data.msg, 60);
    }
  } catch (error) {
    // 处理错误，例如显示错误消息
    message.error("登录异常");
  }
};
</script>

<style scoped>
  * {
    margin: 0px;
    padding: 0px;
  }
  .login-container {
    width: 400px;
    height: 500px;
    background-color: white;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, .1), 0 1px 2px -1px rgba(0, 0, 0, .1);
    border: 1px solid #c0c5cd;
    padding: 40px;
  }
  .login-form-title {
    line-height: 60px;
  }
  .login-form-title-l2 {
    line-height: 30px;
    margin-bottom: 20px;
    color: rgb(84, 84, 84);
  }
  .login-btn {
    width: 100%; 
    background-color: black; 
    height: 50px;
  }
  .oauth-input {
    height: 50px;
    text-indent: 2em;
    margin-bottom: 10px;
    overflow: hidden;
  }
  .oauth-input input {
    margin-left: 20px;
  }

</style>
