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

@IonicPage()
@Component({
  selector: 'page-quote',
  templateUrl: 'quote.html',
})
export class QuotePage {
	
	pool_id:any;
	request_val:any;
	total_price:any=0;
	price_val:any=0;
	labor_val:any=0;
	tested:any[];
	
	private quoteForm : FormGroup;
	parts = [{name:"",price:""}]
	labour = [{name:"",price:""}]

	constructor(public navCtrl: NavController, public navParams: NavParams,public storage:Storage, private vs: ValidationService,private myservice: Myservice, private formBuilder: FormBuilder) {
		this.pool_id = this.navParams.get('pool_id');
		if(this.pool_id)
			this.getSingleQuote();
			
		this.quoteForm = this.formBuilder.group({ 
			parts: this.formBuilder.array([]),
			labour: this.formBuilder.array([]),
			description: ['', Validators.required],
			 
		}); 
		
		this.patchValues();
		this.patchlabours();
		
		    /* const Med = <FormArray>this.quoteForm.controls['parts'];
		 
		 Med.valueChanges.subscribe(value => {
			 this.price_val = parseInt(value[0].price)+this.price_val;
      console.log("changes",this.price_val)  
    });
		   
		   const restt = <FormArray>this.quoteForm.controls['labour'];
		 
		 restt.valueChanges.subscribe(value => {
			 console.log(value)
			 this.price_val = parseInt(value)+this.price_val;
      console.log("changeslabour",this.price_val)  
    });
	 */
	this.tested = [];
	
	}

	ionViewDidLoad() {
		console.log('ionViewDidLoad QuotePage');
	}
	
	get_total(){
		 
		this.price_val = 0;
		this.tested.forEach(x=>{
			this.price_val = parseInt(this.price_val)+parseInt(x);
		})
		console.log(this.price_val);
	}	
	total_val(){
		alert("HH")
		console.log("hello");
		console.log(this.price_val);
		console.log(this.labor_val);
			this.price_val = parseInt(this.price_val)+parseInt(this.labor_val);
		
		console.log(this.price_val);
	}
	
	get_price(amount,index){
		var labour_price = this.quoteForm.controls['labour'].value;
		console.log('labour_price' , labour_price);
		var amounts = parseInt(labour_price);
		 
		this.price_val = 0;
		//if(amount!= NaN){
			this.tested[index] = amount;
			 
			this.tested.forEach(x=>{
				console.log(x);
				if(parseInt(x)>0)
					this.price_val = parseInt(this.price_val)+parseInt(x);
			})
			if(labour_price.length>0){
				labour_price.forEach(x=>{
				console.log(x);
				if(parseInt(x.price)>0)
					 
					this.price_val = parseInt(x.price)+parseInt(this.price_val);
			})
				 
			}
			
			this.price_val = ((this.price_val*20)/100)+this.price_val;
						
			console.log(this.price_val);
		//}
		/*if(amount!=''){
			this.price_val = this.price_val + amount;
		}
		console.log(this.price_val);*/
	}
	
	get_labor(amount,index){
		var parts = this.quoteForm.controls['parts'].value;
		console.log('parts' , parts);
		 
		 
		this.price_val = 0;
		//if(amount!= NaN){
			this.tested[index] = amount;
			 
			this.tested.forEach(x=>{
				console.log(x);
				if(parseInt(x)>0)
					this.price_val = parseInt(this.price_val)+parseInt(x);
			})
			if(parts.length>0){
				parts.forEach(x=>{
				console.log(x);
				if(parseInt(x.price)>0)
					 
					this.price_val = parseInt(x.price)+parseInt(this.price_val);
			})
				 
			}
			
			this.price_val = ((this.price_val*20)/100)+this.price_val;
						
			console.log(this.price_val);
		//}
		/*if(amount!=''){
			this.price_val = this.price_val + amount;
		}
		console.log(this.price_val);*/
	}
	
	/*

	get_price(amount,index){
		var labour_price = this.quoteForm.controls['labour'].value;
		console.log('labour_price' , labour_price);
		var amounts = parseInt(labour_price);
		 
		this.price_val = 0;
		//if(amount!= NaN){
			this.tested[index] = amount;
			 
			this.tested.forEach(x=>{
				console.log(x);
				if(parseInt(x)>0)
					this.price_val = parseInt(this.price_val)+parseInt(x);
			})
			if(amounts>0){
				console.log(amounts);
				this.price_val = amounts+parseInt(this.price_val);
			}
			
			this.price_val = ((this.price_val*20)/100)+this.price_val;
						
			console.log(this.price_val);
		//}
		/*if(amount!=''){
			this.price_val = this.price_val + amount;
		}
		console.log(this.price_val);//
	}	get_labor(amount){
		var labour_price = this.quoteForm.controls['labour'].value;
		this.+ = 0;
		/*var price_vals =  this.price_val
		console.log("in");
		this.price_val = 0;
		this.tested[0] = amount
		
		if(amount!= NaN){
			
		 
		 this.price_val = parseInt(this.tested[0])+parseInt(price_vals);
		 
		} //
		this.tested.forEach(x=>{
			console.log(x);
			if(parseInt(x)>0)
				this.price_val = parseInt(this.price_val)+parseInt(x);
		})
		
		amount = parseInt(amount);
		
		if(amount>0){
			this.price_val = parseInt(amount)+parseInt(this.price_val);
		}
		
		
		this.price_val = ((this.price_val*20)/100)+this.price_val;
		
		console.log(this.price_val);
		 
		/*if(amount!=''){
			this.price_val = this.price_val + amount;
		}
		console.log(this.price_val); //
	} */
	
	patchValues() {
		const Med = <FormArray>this.quoteForm.controls['parts'];
		
		this.parts.forEach(x => {
			Med.push(this.patch(x.name))
		})
	}	

   patch(name) {
		return this.formBuilder.group({
			name: ['',Validators.required],
			price: ['', Validators.compose([Validators.required, Validators.minLength(1), Validators.maxLength(6)])],
		})
	}
	
	addParts() {
		const control = <FormArray>this.quoteForm.controls['parts'];
		control.push(this.initParts())
	}  
	
	initParts() {
		return this.formBuilder.group({
			name: ['',Validators.required],
			price: ['', Validators.compose([Validators.required, Validators.minLength(1), Validators.maxLength(6)])],
		})
	} 
	
	patchlabours() {
		 
		const lab = <FormArray>this.quoteForm.controls['labour'];
		 
		this.labour.forEach(x => {
			lab.push(this.labpatch(x.name))
		}) 
	}
	
	labpatch(name) {
		return this.formBuilder.group({
			name: ['',Validators.required],
			price: ['', Validators.compose([Validators.required, Validators.minLength(1), Validators.maxLength(6)])],
		})
	}
	
	addlabour() {
		const control = <FormArray>this.quoteForm.controls['labour'];
		control.push(this.initlabour())
	}  
	
	initlabour() {
		return this.formBuilder.group({
			name: ['',Validators.required],
			price: ['', Validators.compose([Validators.required, Validators.minLength(1), Validators.maxLength(6)])],
		})
	} 

	back(){
		this.navCtrl.pop();
	}
	
	
	
	save_quote(val){
		 
		var element = <HTMLInputElement> document.getElementById("quote_button");
		element.disabled = true;
		var tax = ((parseInt(this.price_val) * 20)/100)
		//this.price_val  =  tax +  parseInt(this.price_val);
		    setTimeout(() => {
  
			 
		Object.assign(val,{'total_price' : this.price_val,'tax' : tax})
		var myJSON = JSON.stringify(val);
		var objs = Object.assign({}, { 'quote' : myJSON, 'pool_id' : this.pool_id ,'unique_id' : this.request_val.unique_id })
		console.log(objs);
		this.myservice.show_loader();
		this.myservice.load_post(objs,'save_quote').subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				this.myservice.show_alert("Success", data.message);
				this.navCtrl.setRoot('SuccessPage');
			}
			else{
				this.myservice.show_alert("Error", data.message);
			}
		});
		}, 3000)
	}
	
	/*get_total(val){
		console.log(val);
		this.total_price = parseInt(this.total_price)+parseInt(val.target.value)
		console.log(this.total_price);
		
		 
	} */
	
	getSingleQuote(){
		this.myservice.show_loader();
		this.myservice.load_get('getSingleQuote/id/'+this.pool_id).subscribe(data => {
			this.myservice.hide_loader();
			if(data.status == 'success'){
				this.request_val = data.result;
			}
				
		});
	}

}
