import {Component, OnInit, ViewChild} from '@angular/core';
import {LoadingController, 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-ownersjob',
  templateUrl: './ownersjob.page.html',
  styleUrls: ['./ownersjob.page.scss'],
})
export class OwnersjobPage implements OnInit {
    @ViewChild('job_duration')  job_duration;
    @ViewChild('job_recipient') job_recipient;

  jobData: any;
  item_banner:string = 'default.jpg';
  session_image_server:string='';
  curr_session:string="";
  tabs = 'assignjob';
  constructor(
      private navctr: NavController,
      private router: Router,
      public sessionDataProviderService: SessionDataProviderService,
      private wrenchService: WrenchService,
      private loadingCtrl: LoadingController
  ) {

    this.jobData  =  this.router.getCurrentNavigation().extras.state?.jobData;
    this.session_image_server = this.router.getCurrentNavigation().extras.state.session_image_server;
    this.item_banner = this.jobData.banner;
    if (this.jobData== undefined){
      this.onBack();
    }
    //debugger;
  }
  onBack() {
      dispatchEvent(new Event("app-myjob-offers-clear-modals"));
    this.navctr.back();
  }
  ngOnInit() {
    this.curr_session =  this.sessionDataProviderService.session;
    //this.activeJobMsgList();
  }


    validateEmail(email) {
        var re = /\S+@\S+\.\S+/;
        return re.test(email);
    }
  async sendOfferToIndividual(){
    const loading = await this.loadingCtrl.create({
      message: 'Sending...',
      duration: 5000,
    });

      if ( this.job_recipient== undefined || !this.validateEmail( this.job_recipient)){
          this.job_recipient='';
          alert("Enter valid email");
          return;
      }

      this.reqData = {
          action:13025,
          member_id: this.sessionDataProviderService.member_id,
          uid: this.sessionDataProviderService.member_uid,
          sessionid: this.sessionDataProviderService.session,
          job_id: this.jobData.job_id,
          job_uid: this.jobData.job_uid,
          duration: 0,
          job_description: this.jobData.description,
          email: this.job_recipient,
          assign_mode: 110033
      };
    //  console.log('XXX-> ', this.reqData  );

    loading.present();

      this.wrenchService.assignTask(this.reqData).subscribe(
          assigResult => {
              this.assigResult = assigResult;
              console.log("assigResult  RETURN->", this.assigResult);
              if ( this.assigResult != undefined && this.assigResult.internal_return > 0){
                  this.assign_ind_result = "Task sent to the email - " +this.job_recipient;
                  this.job_recipient='';
                    const event = new Event("app-myjob-offers-refresh");
                    // Dispatch the event.
                    dispatchEvent(event);
                  setTimeout(() => {
                      this.assign_ind_result ='';
                  }, 3000);
              }
              else
              {

              }
          }
      );

  }

  assigResult:any;
  assign_ind_result:string='';
  assign_mak_result:string='';


    reqData: {
    action:number,
    member_id: number,
    uid: string,
    sessionid: string,
    job_id: string,
    job_uid: string,
    duration: number,
    job_description: string,
    email:string,
    assign_mode: number
  };

  /*
  'member_id' => int 1
  'sessionid' => string 'FF736CCF2D202B59164BD144152E6BF670EC1171FD54CF831966C0252B1F643D' (length=64)
  'uid' => string '3119b744-42ad-4834-bb83-b737588754ca' (length=36)
  'job_id' => string '98' (length=2)
  'job_uid' => string '473d3239-ecd7-4eb5-859c-78261720cb8e' (length=36)
  'duration' => int 10
  'job_description' => string 'Nitetronic Z6 Anti Snoring Pillow, Best Snore Stopper Device, Clinically Proven Snoring Solution for Snorers, Smart Stop Snoring Technology Suitable for All Sleepers' (length=165)
  'action' => int 13025
  'assign_mode' => int 110022
   */

  async sendOfferToMarket(){
if ( this.job_duration== undefined || this.job_duration < 1){
    alert("Select Duration");
    return;
}
    this.reqData = {
          action:13025,
          member_id: this.sessionDataProviderService.member_id,
          uid: this.sessionDataProviderService.member_uid,
          sessionid: this.sessionDataProviderService.session,
          job_id: this.jobData.job_id,
          job_uid: this.jobData.job_uid,
          duration: this.job_duration,
          job_description: this.jobData.description,
          email: '',
          assign_mode: 110022
    };
// console.log('XXX-> ', this.reqData  );
   // debugger;

    const loading = await this.loadingCtrl.create({
      message: 'Sending...',
      duration: 5000,
    });

    loading.present();

      this.wrenchService.assignTask(this.reqData).subscribe(
          assigResult => {
          this.assigResult = assigResult;
           console.log("assigResult  RETURN->", this.assigResult);
            if ( this.assigResult != undefined && this.assigResult.internal_return > 0){
                this.assign_mak_result = "Task sent to market";
                const event = new Event("app-myjob-offers-refresh");
                // Dispatch the event.
                dispatchEvent(event);
                setTimeout(() => {
                    this.assign_mak_result ='';
                }, 3000);
            }
            else
            {

            }
          }
      );

  }



    // assignTask(reqData){
    //     return this.getPostData("assigntask", reqData);
    // }

}
