<template>
  <div v-if="visible" class="username-dialog-overlay" @click.self="handleClose">
    <div class="username-dialog">
      <div class="username-dialog-content">
        <div class="dialog-title">修改用户名</div>
        <div class="input-group">
          <input 
            v-model="newUsername" 
            type="text" 
            placeholder="请输入新的用户名"
            :disabled="loading"
            @keyup.enter="handleSubmit"
          />
        </div>
      </div>
      <div class="username-dialog-footer">
        <button class="cancel-button" @click="handleClose" :disabled="loading">取消</button>
        <button class="confirm-button" @click="handleSubmit" :disabled="loading || !newUsername.trim()">
          {{ loading ? '修改中...' : '确定' }}
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, defineProps, defineEmits } from 'vue'
import { changeUserName } from '@/api/login.js'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  currentUsername: {
    type: String,
    default: ''
  }
})

const emit = defineEmits(['close', 'success', 'error'])

const newUsername = ref('')
const loading = ref(false)

// 监听弹窗显示状态，重置输入框
watch(() => props.visible, (visible) => {
  if (visible) {
    newUsername.value = props.currentUsername || ''
  }
})

const handleClose = () => {
  if (!loading.value) {
    newUsername.value = ''
    emit('close')
  }
}

const handleSubmit = async () => {
  if (!newUsername.value.trim() || loading.value) {
    return
  }

  if (newUsername.value.trim() === props.currentUsername) {
    emit('close')
    emit('error', '新用户名与当前用户名相同')
    return
  }

  loading.value = true
  
  try {
    await changeUserName(newUsername.value.trim())
    emit('success', newUsername.value.trim())
    newUsername.value = ''
    emit('close')
  } catch (error) {
    emit('close')
    console.error('修改用户名失败:', error)
    const errorMessage = error.response?.data?.message || error.message || '修改用户名失败，请重试'
    emit('error', errorMessage)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.username-dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.username-dialog {
  background: white;
  border-radius: 8px;
  width: 90%;
  max-width: 400px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  animation: dialog-fade-in 0.3s ease;
}

.username-dialog-content {
  padding: 20px;
  text-align: center;
}

.dialog-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  margin-bottom: 20px;
}

.input-group {
  margin-bottom: 0;
}

.input-group input {
  width: 100%;
  padding: 12px 16px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  transition: border-color 0.3s;
  outline: none;
  box-sizing: border-box;
}

.input-group input:focus {
  border-color: #67c23a;
}

.input-group input:disabled {
  background-color: #f5f5f5;
  cursor: not-allowed;
}

.username-dialog-footer {
  padding: 10px 20px 20px;
  text-align: center;
  display: flex;
  justify-content: center;
  gap: 12px;
}

.cancel-button, .confirm-button {
  padding: 8px 24px;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.cancel-button {
  background-color: #909399;
  color: white;
}

.cancel-button:hover:not(:disabled) {
  background-color: #a6a9ad;
}

.confirm-button {
  background-color: #67c23a;
  color: white;
}

.confirm-button:hover:not(:disabled) {
  background-color: #85ce61;
}

.confirm-button:disabled {
  background-color: #c0c4cc;
  cursor: not-allowed;
}

.cancel-button:disabled {
  background-color: #c0c4cc;
  cursor: not-allowed;
}

@keyframes dialog-fade-in {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 移动端适配 */
@media screen and (max-width: 480px) {
  .username-dialog {
    width: 85%;
  }

  .dialog-title {
    font-size: 16px;
  }

  .username-dialog-content {
    padding: 16px;
  }

  .input-group input {
    font-size: 14px;
  }

  .username-dialog-footer {
    flex-direction: column;
    gap: 8px;
  }

  .cancel-button, .confirm-button {
    width: 100%;
    padding: 10px;
    font-size: 13px;
  }
}
</style>