<?php
namespace App\Controllers\Admin;

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

use \App\Controllers\Container;

class StatusOrders extends Container
{
  public function all(Request $request, Response $response, $args){

    $entityManager = $this->container->get('em');
    $ordersRepository = $entityManager->getRepository('App\Models\Entity\DataOrderSellerDb');
    $orders = $ordersRepository->findAll();

    if (empty($orders)) {
      throw new \Exception("Sem pedidos", 404);
    }

    $quantityOrders = count($orders);

    //listar para faturar
    foreach ($orders as $ord) {
      if ($ord->getStatus() == 'ready-for-handling') {
        $readyOrders[] = $ord->getId();
      }
    }

    if (!empty($readyOrders)) {
      $ready = count($readyOrders);
      $manuseio = $readyOrders;
    }else {
      $ready = 0;
    }

    //listar quantidade de pedidos faturados
    foreach ($orders as $ord) {
      if ($ord->getStatus() == 'invoiced') {
        $statusOrders[] = $ord->getId();
      }
    }

    if (!empty($statusOrders)) {
      $invoiced = count($statusOrders);
      $faturado = $statusOrders;
    }else {
      $invoiced = 0;
    }

    //listar quantidade de pedidos cancelados
    foreach ($orders as $ord) {
      if ($ord->getStatus() == 'canceled') {
        $canceledOrders[] = $ord->getId();
      }
    }
    if (!empty($canceledOrders)) {
      $canceled = count($canceledOrders);
      $cancelado = $canceledOrders;
    }else {
      $canceled = 0;
    }

    $return = $response->withJson(['orders'=>$quantityOrders,
                                    'invoiced'=>$invoiced,
                                    'canceled'=>$canceled,
                                    'ready'=>$ready], 200)
                      ->withHeader('Content-type', 'application/json');
    return $return;
  }

  public function get(Request $request, Response $response, $args){
    $route = $request->getAttribute('route');
    $action = $route->getArgument('action');

    $entityManager = $this->container->get('em');
    $ordersRepository = $entityManager->getRepository('App\Models\Entity\DataOrderSellerDb');
    $orders = $ordersRepository->findAll();

    switch ($action) {
      case 'invoiced':
      foreach ($orders as $ord) {
        if ($action == $ord->getStatus()) {
          $orderSeller[] = $ord->getId();
        }
      }
      if (empty($orderSeller)) {
        throw new \Exception("Sem pedidos", 404);
      }

      foreach ($orderSeller as $key => $o) {
        $idOrder[] = $ordersRepository->find($o);
      }

      $return = $response->withJson($idOrder, 200)->withHeader('Content-type', 'application/json');
      return $return;
        break;

      case 'canceled':
      foreach ($orders as $ord) {
        if ($action == $ord->getStatus()) {
          $orderSeller[] = $ord->getId();
        }
      }
      if (empty($orderSeller)) {
        throw new \Exception("Sem pedidos", 404);
      }

      foreach ($orderSeller as $key => $o) {
        $idOrder[] = $ordersRepository->find($o);
      }

      $return = $response->withJson($idOrder, 200)->withHeader('Content-type', 'application/json');
      return $return;
        break;

      case 'ready-for-handling':
      foreach ($orders as $ord) {
        if ($action == $ord->getStatus()) {
          $orderSeller[] = $ord->getId();
        }
      }
      if (empty($orderSeller)) {
        throw new \Exception("Sem pedidos", 404);
      }

      foreach ($orderSeller as $key => $o) {
        $idOrder[] = $ordersRepository->find($o);
      }

      $return = $response->withJson($idOrder, 200)->withHeader('Content-type', 'application/json');
      return $return;
        break;

      default:
        throw new \Exception("Sem pedidos", 404);
        break;
    }

  }
}
