<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" @done="onDone" />
      </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.mobile" />
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="form.nickname" />
          </el-form-item>
          <el-form-item label="邮箱账号">
            <el-input v-model="form.email" placeholder="邮箱账号" />
          </el-form-item>
          <el-form-item label="性别">
            <el-radio-group v-model="form.sex">
              <el-radio value="1">男</el-radio>
              <el-radio value="2">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="个人签名">
            <el-input v-model="form.comments" type="textarea" placeholder="个人签名" :rows="4" />
          </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";

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


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

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

const onDone = (index: string) => {
  activeIndex.value = index;
}

const onSubmit = async () => {
  const {data: modify } = await useServerRequest<ApiResult<User>>('/auth/user',{
    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;
      console.log(path,'=>Path')
      reload();
    },
    { immediate: true }
);
</script>
<style lang="less">
body{
  background: url("https://oss.wsdns.cn/20240328/797a8e323bba4fc6827abf9d8b98ba45.png");
  background-size: 100%;
}
</style>
