<template>
  <el-form
    ref="formRef"
    :show-label="false"
    :show-require-mark="false"
    size="large"
    :model="formInline"
    :rules="rules"
  >
    <el-form-item prop="username">
      <el-input v-model="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 prop="password">
      <el-input
        v-model="formInline.password"
        type="password"
        show-password
        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>
    <div class="mb-6 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>
    </div>
    <el-form-item :show-label="false">
      <el-button
        class="w-full"
        type="primary"
        @click="handleSubmit"
        size="large"
        :loading="loading"
      >
        登录
      </el-button>
    </el-form-item>
    <div class="mb-4 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:" @click="goRegister">注册账号</a>
        </div>
      </div>
    </div>
  </el-form>
</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 { PersonOutline, LockClosedOutline, LogoGithub, LogoFacebook } from '@vicons/ionicons5';
  import { PageEnum } from '@/enums/pageEnum';

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

  const formRef = ref();

  const loading = ref(false);
  const autoLogin = ref(true);
  const LOGIN_NAME = PageEnum.BASE_LOGIN_NAME;

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

  const rules = {
    username: { required: true, message: '请输入用户名', trigger: 'blur' },
    password: { required: true, message: '请输入密码', trigger: 'blur' },
  };
  const emit = defineEmits(['goRegister']);
  const userStore = useUserStore();

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

  const handleSubmit = () => {
    if (!formRef.value) return;
    formRef.value.validate(async (valid) => {
      if (valid) {
        const { username, password } = formInline;
        loading.value = true;

        const params: FormState = {
          username,
          password,
          code:'618'
        };

        try {
          const { code,msg } = await userStore.login(params);
          if (code == ResultEnum.SUCCESS) {
            const toPath = decodeURIComponent((route.query?.redirect || '/') as string);
            ElMessage.success('登录成功，即将进入系统');
            if (route.name === LOGIN_NAME) {
              router.replace('/');
            } else router.replace(toPath);
          } else {
            ElMessage.error(msg || '登录失败');
          }
        } finally {
          loading.value = false;
        }
      } else {
        ElMessage({
          message: '请填写完整信息',
          type: 'error',
        });
      }
    });
  };

  function goRegister() {
    emit('goRegister');
  }
</script>
