import { Component, EventEmitter, Input, Output } from '@angular/core';
import { CommonModule } from '@angular/common';

// 定义团队成员接口
interface TeamMember {
  id: string;
  name: string;
  role: string;
  avatar?: string;
  taskCount: number;
  department: string;
  skills?: string[];
  experience?: number;
}

// 定义项目数据接口
interface ProjectData {
  customerName: string;
  decorationType: string;
  area: number;
  firstDraftDate: string;
  budget?: number;
  style?: string;
}

@Component({
  selector: 'app-team-assignment-dialog',
  standalone: true,
  imports: [CommonModule],
  templateUrl: './team-assignment-dialog.component.html',
  styleUrls: ['./team-assignment-dialog.component.scss']
})
export class TeamAssignmentDialogComponent {
  @Input() projectData: ProjectData | null = null;
  @Output() assignmentConfirmed = new EventEmitter<{
    designer: TeamMember;
    assistant: TeamMember | null;
    manager: TeamMember;
  }>();
  @Output() dialogClosed = new EventEmitter<void>();

  // 选中的团队成员
  selectedDesigner: TeamMember | null = null;
  selectedAssistant: TeamMember | null = null;
  selectedManager: TeamMember | null = null;

  // 提交状态
  isSubmitting = false;

  // 模拟团队成员数据
  designers: TeamMember[] = [
    {
      id: 'designer-1',
      name: '张设计',
      role: '高级设计师',
      avatar: document.baseURI+'/assets/avatars/designer1.jpg',
      taskCount: 3,
      department: '设计部',
      skills: ['现代简约', '北欧风', '工业风'],
      experience: 5
    },
    {
      id: 'designer-2',
      name: '李美美',
      role: '资深设计师',
      avatar: document.baseURI+'/assets/avatars/designer2.jpg',
      taskCount: 2,
      department: '设计部',
      skills: ['新中式', '法式轻奢', '美式'],
      experience: 8
    },
    {
      id: 'designer-3',
      name: '王创意',
      role: '首席设计师',
      avatar: document.baseURI+'/assets/avatars/designer3.jpg',
      taskCount: 1,
      department: '设计部',
      skills: ['高端定制', '别墅设计', '商业空间'],
      experience: 12
    },
    {
      id: 'designer-4',
      name: '陈艺术',
      role: '设计师',
      avatar: document.baseURI+'/assets/avatars/designer4.jpg',
      taskCount: 4,
      department: '设计部',
      skills: ['日式', '混搭', '小户型'],
      experience: 3
    }
  ];

  assistants: TeamMember[] = [
    {
      id: 'assistant-1',
      name: '小刘',
      role: '助理设计师',
      avatar: document.baseURI+'/assets/avatars/assistant1.jpg',
      taskCount: 2,
      department: '设计部',
      skills: ['效果图制作', '方案整理'],
      experience: 2
    },
    {
      id: 'assistant-2',
      name: '小周',
      role: '助理设计师',
      avatar: document.baseURI+'/assets/avatars/assistant2.jpg',
      taskCount: 3,
      department: '设计部',
      skills: ['CAD绘图', '材料整理'],
      experience: 1
    },
    {
      id: 'assistant-3',
      name: '小王',
      role: '助理设计师',
      avatar: document.baseURI+'/assets/avatars/assistant3.jpg',
      taskCount: 1,
      department: '设计部',
      skills: ['3D建模', '渲染'],
      experience: 3
    }
  ];

  managers: TeamMember[] = [
    {
      id: 'manager-1',
      name: '赵经理',
      role: '项目经理',
      avatar: document.baseURI+'/assets/avatars/manager1.jpg',
      taskCount: 5,
      department: '项目部',
      skills: ['项目管理', '客户沟通'],
      experience: 7
    },
    {
      id: 'manager-2',
      name: '孙总监',
      role: '高级项目经理',
      avatar: document.baseURI+'/assets/avatars/manager2.jpg',
      taskCount: 3,
      department: '项目部',
      skills: ['团队协调', '质量控制'],
      experience: 10
    },
    {
      id: 'manager-3',
      name: '钱主管',
      role: '项目经理',
      avatar: document.baseURI+'/assets/avatars/manager3.jpg',
      taskCount: 4,
      department: '项目部',
      skills: ['进度管控', '成本控制'],
      experience: 6
    }
  ];

  constructor() {}

  // 选择设计师
  selectDesigner(designer: TeamMember): void {
    this.selectedDesigner = designer;
  }

  // 选择助理设计师
  selectAssistant(assistant: TeamMember): void {
    this.selectedAssistant = assistant;
  }

  // 选择项目经理
  selectManager(manager: TeamMember): void {
    this.selectedManager = manager;
  }

  // 获取任务状态样式类
  getTaskStatusClass(taskCount: number): string {
    if (taskCount <= 2) {
      return 'status-light';
    } else if (taskCount <= 4) {
      return 'status-medium';
    } else {
      return 'status-heavy';
    }
  }

  // 检查分配是否有效
  isAssignmentValid(): boolean {
    return !!(this.selectedDesigner && this.selectedManager);
  }

  // 确认分配
  confirmAssignment(): void {
    if (!this.isAssignmentValid()) {
      return;
    }

    this.isSubmitting = true;

    // 模拟API调用
    setTimeout(() => {
      this.assignmentConfirmed.emit({
        designer: this.selectedDesigner!,
        assistant: this.selectedAssistant,
        manager: this.selectedManager!
      });
      this.isSubmitting = false;
    }, 1500);
  }

  // 关闭弹窗
  closeDialog(): void {
    this.dialogClosed.emit();
  }
}