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

@Component({
  selector: 'app-market-messages',
  templateUrl: './market-messages.component.html',
  styleUrls: ['./market-messages.component.scss'],
})
export class MarketMessagesComponent implements OnInit {
  @Input('jobData') jobData:any;
  offer_code:string='';

  constructor(      public sessionDataProviderService: SessionDataProviderService,
                    private wrenchService: WrenchService,
                    private router: Router) {

    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){
     return;
    }
    this.offer_code =  this.jobData.offer_code;

    addEventListener('app-market-messages-refresh', () => {
      this.marketMessageList();
    });
  }

  ngOnInit() {
    this.marketMessageList();
  }




  reqData: {
    action:number,
    member_id: number,
    uid: string,
    sessionid: string,
    page:number,
    offer_code:string,
    limit: number
  };

messageTotalResult:any;
messageResult:any;
  async marketMessageList(){

    this.reqData = {
      action:15045,
      member_id: this.sessionDataProviderService.member_id,
      uid: this.sessionDataProviderService.member_uid,
      sessionid: this.sessionDataProviderService.session,
      page: 0,
      offer_code: this.offer_code,
      limit: 20
    };


    this.wrenchService.marketMessages(this.reqData).subscribe(
        messageTotalResult => {
          this.messageTotalResult = messageTotalResult;
          console.log("messageTotalResult  RETURN->", this.messageTotalResult);
          this.messageResult = this.messageTotalResult.result_list;

        }
    );

  }
}
