<template>
  <div class="view-account">
    <div class="view-account-header"></div>
    <div class="view-account-container">
      <div class="view-account-top">
        <div class="view-account-top-logo">
          <img src="~@/assets/images/account-logo.png" alt="" />
        </div>
        <div class="view-account-top-desc">Element plus Admin 中后台前端/设计解决方案</div>
      </div>
      <div class="view-account-form">
        <el-form
          ref="formRef"
          label-placement="left"
          size="large"
          :model="formInline"
          :rules="rules"
        >
          <el-form-item path="username">
            <el-input v-model:value="formInline.username" placeholder="请输入用户名">
              <template #prefix>
                <el-icon class="el-input__icon" size="18" color="#808695">
                  <PersonOutline />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item path="password">
            <el-input
              v-model:value="formInline.password"
              type="password"
              showPasswordOn="click"
              placeholder="请输入密码"
              @keyup.enter="handleSubmit"
            >
              <template #prefix>
                <el-icon class="el-input__icon" size="18" color="#808695">
                  <LockClosedOutline />
                </el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item path="isCaptcha">
            <div class="w-full">
              <mi-captcha width="384" theme-color="#2d8cf0" :logo="logo" @success="onAuthCode" />
            </div>
          </el-form-item>
          <el-form-item class="default-color">
            <div class="flex justify-between">
              <div class="flex-initial">
                <el-checkbox v-model:checked="autoLogin">自动登录</el-checkbox>
              </div>
              <div class="flex-initial order-last">
                <a href="javascript:">忘记密码</a>
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="w-full"
              @click="handleSubmit"
              size="large"
              :loading="loading"
              block
            >
              登录
            </el-button>
          </el-form-item>
          <el-form-item class="default-color">
            <div class="flex view-account-other">
              <div class="flex-initial">
                <span>其它登录方式</span>
              </div>
              <div class="flex-initial mx-2">
                <a href="javascript:">
                  <el-icon class="el-input__icon" size="24" color="#2d8cf0">
                    <LogoGithub />
                  </el-icon>
                </a>
              </div>
              <div class="flex-initial mx-2">
                <a href="javascript:">
                  <el-icon class="el-input__icon" size="24" color="#2d8cf0">
                    <LogoFacebook />
                  </el-icon>
                </a>
              </div>
              <div class="flex-initial" style="margin-left: auto">
                <a href="javascript:">注册账号</a>
              </div>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { useRoute, useRouter } from 'vue-router';
  import { useUserStore } from '@/store/modules/user';
  import { ElMessage } from 'element-plus';
  import { ResultEnum } from '@/enums/httpEnum';
  import logo from '@/assets/images/logo.png';
  import { PersonOutline, LockClosedOutline, LogoGithub, LogoFacebook } from '@vicons/ionicons5';

  interface FormState {
    username: string;
    password: string;
  }

  const formRef = ref();

  const loading = ref(false);
  const autoLogin = ref(true);

  const formInline = reactive({
    username: '',
    password: '',
    isCaptcha: false,
  });

  const rules = {
    username: { required: true, message: '请输入用户名', trigger: 'blur' },
    password: { required: true, message: '请输入密码', trigger: 'blur' },
    isCaptcha: {
      required: true,
      type: 'boolean',
      trigger: 'change',
      message: '请点击按钮进行验证码校验',
      validator: (_, value) => value === true,
    },
  };

  const userStore = useUserStore();

  const router = useRouter();
  const route = useRoute();

  const handleSubmit = (e) => {
    e.preventDefault();
    formRef.value.validate(async (errors) => {
      if (!errors) {
        const { username, password } = formInline;
        loading.value = true;
        const params: FormState = {
          username,
          password,
        };

        const { code, msg } = await userStore.login(params);

        if (code == ResultEnum.SUCCESS) {
          const toPath = decodeURIComponent((route.query?.redirect || '/') as string);
          ElMessage.success('登录成功！');
          router.replace(toPath).then((_) => {
            if (route.name == 'login') {
              router.replace('/');
            }
          });
        } else {
          ElMessage.info(msg || '登录失败');
        }
      } else {
        ElMessage.error('请填写完整信息，并且进行验证码校验');
      }
    });
  };

  const onAuthCode = () => {
    formInline.isCaptcha = true;
  };
</script>

<style lang="scss" scoped>
  .view-account {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: auto;

    &-container {
      flex: 1;
      padding: 32px 0;
      width: 384px;
      margin: 0 auto;
    }

    &-top {
      padding: 32px 0;
      text-align: center;

      &-desc {
        font-size: 14px;
        color: #808695;
      }
    }

    &-other {
      width: 100%;
    }

    .default-color {
      color: #515a6e;

      .ant-checkbox-wrapper {
        color: #515a6e;
      }
    }
  }

  @media (min-width: 768px) {
    .view-account {
      background-image: url('../../assets/images/login.svg');
      background-repeat: no-repeat;
      background-position: 50%;
      background-size: 100%;
    }

    .page-account-container {
      padding: 32px 0 24px 0;
    }
  }
</style>
