import { Component, OnInit } from '@angular/core';
import { NavigationExtras, Router } from '@angular/router';
import { AnimationController, ModalController, NavParams, PopoverController } from '@ionic/angular';
import { ProcessesService } from 'src/app/services/processes.service';
import { ToastService } from 'src/app/services/toast.service';
import { EditEventToApproveComponent } from 'src/app/shared/gabinete-digital/edit-event-to-approve/edit-event.page';
import { BadRequestPage } from 'src/app/shared/popover/bad-request/bad-request.page';
import { SuccessMessagePage } from 'src/app/shared/popover/success-message/success-message.page';
import { EmendMessageModalPage } from '../emend-message-modal/emend-message-modal.page';
import { Location } from '@angular/common';
import { ThemeService } from 'src/app/services/theme.service'
import { RouteService } from 'src/app/services/route.service';


@Component({
  selector: 'app-event-actions-popover',
  templateUrl: './event-actions-popover.page.html',
  styleUrls: ['./event-actions-popover.page.scss'],
})
export class EventActionsPopoverPage implements OnInit {
  serialNumber:string;
  instanceId: string;

  constructor(
    private navParams: NavParams,
    private processes:ProcessesService,
    private router:Router,
    private modalController: ModalController,
    private popoverController: PopoverController,
    private animationController: AnimationController,
    private toastService: ToastService,
    private RouteService: RouteService,
    public ThemeService: ThemeService) {
      this.serialNumber = this.navParams.get('serialNumber');
      this.instanceId = this.navParams.get('InstanceId');
     }

  ngOnInit() {
    window.onresize = (event) => {
      if( window.innerWidth >= 800){
        this.popoverController.dismiss();
      }
    };
  }

  goBack() {
    this.closePopover();

    this.RouteService.goBack()
  }

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

  async approveTask() {
    let body = { "serialNumber": this.serialNumber, "action": "Aprovar" }
    console.log(body);
    
    const loader = this.toastService.loading()
    try {
      await this.processes.PostTaskAction(body).toPromise()

      this.toastService.successMessage();
      this.goBack();
    } catch (error) {

      this.toastService.badRequest()
    }
    finally {
      loader.remove()
    }

  }

  async emendarTask() {
    this.closePopover();
    
    const modal = await this.modalController.create({
      component: EmendMessageModalPage,
      componentProps:{
      },
      cssClass: 'emend-message-modal',
      backdropDismiss: false
    });

    await modal.present();

    modal.onDidDismiss()
    .then( async (res) => {
      console.log(res.data);
      
      if(res.data !== ''){
        let body = { "serialNumber": this.serialNumber, 
        "action": "Emendar",
            "dataFields": {
            "ReviewUserComment": res.data,
          }
        }
        console.log(body);

        const loader = this.toastService.loading()
        
        try { 
          await this.processes.PostTaskAction(body).toPromise();
          this.toastService.successMessage('Pedido enviado');
          this.goBack();
        } catch (error) {
          this.toastService.badRequest();
        }
        finally {
          loader.remove()
        }
      }
      else{
        //this.alertService.presentAlert('Operação cancelada!');
      }
    });
  }

  async rejeitar(){
    let body = { "serialNumber": this.serialNumber, "action": "Rejeitar" }
    console.log(body);

    const loader = this.toastService.loading()
    try {    
      await this.processes.PostTaskAction(body).toPromise();
      await this.toastService.successMessage('Processo rejeitado');
      this.goBack();
    } catch (error) {
      this.toastService.badRequest()
    }
    finally {
      loader.remove()
    }


  }

  async editTask() {
    this.closePopover();
    const modal = await this.modalController.create({
      component: EditEventToApproveComponent,
      componentProps: {
        serialNumber: this.serialNumber,
        InstanceId: this.instanceId
      },
      cssClass: 'modal modal-desktop',
      // backdropDismiss: false
    });

    await modal.present();

    modal.onDidDismiss().then(res => {
    });

  } 
  
}
