import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { URL } from '../mocks/url';

@Injectable({
	providedIn: 'root'
})
export class OrderService {

	constructor(
		private http: HttpClient
	) { }

	async createOrder(userId: any, userCart: any) {
        let cart: any = userCart;

        cart.app_user = {
            user_id: userId
        };

		const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + localStorage.access_Token
			})
		};

		return await this.http.post(URL + '/api/maioraservice/orders/v1/create/', cart, httpOptions).toPromise();
	}

	async getOrders(id: any) {
		const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + localStorage.access_Token
			})
		};

		return await this.http.get(URL + '/api/maioraservice/orders/getorders/user_id/' + id, httpOptions).toPromise();
	}

    async generateInvoice(data: any) {
        const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + localStorage.access_Token
			})
		};

		return await this.http.post(URL + '/Payment/v1/invoice/create', data, httpOptions).toPromise();
    }
}
