import { Component, OnInit } from '@angular/core';
import { OrderService } from '../services/order.service';
import * as moment from 'moment';
import { ICart } from '../cart/cart.page';
import { Router } from '@angular/router';

@Component({
    selector: 'app-orders',
    templateUrl: './orders.component.html',
    styleUrls: ['./orders.component.scss'],
})
export class OrdersComponent implements OnInit {
	userOrders: any = [];
	selected_order: any;
	selected_rating: number = 0;
    userInfo: any;
    show_rate_modal: boolean = false;
    userCart: ICart = {
        orderedlist: []
    };

    constructor(
        private orderService: OrderService,
        private router: Router
    ) { }

    ngOnInit() {
        this.userInfo = JSON.parse(localStorage.userInfo)['User Info'];

		this.orderService.getOrders(this.userInfo.id).then((resp: any)=> {
			this.userOrders = resp.reverse();
		}, (err) => {
			console.log(err);
		});
	}

	getFormattedDate(date, format) {
		return moment(date).format(format);
    }
    
    reOrder(orderedlist: any) {
        for (let i = 0; i < orderedlist.length; i += 1) {
            for (let j = 0; j < orderedlist[i].menuitems.length; j += 1) {
                this.userCart.orderedlist.push({
                    mall_id: orderedlist[i].mall_id,
                    outlet_id: orderedlist[i].outlet_id,
                    menuitem_id: orderedlist[i].menuitems[j].menuitem_id,
                    quantity: orderedlist[i].menuitems[j].item_quantity,
                    pickup_time: orderedlist[i].pickup_time,
                    take_away: true,
                    order_status: null,
                    total_price: orderedlist[i].total_price,
                    soft_delete: false,
                });
            }            
        }        
        localStorage.userCart = JSON.stringify(this.userCart);
        this.router.navigate(['/cart']);
        console.log(this.userCart);
    }

}
