<script setup lang="ts">
import { computed, onMounted, reactive, ref, watch } from 'vue';
import { Crypto } from '@sa/utils';
import { useAuthStore } from '@/store/modules/auth';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
import { getUUID } from '@/utils/zt';
import { localStg } from '@/utils/storage';
import { $t } from '@/locales';
defineOptions({
  name: 'PwdLogin'
});
const pd = new Crypto<{ data: string }>('zhongshuweilai');

const authStore = useAuthStore();
const { formRef, validate } = useNaiveForm();
const isRememberMe = ref(false);
const imgPath = ref('');
interface FormModel {
  userName: string;
  password: string;
  imageCode: string;
}

const model: FormModel = reactive({
  userName: '',
  password: '',
  imageCode: ''
});

onMounted(() => {
  const userName = localStg.get('userName');
  const password = localStg.get('password');
  const obj = pd.decrypt(String(password));
  if (userName && password && obj) {
    model.userName = userName;
    model.password = obj.data;
    isRememberMe.value = true;
  }
});

const rules = computed<Record<keyof FormModel, App.Global.FormRule[]>>(() => {
  // inside computed to make locale reactive, if not apply i18n, you can define it without computed
  const { formRules } = useFormRules();

  return {
    userName: formRules.userName,
    password: formRules.pwd,
    imageCode: formRules.imageCode
  };
});
const sessionUUID = ref('');
async function handleSubmit() {
  await validate();
  if (isRememberMe.value) {
    const pwd = pd.encrypt({ data: model.password });
    localStg.set('userName', model.userName);
    localStg.set('password', pwd);
  }
  await authStore.login({ ...model, sessionUUID: sessionUUID.value });
}
async function getDataCode() {
  const uuid = getUUID();
  // imgPath.value = `${import.meta.env.VITE_SERVICE_BASE_URL}/captcha.jpg?uuid=${uuid}`;
  imgPath.value = import.meta.env.DEV
    ? `${import.meta.env.VITE_SERVICE_BASE_URL}/captcha.jpg?uuid=${uuid}`
    : `https://shop.backend.zswlgz.com/plt/captcha.jpg?uuid=${uuid}`;
  sessionUUID.value = uuid;
}
getDataCode();
watch(
  () => authStore.loginLoading,
  () => {
    if (!authStore.loginLoading) {
      getDataCode();
    }
  }
);
</script>

<template>
  <NForm ref="formRef" :model="model" :rules="rules" size="large" :show-label="false" @keyup.enter="handleSubmit">
    <NFormItem path="userName">
      <NInput v-model:value="model.userName" :placeholder="$t('page.login.common.userNamePlaceholder')" />
    </NFormItem>
    <NFormItem path="password">
      <NInput
        v-model:value="model.password"
        type="password"
        show-password-on="click"
        :placeholder="$t('page.login.common.passwordPlaceholder')"
      />
    </NFormItem>
    <NFormItem path="imageCode">
      <NInput v-model:value="model.imageCode" placeholder="请输入验证码" />
      <img :src="imgPath" alt="" class="ml20px h40px w220px cursor-pointer" @click="getDataCode" />
    </NFormItem>
    <NSpace vertical :size="24">
      <div class="flex-y-center justify-between">
        <NCheckbox v-model:checked="isRememberMe">{{ $t('page.login.pwdLogin.rememberMe') }}</NCheckbox>
      </div>
      <NButton type="primary" size="large" round block :loading="authStore.loginLoading" @click="handleSubmit">
        {{ $t('common.confirm') }}
      </NButton>
    </NSpace>
  </NForm>
</template>

<style scoped></style>
