import { Component, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { ApiService } from 'app/api.service';
import { MatDialog } from '@angular/material';
import { OrdersAddComponent } from 'app/orders-add/orders-add.component';
import { OrdersPackingComponent } from 'app/orders-packing/orders-packing.component';

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

  public filter: any = { name: '', to: new Date(), from: new Date(), delivery_to: new Date(), delivery_from: new Date() };
  public orders: any = [];
  constructor(private title: Title, public dialog: MatDialog, public apiService: ApiService) {
    this.title.setTitle("Pedidos");
    this.filter.from.setDate(this.filter.from.getDate() - 3);
    this.filter.delivery_from.setDate(this.filter.delivery_from.getDate() - 3);
    this.filter.delivery_to.setDate(this.filter.delivery_to.getDate() + 3);
    this.filterdata();
  }

  ngOnInit() {
  }
  filterdata(){
    this.apiService.getOrders(this.filter.name, this.filter.from, this.filter.to, this.filter.delivery_from,this.filter.delivery_to).subscribe((data:any) => {
      this.orders = data.data;
    });
  }
  editOrder(index:number) {
    let data = this.orders[index];
    data.type = 'edit';
    data.delivered_program = data.delivered_program.replace(' ','T');
    const dialogRef = this.dialog.open(OrdersAddComponent, {
      width: '80%',
      data: data
    });
    dialogRef.afterClosed().subscribe(result => { this.filterdata(); });
  }
  nullOrder(index:number) {
    let id = this.orders[index].id;
    let confirm = this.apiService.confirm("Confirme acción","Anular el pedido reversará la mercadería a la bodega de ventas, además de dejar el saldo pagado por el cliente en su cuenta como saldo disponible, ¿Desea continuar?");
    confirm.then((data:any) => {
      if (data.dismiss=="cancel") { }
      else if (data.value) {
        this.apiService.nullOrder(id).subscribe((data:any)=>{
          if (data.res == "OK") {
            this.orders[index].status = '666';
          } else {
            this.apiService.showError("Error",data.msg);
          }
        });
      }
    })
  }
  procesar(index:number) {
    let id = this.orders[index].id;
    let confirm = this.apiService.confirm("Confirme acción","¿Procesar entrega, generar packing y asignar a repartidor?");
    confirm.then((data:any) => {
      if (data.dismiss=="cancel") { }
      else if (data.value) {
        this.apiService.processOrder(id).subscribe((data:any)=>{
          if (data.res == "OK") {
            this.orders[index].status = '7';
            this.apiService.showOk("Procesado","Pedido procesado");
          } else {
            this.apiService.showError("Error",data.msg);
          }
        });
      }
    })
  }
  packing(index:number) {
    let data = this.orders[index];
    data.type = 'edit';
    const dialogRef = this.dialog.open(OrdersPackingComponent, {
      width: '80%',
      data: data
    });
    dialogRef.afterClosed().subscribe(result => { this.filterdata(); });
  }
  getStatus(status:string) {
    if (status == '0') { return 'Programado'; }
    else if (status == '3') { return 'Sin asignar'; }
    else if (status == '7') { return 'Asignado'; }
    else if (status == '10') { return 'En ruta'; }
    else if (status == '15') { return 'Entregado'; }
    else if (status == '-1') { return 'Devuelto'; }
    else if (status == '666') { return 'Anulado'; }
    return 'Desconocido';
  }

}
