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

@IonicPage()
@Component({
  selector: 'page-timing',
  templateUrl: 'timing.html',
})
export class TimingPage {
	schedule ={'start_sun' :'','start_mon' :'','start_tue' :'','start_wed' :'','start_thu' :'','start_fri' :'','start_sat' :'',
							'end_sun' :'','end_mon' :'','end_tue' :'','end_wed' :'','end_thu' :'','end_fri' :'','end_sat' :'',
							'start_break_sun' :'','start_break_mon' :'','start_break_tue' :'','start_break_wed' :'','start_break_thu' :'','start_break_fri' :'','start_break_sat' :'',
							'end_break_sun' :'','end_break_mon' :'','end_break_tue' :'','end_break_wed' :'','end_break_thu' :'','end_break_fri' :'','end_break_sat' :''
				};
	break ={};
	schedule_value ={};
	val_schedule ={};
	test_values ={};
	obj ={};
	visit_array =[];
	user_data:any;
	icons:any;
	constructor(public navCtrl: NavController, public navParams: NavParams,public storage:Storage, private myservice: Myservice) {
			this.storage.get('user_data').then((user_data) => {
				if(user_data){
					this.user_data=user_data
					console.log(this.user_data);
					 this.get_profile();
				}
		})
		 this.icons = 'camera';
		 this.schedule_value={"sun":{"visit":{"start":"" ,"end":"" },"break":{"start":"" , "end":""}}, "mon":{"visit":{
		 "start":"" , "end":""},"break":{"start":"","end":""}},
	"tue":{"visit":{"start":"","end":""},"break":{"start":"","end":""}},
	"wed":{"visit":{"start":"","end":""}, "break":{"start":"","end":""}}, 
	"thu":{"visit":{"start":"","end":""},"break":{"start":"","end":""}},
    "fri":{"visit":{"start":"","end":""},"break":{"start":"","end":""}},
	"sat":{"visit":{"start":"","end":""}, "break":{"start":"","end":""}}};
	 
			 this.test_values={"sun":{"visit":{"start":"" ,"end":"" },"break":{"start":"" , "end":""}}, "mon":{"visit":{
		 "start":"" , "end":""},"break":{"start":"","end":""}},
	"tue":{"visit":{"start":"","end":""},"break":{"start":"","end":""}},
	"wed":{"visit":{"start":"","end":""}, "break":{"start":"","end":""}}, 
	"thu":{"visit":{"start":"","end":""},"break":{"start":"","end":""}},
    "fri":{"visit":{"start":"","end":""},"break":{"start":"","end":""}},
	"sat":{"visit":{"start":"","end":""}, "break":{"start":"","end":""}}};
	}


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

  back(){
    this.navCtrl.pop();
  }

  specialize()
  {
    this.navCtrl.push('SpecializedPage');
  }
  
	SaveSchedule(){
		 					var test_val =false;
		 
		let empty_val = '{"start":"","end":""}';
		if(Object.keys(this.val_schedule).length > 0){
			Object.keys(this.schedule_value).forEach((key) => { 
				if(JSON.stringify(this.schedule_value[key].break) == empty_val) {
					this.schedule_value[key].break = this.val_schedule[key].break;
				}
				if(JSON.stringify(this.schedule_value[key].visit) == empty_val) {
					this.schedule_value[key].visit = this.val_schedule[key].visit;
				}
			});
		}
		
		Object.keys(this.schedule_value).forEach((key) => { 
			if(!test_val){
				 console.log(JSON.stringify(this.schedule_value[key].visit));
				 console.log(key);
				if(JSON.stringify(this.schedule_value[key].visit) == empty_val) {
					
				}else{
					test_val = true;
					
				}
				}
			}); 
		 
	  console.log("length" , test_val);
		console.log(this.schedule_value);
	 var myJSON = JSON.stringify(this.schedule_value);
	 if(test_val){
	  this.myservice.show_loader();
		this.myservice.load_post({'timing' :myJSON , 'id':this.user_data.id},"save_schedule").subscribe(response => {
			this.myservice.hide_loader();
			if(response.status == "error") {
				this.myservice.show_alert("error!", response.message);
			}
			else {
				 
				this.myservice.show_alert("success!", response.message);
				this.navCtrl.push('ListVehiclePage');
				 
			}
		})  
		}
		else{
			this.myservice.show_alert("error!", "Please fill atleast one value");
		}
	
  }
  
  get_profile(){
  
  	this.myservice.show_loader();
		this.myservice.load_get('get_profile/id/'+this.user_data.id).subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				var res = data.data;
				console.log(res);
				if(res.schedule){
				this.val_schedule =res.schedule;
				
				let empty_val = '{"start":"","end":""}';
				if(Object.keys(res.schedule).length > 0){
					Object.keys(this.schedule_value).forEach((key) => { 
						if(JSON.stringify(this.schedule_value[key].break) == empty_val) {
							this.schedule_value[key].break = res.schedule[key].break;
						}
						if(JSON.stringify(this.schedule_value[key].visit) == empty_val) {
							this.schedule_value[key].visit = res.schedule[key].visit;
						}
					});
				}
				
				 
				this.schedule.start_sun=this.val_schedule['sun'].visit.start;
				this.schedule.start_mon=this.val_schedule['mon'].visit.start;
				this.schedule.start_tue=this.val_schedule['tue'].visit.start;
				this.schedule.start_wed=this.val_schedule['wed'].visit.start;
				this.schedule.start_thu=this.val_schedule['thu'].visit.start;
				this.schedule.start_fri=this.val_schedule['fri'].visit.start;
				this.schedule.start_sat=this.val_schedule['sat'].visit.start;
				
				this.schedule.end_sun=this.val_schedule['sun'].visit.end;
				this.schedule.end_mon=this.val_schedule['mon'].visit.end;
				this.schedule.end_tue=this.val_schedule['tue'].visit.end;
				this.schedule.end_wed=this.val_schedule['wed'].visit.end;
				this.schedule.end_thu=this.val_schedule['thu'].visit.end;
				this.schedule.end_fri=this.val_schedule['fri'].visit.end;
				this.schedule.end_sat=this.val_schedule['sat'].visit.end;
				
				this.schedule.start_break_sun=this.val_schedule['sun'].break.start;
				this.schedule.start_break_mon=this.val_schedule['mon'].break.start;
				this.schedule.start_break_tue=this.val_schedule['tue'].break.start;
				this.schedule.start_break_wed=this.val_schedule['wed'].break.start;
				this.schedule.start_break_thu=this.val_schedule['thu'].break.start;
				this.schedule.start_break_fri=this.val_schedule['fri'].break.start;
				this.schedule.start_break_sat=this.val_schedule['sat'].break.start;
				this.schedule.end_break_sun=this.val_schedule['sun'].break.end;
				this.schedule.end_break_mon=this.val_schedule['mon'].break.end;
				this.schedule.end_break_tue=this.val_schedule['tue'].break.end;
				this.schedule.end_break_wed=this.val_schedule['wed'].break.end;
				this.schedule.end_break_thu=this.val_schedule['thu'].break.end;
				this.schedule.end_break_fri=this.val_schedule['fri'].break.end;
				this.schedule.end_break_sat=this.val_schedule['sat'].break.end;
				 }else{
					this.schedule ={'start_sun' :'','start_mon' :'','start_tue' :'','start_wed' :'','start_thu' :'','start_fri' :'','start_sat' :'',
							'end_sun' :'','end_mon' :'','end_tue' :'','end_wed' :'','end_thu' :'','end_fri' :'','end_sat' :'',
							'start_break_sun' :'','start_break_mon' :'','start_break_tue' :'','start_break_wed' :'','start_break_thu' :'','start_break_fri' :'','start_break_sat' :'',
							'end_break_sun' :'','end_break_mon' :'','end_break_tue' :'','end_break_wed' :'','end_break_thu' :'','end_break_fri' :'','end_break_sat' :''
							}
				 }
			}
		});
  }
 date_fetch(evt,day,type,types){
	
	console.log("day", day)
	console.log("evt", evt)
	console.log("type", type)
	console.log("types", types)
	 
 
	/* if(Object.keys(this.obj).length >0){
		console.log("ffg");
		if(type in this.obj[type]){
		Object.assign( this.obj ,{ [type]: evt}  );
		}
	 }else{
		this.obj ={[type]:evt}
	 }
		 
	 console.log("obj" ,this.obj)
	*/
	 
		 console.log( this.schedule_value[day][types][type]);
		 var obj = {};
		 if(this.schedule_value[day][types][type]!=''){
			console.log("in");
			obj = this.schedule_value[day][types]
				Object.assign( obj ,{ [type] : evt}  );
		 }
		 else{
			console.log("else");
			obj = {[type]:evt};
		 }
				
				if(day in this.schedule_value){
					 if(types== 'visit' || this.schedule_value[day].visit.start!= '' || this.schedule_value[day].visit.end!= '')
						Object.assign( this.schedule_value[day] ,{ [types] : obj}  );
					else{
						var start_val = "start_break_"+day; 
						var end_val = "end_break_"+day; 
						this.schedule[start_val]= null
						this.schedule[end_val]= null
						this.myservice.show_alert("Warning", "Please add Visit Timing First" );
						}
				}
				else {  
					if(types== 'visit')
						Object.assign( this.schedule_value,{ [ day] : {[types] : obj} }  );
					else{
						var start_val = "start_break_"+day; 
						var end_val = "end_break_"+day; 
						this.schedule[start_val]= null
						this.schedule[end_val]= null
						this.myservice.show_alert("Warning", "Please add Visit Timing First" );
						}
				}
  
	console.log( this.schedule_value);
	
	}
	
	/*
	
		if(day in this.schedule_value){
					 
					if(types== 'visit' || this.schedule_value[day].visit.start!= '' || this.schedule_value[day].visit.end!= ''){
						if(this.obj)
						Object.assign( this.schedule_value[day] ,{ [types] : this.obj}  );
					}
						 
				}
				else {
					if(types== 'visit'){
					
						Object.assign( this.schedule_value,{ [day] : {[types] : this.obj} }  );
						}
				}  */

}
