<template>
  <div class="container">
    <n-form ref="formRef" label-placement="left" :label-width="160" :model="form" :rules="rules" size="large" require-mark-placement="left">
      <n-form-item :label="t('common.register.username')+'：'" path="loginCode">
        <n-input v-model:value="form.loginCode" :placeholder="t('common.register.usernameTip')" />
      </n-form-item>
      <n-form-item :label="t('common.register.password')+'：'" path="password">
        <n-input v-model:value="form.password" @input="handlePasswordInput" type="password" show-password-on="click" :placeholder="t('common.register.passwordTip')" />
      </n-form-item>
      <n-form-item :label="t('common.register.confirmPassword')+'：'" path="confirmNewPassword" ref="confirmPasswordRef">
        <n-input v-model:value="form.confirmNewPassword" type="password" show-password-on="click" :placeholder="t('common.register.confirmPasswordTip')" />
      </n-form-item>
      <n-form-item :label="t('common.register.companyName')+'：'" path="companyName">
        <n-input v-model:value="form.companyName" :placeholder="t('common.register.companyNameTip')" />
      </n-form-item>
      <n-form-item :label="t('common.register.companyPosition')+'：'" path="addr">
        <n-input v-model:value="form.addr" :placeholder="t('common.register.positionTip')" />
      </n-form-item>
      <n-form-item :label="t('common.register.email')+'：'" path="email">
        <n-input v-model:value="form.email" :placeholder="t('common.register.emailTip')" />
      </n-form-item>
      <n-form-item :label="t('common.register.contactPerson')+'：'" path="name">
        <n-input v-model:value="form.name" :placeholder="t('common.register.contactPersonTip')" />
      </n-form-item>
      <n-form-item :label="t('common.register.phone')+'：'" path="contact">
        <n-input v-model:value="form.contact" :placeholder="t('common.register.phoneTip')" />
      </n-form-item>
      <!-- <n-form-item label="描述：" path="demand">
        <n-input
          v-model:value="form.demand"
          type="textarea"
          placeholder="请输入描述"
        />
      </n-form-item> -->
    </n-form>
    <n-button class="login-btn" attr-type="button" type="info" color="#18A058" :disabled="submitLoading" @click="handleRegister">
      {{ submitLoading ? t('common.register.submitting') : t('common.register.submit') }}
    </n-button>
  </div>
</template>

<script lang="ts" setup>
import { useI18n } from "#imports";
import { createDiscreteApi } from "naive-ui";
import { ref, reactive } from "vue";
import { useUserStore } from "@/store/user";

const emit = defineEmits(["closeSginDialog"]);
const userStore = useUserStore();
const { t, locale, setLocale } = useI18n();
const message = createDiscreteApi(["message"]);

const formRef = ref();
const submitLoading = ref(false);
const confirmPasswordRef = ref();

const form: register = reactive({
  loginCode: "",
  password: "",
  confirmNewPassword: "",
  name: "",
  contact: "",
  email: "",
  companyName: "",
  // demand: "",
  addr: "",
});

const confirmPasswordValidate = (
  rule: FormItemRule,
  value: string
): boolean => {
  return value && value == form.password;
};

const rules: FormRules = {
  loginCode: [
    {
      required: true,
      message: t("common.register.usernameTip"),
      trigger: ["input", "blur"],
    },
  ],
  password: [
    {
      required: true,
      message: t("common.register.passwordTip"),
      trigger: ["input", "blur", "password-input"],
    },
  ],
  confirmNewPassword: [
    {
      required: true,
      message: t("common.register.confirmPasswordTip"),
      trigger: ["input", "blur"],
    },
    {
      required: true,
      validator: confirmPasswordValidate,
      message: t("common.register.passwordDoNotMatch"),
      trigger: ["blur", "password-input"],
    },
  ],
  email: [
    {
      required: true,
      message: t("common.register.emailTip"),
      trigger: ["input", "blur"],
    },
    {
      required: true,
      validator: (rule: FormItemRule, value: string): boolean => {
        return value && isEmail(value);
      },
      message: t("common.register.emailTip1"),
      trigger: ["blur"],
    },
  ],
  name: [
    {
      required: true,
      message: t("common.register.contactPersonTip"),
      trigger: ["input", "blur"],
    },
  ],
  contact: [
    {
      required: true,
      message: t("common.register.phoneTip"),
      trigger: ["input", "blur"],
    },
    {
      required: true,
      validator: (rule: FormItemRule, value: string): boolean => {
        return value && isPhoneNumber(value);
      },
      message: t("common.register.phoneFormat"),
      trigger: ["blur", "change"],
    },
  ],
  companyName: [
    {
      required: true,
      message: t("common.register.companyNameTip"),
      trigger: ["input", "blur"],
    },
  ],
  // addr: [
  //   {
  //     required: true,
  //     message: "请输入公司职务",
  //     trigger: ["input", "blur"],
  //   },
  // ],
  // demand: [
  //   {
  //     required: true,
  //     message: "请输入需求",
  //     trigger: ["input", "blur"],
  //   },
  // ],
};

const handlePasswordInput = () => {
  if (form.confirmPassword) {
    confirmPasswordRef.value?.validate({ trigger: "password-input" });
  }
};

const handleRegister = () => {
  formRef.value?.validate(async (errors: any) => {
    if (!errors) {
      submitLoading.value = true;
      const params = JSON.parse(JSON.stringify(form));
      params.loginCode = encryptByBase64(params.loginCode);
      params.password = encryptByBase64(params.password);
      params.confirmNewPassword = encryptByBase64(params.confirmNewPassword);
      const { code, data } = await register_Api(params);
      if (code === 200) {
        userStore.setToken(data?.token);
        userStore.setUserInfo(data?.user);
        message.message.success(t("common.register.success"));
        emit("closeSginDialog", "success");
      }
      submitLoading.value = false;
    }
  });
};
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  padding: 30px 20px;

  .login-btn {
    display: block;
    width: 50%;
    height: 50px;
    color: #fff;
    font-size: 18px;
    border-radius: 8px;
    margin: 0 auto;

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

  :deep(input::-ms-reveal) {
    display: none;
  }
}
</style>
