<template>
  <!-- <LoginFormTitle
    class="login_title"
    :style="{
      fontSize: `${22 * props.bi}px`,
      height: `${68 * props.bi}px`,
    }"
  /> -->
  <a-form
    class="p-4 enter-x"
    :model="formData"
    :rules="getFormRules"
    ref="formRef"
    v-show="getShow"
    @keypress.enter="handleLogin"
  >
    <FormItem name="account">
      <Input
        size="large"
        v-model:value="formData.account"
        placeholder="请输入账户名"
        class="fix-auto-fill"
        :style="{
          height: `${46 * props.bi}px`,
          fontSize: `${15 * props.bi}px`,
        }"
      >
        <template #prefix>
          <img
            src="/login/account.png"
            :style="{
              height: `${22 * props.bi}px`,
            }"
          />
        </template>
      </Input>
    </FormItem>
    <FormItem name="password">
      <InputPassword
        size="large"
        visibilityToggle
        v-model:value="formData.password"
        placeholder="密码"
        :style="{
          height: `${46 * props.bi}px`,
          fontSize: `${15 * props.bi}px`,
        }"
      >
        <template #prefix>
          <img
            src="/login/password.png"
            :style="{
              height: `${15 * props.bi}px`,
            }"
          />
        </template>
      </InputPassword>
    </FormItem>

    <ARow>
      <ACol :span="12">
        <a-form-item
          :style="{
            'text-align': 'left',
            fontSize: `${15 * props.bi}px`,
          }"
        >
          <Checkbox
            v-model:checked="rememberMe"
            size="small"
            :style="{
              height: `${23.5 * props.bi}px`,
              fontSize: `${15 * props.bi}px`,
            }"
          >
            {{ t('sys.login.rememberMe') }}
          </Checkbox>
        </a-form-item>
      </ACol>
    </ARow>

    <a-form-item>
      <a-button
        size="large"
        block
        @click="handleLogin"
        :loading="loading"
        class="logon"
        :style="{
          fontSize: `${20 * props.bi}px`,
          height: `${46 * props.bi}px`,
        }"
      >
        立即登录
      </a-button>
    </a-form-item>
  </a-form>
</template>
<script lang="ts" setup>
  import { reactive, ref, unref, computed, onMounted } from 'vue';
  import { Checkbox, Form, Input, Row, Col, Button, Divider } from 'ant-design-vue';

  // import LoginFormTitle from './LoginFormTitle.vue';
  import { useI18n } from '@/hooks/web/useI18n';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useUserStore } from '@/store/modules/user';
  import { LoginStateEnum, useLoginState, useFormRules, useFormValid } from './useLogin';
  import { useDesign } from '@/hooks/web/useDesign';

  const props = defineProps(['bi']);

  function computedFormItem() {
    const elements = document.querySelectorAll('.ant-form-item');
    elements.forEach((element) => {
      element.style.marginBottom = `${24 * props.bi}px`; // 修改 margin-bottom
    });
  }

  const ACol = Col;
  const ARow = Row;
  const FormItem = Form.Item;
  const InputPassword = Input.Password;
  const { t } = useI18n();
  const { notification, createErrorModal } = useMessage();
  const { prefixCls } = useDesign('login');
  const userStore = useUserStore();

  const { setLoginState, getLoginState } = useLoginState();
  const { getFormRules } = useFormRules();

  const formRef = ref();
  const loading = ref(false);
  const rememberMe = ref(false);


  const formData = reactive({
    // account: 'vben',
    // password: '123456',
    account: '',
    password: '',
  });

  const { validForm } = useFormValid(formRef);


  const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN);

  async function handleLogin() {
    const data = await validForm();
    if (!data) return;
    try {
      loading.value = true;
      const userInfo = await userStore.login({
        password: data.password,
        account: data.account,
        mode: 'none', //不要默认的错误提示
      });
      localStorage.setItem('fireUserLoginName', userInfo.name);
      if (userInfo) {
        localStorage.setItem('userid', userInfo.id);
        notification.success({
          message: t('sys.login.loginSuccessTitle'),
          description: `${t('sys.login.loginSuccessDesc')}: ${userInfo.name}`,

          duration: 3,
        });
      }
    } catch (error) {
      createErrorModal({
        title: t('sys.api.errorTip'),
        content: (error as unknown as Error).message || t('sys.api.networkExceptionMsg'),
        getContainer: () => document.body.querySelector(`.${prefixCls}`) || document.body,
      });
    } finally {
      loading.value = false;
    }
  }
  onMounted(() => {
    computedFormItem();
  });
</script>
<style lang="less" scoped>
  .login_title {
    display: flex;
    align-items: center;
    text-align: center;
    justify-content: center;

    // height: 68px;
    font-family: Alibaba PuHuiTi;
    font-weight: 900;
    // font-size: 22px;
    color: #131313;
  }
  .logon {
    display: flex;
    align-items: center;
    text-align: center;
    justify-content: center;

    background: #2f80c5;
    border-radius: 6px;

    font-family: Alibaba PuHuiTi;
    font-weight: 500;
    // font-size: 15px;
    color: #ffffff;

    &:hover {
      color: #ffffff !important;
    }
  }

  .bottom {
    position: absolute;
    bottom: 0px;
    left: 0px;
    // height: 50px;
    width: 100%;
    background: #eaf2f9;
    align-items: center;
    text-align: center;
    justify-content: center;

    Button {
      font-family: Alibaba PuHuiTi;
      font-weight: 400;
      font-size: 14px;
      color: #999999;
    }

    span {
      font-family: Alibaba PuHuiTi;
      font-weight: 400;
      color: #999999;
    }
  }
</style>
