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";
import {FamilysuggestPendingService} from "../../store/familysuggest-pending.service";
import {UserWalletService} from "../../store/user-wallet.service";

@Component({
  selector: 'app-pending-suggested',
  templateUrl: './pending-suggested.component.html',
  styleUrls: ['./pending-suggested.component.scss'],
})
export class PendingSuggestedComponent implements OnInit {
    familyPendingData: [];
    isDisabled:boolean= true;

  constructor(      private navctr: NavController,
                    private router: Router,
                    public sessionDataProviderService: SessionDataProviderService,
                    private wrenchService: WrenchService,
                    public familysuggestPendingService: FamilysuggestPendingService,
                    private loadingCtrl: LoadingController,
                    public userWalletService:UserWalletService) {

    addEventListener('app-pending-suggested-closemodal', () => {
      this.setCloseModal();
    });

  }

  ngOnInit() {
    //this.getFamilyPeningData();
    this.familyPendingData = this.familysuggestPendingService.familyPendingData;
    //this.userWalletService.getWalletData();

  }
  suggestion_total:number=0;
  usrData: {
    action:number, member_id: number, uid: string, sessionid: string, limit:20, page:1,offset: 0
  };

  //familyPendingTotalData:any;
 // familyPendingData: [];
  // getFamilyPeningData(){
  //   this.usrData = {action:22010,
  //     member_id: this.sessionDataProviderService.member_id,
  //     uid: this.sessionDataProviderService.member_uid,
  //     sessionid: this.sessionDataProviderService.session ,
  //     limit:20, page:1,offset: 0}
  //
  //   this.wrenchService.getFamilyPeningList(this.usrData).subscribe(
  //       familyPendingTotalData => {
  //         this.familyPendingTotalData = familyPendingTotalData;
  //         console.log("familyPendingTotalData  RETURN->", this.familyPendingTotalData);
  //         this.familyPendingData = this.familyPendingTotalData.result_list;
  //         // debugger;
  //         console.log("FAMILY familyPendingData DATA->", this.familyPendingData);
  //         this.suggestion_total = this.familyPendingData.length;
  //       }
  //   );
  //
  // }
  timelineOptions:any= [
    {val:0,tx:'Select Timeline'},
    {val:1,tx:'1 Day'},
    {val:2,tx:'2 Days'},
    {val:3,tx:'3 Days'},
    {val:4,tx:'4 Days'},
    {val:5,tx:'5 Days'},
    {val:6,tx:'6 Days'},
    {val:7,tx:'1 Week'},
    {val:14,tx:'2 Weeks'},
    {val:21,tx:'3 Weeks'},
    {val:28,tx:'4 Weeks'},
  ];
  isModalOpen:boolean = false;
  isModalOpenBackdrop:boolean = false;
selectedItem:any;
  modalTile:string='';
    processFamilyPending(item){
      console.log("timelineOptions == ",this.timelineOptions);
      console.log("SUGGESTED ITEM - >", item);
      this.selectedItem = item;
      this.walletData = this.userWalletService.walletData;
      this.modalTile = item.firstname+ ' Suggested Task';
      this.price =0;
      this.timeline_days = 0;
      this.isModalOpen = true;
      this.isModalOpenBackdrop= true;
    }

  setCloseModal(){
    this.isModalOpen = false;
    setTimeout(()=>{
      this.isModalOpenBackdrop= false;
    },1000);
  }

  reqData: {
    action:number,
    member_id: number,
    uid: string,
    sessionid: string,
    family_uid: string,
    category : string,
    banner: string,
    title: string,
    timeline_days: number,
    description: string,
    job_detail:string,
    country: string,
    price:number,
    assign_mode: number
  };

@ViewChild('timeline_days') timeline_days;
// @ViewChild('title') title;
// @ViewChild('job_description') job_description;
@ViewChild('job_detail')  job_detail;
@ViewChild('country')  country;
@ViewChild('price') price;

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

  async processTask(){
    if ( this.timeline_days== undefined || this.timeline_days < 1){
      alert("Select Duration");
      return;
    }

    this.reqData = {
      action:13025,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session,
      family_uid: this.selectedItem.family_uid,
      category:'',
      banner: this.selectedItem.banner,
      title: this.selectedItem.title,
      timeline_days: this.timeline_days,
      description: this.selectedItem.description,
      job_detail: this.job_detail,
      country: this.country,
      price: this.price*100,
      assign_mode: 110055
    };
//  console.log('XXX-> ', this.reqData  );
//  debugger;
// return;
    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.";
           // const event = new Event("app-myjob-offers-refresh");
            // Dispatch the event.

            //dispatchEvent(event);
            this.familysuggestPendingService.getFamilyPeningData();
            setTimeout(() => {
              this.assign_mak_result ='';
              this.setCloseModal();
            }, 5000);
          }
          else
          {

          }
        }
    );

  }
  testInputs(){
    console.log("this.walletData=>",this.walletData);
    this.isDisabled = true;
    if (
        this.price != undefined &&
        this.timeline_days != undefined &&
        this.country != undefined &&
        this.job_detail !=undefined &&
        this.isNumeric(this.price) &&
        this.isNumeric(this.timeline_days) &&
        this.price> 0
        && this.country !=''
        && this.job_detail !=''
        && this.timeline_days != 0
    ){
      this.isDisabled = false;
    }
//isDisabled
  }
   isNumeric(value) {
    return /^-?\d+$/.test(value);
  }

}
