<template>
  <div class="login-layout mt-[100px] m-auto sm:w-screen-xl w-full">
    <div class="mt-[100px] m-auto flex sm:flex-row flex-col sm:p-0 p-3">
      <el-card shadow="hover" class="sm:w-[250px] sm:flex sm:mb-0 mb-5 justify-center w-full">
        <div class="flex justify-center pb-4 flex-col justify-center items-center">
          <el-avatar :src="userInfo?.avatar" :size="70" />
          <text class="text-gray-400 py-1 text-sm">更换头像</text>
        </div>
        <!-- 用户菜单 -->
        <UserMenu :activeIndex="activeIndex" />
      </el-card>
      <el-card shadow="hover" class="flash bg-white hover:shadow w-full sm:ml-6 ml-0">
        <template #header>修改密码</template>
        <el-form :model="form" label-width="auto" size="large" label-position="top" class="sm:w-screen-md w-full sm:px-6 sm:py-2">
<!--          <el-form-item label="手机号码">-->
<!--            <el-input disabled v-model="form.phone" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="短信验证码">-->
<!--            <el-space class="flex w-full">-->
<!--              <el-input size="large" placeholder="短信验证码" class="w-full" v-model="form.code" />-->
<!--              <el-button size="large" class="w-full" :disabled="!!countdownTime" @click="sendCode">-->
<!--                <span v-if="!countdownTime">发送验证码</span>-->
<!--                <span v-else>已发送 {{ countdownTime }} s</span>-->
<!--              </el-button>-->
<!--            </el-space>-->
<!--          </el-form-item>-->
          <el-form-item label="旧密码">
            <el-input v-model="form.oldPassword" placeholder="请输入旧密码" />
          </el-form-item>
          <el-form-item label="新密码">
            <el-input v-model="form.password" type="password" placeholder="请输入新密码" />
          </el-form-item>
          <el-form-item label="确认密码">
            <el-input v-model="form.password2" type="password" placeholder="请确认新密码" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="large" @click="onSubmit">保存</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<script setup lang="ts">
import {useConfigInfo, useToken, useWebsite} from "~/composables/configState";
import useFormData from '@/utils/use-form-data';
import type { User } from '@/api/system/user/model';
import { ref } from 'vue'
import {useServerRequest} from "~/composables/useServerRequest";
import type {ApiResult} from "~/api";
import UserMenu from "~/components/UserMenu.vue";
import type {CaptchaResult} from "~/api/passport/login/model";

// 配置信息
const runtimeConfig = useRuntimeConfig();
const route = useRoute();
const website = useWebsite()
const config = useConfigInfo();
const token = useToken();
const userInfo = ref<User>();
const activeIndex = ref('');

// 验证码 base64 数据
const captcha = ref('');
// 验证码内容, 实际项目去掉
const text = ref('');
// 图形验证码
const imgCode = ref('');
// 发送验证码按钮loading
const codeLoading = ref(false);
// 验证码倒计时时间
const countdownTime = ref(0);
// 验证码倒计时定时器
let countdownTimer: number | null = null;

// 配置信息
const { form, assignFields } = useFormData<User>({
  userId: undefined,
  nickname: '',
  username: '',
  phone: '',
  mobile: '',
  sex: '',
  sexName: '',
  email: '',
  oldPassword: '',
  password: '',
  password2: '',
  code: '',
  smsCode: '',
  comments: '',
  remember: true,
  isAdmin: true
});

useHead({
  title: `用户中心 - ${config.value?.siteName}`,
  meta: [{ name: website.value.keywords, content: website.value.comments }]
});


/* 发送短信验证码 */
const sendCode = async () => {
  if (!form.phone) {
    ElMessage.error('请输入手机号码');
    return;
  }
  imgCode.value = text.value;
  codeLoading.value = true;

  const {data: smsCode } = await useServerRequest<ApiResult<CaptchaResult>>('/sendSmsCaptcha',{baseURL: runtimeConfig.public.apiServer,method: "post",body: {
      phone: form.phone
    }});
  if(smsCode.value){
    codeLoading.value = false;
    countdownTime.value = 30;
    // 开始对按钮进行倒计时
    countdownTimer = window.setInterval(() => {
      if (countdownTime.value <= 1) {
        countdownTimer && clearInterval(countdownTimer);
        countdownTimer = null;
      }
      countdownTime.value--;
    }, 1000);
  }
};

const onSubmit = async () => {
  const {data: modify } = await useServerRequest<ApiResult<User>>('/auth/password',{
    baseURL: runtimeConfig.public.apiServer,
    method: 'put',
    body: form
  })
  if(modify.value?.code == 0){
    ElMessage.success('修改成功')
  }
}

const reload = async () => {
  const {data: response} = await useServerRequest<ApiResult<User>>('/auth/user',{baseURL: runtimeConfig.public.apiServer})
  if(response.value?.data){
    userInfo.value = response.value?.data;
    assignFields(response.value?.data);
  }
}

watch(
    () => route.path,
    (path) => {
      activeIndex.value = path;
      reload();
    },
    { immediate: true }
);
</script>
<style lang="less">
body{
  background: url("https://oss.wsdns.cn/20240328/797a8e323bba4fc6827abf9d8b98ba45.png");
  background-size: 100%;
}
</style>
