<template>
  <div class="container">
    <div class="title">
      Change the password
      <div></div>
    </div>
    <div class="form" :class="{ 'mobile-form': !pcShow }">
      <n-form ref="formRef" label-placement="left" label-align="left" :model="form" :rules="rules" size="large" require-mark-placement="left">
        <n-form-item label="Password：" path="newPassword">
          <n-input v-model:value="form.newPassword" @input="handlePasswordInput" type="password" show-password-on="click" placeholder="Please enter the password" />
        </n-form-item>
        <n-form-item label="Confirmpassword：" path="confirmNewPassword" ref="confirmPasswordRef">
          <n-input v-model:value="form.confirmNewPassword" type="password" show-password-on="click" placeholder="Please confirm to enter the password" />
        </n-form-item>
      </n-form>
      <n-button class="login-btn" attr-type="button" type="info" color="#18A058" @click="handleSure">
        {{ submitLoading ? "Checking" : "Confirm" }}
      </n-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import {
  // NForm,
  // NFormItem,
  // NInput,
  // NButton,
  // FormRules,
  // FormItemRule,
  createDiscreteApi,
} from "naive-ui";
import { useRouter, useRoute } from "vue-router";
import { useUserStore } from "@/store/user";
const router = useRouter();
const userStore = useUserStore();
const pcShow = ref(true);

onMounted(() => {
  pcShow.value = !isMobile();
  handleUrl();
});

const emit = defineEmits(["closeSginDialog"]);

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

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

const form: forgot = reactive({
  uuid: "",
  newPassword: "",
  confirmNewPassword: "",
});
const submitLoading = ref(false);

const Api = ref<any>(updatePwd_Api);

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

const rules: FormRules = {
  newPassword: [
    {
      required: true,
      message: "请输入密码",
      trigger: ["input", "blur", "password-input"],
    },
  ],
  confirmNewPassword: [
    {
      required: true,
      message: "请输入密码",
      trigger: ["input", "blur"],
    },
    {
      required: true,
      validator: confirmPasswordValidate,
      message: "两次密码输入不一致",
      trigger: ["blur", "password-input"],
    },
  ],
};

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

const handleSure = () => {
  formRef.value?.validate(async (errors: any) => {
    if (!errors) {
      submitLoading.value = true;
      const params = JSON.parse(JSON.stringify(form));
      params.newPassword = encryptByBase64(params.newPassword);
      params.confirmNewPassword = encryptByBase64(params.confirmNewPassword);
      const { code, data } = await Api.value(params);
      if (code === 200) {
        userStore.setToken(data?.token);
        userStore.setUserInfo(data?.user);
        router.push("/");
      }
      submitLoading.value = false;
    }
  });
};

const handleUrl = () => {
  if (typeof window !== "undefined") {
    const queryUrl = window.location.search;
    const queryStr = queryUrl?.split("?")[1];
    const queryParams = queryStr?.split("&");
    let obj = {};
    if (queryParams && queryParams.length > 0) {
      for (let item of queryParams) {
        let arr = item.split("=");
        obj[arr[0]] = arr[1];
      }
    }
    if (obj.uuid) {
      form.uuid = obj.uuid;
      Api.value = forget_Api;
    } else {
      Api.value = updatePwd_Api;
    }
  }
};
</script>

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

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

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

  .mobile-form {
    top: 60%;
    width: 85%;
    padding: var(--size-20);
  }

  // .n-form {
  //   :deep(.n-form-item-label__text) {
  //     color: #1a1a1a;
  //     font-size: 16px;
  //   }
  //   .n-input {
  //     border-radius: 8px;
  //   }
  // }
  .n-form {
    margin-top: var(--size-30);
  }
  :deep(.n-form-item-blank) {
    border-bottom: var(--size-1) 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: var(--size-10) var(--size-10) var(--size-10) 0;
    border-bottom: var(--size-1) solid #e6e6e6;
  }
  :deep(.n-form-item-label__text) {
    height: var(--size-30);
  }
  :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;
  }
}
</style>
