import { Component, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Router } from '@angular/router';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-consultation-list',
  standalone: true,
  imports: [CommonModule, RouterModule, FormsModule],
  templateUrl: './consultation-list.component.html',
  styleUrls: ['./consultation-list.component.scss']
})
export class ConsultationListComponent implements OnInit {
  // 咨询数据
  consultations = [
    {
      id: '1',
      customerName: '张先生',
      content: '咨询关于厨房改造的预算和工期，希望能在下个月开始施工，请尽快联系我',
      priority: 'normal',
      createdAt: new Date('2024-01-15T10:30:00'),
      status: 'pending'
    },
    {
      id: '2',
      customerName: '李女士',
      content: '询问客厅设计风格建议，希望能提供一些现代简约风格的方案参考',
      priority: 'urgent',
      createdAt: new Date('2024-01-15T11:45:00'),
      status: 'pending'
    },
    {
      id: '3',
      customerName: '王先生',
      content: '需要全屋设计方案咨询，预算在20万左右，希望能安排设计师上门测量',
      priority: 'normal',
      createdAt: new Date('2024-01-15T14:20:00'),
      status: 'pending'
    },
    {
      id: '4',
      customerName: '陈女士',
      content: '想了解卧室装修的具体流程和注意事项，特别是环保材料的选择',
      priority: 'normal',
      createdAt: new Date('2024-01-15T09:15:00'),
      status: 'completed'
    },
    {
      id: '5',
      customerName: '刘先生',
      content: '紧急！明天要看房，需要设计师现场指导，请务必今天联系我',
      priority: 'urgent',
      createdAt: new Date('2024-01-15T16:30:00'),
      status: 'pending'
    }
  ];

  filteredConsultations = [...this.consultations];
  searchTerm = '';

  constructor(private router: Router) {}

  ngOnInit(): void {
    this.onSearch();
  }

  // 搜索功能
  onSearch(): void {
    if (!this.searchTerm.trim()) {
      this.filteredConsultations = [...this.consultations];
      return;
    }

    this.filteredConsultations = this.consultations.filter(consultation =>
      consultation.customerName.toLowerCase().includes(this.searchTerm.toLowerCase()) ||
      consultation.content.toLowerCase().includes(this.searchTerm.toLowerCase())
    );
  }

  // 查看咨询详情
  viewConsultation(consultation: any): void {
    console.log('查看咨询:', consultation);
    // 这里可以导航到详情页面
  }

  // 处理咨询
  handleConsultation(consultation: any, event: Event): void {
    event.stopPropagation();
    console.log('处理咨询:', consultation);
  }

  // 查看详情
  viewDetails(consultation: any, event: Event): void {
    event.stopPropagation();
    console.log('查看详情:', consultation);
  }

  // 返回上一页
  goBack(): void {
    this.router.navigate(['/customer-service']);
  }

  // 获取优先级标签
  getPriorityLabel(priority: string): string {
    switch (priority) {
      case 'urgent':
        return '紧急';
      case 'high':
        return '高';
      case 'normal':
        return '普通';
      case 'low':
        return '低';
      default:
        return '普通';
    }
  }

  // 获取状态标签
  getStatusLabel(status: string): string {
    switch (status) {
      case 'pending':
        return '待处理';
      case 'processing':
        return '处理中';
      case 'completed':
        return '已完成';
      default:
        return '待处理';
    }
  }

  // 格式化时间
  formatTime(date: Date): string {
    const now = new Date();
    const diff = now.getTime() - date.getTime();
    const minutes = Math.floor(diff / (1000 * 60));
    const hours = Math.floor(diff / (1000 * 60 * 60));
    const days = Math.floor(diff / (1000 * 60 * 60 * 24));

    if (minutes < 60) {
      return `${minutes}分钟前`;
    } else if (hours < 24) {
      return `${hours}小时前`;
    } else {
      return `${days}天前`;
    }
  }

  // 获取头像颜色
  getAvatarColor(name: string): string {
    const colors = ['#007AFF', '#34C759', '#FF9500', '#FF3B30', '#AF52DE', '#5AC8FA'];
    const index = name.charCodeAt(0) % colors.length;
    return colors[index];
  }
}