<template>
  <div id="signin" v-loading="loading">
    <div class="logo">
      <img width="65%" :src="img_logo" alt="logo" />
    </div>
    <div class="title">
      <span class="tit1">{{ t('forget.title') }}</span>
    </div>
    <el-form
      :model="params"
      :rules="rules"
      ref="formRef"
      label-width="0"
      class="form"
    >
      <el-form-item prop="email">
        <el-input
          class="m-input"
          v-model.trim="params.email"
          :placeholder="t('forget.form')"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <a
          class="rbt-btn btn-gradient hover-icon-reverse btn-sm w-100"
          href="javascript:;"
          @click="send"
        >
          <div class="icon-reverse-wrapper">
            <span class="btn-text">{{ t('forget.forget') }}</span>
            <span class="btn-icon">
              <el-icon><Right /></el-icon>
            </span>
            <span class="btn-icon">
              <el-icon><Right /></el-icon>
            </span>
          </div>
        </a>
      </el-form-item>
      <el-form-item>
        <el-button
          style="height: 40px"
          class="s-btn"
          @click="cancel"
        >{{ t('forget.cancel') }}
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, inject } from 'vue'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { ElForm, ElFormItem, ElInput, ElButton, ElIcon } from 'element-plus'
import { ArrowRight, Right } from '@element-plus/icons-vue'
import logo from '@/assets/images/cwg-logo.png'
import Service from '@/service/login'
import Config from '@/config/index'

const { t } = useI18n()
const router = useRouter()
const { Code, Pattern } = Config
const Session = inject('session')
const pigeon = inject('pigeon')

const flag = ref(false)
const loading = ref(false)
const formRef = ref(null)
const img_logo = logo

const params = reactive({
  email: '',
})

// 验证规则
const emailValidator = (rule, value, callback) => {
  if (Pattern.Email.test(value)) {
    callback()
  } else {
    callback(new Error(t('vaildate.email.format')))
  }
}

const rules = {
  email: [
    { validator: emailValidator, trigger: 'blur' },
  ],
}

// 发送
const send = async () => {
  if (!formRef.value) return

  formRef.value.validate(async (valid) => {
    if (!valid) return false

    if (flag.value) return

    flag.value = true
    const res = await Service.forgetPwd(params)

    if (res.code === Code.StatusOK) {
      loading.value = true
      pigeon.MessageOK(res.msg)


      setTimeout(() => {
        loading.value = false
        router.push({ path: '/signin' }).catch(() => {
        })
      }, 1000)
    } else {
      pigeon.MessageError(res.msg)

    }

    flag.value = false
  })
}

const cancel = () => {
  router.push({ path: '/signin' }).catch(() => {
  })
}

onMounted(() => {
})
</script>

<style lang="scss" scoped>
#signin {
  width: 100%;
  max-width: 530px;
  height: auto;
  padding-top: 35px;
  box-sizing: border-box;
  background-color: #fff;

  .title {
    margin: 32px 0;
    font-size: 24px;
    font-weight: bolder;

    i {
      margin-right: 10px;
    }

    .tit1 {
      font-size: 34px;
      line-height: 1.14;
      font-weight: bold;
      color: #000000;
    }
  }

  .form {
    padding: 20px 35px 35px;

    .forget {
      text-align: right;

      a {
        @include font_main();
      }
    }

    .words {
      text-align: left;
      line-height: 24px;

      a {
        margin-left: 1em;
        @include font_main();
      }
    }

    .s-btn {
      width: 100%;
    }

    .rbt-btn {
      width: 100%;
      padding: 0 22px;
      background: #e61f1e;
      height: 45px;
      line-height: 43px;
      color: #ffffff;
      font-size: 16px;
      letter-spacing: 0.5px;
      font-weight: 500;
      display: inline-block;
      position: relative;
      z-index: 1;
      transition: all 0.4s ease-in-out;
      border-radius: 6px;
      border: 0 none;
      outline: none;
      background-size: 300% 100%;
      background-image: linear-gradient(
          to right,
          #0e2453,
          #0d2862,
          #0e2453,
          #0d2862
      );
      box-sizing: border-box;
    }

    .rbt-btn.hover-icon-reverse .icon-reverse-wrapper {
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .rbt-btn.hover-icon-reverse .btn-text {
      display: inline-block;
      transition: transform 0.6s 0.125s cubic-bezier(0.1, 0.75, 0.25, 1);
      margin-inline-start: -23px;
    }

    .rbt-btn i {
      padding-left: 6px;
      display: inline-block;
      top: 2px;
      position: relative;
      font-size: 17px;
    }

    .rbt-btn.hover-icon-reverse .btn-icon + .btn-icon {
      margin-inline-end: 0;
      display: inline-block;
      margin-inline-start: 0;
      margin-inline-end: 0;
      opacity: 0;
      transform: translateX(-10px);
      transition-delay: 0s;
      order: -2;
    }

    .rbt-btn.hover-icon-reverse .btn-icon {
      transition: opacity 0.4s 0.25s, transform 0.6s 0.25s;
      transition-timing-function: cubic-bezier(0.1, 0.75, 0.25, 1);
    }

    .rbt-btn.btn-gradient:hover {
      background-color: inherit;
      background-position: 102% 0;
      transition: all 0.4s ease-in-out;
      background-image: linear-gradient(
          to right,
          #e61f1e,
          #fe5924,
          #fe5924,
          #e61f1e
      );
    }

    .rbt-btn:hover {
      color: #ffffff;
      transform: translate3d(0, -2px, 0);
    }

    .rbt-btn:hover.hover-icon-reverse .btn-icon + .btn-icon {
      opacity: 1;
      transform: translateX(0);
      transition-delay: 0.225s;
    }

    .rbt-btn:hover.hover-icon-reverse .btn-icon {
      opacity: 0;
      transition-delay: 0s;
      transform: translateX(10px);
    }

    .rbt-btn.hover-icon-reverse .btn-text {
      display: inline-block;
      transition: transform 0.6s 0.125s cubic-bezier(0.1, 0.75, 0.25, 1);
      margin-inline-start: -23px;
    }

    .rbt-btn:hover.hover-icon-reverse .btn-text {
      transition-delay: 0.1s;
      transform: translateX(23px);
    }
  }
}
</style>

<style lang="scss" scoped>
#signin {
  .el-input__inner {
    border: none;
    border-bottom: 1px solid;
    border-radius: 0;
    @include border_gray_1();
    color: #000;
  }

  .el-form-item {
    margin-bottom: 20px;
  }

  .el-form-item__content {
    line-height: 40px;
    //line-height: initial !important;
  }
}
</style>
