import { Component,ViewChild } from '@angular/core';
import { IonicPage, NavController, NavParams,Content,Slides , Events,Platform} from 'ionic-angular';
import { Storage } from '@ionic/storage';
import {Myservice} from '../../providers/myservice';


@IonicPage()
@Component({
  selector: 'page-list-vehicle',
  templateUrl: 'list-vehicle.html',
})
export class ListVehiclePage {

  currentIndex:any;
  slidertab:any;
  user_data:any;
  request_list:any;

  @ViewChild(Content) content: Content;
  @ViewChild('mySlider') slider: Slides;

  constructor(platform: Platform,public navCtrl: NavController, public navParams: NavParams,public storage:Storage, private myservice: Myservice,public events:Events) {
	    let backAction =  platform.registerBackButtonAction(() => {
        console.log("backPressed tabs page");
       platform.exitApp();
        backAction();
      },2)
	this.storage.get('user_data').then((user_data) => {
				console.log(user_data);
			if(user_data){
				this.user_data= user_data;
				this.get_quote_request()
			}else
				this.navCtrl.push('LoginPage');
	})
    let id = this.navParams.get("id");
    this.slidertab = id;
    console.log("id", id);
    setTimeout(() => {
    this.goToSlide(id);
    }, 500)
    this.slidertab = 0;
	events.subscribe('user:read_stat', userdetails => {
					console.log("read");
						this.get_quote_request();
						
				
					});
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ListVehiclePage');
  }

  goToSlide(id) {
    this.slider.slideTo(id, 500);
    }
    
    slideChanged() {
    let currentIndex = this.slider.getActiveIndex();
    this.slidertab = currentIndex;
    console.log("Current index is", currentIndex);
    }

    details(id){
      this.navCtrl.push('VehicleDetailPage',{'pool_id': id});
    }
	get_quote_request(){
		this.myservice.show_loader();
		this.myservice.load_get('get_quote_request/id/'+this.user_data.id).subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				this.request_list = data.result;
				console.log(this.request_list);
				this.events.publish('user:notifications', this.request_list);
			}
		});
	}
    

}
