<template>
  <LoginFormTitle v-show="getShow" class="enter-x" />
  <Form
    class="p-4 enter-x"
    :model="formData"
    :rules="getFormRules"
    ref="formRef"
    v-show="getShow"
    @keypress.enter="handleLogin"
  >
    <FormItem name="account" class="enter-x">
      <Input
        size="large"
        v-model:value="formData.account"
        :placeholder="t('sys.login.userName')"
        class="fix-auto-fill"
      />
    </FormItem>
    <FormItem name="password" class="enter-x">
      <InputPassword
        size="large"
        visibilityToggle
        v-model:value="formData.password"
        :placeholder="t('sys.login.password')"
      />
    </FormItem>

    <!-- <ARow class="enter-x">
      <ACol :span="12">
        <FormItem name="captcha">
          <Input
            size="large"
            v-model:value="formData.captcha"
            :placeholder="t('sys.login.captcha')"
            class="captcha"
          />
        </FormItem>
      </ACol>
      <ACol :span="12" :style="{ 'text-align': 'right' }">
        <img class="captcha-img" :src="`http://192.168.0.47:8190/captcha.jpg?t=${Date.now()}`" />
      </ACol>
    </ARow> -->

    <FormItem name="drag" class="enter-x" :style="{ 'text-align': 'right' }">
      <BasicDragVerify
        ref="el3"
        :key="dragCompWidth"
        @success="handleSuccess"
        :width="dragCompWidth"
        :text="t('sys.login.loginDragValidate')"
        :successText="t('sys.login.loginDragSuccess')"
      />
    </FormItem>

    <ARow class="enter-x" v-if="false">
      <ACol :span="12">
        <FormItem>
          <Checkbox v-model:checked="rememberMe" size="small">
            {{ t('sys.login.rememberMe') }}
          </Checkbox>
        </FormItem>
      </ACol>
      <ACol :span="12">
        <FormItem :style="{ 'text-align': 'right' }">
          <Button type="link" size="small" @click="setLoginState(LoginStateEnum.REGISTER)">
            {{ t('sys.login.registrationLabel') }}
          </Button>
        </FormItem>
      </ACol>
    </ARow>

    <FormItem class="enter-x">
      <Button type="primary" size="large" block @click="handleLogin" :loading="loading">
        {{ t('sys.login.loginButton') }}
      </Button>
      <!-- <Button
        size="large"
        class="mt-4 enter-x"
        block
        @click="setLoginState(LoginStateEnum.REGISTER)"
      >
        {{ t('sys.login.registerButton') }}
      </Button> -->
    </FormItem>

    <!-- <ARow class="enter-x">
      <ACol :md="8" :xs="24">
        <Button block @click="setLoginState(LoginStateEnum.MOBILE)">
          {{ t('sys.login.mobileSignInFormTitle') }}
        </Button>
      </ACol>
      <ACol :md="8" :xs="24" class="!my-2 !md:my-0 xs:mx-0 md:mx-2">
        <Button block @click="setLoginState(LoginStateEnum.QR_CODE)">
          {{ t('sys.login.qrSignInFormTitle') }}
        </Button>
      </ACol>
      <ACol :md="7" :xs="24">
        <Button block @click="setLoginState(LoginStateEnum.REGISTER)">
          {{ t('sys.login.registerButton') }}
        </Button>
      </ACol>
    </ARow> -->

    <!-- <Divider class="enter-x">{{ t('sys.login.otherSignIn') }}</Divider> -->

    <!-- <div class="flex justify-evenly enter-x" :class="`${prefixCls}-sign-in-way`">
      <GithubFilled />
      <WechatFilled />
      <AlipayCircleFilled />
      <GoogleCircleFilled />
      <TwitterCircleFilled />
    </div> -->
  </Form>
</template>
<script lang="ts" setup>
  import { reactive, ref, unref, computed, watch } from 'vue';
  // Divider，Row, Col,Checkbox
  import { Form, Input, Button, Row, Col, Checkbox } from 'ant-design-vue';
  // import {
  //   GithubFilled,
  //   WechatFilled,
  //   AlipayCircleFilled,
  //   GoogleCircleFilled,
  //   TwitterCircleFilled,
  // } from '@ant-design/icons-vue';
  import LoginFormTitle from './LoginFormTitle.vue';

  import { BasicDragVerify } from '/@/components/Verify/index';

  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';
  import { useLocaleStore } from '/@/store/modules/locale';

  //import { onKeyStroke } from '@vueuse/core';

  const ACol = Col;
  const ARow = Row;
  const FormItem = Form.Item;
  const InputPassword = Input.Password;
  const { t } = useI18n();
  const localeStore = useLocaleStore();
  const { notification, createErrorModal } = useMessage();
  const { prefixCls } = useDesign('login');
  const userStore = useUserStore();
  // setLoginState
  const { getLoginState, setLoginState } = useLoginState();
  const { getFormRules } = useFormRules();

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

  const formData = reactive({
    account: '',
    password: '',

    // password: encodeStr(window.btoa('zfb123456')),
    // captcha: '',
  });

  const { validForm } = useFormValid(formRef);

  //onKeyStroke('Enter', handleLogin);

  const getShow = computed(() => unref(getLoginState) === LoginStateEnum.LOGIN);
  const dragCompWidth = computed(() => {
    return localeStore.getLocale === 'ja' ? '330px' : '200px';
  });
  watch(
    () => localeStore.getLocale,
    async () => {
      isDragValidate.value = false;
    },
    {
      immediate: true,
    },
  );
  function handleSuccess() {
    // console.log('11');
    isDragValidate.value = true;
  }

  async function handleLogin() {
    const data = await validForm();
    if (!data) return;
    if (!isDragValidate.value) {
      notification.info({
        message: t('sys.login.loginDragValidate'),
        description: `${t('sys.login.loginAgain')}`,
        duration: 3,
      });
      return;
    }
    try {
      loading.value = true;
      const userInfo = await userStore.login({
        password: data.password,
        // userName: data.account,
        phoneNum: data.account,
        randomcode: data.captcha,
        rememberMe: false,
        mode: 'none', //不要默认的错误提示
      });

      if (userInfo) {
        notification.success({
          message: t('sys.login.loginSuccessTitle'),
          description: `${t('sys.login.loginSuccessDesc')}: ${userInfo.userName}`,
          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;
    }
  }
</script>
<style>
  .captcha {
    min-width: 80px !important;
  }
  .captcha-img {
    height: 40px;
    display: inline-block;
  }
</style>
