<?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 ItemsAll extends Container {

    public function all(Request $request, Response $response, $args) {
        $route = $request->getAttribute('route');
        $dock = $route->getArgument('dock');
        $entityManager = $this->container->get('em');
        $userSellerNew = $entityManager->getRepository('App\Models\Entity\Seller\UserSeller');
        $queryUserSeller = $userSellerNew->createQueryBuilder('t')->select('t.id')->where("t.warehouseId = '" . $dock . "'")->getQuery();
        $usersdd = $queryUserSeller->getResult();
        $usersdd[0]['id'];
        $stockHasRepositoryNew = $entityManager->getRepository('App\Models\Entity\StockHasUserSeller');



        $stockRepository = $entityManager->getRepository('App\Models\Entity\StockDbNew');
        $queryProductsSeller = $stockHasRepositoryNew->createQueryBuilder('t')->select('t.quantidade,t.idStock,al.nameProduct,al.ean,al.sizeProduct,al.skuBhan,al.codGroup')->innerJoin('App\Models\Entity\StockDbNew', 'al')->where("t.idUserSeller = '" . $usersdd[0]['id'] . "'")->andWhere('t.idStock = al.id')->getQuery();
        $productsSeller = $queryProductsSeller->getResult();
        $products = $stockRepository->findAll();

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

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

}
