import {Component, OnInit, ViewChild} from '@angular/core';
import {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";
import {WaitingInterestService} from "../../store/waiting-interest.service";


@Component({
  selector: 'app-marketdetail',
  templateUrl: './marketdetail.page.html',
  styleUrls: ['./marketdetail.page.scss'],
})
export class MarketdetailPage implements OnInit {
  @ViewChild('yourmessage') yourmessage;
  tabs = 'lesson';
  session_image_server:string='';
  curr_session:string='';

  jobData: any;
  constructor(
      private navctr: NavController,
      private router: Router,
      public sessionDataProviderService: SessionDataProviderService,
      private wrenchService: WrenchService,
      private waitingInterestService:WaitingInterestService,
      private loadingCtrl: LoadingController
  ) {

    this.jobData  =  this.router.getCurrentNavigation().extras.state;
    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();
    }
  }

  interestStatus: boolean= false;
  interestDate: string ='10-10-2020';
  ngOnInit() {
    this.session_image_server = this.sessionDataProviderService.session_image_server;
    this.curr_session=this.sessionDataProviderService.session;
    this.interestStatus = this.waitingInterestService.interestStatus(this.jobData, this.waitingInterestService.interestJobsData);
    this.interestDate = this.waitingInterestService.interestDate;
  }


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

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

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

  reqData: {
    action:number, member_id: number, uid: string, sessionid: string, msg_type:string,
    offer_code:string,
    yourmessage:string
  };
  interest_status:string = "";
  interest_msg_status:string = "";

  InterestReqResult:any;
  async sendJobInterestMessage(){
    const loading = await this.loadingCtrl.create({
      message: 'Sending Message',
      duration: 3000,
    });

    this.reqData = {action:13033,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session ,
      msg_type: "JOB",
      offer_code:this.jobData.offer_code,
      yourmessage:''}

    loading.present();
    this.wrenchService.sendMarketInterest(this.reqData).subscribe(
        reqResult => {
          this.InterestReqResult = reqResult;
          console.log("jobManagerJobsTotalData  RETURN->", this.InterestReqResult);
          this.interest_status =  this.InterestReqResult?.status;
          this.waitingInterestService.getInterestList();
        }
    );

  }

  //yourmessage:string='';
  sendJobmarketMessage(){
    if (this.yourmessage.length < 5){
      alert("Enter Message to send");
      return;
    }

    this.reqData = {action:13036,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session ,
      msg_type: "JOB",
      offer_code:this.jobData.offer_code,
      yourmessage: this.yourmessage
    }

    this.wrenchService.sendMarketMessage(this.reqData).subscribe(
        reqResult => {
          this.InterestReqResult = reqResult;
          console.log("jobManagerJobsTotalData  RETURN->", this.InterestReqResult);
          this.interest_msg_status =  this.InterestReqResult?.status;
          this.waitingInterestService.getInterestList();
        }
    );

  }

}
