import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { LotComment, LotCommentService } from './lot-comment.service';
import {  ToastrService } from 'ngx-toastr';


@Component({
  selector: 'app-lot-comments',
  templateUrl: './lot-comment.component.html',
  styleUrls: ['./lot-comment.component.css']
})
export class LotCommentComponent implements OnInit {

  queryTypes = ['File Errors', 'Incomplete Scans', 'Missing Pages', 'Others'];
  queryStatuses = ['Resolved', 'Addressed', 'Submitted'];

  selectedType: string = '';
  selectedStatus: string = '';

  commentsList: LotComment[] = [];
  filteredComments: LotComment[] = [];

  projectdefinitionid!: number;
  searchText: any;
  comments: any;

  constructor(
    private route: ActivatedRoute,
    private lotCommentService: LotCommentService,
     private toastr: ToastrService,
  ) {}

ngOnInit(): void {
  debugger;
  this.route.paramMap.subscribe(params => {
    const idParam = params.get('projectdefinitionid');
    if (!idParam) {
      console.error('No projectdefinitionid in URL!');
      return;
    }
    this.projectdefinitionid = Number(idParam);
    this.loadComments();
  });
}


  loadComments(): void {
   this.lotCommentService.getCommentsByProject(this.projectdefinitionid)
  .subscribe({
    next: (res) => {
      console.log('Lot comments response:', res);
      this.commentsList = res;
      this.filteredComments = res;
    },
    error: (err) => console.error('Error loading comments:', err)
  });

  }

  // onFilter(): void {
  //   this.filteredComments = this.commentsList.filter(comment => {
  //     const matchesType = this.selectedType ? comment.comment_text.includes(this.selectedType) : true;
  //     const matchesStatus = this.selectedStatus ? comment.comment_text.includes(this.selectedStatus) : true;
  //     return matchesType && matchesStatus;
  //   });
  // }

updateQueryType(comment: any) {
  this.lotCommentService.updateQueryType(comment.jobid, comment.querytype).subscribe({
    next: () => {
    this.toastr.success(`Query type updated for Job ID `);
    },
    error: (err) => {
      console.error('Error updating query type:', err);
    }
  });
}

updateStatus(comment: any) {
  this.lotCommentService.updateStatus(comment.jobid, comment.status).subscribe({
    next: () => {
      this.toastr.success(`Status updated for Job ID `);
    },
    error: (err) => {
      console.error('Error updating status:', err);
    }
  });
}

updateStartEndpage(comment: any) {
  if(comment.startpage != 0 && comment.endpage != 0) {
      this.lotCommentService.updateStartEndpage(comment.cid, comment.startpage,comment.endpage,'updatepage').subscribe({
      next: () => this.toastr.success('Page details updated successfully '),
      error: (err) => console.error('Error updating page details:', err)
    });
  }
}
    
onQueryTypeChange(comment: any) {
  this.lotCommentService.updateQueryType(comment.jobid, comment.querytype).subscribe({
    next: () => this.toastr.success(`Query type updated Successfully `),
    error: (err) => console.error('Error updating query type:', err)
  });
}

onStatusChange(comment: any) {
  this.lotCommentService.updateStatus(comment.jobid, comment.status).subscribe({
    next: () => this.toastr.success(`Status updated Successfully `),
    error: (err) => console.error('Error updating status:', err)
  });
}

onRefresh(): void {
  this.selectedType = '';
  this.selectedStatus = '';
  this.loadComments();
}

onFilter(): void {
  this.filteredComments = this.commentsList.filter(comment => {
    const matchesType = this.selectedType ? comment.querytype === this.selectedType : true;
    const matchesStatus = this.selectedStatus ? comment.status === this.selectedStatus : true;
    return matchesType && matchesStatus;
  });
}

}
