<template>
  <div class="mobile-profile">
    <!-- 头像区域 -->
    <div class="mobile-profile__avatar-section">
      <el-avatar :size="72" :src="userInfo.avatar || defaultAvatar" class="mobile-profile__avatar" />
      <div class="mobile-profile__name">{{ userInfo.nickname || '用户' }}</div>
      <div class="mobile-profile__username">{{ userInfo.username || '' }}</div>
    </div>

    <!-- 基本信息 -->
    <div class="mobile-profile__section">
      <div class="mobile-profile__section-title">基本信息</div>
      <div class="mobile-profile__card">
        <div class="mobile-profile__item">
          <span class="mobile-profile__label">昵称</span>
          <span class="mobile-profile__value">{{ userInfo.nickname || '-' }}</span>
        </div>
        <div class="mobile-profile__item">
          <span class="mobile-profile__label">手机号</span>
          <span class="mobile-profile__value">{{ userInfo.mobile || '-' }}</span>
        </div>
        <div class="mobile-profile__item">
          <span class="mobile-profile__label">邮箱</span>
          <span class="mobile-profile__value">{{ userInfo.email || '-' }}</span>
        </div>
        <div class="mobile-profile__item">
          <span class="mobile-profile__label">性别</span>
          <span class="mobile-profile__value">{{ getSexText(userInfo.sex) }}</span>
        </div>
      </div>
    </div>

    <!-- 组织信息 -->
    <div class="mobile-profile__section">
      <div class="mobile-profile__section-title">组织信息</div>
      <div class="mobile-profile__card">
        <div class="mobile-profile__item">
          <span class="mobile-profile__label">部门</span>
          <span class="mobile-profile__value">{{ userInfo.dept?.name || '-' }}</span>
        </div>
        <div class="mobile-profile__item">
          <span class="mobile-profile__label">岗位</span>
          <span class="mobile-profile__value">{{ userInfo.posts?.map(p => p.name).join('、') || '-' }}</span>
        </div>
        <div class="mobile-profile__item">
          <span class="mobile-profile__label">角色</span>
          <span class="mobile-profile__value">{{ userInfo.roles?.map(r => r.name).join('、') || '-' }}</span>
        </div>
      </div>
    </div>

    <!-- 账号信息 -->
    <div class="mobile-profile__section">
      <div class="mobile-profile__section-title">账号信息</div>
      <div class="mobile-profile__card">
        <div class="mobile-profile__item">
          <span class="mobile-profile__label">用户名</span>
          <span class="mobile-profile__value">{{ userInfo.username || '-' }}</span>
        </div>
        <div class="mobile-profile__item">
          <span class="mobile-profile__label">最近登录IP</span>
          <span class="mobile-profile__value">{{ userInfo.loginIp || '-' }}</span>
        </div>
        <div class="mobile-profile__item">
          <span class="mobile-profile__label">最近登录时间</span>
          <span class="mobile-profile__value">{{ formatDate2(userInfo.loginDate) }}</span>
        </div>
        <div class="mobile-profile__item">
          <span class="mobile-profile__label">注册时间</span>
          <span class="mobile-profile__value">{{ formatDate2(userInfo.createTime) }}</span>
        </div>
      </div>
    </div>

    <!-- 编辑按钮 -->
    <div class="mobile-profile__actions">
      <el-button type="primary" round class="mobile-profile__edit-btn" @click="editDialogVisible = true">
        编辑个人信息
      </el-button>
    </div>

    <!-- 编辑对话框 -->
    <el-dialog v-model="editDialogVisible" title="编辑个人信息" width="90%" center>
      <el-form ref="editFormRef" :model="editForm" :rules="editRules" label-position="top">
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="editForm.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="editForm.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="editForm.sex">
            <el-radio :value="1">男</el-radio>
            <el-radio :value="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitEdit">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
import { ArrowLeft } from '@element-plus/icons-vue'
import { formatDate } from '@/utils/formatTime'
import {
  getUserProfile,
  updateUserProfile,
  ProfileVO,
  UserProfileUpdateReqVO
} from '@/api/system/user/profile'
import { useUserStore } from '@/store/modules/user'

defineOptions({ name: 'ProfileMobile' })

const router = useRouter()
const message = useMessage()
const userStore = useUserStore()

const defaultAvatar = 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'
const userInfo = ref<Partial<ProfileVO>>({})

const editDialogVisible = ref(false)
const editFormRef = ref()
const editForm = reactive<UserProfileUpdateReqVO>({
  nickname: '',
  mobile: '',
  email: '',
  sex: 1
})

const editRules = {
  nickname: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
  ],
  mobile: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ]
}

const formatDate2 = (date: any) => {
  if (!date) return '-'
  return formatDate(new Date(date), 'YYYY-MM-DD HH:mm')
}

const getSexText = (sex?: number) => {
  if (sex === 1) return '男'
  if (sex === 2) return '女'
  return '未设置'
}

const goBack = () => {
  router.back()
}

/** 获取用户信息 */
const loadUserInfo = async () => {
  try {
    userInfo.value = await getUserProfile()
  } catch (e) {
    console.error('获取用户信息失败', e)
  }
}

/** 打开编辑时填充表单 */
watch(editDialogVisible, (val) => {
  if (val && userInfo.value) {
    editForm.nickname = userInfo.value.nickname || ''
    editForm.mobile = userInfo.value.mobile || ''
    editForm.email = userInfo.value.email || ''
    editForm.sex = userInfo.value.sex || 1
  }
})

/** 提交编辑 */
const submitEdit = async () => {
  const valid = await editFormRef.value?.validate()
  if (!valid) return
  try {
    await updateUserProfile(editForm)
    message.success('修改成功')
    editDialogVisible.value = false
    await loadUserInfo()
    userStore.setUserNicknameAction(editForm.nickname || '')
  } catch (e) {
    console.error('修改个人信息失败', e)
  }
}

onMounted(() => {
  loadUserInfo()
})
</script>

<style lang="scss" scoped>
.mobile-profile {
  min-height: 100vh;
  background: #f5f7fa;
  padding-bottom: 30px;
}

.mobile-profile__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: #fff;
  border-bottom: 1px solid #eee;
  position: sticky;
  top: 0;
  z-index: 10;
}

.mobile-profile__back {
  font-size: 20px;
  color: #333;
  cursor: pointer;
  width: 32px;
}

.mobile-profile__title {
  font-size: 17px;
  font-weight: 600;
  color: #303133;
}

.mobile-profile__placeholder {
  width: 32px;
}

.mobile-profile__avatar-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px 20px 20px;
  background: linear-gradient(180deg, #fff 0%, #f5f7fa 100%);
}

.mobile-profile__avatar {
  border: 3px solid #409eff;
}

.mobile-profile__name {
  margin-top: 10px;
  font-size: 18px;
  font-weight: 600;
  color: #303133;
}

.mobile-profile__username {
  margin-top: 4px;
  font-size: 13px;
  color: #909399;
}

.mobile-profile__section {
  margin: 12px 16px 0;
}

.mobile-profile__section-title {
  font-size: 13px;
  font-weight: 500;
  color: #909399;
  margin-bottom: 8px;
  padding-left: 4px;
}

.mobile-profile__card {
  background: #fff;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.mobile-profile__item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;

  &:not(:last-child) {
    border-bottom: 1px solid #f0f0f0;
  }
}

.mobile-profile__label {
  font-size: 14px;
  color: #606266;
  flex-shrink: 0;
}

.mobile-profile__value {
  font-size: 14px;
  color: #303133;
  text-align: right;
  word-break: break-all;
  margin-left: 16px;
}

.mobile-profile__actions {
  margin: 24px 16px 0;
}

.mobile-profile__edit-btn {
  width: 100%;
  height: 44px;
  font-size: 16px;
}
</style>
