<?php
namespace App\Controllers\Seller;

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

use \App\Models\Entity\DataOrderSellerDb;

use \App\Controllers\Container;

class SellerOrderList extends Container
{
  //lista pedidos com parametro do seller get{warehouserId}
  public function get(Request $request, Response $response, $args){
    $route = $request->getAttribute('route');
    $id = $route->getArgument('id');

    //var_dump($id);

    $entityManager = $this->container->get('em');
    $ordersRepository = $entityManager->getRepository('App\Models\Entity\DataOrderSellerDb');
    $dql = "SELECT u FROM App\Models\Entity\DataOrderSellerDb u WHERE u.status<>:status AND u.status<>:statusTwo ORDER BY u.creationDate DESC";
    

    $entityManager = $this->container->get('em');
    $query = $entityManager->createQuery($dql);

    $query->setParameters([
      'status' => 'payment-pending',
      'statusTwo' => 'handling'
    ]);

    $results = $query->getResult();

    $order = $results;

    foreach ($order as $key => $ord) {
      if ($id == $ord->getWarehouseId()) {
        $orderSeller[] = $ord->getId();
      }
    }
    //var_dump($orderSeller);
    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;

  }

  //busca por data de inicio e fim
  public function orderdata(Request $request, Response $response, $args){
    $route = $request->getAttribute('route');
    $id = $route->getArgument('id');
    $start = $route->getArgument('start');
    $end = $route->getArgument('end');

    $today = new \DateTime();

    if ($end > $today->format('Y-m-d')) {
      throw new \Exception("data final maior que a data de hoje", 404);
    }

    if ($end < $start) {
      throw new \Exception("data final e menor que data inicial", 404);
    }
    
    //se data final for igual hoje, adiciona um dia para BETWEEN executar a query
    if ($end == $today->format('Y-m-d')) {
      $end = date('Y-m-d', strtotime($end . "+1 days"));
    }

    $dql = "SELECT u FROM App\Models\Entity\DataOrderSellerDb u WHERE u.warehouseId=:dock AND u.creationDate BETWEEN :start AND :final";
    

    $entityManager = $this->container->get('em');
    $query = $entityManager->createQuery($dql);

    $query->setParameters([
      'dock' => $id,
      'start' => $start,
      'final' => $end
    ]);

    $results = $query->getResult();

    return $response->withJson($results, 200)->withHeader('Content-type', 'application/json');
  }

  //lista pedido expecifico com parametro order{id}
  public function order(Request $request, Response $response, $args){
    $route = $request->getAttribute('route');
    $dock = $route->getArgument('id');
    $idOrder = $route->getArgument('order');

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

    foreach ($ordSeller as $ord) {
      if ($dock == $ord->getWarehouseId() && $idOrder == $ord->getOrderId()) {
        $idDB = $ord->getId();
      }
    }

    if (empty($idDB)) {
      throw new \Exception("Nao existe este pedido", 404);
    }

    $idSellerOrder = $orderSellerRepository->find($idDB);

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

//lista itens com orderId como parametro items{orderId}
  public function items(Request $request, Response $response, $args){
    $route = $request->getAttribute('route');
    $idOrder = $route->getArgument('order');

    $entityManager = $this->container->get('em');
    $itemsRepository = $entityManager->getRepository('App\Models\Entity\ItemsOrderDb');
    $items = $itemsRepository->findAll();

    foreach ($items as $i) {
      if ($idOrder == $i->getOrderId()) {
        $idOrderItem[] = $i->getId();
      }
    }

    if (empty($idOrderItem)) {
      throw new \Exception("Nao existe item no pedido", 404);
    }

    foreach ($idOrderItem as $key => $item) {
      $idItem[] = $itemsRepository->find($item);
    }

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