<template>
  <div class="container">
    <div class="title">
      Forget the password
      <div></div>
      <span @click="handleSure()" class="iconfont icon-shanchu1"></span>
    </div>
    <template v-if="!emailSent">
      <n-form ref="formRef" label-placement="left" label-align="left" :model="form" :rules="rules" size="large" require-mark-placement="left">
        <n-form-item :label="'Email address：'" path="email">
          <n-input v-model:value="form.email" placeholder="Please enter your email address" />
        </n-form-item>
      </n-form>
      <n-button class="login-btn" attr-type="button" type="info" color="#18A058" :disabled="submitLoading" @click="handleSend">
        {{ submitLoading ? t('common.login.sending') : t('common.login.clickToSend') }}
      </n-button>
    </template>

    <template v-else>
      <p style="text-align: center">
        <n-icon size="30" color="#00C849" :component="MdCheckmarkCircle" />
      </p>
      <p class="tip">
        {{t('common.login.emailSendSuccess')}}
      </p>
      <n-button class="login-btn" attr-type="button" type="info" color="#18A058" @click="handleSure">
        Confirm
      </n-button>
    </template>
  </div>
</template>

<script lang="ts" setup>
import { useI18n } from "#imports";
import { createDiscreteApi } from "naive-ui";
import { MdCheckmarkCircle } from "@vicons/ionicons4";
import { ref, reactive } from "vue";
const { t, locale, setLocale } = useI18n();
const emit = defineEmits(["closeSginDialog"]);

const message = createDiscreteApi(["message"]);

const formRef = ref();
const submitLoading = ref(false);
const form = reactive<object>({
  email: "",
});

const rules: FormRules = {
  email: [
    {
      required: true,
      message: "Please enter your email address.",
      trigger: ["input", "blur"],
    },
    {
      required: true,
      validator: (rule: FormItemRule, value: string): boolean => {
        return value && isEmail(value);
      },
      message: "Please enter a properly formatted email address",
      trigger: ["blur"],
    },
  ],
};

const emailSent = ref<boolean>(false);

const handleSend = () => {
  formRef.value?.validate(async (errors: any) => {
    if (!errors) {
      submitLoading.value = true;
      const { code } = await email_Api(form);
      if (code === 200) {
        message.message.success("send successfully");
        emailSent.value = true;
      }
      submitLoading.value = false;
    }
  });
};

const handleSure = () => {
  emit("closeSginDialog", "success");
};
</script>

<style lang="scss" scoped>
@import "~/assets/css/tool.scss";
.container {
  padding: 30px 58px 80px;
  background: #ffffff;
  border-radius: 20px;
  position: relative;
  .title {
    font-size: 38px;
    font-family: Arial, Arial-Bold;
    font-weight: 700;
    text-align: center;
    color: #1a1a1a;
    > div {
      width: 71px;
      height: 6px;
      background: linear-gradient(90deg, #719d59 2%, #479f82 98%);
      border-radius: 3px;
      margin: auto;
    }
    > span {
      position: absolute;
      right: 20px;
      top: 15px;
      cursor: pointer;
    }
  }

  .login-btn {
    width: 100%;
    height: 58px;
    background: linear-gradient(90deg, #60ab91, #84a86c);
    border-radius: 8px;
    font-size: 18px;
    margin-top: 10px;

    :deep(.n-button__content) {
      display: block;
    }
  }

  .tip {
    font-size: 16px;
    line-height: 32px;
  }
}
.n-form {
  margin-top: 30px;
}
:deep(.n-form-item-blank) {
  border-bottom: 1px solid #e6e6e6;
}
:deep(.n-form-item-label) {
  color: #666666;
  font-size: var(--size-14);
  font-family: Microsoft YaHei, Microsoft YaHei-Regular;
  line-height: var(--size-20);
  padding: 10px 10px 10px 0;
  border-bottom: 1px solid #e6e6e6;
}
:deep(.n-form-item-label__text) {
  height: 30px;
}
:deep(.n-input-wrapper) {
  font-size: var(--size-14);
  font-family: Microsoft YaHei, Microsoft YaHei-Regular;
  line-height: var(--size-20);
  padding: 0px 0 10px;
}
:deep(.n-form-item-blank--error .n-input) {
  --n-border-error: var(--size-1) solid transparent !important;
  --n-border-focus-error: var(--size-1) solid transparent !important;
  --n-border-hover-error: var(--size-1) solid transparent !important;
  --n-box-shadow-focus-error: var(--size-1) solid transparent !important;
}
:deep(input::-ms-reveal) {
  display: none;
}
@include responseTo("phone") {
  .container {
    padding: 10px 20px 30px;
    .title {
      font-size: 20px;
      > div {
        width: 30px;
        height: 4px;
        border-radius: 3px;
      }
    }
    .n-form {
      margin-top: 15px;
      .icon {
        height: 26px;
        padding-right: 10px;
        margin-right: 10px;
        border-right: 1px solid #cccccc;
      }
      .icon1 {
        height: 26px;
        padding-right: 14px;
        margin-right: 10px;
        border-right: 1px solid #cccccc;
      }
      .icon2 {
        height: 26px;
        padding-right: 13px;
        margin-right: 10px;
        border-right: 1px solid #cccccc;
      }
    }
    .login-btn {
      height: 45px;
      margin-top: 0;
    }
  }
}
</style>
