import { Component, Inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatDialogModule, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatSelectModule } from '@angular/material/select';
import { MatDatepickerModule } from '@angular/material/datepicker';
import { MatNativeDateModule } from '@angular/material/core';
import { MatIconModule } from '@angular/material/icon';

interface Project {
  id: string;
  name: string;
  customer: string;
  status: 'pending' | 'in-progress' | 'completed' | 'on-hold';
  designer: string;
  startDate: string;
  endDate: string;
  budget: number;
  progress: number;
}

@Component({
  selector: 'app-project-dialog',
  standalone: true,
  imports: [
    CommonModule,
    FormsModule,
    MatButtonModule,
    MatDialogModule,
    MatFormFieldModule,
    MatInputModule,
    MatSelectModule,
    MatDatepickerModule,
    MatNativeDateModule,
    MatIconModule
  ],
  templateUrl: './project-dialog.html',
  styleUrl: './project-dialog.scss'
})
export class ProjectDialogComponent {
  project: Project;
  isEditMode: boolean;
  designers: string[] = ['张设计师', '刘设计师', '王设计师', '陈设计师', '杨设计师'];
  statuses: { value: string; label: string }[] = [
    { value: 'pending', label: '待开始' },
    { value: 'in-progress', label: '进行中' },
    { value: 'completed', label: '已完成' },
    { value: 'on-hold', label: '暂停中' }
  ];

  constructor(
    public dialogRef: MatDialogRef<ProjectDialogComponent>,
    @Inject(MAT_DIALOG_DATA) public data: Project
  ) {
    this.isEditMode = !!data.id;
    this.project = {
      id: data.id || '',
      name: data.name || '',
      customer: data.customer || '',
      status: data.status || 'pending',
      designer: data.designer || '',
      startDate: data.startDate || '',
      endDate: data.endDate || '',
      budget: data.budget || 0,
      progress: data.progress || 0
    };
  }

  onCancel(): void {
    this.dialogRef.close();
  }

  onSave(): void {
    if (this.isFormValid()) {
      this.dialogRef.close(this.project);
    }
  }

  isFormValid(): boolean {
    return this.project.name.trim() !== '' &&
           this.project.customer.trim() !== '' &&
           this.project.designer.trim() !== '' &&
           this.project.startDate !== '' &&
           this.project.endDate !== '' &&
           this.project.budget > 0 &&
           this.project.progress >= 0 &&
           this.project.progress <= 100;
  }

  onStartDateChange(date: Date): void {
    if (date) {
      this.project.startDate = date.toISOString().split('T')[0];
    } else {
      this.project.startDate = '';
    }
  }

  onEndDateChange(date: Date): void {
    if (date) {
      this.project.endDate = date.toISOString().split('T')[0];
    } else {
      this.project.endDate = '';
    }
  }

  onProgressChange(): void {
    // 确保进度在0-100之间
    this.project.progress = Math.max(0, Math.min(100, this.project.progress));
  }

  formatCurrency(amount: number): string {
    return new Intl.NumberFormat('zh-CN', {
      style: 'currency',
      currency: 'CNY',
      minimumFractionDigits: 0
    }).format(amount);
  }
}