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

// 评价维度接口
export interface ReviewDimension {
  id: string;
  label: string;
  description?: string;
  score: number;
  subDimensions?: ReviewDimension[];
}

// 评价数据接口
export interface CustomerReviewData {
  // 总体评价 (0-5分)
  overallSatisfaction: number;
  
  // 服务质量维度 (0-5分)
  serviceTimeliness: number;        // 服务时效
  suggestionResponse: number;       // 建议回应
  smallImageDelivery: number;       // 小图交付
  revisionEfficiency: number;       // 改图不及时
  
  // 图纸渲染质量 (0-5分)
  renderingQuality: {
    materials: number;              // 材质
    lighting: number;               // 灯光
    texture: number;                // 纹理
    structure: number;              // 硬装结构
  };
  
  // 沟通体验 (0-5分)
  communication: {
    documentation: number;          // 资料齐情况
    requirementUnderstanding: number; // 需求误解
    communicationSufficiency: number; // 缺少沟通
    contentRelevance: number;       // 文不对题
  };
  
  // 多个场景评价 - 对应设计师
  scenes: SceneReview[];
  
  // 改进建议
  improvementSuggestions: string;
  
  // 下次合作优化点
  nextCooperationOptimization: string;
  
  // 合作意愿
  willCooperateAgain: boolean;
  
  // 投诉内容（可选）
  complaintContent?: string;
  
  // 评价时间
  reviewDate: Date;
}

// 场景评价接口
export interface SceneReview {
  sceneName: string;
  designerName: string;
  score: number;
  comments?: string;
}

@Component({
  selector: 'app-customer-review-form',
  standalone: true,
  imports: [CommonModule, FormsModule],
  templateUrl: './customer-review-form.html',
  styleUrls: ['./customer-review-form.scss']
})
export class CustomerReviewFormComponent {
  @Input() projectName: string = '';
  @Input() designerName: string = '';
  @Input() scenes: string[] = ['客厅', '餐厅', '卧室', '厨房', '卫生间', '阳台'];
  @Output() reviewSubmitted = new EventEmitter<CustomerReviewData>();
  
  // 默认评价数据
  reviewData: CustomerReviewData = {
    overallSatisfaction: 5,
    serviceTimeliness: 5,
    suggestionResponse: 5,
    smallImageDelivery: 5,
    revisionEfficiency: 5,
    renderingQuality: {
      materials: 5,
      lighting: 5,
      texture: 5,
      structure: 5
    },
    communication: {
      documentation: 5,
      requirementUnderstanding: 5,
      communicationSufficiency: 5,
      contentRelevance: 5
    },
    scenes: [],
    improvementSuggestions: '',
    nextCooperationOptimization: '',
    willCooperateAgain: true,
    complaintContent: '',
    reviewDate: new Date()
  };

  // 是否显示投诉内容
  showComplaint: boolean = false;

  constructor() {
    // 初始化场景评价
    this.scenes.forEach(scene => {
      this.reviewData.scenes.push({
        sceneName: scene,
        designerName: this.designerName,
        score: 5,
        comments: ''
      });
    });
  }

  // 提交评价
  submitReview(): void {
    // 验证数据
    if (this.validateReview()) {
      this.reviewSubmitted.emit(this.reviewData);
      this.resetForm();
    } else {
      alert('请完成所有必填的评价项目');
    }
  }

  // 验证评价数据
  private validateReview(): boolean {
    // 检查总体评分
    if (this.reviewData.overallSatisfaction === 0) return false;
    
    // 检查所有场景评分
    for (const scene of this.reviewData.scenes) {
      if (scene.score === 0) return false;
    }
    
    return true;
  }

  // 重置表单
  private resetForm(): void {
    this.reviewData = {
      overallSatisfaction: 5,
      serviceTimeliness: 5,
      suggestionResponse: 5,
      smallImageDelivery: 5,
      revisionEfficiency: 5,
      renderingQuality: {
        materials: 5,
        lighting: 5,
        texture: 5,
        structure: 5
      },
      communication: {
        documentation: 5,
        requirementUnderstanding: 5,
        communicationSufficiency: 5,
        contentRelevance: 5
      },
      scenes: this.scenes.map(scene => ({
        sceneName: scene,
        designerName: this.designerName,
        score: 5,
        comments: ''
      })),
      improvementSuggestions: '',
      nextCooperationOptimization: '',
      willCooperateAgain: true,
      complaintContent: '',
      reviewDate: new Date()
    };
    this.showComplaint = false;
  }

  // 获取平均分
  getAverageScore(): number {
    const scores = [
      this.reviewData.overallSatisfaction,
      this.reviewData.serviceTimeliness,
      this.reviewData.suggestionResponse,
      this.reviewData.smallImageDelivery,
      this.reviewData.revisionEfficiency,
      this.reviewData.renderingQuality.materials,
      this.reviewData.renderingQuality.lighting,
      this.reviewData.renderingQuality.texture,
      this.reviewData.renderingQuality.structure,
      this.reviewData.communication.documentation,
      this.reviewData.communication.requirementUnderstanding,
      this.reviewData.communication.communicationSufficiency,
      this.reviewData.communication.contentRelevance,
      ...this.reviewData.scenes.map(scene => scene.score)
    ];
    
    const sum = scores.reduce((total, score) => total + score, 0);
    return Math.round((sum / scores.length) * 10) / 10;
  }

  // 切换投诉显示
  toggleComplaint(): void {
    this.showComplaint = !this.showComplaint;
    if (!this.showComplaint) {
      this.reviewData.complaintContent = '';
    }
  }

  // 生成星级显示
  generateStars(score: number): string[] {
    const stars = [];
    for (let i = 1; i <= 5; i++) {
      if (i <= score) {
        stars.push('★');
      } else if (i - 0.5 <= score) {
        stars.push('☆');
      } else {
        stars.push('☆');
      }
    }
    return stars;
  }
}