<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-[420px] sm:h-[450px] flex justify-around relative border-0" style="border: 0;">
      <div class="login-bar absolute top-0 right-0 cursor-pointer">
        <div class="go-to-register cursor-pointer">
          <img src="/assets/images/O1CN01yz6fEl1MwaRtkJyvf_!!6000000001499-55-tps-70-70.svg" alt=""/>
        </div>
        <span class="absolute top-3.5 right-2 text-sm text-white font-bold cursor-pointer"><el-icon size="20"><FullScreen /></el-icon></span>
      </div>
      <!-- 登录界面 -->
      <el-space class="tabs pt-3 text-xl flex justify-center" v-if="loginBar">
        <el-tabs v-model="activeName" class="">
          <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" placeholder="登录账号" maxlength="20" :prefix-icon="Avatar" v-model="form.username" />
                </el-form-item>
                <el-form-item>
                  <el-input type="password" size="large" maxlength="100" placeholder="登录密码" :prefix-icon="Briefcase" 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="短信验证码" maxlength="6" class="w-full" v-model="form.code" @keyup.enter.prevent="onSubmitBySms" />
                    <el-button size="large" class="w-full" :disabled="!!countdownTime" @click="checkUser">
                      <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="loginBar">
        <div class="clearfix flex justify-center">
          <nuxt-link to="/passport/login?type=register"><span class="text-sm text-blue-400">免费注册</span></nuxt-link>
        </div>
      </template>
      <!-- 注册界面 -->
      <el-space class="tabs pt-3 text-xl flex justify-center" v-if="!loginBar">
        <el-tabs v-model="activeName">
          <el-tab-pane label="手机号注册" name="account">
            <span class="text-sm text-gray-400">
              未注册手机号验证通过后将自动注册
            </span>
            <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="短信验证码" maxlength="6" class="w-full" v-model="form.code" />
                    <el-button size="large" class="w-full" :disabled="!!countdownTime" @click="checkUser">
                      <span v-if="!countdownTime">发送验证码</span>
                      <span v-else>已发送 {{ countdownTime }} s</span>
                    </el-button>
                  </el-space>
                </el-form-item>
                <el-form-item>
                  <el-checkbox v-model="form.isAgree">我已阅读并同意</el-checkbox>
                  <a href="#" class="text-gray-700">《用户协议》</a>
                  <a href="#" class="text-gray-700">《隐私政策》</a>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" size="large" class="w-full" :disabled="!form.isAgree" @click="onRegister">注册</el-button>
                </el-form-item>
                <div class="clearfix flex justify-center">
                  <nuxt-link to="/passport/login"><span class="text-sm text-blue-400">已有账号，立即登录</span></nuxt-link>
                </div>
              </el-form>
            </div>
          </el-tab-pane>
        </el-tabs>
      </el-space>
    </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 { Shop, Key, Avatar, Briefcase, FullScreen } from '@element-plus/icons-vue'
import {useServerRequest} from "~/composables/useServerRequest";
import type {ApiResult} from "~/api";
import type {CaptchaResult, LoginResult} from "~/api/passport/login/model";
import {getCaptcha, loginBySms, register, sendSmsCaptcha} from "~/api/passport/login";

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

const activeName = ref('account')

// 登录注册切换
const loginBar = ref<boolean>(true)
// 验证码 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>({
  userId: undefined,
  username: '',
  phone: '',
  password: '',
  code: '',
  smsCode: '',
  isAgree: false,
  remember: true,
  isSuperAdmin: true
});

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

const checkUser = async () => {
  const {data: hasUser } = await useServerRequest<ApiResult<CaptchaResult>>('/existence',{baseURL: runtimeConfig.public.apiServer,method: "get",params: {
      field: 'phone', value: form.phone
    }});
  if(hasUser.value?.code == 0 || loginBar.value){
    await sendCode();
  }
  if(hasUser.value?.code != 0 && loginBar.value){
    ElMessage.error('该手机号码未注册');
  }
};


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

  sendSmsCaptcha({
    phone: form.phone
  }).then(res => {
    codeLoading.value = false;
    countdownTime.value = 30;
    // 开始对按钮进行倒计时
    countdownTimer = window.setInterval(() => {
      if (countdownTime.value <= 1) {
        countdownTimer && clearInterval(countdownTimer);
        countdownTimer = null;
      }
      countdownTime.value--;
    }, 1000);
  }).catch(msg => {
    ElMessage.error(msg)
  })
};

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

const onLoginBar = () => {
  // if(loginBar.value){
  //   return navigateTo(`/passport/register`)
  // }
  loginBar.value = !loginBar.value
  activeName.value = loginBar.value ? 'account' : 'sms'
}

/* 获取图形验证码 */
const changeCaptcha = async () => {
  getCaptcha().then(captchaData => {
    captcha.value = captchaData.base64;
    text.value = captchaData.text;
  })
};


useHead({
  title: `登录页 - ${config.value?.siteName || 'WEB应用开发平台'}`
});

/**
 * 执行登录
 */
const onSubmit = async () => {
  const {data: response} = await useServerRequest<ApiResult<LoginResult>>('/login',{baseURL: 'https://server.gxbsnx.com/api',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 () => {
  loginBySms({
    phone: form.phone,
    code: form.code,
    isSuperAdmin: true
  }).then(response => {
    ElMessage.success('登录成功')
    doLogin(response)
  }).catch(() => {
    ElMessage.error('验证码错误')
    changeCaptcha()
  })
}

/**
 * 账号密码注册
 */
const onRegister = async () => {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading'
  })

  register({
    companyName: '应用名称',
    username: form.phone,
    phone: form.phone,
    password: form.password,
    code: form.code,
    email: form.email,
    isSuperAdmin: true
  }).then(response => {
      loading.close();
      ElMessage.success('注册成功')
      doLogin(response)
  }).catch(() => {
    loading.close();
  })
  // await useClientRequest<ApiResult<LoginResult>>('/register',{method: "post",body: {
  //     companyName: '应用名称',
  //     username: form.phone,
  //     phone: form.phone,
  //     password: form.password,
  //     code: form.code,
  //     email: form.email,
  //     isSuperAdmin: true
  //   }}).then(response => {
  //   // 登录成功
  //   if(response?.code == 0){
  //     loading.close();
  //     ElMessage.success(response?.message)
  //     doLogin(response.data)
  //   }
  //   if(response?.code != 0){
  //     loading.close;
  //     ElMessage.error(response?.message)
  //     changeCaptcha()
  //   }
  // }).catch(() => {
  //   loading.close();
  // }).finally(() => {
  //   loading.close();
  // })

}


// 登录成功执行
const doLogin = async (data: any) => {
  const access_token = data?.access_token
  if(access_token){
    localStorage.setItem('token',access_token);
    token.value = access_token;
    console.log(access_token,'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;
    user.value.balance = data.user.balance;
    localStorage.setItem('UserId',data.user.userId);
    localStorage.setItem('Avatar',data.user.avatar);
    localStorage.setItem('ServerTenantId',data.user.tenantId);
    // localStorage.setItem('TenantId',data.user.tenantId);
  }
  setTimeout(() => {
    navigateTo('/')
  },500)
}

changeCaptcha();

watch(
    () => route.query.type,
    (type) => {
      loginBar.value = true
      activeName.value = 'account'
      if(type == 'register'){
        loginBar.value = false
      }
    },
    { immediate: true }
);
</script>
<style lang="less">
.login{
  background: url("https://oss.wsdns.cn/20240904/6f5dc87c37334c4da3453826352a37d1.jpg");
  background-size: 100%;
}

/* 改变未激活标签的颜色 */
.el-tabs__item {
  color: #606266;
  font-size: 18px;
}
</style>
