<script lang="ts" setup>
import { GlobalFooter } from 'antdv-pro-layout';
import { Modal, message } from 'ant-design-vue/lib';
import { computed, onMounted, reactive, toRaw } from 'vue';
import { register } from '@/api/login';
import { regExpPasswd, regExpUserName } from '@/utils/regular-utils';
import { useRouter, useRoute } from 'vue-router';
import useAppStore from '@/store/modules/app';
import useI18n from '@/hooks/useI18n';
import { RESULT_CODE_SUCCESS } from '@/constants/result-constants';
import { parseUrlPath } from '@/plugins/file-static-url';
const { t, currentLocale } = useI18n();
const router = useRouter();
const route = useRoute();
const appStore = useAppStore();

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;
    });
}

// LOGO地址
const logoUrl = computed(() => {
  let url =
    appStore.logoType === 'brand'
      ? parseUrlPath(appStore.filePathBrand)
      : parseUrlPath(appStore.filePathIcon);

  if (url.indexOf('{language}') === -1) {
    return url;
  }
  // 语言参数替换
  const local = currentLocale.value;
  const lang = local.split('_')[0];
  return url.replace('{language}', lang);
});

/**
 * 国际化翻译转换
 */
function fnLocale() {
  let title = route.meta.title as string;
  if (title.indexOf('router.') !== -1) {
    title = t(title);
  }
  appStore.setTitle(title);
}

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

<template>
  <div class="container">
    <div class="top">
      <div class="header">
        <a href="/" target="_self">
          <template v-if="appStore.logoType === 'icon'">
            <img :src="logoUrl" class="logo" alt="logo" />
            <span class="title">{{ appStore.appName }}</span>
          </template>
          <template v-if="appStore.logoType === 'brand'">
            <img :src="logoUrl" class="logo" alt="logo" />
          </template>
        </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>

    <GlobalFooter
      class="footer"
      :links="false"
      :copyright="appStore.copyright"
    />
  </div>
</template>

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

.top {
  text-align: center;

  a {
    text-decoration: none;
  }

  .header {
    height: 48px;
    line-height: 48px;
    .logo {
      height: 48px;
      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: 14px;
    margin-bottom: 28px;
    color: #666;
    font-size: 18px;
  }
}

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

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

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