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

@Component({
  selector: 'app-myjob-offers',
  templateUrl: './myjob-offers.component.html',
  styleUrls: ['./myjob-offers.component.scss'],
})
export class MyjobOffersComponent implements OnInit {
  @Input('jobData') jobData:any;
  isModalOpen:boolean = false;
  isModalOpenBackdrop:boolean = false;
  total_item:number = 0;

  tabs = 'about';
  session_image_server:string='';
  curr_session:string="";
  constructor(
      private navctr: NavController,
      private router: Router,
      public sessionDataProviderService: SessionDataProviderService,
      private wrenchService: WrenchService
  ) {

    addEventListener('app-myjob-offers-refresh', () => {
      this.getPendingOffersData();
    });

    addEventListener('app-myjob-offers-clear-modals', () => {
      this.setCloseModal();
    });

    this.getPendingOffersData();
  }

  ngOnInit() {
   // console.log('AMEYE===>', this.jobData);
  }
  modal_item_title:string='';
  modal_item_description:string='';
  modal_item_job_detail:string='';

  currentJobSelected:any;

  openDetailModal(item){

    this.currentJobSelected = item;

    this.modal_item_title= item.title;
    this.modal_item_description= item.description;
    this.modal_item_job_detail=item.job_detail;

    this.isModalOpen = true;
    this.isModalOpenBackdrop= true;
  }
  setCloseModal(){
    this.modal_item_title= '';
    this.modal_item_description= '';
    this.modal_item_job_detail='';
    this.isModalOpen = false;
    setTimeout(()=>{ this.isModalOpenBackdrop= false;},1000);
}
  usrData: {
    action:number, member_id: number, uid: string, sessionid: string, limit:20, page:1,offset: 0
  };

  jobManagerOffersTotalData:any;
  jobManagerOffersData: [];
  async getPendingOffersData(){
    this.curr_session =  this.sessionDataProviderService.session;
    this.usrData = {action:11309,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session ,
      limit:20, page:1,offset: 0}

    this.wrenchService.jobmanageroffers(this.usrData).subscribe(
        jobManagerOffersTotalData => {
          this.jobManagerOffersTotalData = jobManagerOffersTotalData;
          console.log("jobManagerOffersTotalData  RETURN->", this.jobManagerOffersTotalData);
            if (this.jobData !=undefined && this.jobData.job_uid !=''){
              this.jobManagerOffersData = this.jobManagerOffersTotalData.result_list.filter((item)=>item.job_uid==this.jobData.job_uid);
            }
            else{
              this.jobManagerOffersData = this.jobManagerOffersTotalData.result_list;
            }
            this.total_item = this.jobManagerOffersData.length;

          this.session_image_server = this.jobManagerOffersTotalData.session_image_server;

          // debugger;
          console.log("jobManagerOffersData RETURN DATA->", this.jobManagerOffersTotalData);
        }
    );

  }
}
