<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="0">低</option>
            <option :value="1">中</option>
            <option :value="2">高</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<0 | 1 | 2>(1);

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, {
      id: 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;
  padding: 20px 16px;
  z-index: 1000;
  backdrop-filter: blur(4px);
}

.dialog-content {
  background: white;
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  width: min(100%, 420px);
  border: 1px solid rgba(99, 102, 241, 0.12);
  overflow: hidden;
  animation: dialogSlideIn 0.22s 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: 12px 14px;
  border-bottom: 1px solid #e5e7eb;
}

.dialog-title {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  color: #1e293b;
  line-height: 1.3;
}

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

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

.dialog-body {
  display: grid;
  gap: 14px;
  padding: 14px;
}

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

.form-label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  font-weight: 600;
  color: #334155;
}

.form-input {
  width: 100%;
  min-height: 40px;
  padding: 8px;
  border: none;
  border-bottom: 2px solid #e2e8f0;
  border-radius: 0;
  font-size: 14px;
  transition: all 0.2s ease;
  background: transparent;
  color: #1e293b;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-bottom-color: #6366f1;
  background: transparent;
  box-shadow: none;
}

.form-select {
  width: 100%;
  min-height: 40px;
  padding: 10px 40px 10px 14px;
  border: 2px solid #e2e8f0;
  border-radius: 14px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s ease;
  background: #f8fafc;
  color: #1e293b;
  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 10px center;
  background-size: 18px;
}

.form-select:hover {
  border-color: #cbd5e1;
  background-color: #f8fafc;
}

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

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

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

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

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

.dialog-footer {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding: 12px 16px 16px;
  border-top: 1px solid #e5e7eb;
  background: #f8fafc;
}

.btn-cancel {
  min-width: 76px;
  padding: 8px 18px;
  background: #e5e7eb;
  color: #374151;
  border: none;
  border-radius: 14px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: background 0.2s;
}

.btn-cancel:hover {
  background: #d1d5db;
}

.btn-save {
  min-width: 76px;
  padding: 8px 18px;
  background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
  color: white;
  border: none;
  border-radius: 14px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: all 0.3s ease;
  box-shadow: 0 4px 12px -2px rgba(37, 99, 235, 0.4);
}

.btn-save:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 16px -2px rgba(37, 99, 235, 0.5);
}

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

@media (max-width: 768px) {
  .dialog-content {
    width: 100%;
    border-radius: 20px;
  }

  .dialog-header {
    padding: 12px 14px;
  }

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

  .dialog-body {
    gap: 12px;
    padding: 14px;
  }

  .dialog-footer {
    padding: 10px 14px 14px;
  }

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