<template>
  <div v-if="visible" class="dialog-overlay" @click.self="handleCancel">
    <div class="dialog-content">
      <div class="dialog-header">
        <h2 class="dialog-title">编辑任务</h2>
        <button @click="handleCancel" class="btn-close">×</button>
      </div>
      <div class="dialog-body">
        <div class="form-group">
          <label class="form-label">任务标题</label>
          <input
            v-model="taskTitle"
            type="text"
            placeholder="输入任务标题..."
            class="form-input"
            @keyup.enter="handleSave"
          />
        </div>
        <div class="form-group">
          <label class="form-label">优先级</label>
          <select v-model="taskPriority" class="form-select">
            <option value="" disabled>请选择优先级</option>
            <option value="Low">低</option>
            <option value="Medium">中</option>
            <option value="High">高</option>
          </select>
        </div>
      </div>
      <div class="dialog-footer">
        <button @click="handleCancel" class="btn-cancel">取消</button>
        <button @click="handleSave" class="btn-save">保存</button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import { taskApi } from '../api/tasks';
import type { Task } from '../types/task';

interface Props {
  visible: boolean;
  task: Task | null;
}

const props = defineProps<Props>();

const emit = defineEmits<{
  (e: 'update:visible', value: boolean): void;
  (e: 'saved', task: Task): void;
}>();

const taskTitle = ref('');
const taskPriority = ref<'Low' | 'Medium' | 'High'>('Medium');

watch(() => props.task, (newTask) => {
  if (newTask) {
    taskTitle.value = newTask.title;
    taskPriority.value = newTask.priority;
  }
}, { immediate: true });

const handleSave = async () => {
  if (!props.task || !taskTitle.value.trim()) {
    alert('请输入任务标题');
    return;
  }

  try {
    const response = await taskApi.updateTask(props.task.id, {
      title: taskTitle.value.trim(),
      priority: taskPriority.value,
    });

    if (response.success && response.data) {
      emit('saved', response.data);
      emit('update:visible', false);
    }
  } catch (error) {
    console.error('Failed to update task:', error);
    alert('更新任务失败');
  }
};

const handleCancel = () => {
  emit('update:visible', false);
};
</script>

<style scoped>
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
}

.dialog-content {
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
  width: 90%;
  max-width: 500px;
  animation: dialogSlideIn 0.3s ease;
}

@keyframes dialogSlideIn {
  from {
    opacity: 0;
    transform: translateY(-20px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid #e5e7eb;
}

.dialog-title {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  color: #1e293b;
}

.btn-close {
  width: 32px;
  height: 32px;
  border: none;
  background: #f1f5f9;
  color: #64748b;
  border-radius: 8px;
  cursor: pointer;
  font-size: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  line-height: 1;
}

.btn-close:hover {
  background: #e2e8f0;
  color: #334155;
}

.dialog-body {
  padding: 24px;
}

.form-group {
  margin-bottom: 20px;
}

.form-group:last-child {
  margin-bottom: 0;
}

.form-label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 600;
  color: #374151;
}

.form-input {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e5e7eb;
  border-radius: 10px;
  font-size: 15px;
  transition: all 0.3s ease;
  background: #f8fafc;
  color: #1e293b;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #6366f1;
  background: white;
  box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1);
}

.form-select {
  width: 100%;
  padding: 12px 40px 12px 16px;
  border: 2px solid #e5e7eb;
  border-radius: 10px;
  font-size: 15px;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #f8fafc;
  color: #64748b;
  box-sizing: border-box;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%2364748b'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 20px;
}

.form-select:hover {
  border-color: #94a3b8;
  background-color: #f1f5f9;
}

.form-select:focus {
  outline: none;
  border-color: #6366f1;
  background: white;
  color: #1e293b;
  box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1);
}

.form-select option {
  padding: 10px;
  background: white;
  color: #1e293b;
}

.form-select option[value="Low"] {
  color: #22c55e;
}

.form-select option[value="Medium"] {
  color: #f59e0b;
}

.form-select option[value="High"] {
  color: #ef4444;
}

.dialog-footer {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  padding: 20px 24px;
  border-top: 1px solid #e5e7eb;
}

.btn-cancel {
  padding: 10px 24px;
  background: #f1f5f9;
  color: #475569;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: all 0.3s ease;
}

.btn-cancel:hover {
  background: #e2e8f0;
  color: #334155;
}

.btn-save {
  padding: 10px 24px;
  background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
  color: white;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px -2px rgba(99, 102, 241, 0.4);
}

.btn-save:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px -2px rgba(99, 102, 241, 0.5);
}

.btn-save:active {
  transform: translateY(0);
}

@media (max-width: 768px) {
  .dialog-content {
    width: 95%;
    max-width: none;
    border-radius: 14px;
  }

  .dialog-header {
    padding: 16px 20px;
  }

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

  .dialog-body {
    padding: 20px;
  }

  .dialog-footer {
    padding: 16px 20px;
  }

  .btn-cancel,
  .btn-save {
    padding: 10px 20px;
    font-size: 14px;
  }
}
</style>
