import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'app-resume-upload',
  standalone: true,
  imports: [CommonModule, FormsModule],
  templateUrl: './resume-upload.html',
  styleUrls: ['./resume-upload.scss']
})
export class ResumeUpload {
  isUploadPanelOpen = false;
  isDragging = false;
  uploadProgress = 0;
  uploadStatus: 'idle' | 'uploading' | 'success' | 'error' = 'idle';
  fileName = '';
  fileSize = '';
  
  targetCompanies = [
    { id: 1, name: '量子科技', selected: false },
    { id: 2, name: '神经矩阵', selected: false },
    { id: 3, name: '数据视野', selected: false },
    { id: 4, name: '界面未来', selected: false },
    { id: 5, name: '链芯科技', selected: false }
  ];
  
  toggleUploadPanel() {
    this.isUploadPanelOpen = !this.isUploadPanelOpen;
    if (!this.isUploadPanelOpen) {
      this.resetUpload();
    }
  }
  
  onDragOver(event: DragEvent) {
    event.preventDefault();
    event.stopPropagation();
    this.isDragging = true;
  }
  
  onDragLeave(event: DragEvent) {
    event.preventDefault();
    event.stopPropagation();
    this.isDragging = false;
  }
  
  onDrop(event: DragEvent) {
    event.preventDefault();
    event.stopPropagation();
    this.isDragging = false;
    
    if (event.dataTransfer?.files) {
      this.handleFiles(event.dataTransfer.files);
    }
  }
  
  onFileSelected(event: Event) {
    const input = event.target as HTMLInputElement;
    if (input.files && input.files.length > 0) {
      this.handleFiles(input.files);
    }
  }
  
  handleFiles(files: FileList) {
    const file = files[0];
    if (file.type === 'application/pdf' || file.type === 'application/msword' || file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
      this.fileName = file.name;
      this.fileSize = this.formatFileSize(file.size);
      this.startUpload();
    } else {
      this.uploadStatus = 'error';
    }
  }
  
  formatFileSize(bytes: number): string {
    if (bytes === 0) return '0 Bytes';
    const k = 1024;
    const sizes = ['Bytes', 'KB', 'MB', 'GB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));
    return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
  }
  
  startUpload() {
    this.uploadStatus = 'uploading';
    this.uploadProgress = 0;
    
    const interval = setInterval(() => {
      this.uploadProgress += 5;
      if (this.uploadProgress >= 100) {
        clearInterval(interval);
        setTimeout(() => {
          this.uploadStatus = 'success';
        }, 500);
      }
    }, 200);
  }
  
  resetUpload() {
    this.uploadStatus = 'idle';
    this.uploadProgress = 0;
    this.fileName = '';
    this.fileSize = '';
    this.isDragging = false;
  }
  
  toggleCompanySelection(company: any) {
    company.selected = !company.selected;
  }
  
  submitResume() {
    const selectedCompanies = this.targetCompanies.filter(c => c.selected);
    if (selectedCompanies.length === 0) {
      alert('请至少选择一家目标公司');
      return;
    }
    
    // 这里应该是实际的提交逻辑
    console.log('提交简历到:', selectedCompanies.map(c => c.name).join(', '));
    this.toggleUploadPanel();
  }
}
