<template>
  <div class="xl:w-screen-xl m-auto py-4 my-20">
    <el-page-header :icon="ArrowLeft" @back="goBack">
      <template #content>
        <span class="text-large font-600 mr-3"> 用户中心 </span>
      </template>
      <div class="login-layout mt-10 sm:w-screen-xl w-full">
        <div class="m-auto flex sm:flex-row flex-col sm:px-0 px-3">
          <!-- 用户菜单 -->
          <UserMenu :activeIndex="activeIndex" @done="onDone" class="sm:flex hidden"/>
          <div class="flash bg-white rounded-lg w-full">
            <div class="title text-xl text-gray-700 md:px-8 p-4 md:mt-3 font-500">账号信息</div>
            <div class="sm:w-screen-md w-full sm:px-4 sm:py-2">
              <Base :form="form"/>
            </div>
          </div>
        </div>
      </div>
    </el-page-header>
  </div>
</template>
<script setup lang="ts">
import { ArrowLeft,View,Search } from '@element-plus/icons-vue'
import {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 Base from './components/Base.vue';

// 配置信息
const runtimeConfig = useRuntimeConfig();
const route = useRoute();
const router = useRouter();
const website = useWebsite()
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,
  tenantId: undefined,
  tenantName: undefined
});

useHead({
  title: `用户中心`,
  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 token = localStorage.getItem('token');
  if (!token || token == '') {
      navigateTo('/passport/login');
      return false;
  }
  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);
  }
}

const goBack = () => {
  router.back(); // 返回上一页
}

watch(
    () => route.path,
    (path) => {
      activeIndex.value = path;
      console.log(path, '=>Path')
      reload();
    },
    {immediate: true}
);
</script>
