import { DOCUMENT } from '@angular/common';
import { Component, Inject, OnInit } from '@angular/core';
import { MatDialog, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { first } from 'rxjs/operators';
import { ProjectService } from 'src/app/services/projects.service';
import { ToastService } from 'src/app/services/toast.service';
import { StorageService } from 'src/app/core/services/storage.service';
import {removeDataStorage} from 'src/app/custom/common'

@Component({
  selector: 'app-modal-delete',
  templateUrl: './modal-delete.component.html',
  styleUrls: ['./modal-delete.component.scss'],
})
export class ModalDeleteComponent implements OnInit {
  public label = '';
  public modulename!: any;
  constructor(
    public dialog: MatDialog,
    @Inject(MAT_DIALOG_DATA) public data: any,
    @Inject(DOCUMENT) private document: Document,
    private projectService: ProjectService,
    private toast: ToastService,
    public storage: StorageService,
  ) {}

  ngOnInit(): void {
    const dd = this.storage.getItem('__domaineDD');
    const __domainedd = dd ? JSON.parse(dd) : {};
    this.modulename = (this.data && this.data.data && this.data.data.from != undefined) ? this.data.data.from : __domainedd.modulename
    this.label = (this.data && this.data.data && this.data.data.from != undefined) ? this.data.data.from : __domainedd.label
    
    // switch (this.data.from) {
    //   case 'projets':
    //     this.label = 'projet(s)';
    //     break;
    //   case 'cotations':
    //     this.label = 'operation(s)';
    //     break;

    //   case 'depots':
    //     this.label = 'dépôt(s)';
    //     break;

    //   case 'prospects':
    //     this.label = 'Prospect(s)';
    //     break;

    //   case 'opportunite':
    //     this.label = 'Opportunité(s)';
    //     break;

    //   case 'estimations':
    //     this.label = 'Cotation(s)';
    //     break;

    //   case 'accounts':
    //     this.label = 'Compte(s)';
    //     break;

    //   case 'contacts':
    //     this.label = 'Contact(s)';
    //     break;

    //   case 'ApportAffaire':
    //     this.label = 'Apport Affaire(s)';
    //     break;
    //   case 'Prestations':
    //     this.label = 'Prestation(s)';
    //     break;

    //   case 'documents':
    //     this.label = 'Document(s)';
    //     break;
    //   case 'Tickets':
    //     this.label = 'Ticket(s)';
    //     break;

    //   case 'paiements':
    //     this.label = 'Paiement(s)';
    //     break;

    //   case 'Ordredetransfert':
    //     this.label = 'Ordre de transfert(s)';
    //     break;
      
    //   case 'Books':
    //     this.label = 'Book(s)';
    //     break;

    //   case 'Contrats':
    //     this.label = 'Contrat(s)';
    //     break;

    //   default:
    //   // code block
    // }
  }

  public closeDialog() {
    this.dialog.closeAll();
  }

  public delete() {
    // switch (this.data.from) {
      // case 'projets':
        this.projectService
          .removeProject(this.data.records,this.modulename)
          .pipe(first())
          .subscribe(
            (data: any) => {
              if (data && data.success) {
                //console.log(this.data.records);
                removeDataStorage(this.modulename,this.data.records)
                this.toast.showSuccess('Supression avec succès');
                this.closeDialog();
              } else {
                console.log('fmlfj jen',data.errors[0].message);
                this.toast.showError(data.message + data.errors[0].message);
              }
            },
            (error) => {
              this.toast.showError(error.error.message);
              // console.log( error.error.errors[0] );
              // console.log( error.error.message, error.error.message.errors );
              
            }
          );
        // break;
 
    //   default:
    //   // code block
    // }
  }
}
