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

@Component({
  selector: 'app-familypending',
  templateUrl: './familypending.component.html',
  styleUrls: ['./familypending.component.scss'],
})
export class FamilypendingComponent implements OnInit {
  session_image_server:string='';
  curr_session:string="";
  constructor(
      public router: Router,
      public sessionDataProviderService: SessionDataProviderService,
      private alertController:AlertController,
      private wrenchService: WrenchService) { }

  ngOnInit() {
    this.getMyOffersData();
  }
  async startNewTasks(item){

    // this.sessionDataProviderService.DestroySessionOnLogout();
    // this.router.navigate(['login']);
    //this.requestLogout();

    const alert = await this.alertController.create({
      header: "Start Now ?",
      message: "Confirm you are start : "+ item.title ,
      buttons: [
        {
          text: "Cancel",
          role: "cancel",
          handler: () => {
            this.getMyOffersData();
          },
        },
        {
          text: "Start Now",
          handler: () => {
            this.sendOfferResponse(item, 100); // accepted
           // this.getMyOffersData();
          },
        },
      ],
    });
    await alert.present();
  }
  responseData: {
    action:number,
    member_id: number,
    uid: string,
    sessionid: string,
    offer_result:number,
    contract:string,
    offer_code:string
  };
  offerRespData:any;
  sendOfferResponse(item, respType){

    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
    }
    this.wrenchService.offferResponse(this.responseData).subscribe(
        offerRespData => {
          this.offerRespData = offerRespData;
          console.log("offerRespData  RETURN->", this.offerRespData);
          this.getMyOffersData();
        }
    );
  }

  usrData: {
    action:number, member_id: number, uid: string,
    sessionid: string,
    limit:20,
    page:1, offset:number
  };
  total_offers:number = 0;
  myOffersTotalData:any;
  myOffersData: [];
  getMyOffersData(){
    this.curr_session =  this.sessionDataProviderService.session;
    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.wrenchService.getMyOffers(this.usrData).subscribe(
        myOffersTotalData => {
          this.myOffersTotalData = myOffersTotalData;
          console.log("myOffersTotalData  RETURN->", this.myOffersTotalData);
          this.session_image_server = this.myOffersTotalData.session_image_server;
          this.myOffersData = this.myOffersTotalData.result_list;
          console.log("myOffersData RETURN DATA->", this.myOffersData);
          this.total_offers =  this.myOffersData.length;
        }
    );

  }
}
