<template>
  <div class="xl:w-screen-xl m-auto py-4 mt-12 px-4 sm:px-0 sm:mt-2">
    <el-page-header :icon="ArrowLeft" @back="goBack">
      <template #content>
        <span class="text-large font-600"> 修改资料 </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">
          <div class="flash bg-white rounded-lg w-full">
            <div class="lg:w-screen-lg w-full sm:px-4 sm:py-4 mb-10 mt-4">
              <el-form :model="form" label-width="auto" size="large" label-position="top">
                <el-row :gutter="24">
                  <el-col :span="8" :xs="24">
                    <el-form-item label="上传头像" class="px-4">
                      <el-upload
                          v-model:file-list="avatar"
                          action="https://server.gxwebsoft.com/api/oss/upload"
                          :headers="{
                          Authorization: token,
                          TenantId: 5,
                        }"
                          :limit="1"
                          list-type="picture-card"
                          :on-preview="handlePictureCardPreview"
                          :on-remove="avatarRemove"
                          :on-success="avatarSuccess"
                      >
                        <el-icon>
                          <Plus/>
                        </el-icon>
                      </el-upload>
                    </el-form-item>
                  </el-col>
                  <el-col :span="16" :xs="24">
                    <el-form-item label="手机号码" class="px-4">
                      <el-input disabled v-model="form.mobile"/>
                    </el-form-item>
                    <el-form-item label="昵称" class="px-4">
                      <el-input v-model="form.nickname"/>
                    </el-form-item>
                    <el-form-item label="邮箱账号" class="px-4">
                      <el-input v-model="form.email" placeholder="邮箱账号"/>
                    </el-form-item>
                    <el-form-item label="性别" class="px-4">
                      <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="个人签名" class="px-4">
                      <el-input v-model="form.introduction" type="textarea" placeholder="个人签名" :rows="4"/>
                    </el-form-item>
                    <el-form-item class="px-4">
                      <el-button type="primary" class="sm:w-auto w-full" size="large" @click="onSubmit">保存</el-button>
                    </el-form-item>
                  </el-col>
                </el-row>
              </el-form>
            </div>
          </div>
        </div>
      </div>
    </el-page-header>
  </div>
</template>
<script setup lang="ts">
import {ArrowLeft, View, Search, Plus} from '@element-plus/icons-vue'
import type {UploadProps, UploadUserFile} from 'element-plus'
import useFormData from '@/utils/use-form-data';
import type {User} from '@/api/system/user/model';
import {ref} from 'vue'
import {updateUser} from "~/api/layout";

// 配置信息
const token = useToken();
const route = useRoute();
const router = useRouter();
const user = useUser();
const activeIndex = ref('');
const avatar = ref<UploadUserFile[]>([])
const dialogImageUrl = ref('')
const dialogVisible = ref(false)

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

useHead({
  title: `用户中心`
});

const reload = async () => {
  // 未登录状态(是否强制登录)
  const token = localStorage.getItem('token');
  if (!token || token == '') {
    navigateTo('/passport/login');
    return false;
  }
  if (user.value) {
    form.userId = user.value.userId;
    form.nickname = user.value.nickname;
    form.realName = user.value.realName;
    form.avatar = user.value.avatar;
    form.mobile = user.value.mobile;
    form.email = user.value.email;
    form.sex = user.value.sex;
    form.comments = user.value.comments;
    form.introduction = user.value.introduction
    avatar.value = []
    if(form.avatar){
      avatar.value.push({
        uid: form.userId,
        url: form.avatar,
        name: '用户头像',
      })
    }
  }
}

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

const avatarRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
  form.avatar = '';
}

const avatarSuccess = (e: any) => {
  form.avatar = e.data.downloadUrl
}

const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url!
  dialogVisible.value = true
}

const onSubmit = () => {
  form.tenantId = Number(localStorage.getItem('ServerTenantId'));
  updateUser(form).then(() => {
    ElMessage.success('修改成功');
  });
}

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