<template>
  <div class="page">
    <div class="form" :class="{ 'mobile-form': !pcShow }">
      <div class="title">修改密码</div>
      <n-form ref="formRef" label-placement="left" :label-width="100" :model="form" :rules="rules" size="large" require-mark-placement="left">
        <n-form-item label="密码：" path="newPassword">
          <n-input v-model:value="form.newPassword" @input="handlePasswordInput" type="password" show-password-on="click" placeholder="请输入密码" />
        </n-form-item>
        <n-form-item label="确认密码：" path="confirmNewPassword" ref="confirmPasswordRef">
          <n-input v-model:value="form.confirmNewPassword" type="password" show-password-on="click" placeholder="请输入确认密码" />
        </n-form-item>
      </n-form>
      <n-button class="login-btn" attr-type="button" type="info" color="#18A058" @click="handleSure">
        {{ submitLoading ? "提交中..." : "确认" }}
      </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>
.page {
  background-color: #fff;

  .title {
    text-align: center;
    margin-bottom: 32px;
    font-size: 24px;
    font-weight: bold;
    color: #1a1a1a;
  }

  .form {
    position: fixed;
    top: 50%;
    left: 50%;
    width: 550px;
    padding: 50px 70px;
    transform: translate(-50%, -50%);
    background: #ffffff;
    border-radius: 20px;
    box-shadow: 0 0 8px 8px #e1e1e1;

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

  .mobile-form {
    top: 60%;
    width: 85%;
    padding: 20px;
  }

  // .n-form {
  //   :deep(.n-form-item-label__text) {
  //     color: #1a1a1a;
  //     font-size: 16px;
  //   }
  //   .n-input {
  //     border-radius: 8px;
  //   }
  // }
}
</style>
