import { Component, OnInit } from '@angular/core';
import {AlertController, LoadingController, NavController} from "@ionic/angular";
import {Router} from "@angular/router";
import { WrenchService } from 'src/app/services/wrench.service';
import {SessionDataProviderService} from "../../store/session-data-provider.service";
@Component({
  selector: 'app-joboffers',
  templateUrl: './joboffers.page.html',
  styleUrls: ['./joboffers.page.scss'],
})
export class JoboffersPage implements OnInit {


  tabs = 'about';

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

  ngOnInit() {
    this.getMyOffersData();
  }

  onBack() {
    this.navctr.back();
  }

  onMentor() {
    this.router.navigate(['mentor']);
  }

  onEnroll() {
    this.router.navigate(['enroll']);
  }

  session_image_server:string='';
  curr_session:string='';
  //
  usrData: {
    action:number, member_id: number, uid: string,
    sessionid: string,
    limit:20,
    page:1, offset:number
  };
  total_offers:number = 0;
  myOffersTotalData:any;
  myOffersData: [];
  async getMyOffersData(){
    this.usrData = {action:15010,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session ,
      limit:20, page:1,offset: 0}

    this.curr_session =  this.sessionDataProviderService.session;
    this.wrenchService.getMyOffers(this.usrData).subscribe(
        myOffersTotalData => {
          this.myOffersTotalData = myOffersTotalData;
          this.session_image_server = this.myOffersTotalData.session_image_server;
          console.log("myOffersTotalData  RETURN->", this.myOffersTotalData);
          this.myOffersData = this.myOffersTotalData.result_list;
          console.log("myOffersData RETURN DATA->", this.myOffersData);
          this.total_offers =  this.myOffersData.length;
        }
    );

  }


  async requestStart(item) {
    const alert = await this.alertController.create({
      header: "Ready to start",
      message: "You will start the task now, offer shall be completed in allocated timeline.",
      buttons: [
        {
          text: "Cancel",
          role: "cancel",
          handler: () => {
            this.getMyOffersData();
          },
        },
        {
          text: "Start Now",
          handler: () => {
            this.sendOfferResponse(item, 100);
            this.getMyOffersData();
          },
        },
      ],
    });
    await alert.present();
  }

  async requestReject(item) {
    const alert = await this.alertController.create({
      header: "Confirm Reject",
      message: "This task will go away, you dont have to do anything.",
      buttons: [
        {
          text: "Cancel",
          role: "cancel",
          handler: () => {
            this.getMyOffersData();
          },
        },
        {
          text: "Reject",
          handler: () => {
            this.sendOfferResponse(item, 333);
            this.getMyOffersData();
          },
        },
      ],
    });
    await alert.present();
  }

  responseData: {
    action:number,
    member_id: number,
    uid: string,
    sessionid: string,
    offer_result:number,
    contract:string,
    offer_code:string
  };
/*
'offer_result' => int 100
  'offer_code' => string 'W047P6BRRQ' (length=10)
  'contract' => string 'W047P6BRRQ' (length=10)
 */

  offerRespData:any;
  // offferResponse(respData)
  async sendOfferResponse(item, respType){
    const loading = await this.loadingCtrl.create({
      message: 'Processing',
      duration: 3000,
    });
    this.responseData = {
      action:15010,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session ,
      offer_result:respType,
      contract:item.contract,
      offer_code: item.contract
    };
    loading.present();
    this.wrenchService.offferResponse(this.responseData).subscribe(
        offerRespData => {
          this.offerRespData = offerRespData;
          console.log("offerRespData  RETURN->", this.offerRespData);
          this.getMyOffersData();
        }
    );
  }

}
