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

	price_val = [];
	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,private alertCtrl: AlertController) {
		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')
	}
	add_services(){
		this.navCtrl.push('OfferedPage');
	}
	
	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;
				 this.price_val=data.result;
			}
		});
	}
	
	
	updatePrice(){
		var test_val =false;
		console.log(this.services_offered.length);
		console.log(this.price_val.length);
		 Object.keys(this.price_val).forEach((key) => { 
			
				 console.log(this.price_val[key].price);
				 console.log(key);
				if((this.price_val[key].price !=undefined) && (this.price_val[key].price !='')) {
					test_val = true;
				}else
					test_val =false;
				 
			}); 
	
		if(test_val){
		 this.myservice.show_loader();
		this.myservice.load_post(this.price_val,'updatePrice').subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				this.navCtrl.push('Selected_services')
			}
			else{
				this.myservice.show_alert("Error", data.message);
			}
		}); 
		}
	}
	delete_service(id){
	
		 
  let alert = this.alertCtrl.create({
    title: 'Delete Service!',
    message: 'Sure you want to delete service?',
    buttons: [
      {
        text: 'Back',
        role: 'cancel',
        handler: () => {
          console.log('no clicked');
        }
      },
      {
        text: 'Delete Service',
        handler: () => {
          this.myservice.show_loader();
		this.myservice.load_get('deleteService/id/'+id).subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				this.navCtrl.push('Selected_services')
			}
			else{
				this.myservice.show_alert("Error", data.message);
			}
		}); 
        }
      }
    ]
  });
  alert.present();

		
	}
	
	
	updateCheckedOptions(serv:Object, evt) {
    
	  var obj = {'price' : evt.target.value};
	  Object.assign(serv,obj)
	 
	  if(this.price_val.length >0){
		if(this.contains(this.price_val,serv) == false)
			this.price_val.push(serv);
		}
	else
		 this.price_val[0] = serv;
	}
	
	 contains(a, obj) {
      for (var i = 0; i < a.length; i++) {
          if (JSON.stringify(a[i]) === JSON.stringify(obj)) {
              return true;
          }
      }
      return 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);
			}
		});
		 
	}
}
