import { ActivatedRoute, Params } from '@angular/router';
import { Component, OnInit } from '@angular/core';
import { AlertService, QuotesService, UserService } from '../_services/';
import { Quote } from '../_models/quote';

@Component({
  selector: 'app-quote-detail',
  templateUrl: './quote-detail.component.html',
  styleUrls: ['./quote-detail.component.css']
})
export class QuoteDetailComponent implements OnInit {

	quoteId: number;
	quote: Quote;
	seller: any;
	rearGlass: any = null;
	sideGlass: any;

	constructor(private quotesService: QuotesService, private userService: UserService, private route: ActivatedRoute, private alertService: AlertService) { }

	ngOnInit() {
		this.quoteId = this.route.snapshot.params['id'];
		this.getQuote();
	}

	getQuote(){
		this.quotesService.getById(this.quoteId).subscribe((quote: any) => {
			this.quote = quote;
			
			if(this.quote.rearGlass != ""){
				this.rearGlass = JSON.parse(this.quote.rearGlass);
			}

			if(this.quote.sideGlass != ""){
				this.sideGlass = JSON.parse(this.quote.sideGlass);
			}

			if(this.quoteId != null){
				this.getSeller();
			}
		});
	}

	getSeller(){
		this.userService.getById(this.quote.userId).subscribe((user: any) => {
			this.seller = user;
		});
	}

}
