// import { CommonModule } from '@angular/common';
// import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
// import { ActivatedRoute, RouterModule } from '@angular/router';
// import { ZardCardComponent } from '@shared/components/card/card.component';
// import { ReportService } from 'src/app/core/services/report';
// import { CourseReportDetail, CourseReportDetailRow } from 'src/app/core/interfaces/report';
// import { ZardButtonComponent } from '@shared/components/button/button.component';

// @Component({
//   standalone: true,
//   selector: 'app-report-courses-detail',
//   templateUrl: './report-detail.html',
//   changeDetection: ChangeDetectionStrategy.OnPush,
//   imports: [CommonModule, RouterModule, ZardCardComponent, ZardButtonComponent],
// })
// export class ReportCoursesDetailPage {
//   detail = signal<CourseReportDetail | undefined>(undefined);
//   editMode = signal(false);
//   editedRows = signal<CourseReportDetailRow[]>([]);
//   private originalRows = signal<CourseReportDetailRow[]>([]);

//   constructor(private route: ActivatedRoute, private api: ReportService) {
//     const id = this.route.snapshot.params['id'];
//     this.api.getDetail(id).subscribe((d) => {
//       this.detail.set(d);
//       this.editedRows.set(d?.rows ?? []);
//       this.originalRows.set(d?.rows ? d.rows.map((r) => ({ ...r })) : []);
//     });
//   }

//   onValidate() {
//     const id = this.detail()?.summary.id;
//     if (!id) return;
//     // Persist edited rows then validate
//     this.api.modifyRows(id, this.editedRows()).subscribe(() => {
//       this.api.validate(id).subscribe((s) => {
//         if (this.detail()) this.detail.set({ summary: s!, rows: this.editedRows() });
//         // Commit current edits as the new baseline
//         this.originalRows.set(this.editedRows().map((r) => ({ ...r })));
//         this.editMode.set(false);
//       });
//     });
//   }
//   onConfirm() {
//     const id = this.detail()?.summary.id;
//     if (!id) return;
//     this.api.confirm(id).subscribe((s) => {
//       if (this.detail()) this.detail.set({ summary: s!, rows: this.editedRows() });
//       // Confirm also commits the current edits as baseline
//       this.originalRows.set(this.editedRows().map((r) => ({ ...r })));
//       this.editMode.set(false);
//     });
//   }
//   onReset() {
//     const id = this.detail()?.summary.id;
//     if (!id) return;
//     this.api.resetStatus(id).subscribe((s) => {
//       if (this.detail()) this.detail.set({ summary: s!, rows: this.detail()!.rows });
//       // Reset discards uncommitted edits
//       this.editedRows.set(this.originalRows().map((r) => ({ ...r })));
//       this.editMode.set(false);
//     });
//   }

//   onEditToggle() {
//     if (this.detail()?.summary.confirmed) return;
//     const currentlyEditing = this.editMode();
//     if (currentlyEditing) {
//       // Leaving edit mode without validation: revert to original snapshot
//       this.editedRows.set(this.originalRows().map((r) => ({ ...r })));
//       this.editMode.set(false);
//     } else {
//       this.editMode.set(true);
//     }
//   }

//   onChangeMontant(index: number, value: any) {
//     const v = Number(value);
//     this.editedRows.update((rows: CourseReportDetailRow[]) => {
//       const current = rows[index];
//       if (!current) return rows;
//       current.montant = Number.isFinite(v) ? v : current.montant;
//       return rows;
//     });
//   }

//   onChangeNombre(index: number, value: any) {
//     const v = Number(value);
//     this.editedRows.update((rows: CourseReportDetailRow[]) => {
//       const current = rows[index];
//       if (!current) return rows;
//       current.nombre = Number.isFinite(v) ? v : current.nombre;
//       return rows;
//     });
//   }

//   onToggleDistributed(index: number, value: any) {
//     const checked = !!value;
//     this.editedRows.update((rows: CourseReportDetailRow[]) => {
//       const current = rows[index];
//       if (!current) return rows;
//       current.distributed = checked;
//       return rows;
//     });
//   }

//   onToggleExterne(index: number, value: any) {
//     const checked = !!value;
//     this.editedRows.update((rows: CourseReportDetailRow[]) => {
//       const current = rows[index];
//       if (!current) return rows;
//       current.externe = checked;
//       return rows;
//     });
//   }

//   trackByRow(index: number, row: CourseReportDetailRow) {
//     return row.typeGain + '|' + row.typeJeu + '|' + index;
//   }

//   isRowDirty(index: number): boolean {
//     const current = this.editedRows()[index];
//     const original = this.originalRows()[index];
//     if (!current || !original) return false;
//     return (
//       current.montant !== original.montant ||
//       current.nombre !== original.nombre ||
//       !!current.distributed !== !!original.distributed ||
//       !!current.externe !== !!original.externe
//     );
//   }
// }
