<?php
namespace App\Controllers\Admin;

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

use \App\Controllers\Container;

class ClientOrderList extends Container
{
  // lista todos os sellers parceiros
  public function all(Request $request, Response $response, $args){

    $entityManager = $this->container->get('em');
    $sellerRepository = $entityManager->getRepository('App\Models\Entity\Admin\ClientSeller');
    $sellers = $sellerRepository->findAll();

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

  // lista pedidos por seller
  public function get(Request $request, Response $response, $args){
    $route = $request->getAttribute('route');
    $warehouseId = $route->getArgument('warehouseId');

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

    foreach ($orderSeller as $s) {
      if ($warehouseId == $s->getWarehouseId()) {
        $idSeller[] = $s->getId();
      }
    }

    if (empty($idSeller)) {
      throw new \Exception("Seller sem pedidos", 404);
    }

    foreach ($idSeller as $id) {
      $client[] = $orderSellerRepository->find($id);
    }

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