import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { AnimationController, ModalController, NavParams, PopoverController } from '@ionic/angular';
import { AddNotePage } from 'src/app/modals/add-note/add-note.page';
import { BookMeetingModalPage } from 'src/app/pages/gabinete-digital/expediente/book-meeting-modal/book-meeting-modal.page';
import { DeplomaService } from 'src/app/Rules/deploma.service';
import { ProcessesService } from 'src/app/services/processes.service';
import { ToastService } from 'src/app/services/toast.service';
import { Location } from '@angular/common';
import { RouteService } from 'src/app/services/route.service';

@Component({
  selector: 'app-deploma-options',
  templateUrl: './deploma-options.page.html',
  styleUrls: ['./deploma-options.page.scss'],
})
export class DeplomaOptionsPage implements OnInit {


  serialNumber: string;
  profile: string;
  task: any
  fulltask: any


  constructor(public popoverController: PopoverController,
    private modalController: ModalController,
    private activatedRoute: ActivatedRoute,
    private processes: ProcessesService,
    private navParams: NavParams,
    private toastService: ToastService,
    private router: Router,
    private deplomaService: DeplomaService,
    private RouteService: RouteService) {
      this.serialNumber = this.navParams.get('serialNumber');
      this.task = this.navParams.get('task');
      this.fulltask = this.navParams.get('fulltask');
    }

  ngOnInit() {
    console.log(this.serialNumber);
  }

  async openAddNoteModal(actionName:string) {
    this.popoverController.dismiss();
    let classs;
    if( window.innerWidth <= 800) {
      classs = 'modal modal-desktop'
    } else  {
      classs = 'modal modal-desktop'
    }
    const modal = await this.modalController.create({
      component: AddNotePage,
      componentProps:{
      },
      cssClass: classs,
      backdropDismiss: true
    });

    await modal.present();

    modal.onDidDismiss().then(async (res) => {
      if(res.data){


        const DocumentToSave = res.data.documents.map((e) => {
          return {
            ApplicationId: e.ApplicationType,
            SourceId: e.Id,
          }
        });

        let docs = {
          ProcessInstanceID: "",
          Attachments: DocumentToSave,
        }

        if(actionName == 'Solicitar assinatura') {
          await this.askSignature(res.data.note, docs);
          this.goBack();
        } else if(actionName == 'Solicitar alteração') {
          await this.askToChange(res.data.note, docs);
          this.goBack();
        } else if (actionName == 'Assinar Diploma') {
          await this.sign(res.data.note, docs);
          this.goBack();
        } else if(actionName == 'Concluir diploma'){
          await this.finish(res.data.note, docs);
          this.goBack();
        }
      }
    });
  }

  async askToChange(note:string, documents:any){
    let body = {
      "serialNumber": this.serialNumber,
      "action": "Retificar",
      "ActionTypeId": 99999841,
      "dataFields": {
        "ReviewUserComment": note,
      },
      "AttachmentList" :documents,
    }

    const loader = this.toastService.loading()

    try {
      await this.processes.CompleteTask(body).toPromise()
      this.close();
      this.toastService._successMessage()
    } catch (error) {
      this.toastService._badRequest()
    }     finally {
      loader.remove()
    }

  }


  async openBookMeetingModal(task: any) {
    this.popoverController.dismiss();
    let classs;
    if( window.innerWidth <= 800){
      classs = 'book-meeting-modal modal modal-desktop'
    } else  {
      classs = 'modal modal-desktop showAsideOptions'
    }
    const modal = await this.modalController.create({
      component: BookMeetingModalPage,
      componentProps: {
        task: this.task,
      },
      cssClass: classs,
      backdropDismiss: false
    });
    await modal.present();
    modal.onDidDismiss();
  }

  
  async askSignature(note:string, documents:any) {
    let body = {
      "serialNumber": this.serialNumber,
      "action": "Aprovar",
      "ActionTypeId": 99999840,
      "dataFields": {
        "ReviewUserComment": note,
      },
      "AttachmentList" :documents,
    }

    const loader = this.toastService.loading()

    try {
      await this.processes.CompleteTask(body).toPromise()
      this.close();
      this.toastService._successMessage()
    } catch (error) {
      this.toastService._badRequest()
    }     finally {
      loader.remove()
    }

  } 


  async sign(note:string, documents:any) {
    let body = {
      "serialNumber": this.serialNumber,
      "action": "Assinado",
      "ActionTypeId": 99999842,
      "dataFields": {
        "ReviewUserComment": note,
      },
      "AttachmentList" :documents,
    }

    const loader = this.toastService.loading()

    try {
      await this.processes.CompleteTask(body).toPromise()
      this.close();
      this.toastService._successMessage()
    } catch (error) {
      this.toastService._badRequest()
    } finally {
      loader.remove()
    }
  }


  async finish(note:string, documents:any){

    let body = {
      "serialNumber": this.serialNumber,
      "action": "Concluir",
      "ActionTypeId": 95,
      "dataFields": {
        "ReviewUserComment": note,
      },
      "AttachmentList" :documents,
    }

    const loader = this.toastService.loading()

    try {
      await this.processes.CompleteTask(body).toPromise();
      this.toastService._successMessage('Processo concluído')
    } catch (error) {
      this.toastService._badRequest()
    }
    finally {
      loader.remove()
    }

  }

  goBack() {
    this.RouteService.goBack()
  }


  close() {
    this.popoverController.dismiss();
  }

}
