import { Inject, Component, OnInit, OnDestroy, ViewEncapsulation} from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { UntypedFormBuilder, Validators, UntypedFormGroup, FormControl } from '@angular/forms';
import { LineItem } from '../lineitem.model';
import { Order } from '../../order/order.model';

import { LanguageService } from '../../language.service';
import { LanguageData } from '../../language.model';

@Component({
	selector: 'app-item-add',
	templateUrl: './item-add.component.html',
	styleUrls: ['./item-add.component.css']
})

export class ItemAddComponent implements OnInit {

	orderForm: UntypedFormGroup;
	lineItem: LineItem;
	//order: Order;
    //
    // Language Support
    //
    vE = {
    	itemaddAdd2Order : '',
    	itemaddCancelBtn : '',
    	itemaddSaveBtn : ''
    }
    //
    // ------------------------------------------------------------
	constructor(
		private languageService : LanguageService,
		private fb: UntypedFormBuilder,
		private dialogRef: MatDialogRef<ItemAddComponent>,
		@Inject(MAT_DIALOG_DATA) public data: LineItem
	) {
		this.orderForm = fb.group({
			qty: [data.qty, Validators.required]
		});
		this.lineItem = {
			qty : data.qty,
			name : data.name,
			cost : data.cost
		}
	}

	ngOnInit() {
        this.chgUILanguage();
	}

	close() {
		this.dialogRef.close();
	}

	save() {
		this.lineItem.qty = this.orderForm.value.qty;
		this.dialogRef.close(this.lineItem);
	}

	chgUILanguage(){
        console.log('ITEMADD.chgUILanguage Called ');
 		this.languageService.getViewElements('itemadd').subscribe(
				response => { 
					console.log('Received >> ', response );
					console.log('Language Code : ', response.language);
					console.log('Component Name : ',response.component);
					console.log('Language data : ',response.data);

			        this.vE.itemaddAdd2Order = response.data.itemadd_add_2order;
			    	this.vE.itemaddCancelBtn = response.data.itemadd_cancel_btn;
			    	this.vE.itemaddSaveBtn = response.data.itemadd_save_btn;
			    },
				error  => { console.log('Error >> ',error); }
			);
	}

}
