<script setup lang="ts">
import { useI18n } from "vue-i18n";
import { ref, reactive } from "vue";
import Motion from "../utils/motion";
import { message } from "@/utils/message";
import { updateRules } from "../utils/rule";
import type { FormInstance, FormItemProp } from "element-plus";
import { useVerifyCode } from "../utils/verifyCode";
import { $t, transformI18n } from "@/plugins/i18n";
import { clone } from "@pureadmin/utils";
import { useUserStoreHook } from "@/store/modules/user";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { postGetCodeAPI, postResetPasswordAPI } from "@/api/login";
import Lock from "@iconify-icons/ri/lock-fill";
import UserName from "@iconify-icons/ri/user-4-line";
import Mail from "@iconify-icons/ri/mail-open-line";

const { t } = useI18n();
const loading = ref(false);
const timer = ref(null);
const ruleForm = reactive({
  username: "",
  email: "",
  code: "",
  password: "",
  repeatPassword: ""
});
const ruleFormRef = ref<FormInstance>();
const { isDisabled, text } = useVerifyCode();
const repeatPasswordRule = [
  {
    validator: (rule, value, callback) => {
      if (value === "") {
        callback(new Error(transformI18n($t("login:PassWordSureReg"))));
      } else if (ruleForm.password !== value) {
        callback(new Error(transformI18n($t("login:PassWordDifferentReg"))));
      } else {
        callback();
      }
    },
    trigger: "blur"
  }
];

const onUpdate = async (formEl: FormInstance | undefined) => {
  loading.value = true;
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const res = await postResetPasswordAPI({
        username: ruleForm.username,
        password: ruleForm.password,
        code: ruleForm.code,
        mail: ruleForm.email
      });
      if (res.success) {
        message(res.msg, {
          type: "success"
        });
        loading.value = false;
        useUserStoreHook().SET_CURRENTPAGE(0);
      } else {
        message(res.msg, {
          type: "error"
        });
        loading.value = false;
      }
    } else {
      loading.value = false;
    }
  });
};
const start = async (
  formEl: FormInstance | undefined,
  props: FormItemProp,
  time = 120
) => {
  if (!formEl) return;
  const initTime = clone(time, true);
  await formEl.validateField(props, async isValid => {
    if (isValid) {
      const res = await postGetCodeAPI({
        username: ruleForm.username,
        title: "重置",
        mail: ruleForm.email
      });
      if (res.code === 200) {
        clearInterval(timer.value);
        isDisabled.value = true;
        text.value = `${time}`;
        timer.value = setInterval(() => {
          if (time > 0) {
            time -= 1;
            text.value = `${time}`;
          } else {
            text.value = "";
            isDisabled.value = false;
            clearInterval(timer.value);
            time = initTime;
          }
        }, 1000);
      } else {
        message(res.msg, { type: "error" });
      }
    }
  });
};
function onBack() {
  useUserStoreHook().SET_CURRENTPAGE(0);
}
</script>

<template>
  <el-form
    ref="ruleFormRef"
    :model="ruleForm"
    :rules="updateRules"
    size="large"
  >
    <Motion>
      <el-form-item
        :rules="[
          {
            required: true,
            message: transformI18n($t('login:UsernameReg')),
            trigger: 'blur'
          }
        ]"
        prop="username"
      >
        <el-input
          v-model="ruleForm.username"
          clearable
          :placeholder="t('login:Username')"
          :prefix-icon="
            useRenderIcon(UserName, {
              color: '#4380FF',
              width: 32,
              height: 32
            })
          "
        />
      </el-form-item>
    </Motion>
    <Motion :delay="100">
      <el-form-item prop="email">
        <el-input
          v-model="ruleForm.email"
          clearable
          :placeholder="t('login:Email')"
          :prefix-icon="
            useRenderIcon(Mail, {
              color: '#4380FF',
              width: 32,
              height: 32
            })
          "
        />
      </el-form-item>
    </Motion>
    <Motion :delay="100">
      <el-form-item prop="code">
        <div class="w-full flex justify-between">
          <el-input
            v-model="ruleForm.code"
            clearable
            :placeholder="t('login:EmailVerifyCode')"
            :prefix-icon="
              useRenderIcon('ri:shield-keyhole-line', {
                color: '#4380FF',
                width: 32,
                height: 32
              })
            "
          />
          <el-button
            :disabled="isDisabled"
            class="ml-2"
            @click="start(ruleFormRef, 'email')"
          >
            {{
              text.length > 0
                ? text + t("login:Info")
                : t("login:GetVerifyCode")
            }}
          </el-button>
        </div>
      </el-form-item>
    </Motion>

    <Motion :delay="150">
      <el-form-item prop="password">
        <el-input
          v-model="ruleForm.password"
          clearable
          show-password
          :placeholder="t('login:Password')"
          :prefix-icon="
            useRenderIcon(Lock, {
              color: '#4380FF',
              width: 32,
              height: 32
            })
          "
        />
      </el-form-item>
    </Motion>

    <Motion :delay="200">
      <el-form-item :rules="repeatPasswordRule" prop="repeatPassword">
        <el-input
          v-model="ruleForm.repeatPassword"
          clearable
          show-password
          :placeholder="t('login:Sure')"
          :prefix-icon="
            useRenderIcon(Lock, {
              color: '#4380FF',
              width: 32,
              height: 32
            })
          "
        />
      </el-form-item>
    </Motion>

    <Motion :delay="250">
      <el-form-item>
        <div class="w-full flex justify-center items-center">
          <el-button
            class="w-[75%]"
            size="default"
            type="primary"
            round
            :loading="loading"
            @click="onUpdate(ruleFormRef)"
          >
            {{ t("login:Definite") }}
          </el-button>
        </div>
      </el-form-item>
    </Motion>

    <Motion :delay="300">
      <el-form-item>
        <div class="w-full flex justify-center items-center">
          <el-button class="w-[75%]" size="default" round @click="onBack">
            {{ t("login:Back") }}
          </el-button>
        </div>
      </el-form-item>
    </Motion>
  </el-form>
</template>
