import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } 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-selected_services',
  templateUrl: 'selected_services.html',
})
export class Selected_services {

	todo = {}
	services_offered:any;
	user_data:any;
	private priceForm : FormGroup;
	
	constructor(public navCtrl: NavController, public navParams: NavParams, private formBuilder: FormBuilder, 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_services()
				}
		})
		
	}
	

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

	back(){
		this.navCtrl.pop();
	}
	timing(){
		this.navCtrl.push('TimingPage')
	}
	edit_services(){
		this.navCtrl.push('Services')
	}

	login(){
		this.navCtrl.push("AvailablePage");
	}
	
	get_services(){
		this.myservice.show_loader();
		this.myservice.load_get('get_selected_services/id/'+this.user_data.id).subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				this.services_offered = data.result;
			}
		});
	}
	
	
	addPrice(){
		this.myservice.show_loader();
		this.myservice.load_post(this.services_offered,'addPrice').subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				this.navCtrl.push('TimingPage')
			}
			else{
				this.myservice.show_alert("Error", data.message);
			}
		});
	}
	
	updateCheckedOptions(serv:Object, evt) {
      console.log(serv)
	  var obj = {'price' : evt.target.value};
	  Object.assign(serv,obj)
	  //serv.price =evt.target.value
     
	}
	 
	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);
			}
		});
		 
	}
}
