<?php
namespace App\Controllers\Seller\Warehouse;

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

use \App\Controllers\Container;

class ItemsSeller extends Container
{
    public function searchgrid(Request $request, Response $response, $args){
        // env.php define variaveis
        $token = getenv('token');
        $appKey = getenv('appkey');

        $route = $request->getAttribute('route');
        $dock = $route->getArgument('dock');
        $group = $route->getArgument('item');

        $entityManager = $this->container->get('em');
        $stockRepository = $entityManager->getRepository('App\Models\Entity\StockDb');
        $stock = $stockRepository->findAll();

        foreach ($stock as $s) {
            if ($group == $s->getCodGroup()) {
                $product[] = $s->getId();
            }
        }

        if (empty($product)) {
            throw new \Exception("Item nao cadastrado na base", 404);
        }

        foreach ($product as $key => $o) {
            $items[] = $stockRepository->find($o);
        }
      
        return $response->withJson($items, 200)->withHeader('Content-type', 'application/json');
        
    }
}