import { Component, OnInit } from '@angular/core';
import {AlertController, NavController} from "@ionic/angular";
import {Router} from "@angular/router";
import {SessionDataProviderService} from "../../store/session-data-provider.service";
import {WrenchService} from "../../services/wrench.service";

@Component({
  selector: 'app-jobintprocess',
  templateUrl: './jobintprocess.page.html',
  styleUrls: ['./jobintprocess.page.scss'],
})
export class JobintprocessPage implements OnInit {
  session_image_server:string='';
  curr_session:string='';
  thisObj:any;
  jobData: any;
  allInterest: any;
  selected_job_uid:string='';

  constructor(      private navctr: NavController,
                    private router: Router,
                    private alertController:AlertController,
                    public sessionDataProviderService: SessionDataProviderService,
                    private wrenchService: WrenchService) {

    this.jobData  =  this.router.getCurrentNavigation().extras.state.selItem;
    this.allInterest =  this.router.getCurrentNavigation().extras.state.allInterest;
    console.log("XXXXX 1",   this.router.getCurrentNavigation().extras);
    console.log("XXXXX 2",   this.router.getCurrentNavigation().extras.state);
    //console.log("XXXXX 3",   this.router.getCurrentNavigation().extras.state.ID);
    if (this.jobData==undefined){
      this.onBack();
    }
    this.selected_job_uid  = this.jobData.job_uid;
    this.filterInterestList(this.allInterest);
    this.thisObj =  this;
  }

  filterIntList:[];
  filterInterestList(newAllInterest){
    this.filterIntList = newAllInterest.filter((item)=> item.job_uid ==  this.selected_job_uid);
   // his.jobsData = AllResult.filter((item) => item.status_description == 'ACTIVE')
  }

  async intResponse(thisObj,ProcessItemData,  approveStatus){

    var actionMessage = '';
    var messageHeader ='';
    var promtMessage='';
    var procc = '';
    var reqResult:any;

    switch (approveStatus){
      case "APPROVED":
        actionMessage ="Confirm you are about to approve this task for this user ?";
        messageHeader = "Confirm Approval";
        promtMessage="Approve";
        procc = "ACCEPT";
        break;

      case "REJECT":
        actionMessage ="Confirm you are about to decline this task for this user ?";
        messageHeader = "Confirm Rejection";
        promtMessage = "Decline";
        break;
    }
    console.log(ProcessItemData);
    //debugger;
    //alert("It is me");
    const alert = await thisObj.alertController.create({
      header: messageHeader,
      message: actionMessage,
      buttons: [
        {
          text: "Cancel",
          role: "cancel",
          handler: () => {
            // Do nothing - just close
          },
        },
        {
          text: promtMessage,
          handler: () => {
            // call Service now to process
/*
array (size=8)
  'member_id' => int 1
  'sessionid' => string '6959759CD031A5BE65EF313E74BE21F067A8440150902718232E1248D75ECE05' (length=64)
  'uid' => string '3119b744-42ad-4834-bb83-b737588754ca' (length=36)
  'proc' => string 'ACCEPT' (length=6)
  'client_uid' => string 'c17a43e0-3b70-4128-895c-4dc2d99f17a8' (length=36)
  'offer_code' => string 'XWT8B7W377' (length=10)
  'offer_uid' => string '10088dc3-c1ed-4782-808c-ae64940d3405' (length=36)
 */


            thisObj.wrenchService.offersInterestProcces(ProcessItemData).subscribe(
                reqResult => {
                  reqResult = reqResult;
                  console.log("reqResult  RETURN->", reqResult);
                 // debugger;
                    if (reqResult != null && reqResult.internal_return> 0  ){
                      //alert("Completed");
                      thisObj.onBack();
                    }
                    else{
                      // will plan a message anyway
                      thisObj.onBack();
                    }
                });
          },
        },
      ],
    });
    await alert.present();
  }


  ngOnInit() {
    this.session_image_server = this.sessionDataProviderService.session_image_server;
    this.curr_session=this.sessionDataProviderService.session;
  }
  onBack() {
    this.navctr.back();
  }
}
