<template>
  <div class="login flex justify-around py-24 h-[700px] items-center">
    <div class="flash">

    </div>
    <el-card class="m-5 w-screen-sm sm:w-[430px] sm:h-[520px] flex justify-around">
      <el-space class="tabs pt-5 text-xl flex justify-center">
        <el-tabs v-model="activeName" class="demo-tabs">
          <el-tab-pane label="账号登录" name="account">
            <div class="custom-style my-4">
              <el-form :model="form" label-width="auto" class="w-[330px]">
                <el-form-item>
                  <el-input class="w-full" size="large" maxlength="11" placeholder="请输入账号|手机号码" v-model="form.username">
                    <template #prepend>+86</template>
                  </el-input>
                </el-form-item>
                <el-form-item>
                  <el-input type="password" size="large" placeholder="登录密码" v-model="form.password" />
                </el-form-item>
                <el-form-item>
                  <el-space class="flex justify-between w-full">
                    <el-input size="large" placeholder="图形验证码" maxlength="5" v-model="form.code" @keyup.enter.prevent="onSubmit" />
                    <el-image alt="" :src="captcha" @click="changeCaptcha" />
                  </el-space>
                </el-form-item>

                <el-form-item label="记住密码">
                  <el-switch v-model="form.remember" />
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" size="large" class="w-full" @click="onSubmit">登录</el-button>
                </el-form-item>
              </el-form>
            </div>
          </el-tab-pane>
          <el-tab-pane label="短信登录" name="sms">
            <div class="custom-style my-4">
              <el-form :model="form" label-width="auto" class="w-[330px]">
                <el-form-item>
                  <el-input class="w-full" size="large" maxlength="11" placeholder="请输入手机号码" v-model="form.phone">
                    <template #prepend>+86</template>
                  </el-input>
                </el-form-item>
                <el-form-item>
                  <el-space class="flex justify-between w-full">
                    <el-input size="large" placeholder="短信验证码" class="w-full" v-model="form.code" @keyup.enter.prevent="onSubmitBySms" />
                    <el-button size="large" class="w-full" :disabled="!!countdownTime" @click="sendCode">
                      <span v-if="!countdownTime">发送验证码</span>
                      <span v-else>已发送 {{ countdownTime }} s</span>
                    </el-button>
                  </el-space>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" size="large" class="w-full" @click="onSubmitBySms">登录</el-button>
                </el-form-item>
              </el-form>
            </div>
          </el-tab-pane>
        </el-tabs>
      </el-space>
      <!-- 快捷登录 --->
      <template v-if="activeName == 'account'">
        <div class="clearfix flex justify-center">
          <el-divider>
            <span class="text-gray-400">其他登录方式</span>
          </el-divider>
        </div>
        <div class="clearfix flex justify-center">
          <el-button circle :icon="ElIconUserFilled"></el-button>
        </div>
      </template>
    </el-card>
  </div>
</template>
<script setup lang="ts">
import {useConfigInfo, useToken, useUser, useWebsite} from "~/composables/configState";
import useFormData from '@/utils/use-form-data';
import type { User } from '@/api/system/user/model';
import { ref } from 'vue'
import {useServerRequest} from "~/composables/useServerRequest";
import type {ApiResult} from "~/api";
import type {CaptchaResult, LoginResult} from "~/api/passport/login/model";

// 配置信息
const runtimeConfig = useRuntimeConfig();
const website = useWebsite();
const config = useConfigInfo();
const token = useToken();
const user = useUser();

const activeName = ref('account')

// 验证码 base64 数据
const captcha = ref('');
// 验证码内容, 实际项目去掉
const text = ref('');
// 图形验证码
const imgCode = ref('');
// 发送验证码按钮loading
const codeLoading = ref(false);
// 验证码倒计时时间
const countdownTime = ref(0);
// 验证码倒计时定时器
let countdownTimer: number | null = null;

// 配置信息
const { form } = useFormData<User>({
  username: '',
  phone: '',
  password: '',
  code: '',
  smsCode: '',
  remember: true,
  isAdmin: true
});

/* 显示发送短信验证码弹窗 */
const openImgCodeModal = () => {
  if (!form.phone) {
    ElMessage.error('请输入手机号码');
    return;
  }
  // imgCode.value = text.value;
};


/* 发送短信验证码 */
const sendCode = async () => {
  if (!form.phone) {
    ElMessage.error('请输入手机号码');
    return;
  }
  imgCode.value = text.value;
  codeLoading.value = true;

  const {data: smsCode } = await useServerRequest<ApiResult<CaptchaResult>>('/sendSmsCaptcha',{baseURL: runtimeConfig.public.apiServer,method: "post",body: {
    phone: form.phone
    }});
  if(smsCode.value){
    codeLoading.value = false;
    countdownTime.value = 30;
    // 开始对按钮进行倒计时
    countdownTimer = window.setInterval(() => {
      if (countdownTime.value <= 1) {
        countdownTimer && clearInterval(countdownTimer);
        countdownTimer = null;
      }
      countdownTime.value--;
    }, 1000);
  }
};

const navigateTo = (url: string) => {
  window.location.href = url;
}

/* 获取图形验证码 */
const changeCaptcha = async () => {
  const {data: captchaInfo } = await useServerRequest<ApiResult<CaptchaResult>>('/captcha',{baseURL: runtimeConfig.public.apiServer});
  const captchaData = captchaInfo.value?.data
  if(captchaData){
    captcha.value = captchaData.base64;
    text.value = captchaData.text;
  }

  // 已经登录跳转首页
  if(token.value && token.value.length > 0){
    navigateTo('/user')
    return;
  }
};


useHead({
  title: `登录页 - ${config.value?.siteName || 'WEB应用开发平台'}`,
  meta: [{ name: website.value.keywords, content: website.value.comments }]
});

/**
 * 执行登录
 */
const onSubmit = async () => {
  const {data: response} = await useServerRequest<ApiResult<LoginResult>>('/login',{baseURL: runtimeConfig.public.apiServer,method: "post",body: form})
  // 登录成功
  if(response.value?.code == 0){
    ElMessage.success(response.value?.message)
    await doLogin(response.value.data)
  }
  if(response.value?.code != 0){
    ElMessage.error(response.value?.message)
    await changeCaptcha()
  }
}

/**
 * 短信验证码登录
 */
const onSubmitBySms = async () => {
  const {data: response} = await useServerRequest<ApiResult<LoginResult>>('/loginBySms',{baseURL: runtimeConfig.public.apiServer,method: "post",body: {
      phone: form.phone,
      code: form.code,
      tenantId: 5
    }})
  // 登录成功
  if(response.value?.code == 0){
    ElMessage.success(response.value?.message)
    await doLogin(response.value.data)
  }
  if(response.value?.code != 0){
    ElMessage.error(response.value?.message)
    await changeCaptcha()
  }
}

// 登录成功执行
const doLogin = async (data: any) => {
  const access_token = data?.access_token
  if(access_token){
    token.value = access_token;
  }
  if(data.user){
    user.value.userId = data.user.userId;
    user.value.phone = data.user.phone;
    user.value.gradeId = data.user.gradeId;
    user.value.gradeName = data.user.gradeName;
    user.value.avatar = data.user.avatar;
    localStorage.setItem('UserId',data.user.userId);
  }
  setTimeout(() => {
    navigateTo('/user')
    return;
  },500)
}

changeCaptcha();
</script>
<style lang="less">
.login{
  background: url("https://oss.wsdns.cn/20240904/6f5dc87c37334c4da3453826352a37d1.jpg");
  background-size: 100%;
}
</style>
