import { Component, OnInit, Input } from '@angular/core'; import { OrderService } from '../services/order.service'; import * as moment from 'moment'; @Component({ selector: 'app-orders', templateUrl: './orders.component.html', styleUrls: ['./orders.component.scss'] }) export class OrdersComponent implements OnInit { showRejectionPopup: boolean = false; orderList: any = []; tempOrderList: any = []; orderStatus: any = []; searchTerm: string = ''; showFilter: boolean = false; filterOption: { orderStatus: string, orderstatus_id: number } = { orderStatus: 'All', orderstatus_id: 0 }; @Input() params: any; constructor( private orderService: OrderService ) { } ngOnInit() { if (this.params) { try { switch(this.params.filterType) { case 'pending': this.filterOption = { orderStatus: 'Pending', orderstatus_id: 6 }; break; case 'delivered': this.filterOption = { orderStatus: 'Delivered', orderstatus_id: 3 }; break; case 'confirmed': this.filterOption = { orderStatus: 'Accepted', orderstatus_id: 1 }; break; default: this.filterOption = { orderStatus: 'All', orderstatus_id: 0 }; break; } } catch { this.filterOption = { orderStatus: 'All', orderstatus_id: 0 }; } } this.orderService.getOrders().then((data) => { this.orderList = data; this.orderList = this.orderList.filter((order) => { return order.orderstatus; }); this.tempOrderList = this.orderList.filter((order) => { return order.orderstatus; }); }, () => { alert("Error getting the orders"); }); this.orderService.getallOrderStatus().then((data: any) => { this.orderStatus = data; this.orderStatus.push({ orderStatus: 'All', orderstatus_id: 0 }); }, () => { alert("Error getting the order statuses"); }); } getFormattedDate(date: any, format: string) { return moment(date).format(format); } getFixedDecimalPoints(price: string | number) { return Number(price).toFixed(2); } getOrderStatus(id: string | number) { return this.orderStatus.find((status: any) => { return id === status.orderstatus_id }); } updateOrder(order) { this.orderService.updateOrder(order).then((data) => { console.log(data); }, (err) => { console.log(err) alert("Failed to update order"); }) } getFilteredOrders(id: number, orders?: any) { if (id > 0) { return this.tempOrderList.filter((order) => { return order.orderstatus.orderstatus_id === id; }); } else { return this.tempOrderList; } } searchOrders() { this.tempOrderList = this.orderList.filter((order: any) => { return order.orders_id.toString().toLowerCase().includes(this.searchTerm.toLowerCase()); }); } }