<script lang="ts" setup>
import { Modal, message } from 'ant-design-vue/lib';
import { reactive, toRaw } from 'vue';
import { register } from '@/api/login';
import { regExpPasswd, regExpUserName } from '@/utils/regular-utils';
import { useRouter } from 'vue-router';
import useI18n from '@/hooks/useI18n';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
const { t } = useI18n();
const router = useRouter();

let state = reactive({
  /**表单属性 */
  form: {
    /**账号 */
    username: '',
    /**密码 */
    password: '',
    /**确认密码 */
    confirmPassword: '',
  },
  /**表单提交点击状态 */
  formClick: false,
});

/**表单验证确认密码是否一致 */
function fnEqualToPassword(
  rule: Record<string, any>,
  value: string,
  callback: (error?: string) => void
) {
  if (!value) {
    return Promise.reject(t('views.register.passwordErr'));
  }
  if (state.form.password === value) {
    return Promise.resolve();
  }
  return Promise.reject(t('views.register.passwordConfirmErr'));
}

/**表单验证通过 */
function fnFinish() {
  state.formClick = true;
  // 发送请求
  const hide = message.loading(t('common.loading'), 0);
  register(toRaw(state.form))
    .then(res => {
      if (res.code === RESULT_CODE_SUCCESS) {
        Modal.success({
          title: t('common.tipTitle'),
          content: t('views.register.tipContent', {
            username: state.form.username,
          }),
          okText: t('views.register.tipBtn'),
          onOk() {
            router.push({ name: 'Login' });
          },
        });
      } else {
        message.error(`${res.msg}`, 3);
      }
    })
    .finally(() => {
      hide();
      state.formClick = false;
    });
}
</script>

<template>
  <div class="container">
    <div class="top">
      <div class="header">
        <a href="/" target="_self"
          ><img src="@/assets/logo.png" class="logo" alt="logo" />
          <span class="title">{{ t('common.title') }}</span>
        </a>
      </div>
      <div class="desc">{{ t('common.desc') }}</div>
    </div>

    <div class="main">
      <a-form :model="state.form" name="tabForm" @finish="fnFinish">
        <a-form-item
          name="username"
          :rules="[
            {
              required: true,
              pattern: regExpUserName,
              message: t('valid.userNameReg'),
            },
          ]"
        >
          <a-input
            v-model:value="state.form.username"
            size="large"
            :placeholder="t('valid.userNameHit')"
            :maxlength="30"
          >
            <template #prefix>
              <UserOutlined class="prefix-icon" />
            </template>
          </a-input>
        </a-form-item>
        <a-form-item
          name="password"
          :rules="[
            {
              required: true,
              pattern: regExpPasswd,
              message: t('valid.passwordReg'),
            },
          ]"
        >
          <a-input-password
            v-model:value="state.form.password"
            size="large"
            :placeholder="t('valid.passwordHit')"
            :maxlength="26"
          >
            <template #prefix>
              <LockOutlined class="prefix-icon" />
            </template>
          </a-input-password>
        </a-form-item>
        <a-form-item
          name="confirmPassword"
          :rules="[
            {
              required: true,
              min: 6,
              max: 26,
              validator: fnEqualToPassword,
            },
          ]"
        >
          <a-input-password
            v-model:value="state.form.confirmPassword"
            size="large"
            :placeholder="t('valid.passwordConfirmHit')"
            :maxlength="26"
          >
            <template #prefix>
              <LockOutlined class="prefix-icon" />
            </template>
          </a-input-password>
        </a-form-item>

        <a-button
          block
          type="primary"
          size="large"
          html-type="submit"
          :loading="state.formClick"
        >
          {{ t('views.register.registerBtn') }}
        </a-button>
        <a-button
          block
          type="default"
          size="large"
          style="margin-top: 16px"
          @click="() => router.push({ name: 'Login' })"
        >
          {{ t('views.register.loginBtn') }}
        </a-button>
      </a-form>
    </div>
  </div>
</template>

<style lang="less" scoped>
.container {
  position: relative;
  width: 100%;
  min-height: 100%;
  padding: 110px 0 144px;
  background-image: url(../assets/background.svg);
  background-repeat: no-repeat;
  background-position: center 110px;
  background-size: 100%;
}

.top {
  text-align: center;

  a {
    text-decoration: none;
  }

  .header {
    height: 44px;
    line-height: 44px;
    .logo {
      height: 44px;
      margin-right: 16px;
      vertical-align: top;
      border-style: none;
      border-radius: 6.66px;
    }
    .title {
      position: relative;
      top: 2px;
      color: rgba(0, 0, 0, 0.85);
      font-weight: 600;
      font-size: 33px;
      font-family: Avenir, Helvetica Neue, Arial, Helvetica, sans-serif;
    }
  }

  .desc {
    margin-top: 12px;
    margin-bottom: 40px;
    color: rgba(0, 0, 0, 0.45);
    font-size: 14px;
  }
}

.main {
  width: 368px;
  min-width: 260px;
  margin: 0 auto;

  .prefix-icon {
    color: #8c8c8c;
    font-size: 16px;
  }
}

.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
}
</style>
