import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams ,ViewController ,ModalController, Events} from 'ionic-angular';
import {Validators, FormBuilder, FormGroup, FormControl,FormArray } from '@angular/forms';
import { Storage } from '@ionic/storage';
import {Myservice} from '../../providers/myservice';

@IonicPage()
@Component({
  selector: 'page-offered',
  templateUrl: 'offered.html',
})
export class OfferedPage {

	todo = {}
	 
	services_offered:any;
	selected_services:any;
	user_data:any;
	timing:any;
	schedule:any;
	today_dt:any;
	page:any;
	offers:any;
	garage_id:any;
	tom:any;
	book_message:any;
	service_message:any;
	device_id:any;
	total_price:any=0;
	total_taxprice:any=0;
	quote_val:any;
	services_pg:any;
	service_selected=[];
	myForm: FormGroup;
	bookvals = {'book_date': '', 'book_time': '','user_id':'','service_id':'','service' : [],'total_price' : ''};
	
	constructor(public navCtrl: NavController, public navParams: NavParams, private formBuilder: FormBuilder, public storage: Storage, private myservice: Myservice,public viewCtrl: ViewController,public modalCtrl: ModalController,public events: Events) {
	this.storage.get('user_data').then((user_data) => {
		if(user_data){
			this.user_data =user_data;
		}
	})
	if(this.navParams.get('offers')){
		this.offers = this.navParams.get('offers');
		this.offers = this.offers.offers
		 this.total_price = (parseInt(this.offers.total_price))
			
			 
		 
		console.log("offers" ,this.offers )
	}
	if(this.navParams.get('services')){
		this.services_pg = this.navParams.get('services');
		 
		console.log("services_pg" ,this.services_pg )
	}
	
	this.storage.get('get_deviceId').then((deviceId) => {
				console.log(deviceId);
				if(deviceId){
					this.device_id = deviceId.userId
				}
				else
					this.device_id = '';
			})
		this.page = this.navParams.get('page');
		this.garage_id = this.navParams.get('garage_id')
		console.log(this.garage_id);
		this.timing = this.navParams.get('timing');
		this.schedule = this.timing.timing;
		this.book_message = false;this.service_message = false;
		this.quote_val = this.navParams.get('quote_val');
		if(this.page = 'quotes' )
			this.total_price = (parseInt(this.quote_val.price))
		console.log(this.quote_val);
		var tdyDate = new Date();
		var tdys = new Date(tdyDate);
		tdys.setDate(tdyDate.getDate());
		this.today_dt = tdys.toISOString();
		 
		var nextDays = new Date(tdyDate);
		nextDays.setDate(tdyDate.getDate()+1);
		this.tom = nextDays.toISOString();
		
		this.get_selected_services()
		this.get_services()
		
		this.myForm = formBuilder.group({
			services: this.formBuilder.array([])
		})  
		
		if(this.page == 'offers'){
			var offers = this.navParams.get('offers');
			console.log("offers",offers);
			/* this.service_selected.push({'service_id':offers.offers.garage_service_id,'offer_id':offers.offers.id, 'service_charge':offers.offers.price,
			'price':offers.offers.discounted_price, 'discount' : offers.offers.discount,'service':offers.offers.service,'garage_id':this.garage_id}); */
		}

	}
	

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

	back(){
		this.navCtrl.pop();
	}
	timings(){
		this.navCtrl.push('TimingPage')
	}
	
	get_services(){
		this.myservice.show_loader();
		this.myservice.load_get('get_services').subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				this.services_offered = data.result;
				
					 
				console.log(this.services_offered)
			}
		});
	}
	
		get_selected_services(){
		this.myservice.show_loader();
		this.myservice.load_get('get_selected_services/id/'+this.garage_id).subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				this.selected_services = data.result;
				 
			}
		});
	}
	
	
	updateCheckedOptions(serv, isChecked: boolean) {
	 console.log(this.total_price)
	  
		 this.total_taxprice = 0
		if(isChecked){
			this.total_price = parseInt(serv.price)+parseInt(this.total_price) 
			//this.total_taxprice =  ((parseInt(this.total_price) * 20)/100)+(parseInt(this.total_price))
		}else{
			this.total_price =parseInt(this.total_price) - parseInt(serv.price)
			//this.total_taxprice =  ((parseInt(this.total_price) * 20)/100)+(parseInt(this.total_price))
		}
		 
		this.service_selected.push({'service_id':serv.id,'service':serv.service,'service_desc':serv.description,'price':serv.price,'garage_id':this.garage_id});
		const services = <FormArray>this.myForm.controls.services;
		services.push(new FormControl(serv));
		/* if(isChecked) {
			} else {
			let index = services.controls.findIndex(x => x.value.id == serv.id);
			services.removeAt(index);
			} 
		*/
	}
	
	/*add_tax(){
		this.total_price = ((parseInt(this.total_price) * 20)/100)+(parseInt(this.total_price))
	}*/
	
	select_time(dates,times){
		console.log("userdata " ,this.user_data)
		if(this.user_data){	
		this.bookvals = {'book_date': dates, 'book_time': times,'user_id':this.user_data.id,'service_id':'','service' : [],'total_price' : ''};
		console.log(this.bookvals);
		}else{
		
		
		let modal = this.modalCtrl.create('Loginbook');
		modal.onDidDismiss(data => {
			 console.log(data);
			 if(data){
			if(data.user_data.id) {
				this.user_data = data.user_data;
				this.bookvals = {'book_date': dates, 'book_time': times,'user_id':data.user_data.id,'service_id':'','service' : [],'total_price' : ''};
				console.log(this.bookvals);  
			 }}
		});
		modal.present();
		}
	}
	
	book(){
	 
		 var obj = {'service' : this.service_selected,'total_price' :this.total_price}
		 this.bookvals = Object.assign(this.bookvals, obj);
		 
		 if(this.page == 'quotes'){
			 
			 this.bookvals = Object.assign(this.bookvals, this.quote_val ); 
			 
		} if(this.page == 'offers'){
			 
			 this.bookvals = Object.assign(this.bookvals, this.offers ); 
			 
		}
		 console.log(this.bookvals);
		 if(this.bookvals){ 
			 if(this.bookvals.service.length>=0){ 
		 if(this.bookvals.book_time && this.bookvals.service[0].service_id){
			this.book_message = false;
			this.service_message = false;
			
		
		 this.navCtrl.push('OverviewPage',{'bookvals':this.bookvals,'garage_name':this.navParams.get('garage_name'),'garage_id':this.garage_id,'page' : this.page});
		 }
		else{
			if(this.bookvals.book_time==undefined && this.bookvals.service[0].service_id==undefined){
				this.book_message = true;this.service_message = true;
			}
			else{
		if(this.bookvals.book_time=='' || this.bookvals.book_time==undefined){
				this.book_message = true;
				this.service_message = false;
				 
				}
			if(this.bookvals.service[0].service_id=='' || this.bookvals.service[0].service_id==undefined){
				this.service_message = true;
				this.book_message = false;
				 
				 }
				}
			 
		 }}
		 
		 }
		 
	 
	}
	
	
	logForm(form){
		 
		 var arr=[];
		 for (var i = 0; i < form.services.length; i++) { 
			 console.log(form.services[i])
			 arr[i]={ 'garage_id' :this.user_data.id , 'service_id' : form.services[i].id };
			 
		}
		this.myservice.show_loader();
		this.myservice.load_post({arr},'add_services').subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				this.navCtrl.push('Services')	
			}
			else{
				this.myservice.show_alert("Error", data.message);
			}
		});
		 
	}
	
		get_next_dates(dates,status){ 
	
		var nextDay = new Date(dates);
		if(status=='next')
			nextDay.setDate(nextDay.getDate() + 1);
		else if(status=='prev')
			nextDay.setDate(nextDay.getDate() - 1);
		else
			nextDay.setDate(nextDay.getDate());
		var	MyDateString =nextDay.getFullYear() + '-' + ('0' + (nextDay.getMonth()+1)).slice(-2) + '-' + ('0' + nextDay.getDate()).slice(-2);
		var date_string= nextDay.toLocaleDateString();
		var arr = [];
		arr = date_string.split("/");
		this.timing.dates = MyDateString;
		console.log(MyDateString);
		let dates1 =arr[2]+"-"+arr[0]+"-"+arr[1];
		
		var obj = {
			id: this.garage_id,
			date: MyDateString
		};
		 
		this.myservice.show_loader();
		this.myservice.load_post(obj, "getshopDetails_by_date").subscribe(response => {
			this.myservice.hide_loader();
			if(response.status=='success') {
				this.schedule =response.result.timing.timing;
				
			}
			else {
				this.myservice.show_alert("Error!", response.message);
			 
			  
			}
		}) 
	}
}
