<?php

namespace App\Controllers\DB;

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

use \App\Controllers\Container;
use \App\Models\Entity\StockDb;

class TableItemsController extends Container
{
  public function table(Request $request, Response $response, $args){

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

    if (($handle = fopen("itens-atual.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

          //var_dump($data[0]);
            $arq = (new StockDb())->setSkuVtex($data[0])
                                  ->setNameProduct($data[1])
                                  ->setSizeProduct($data[2])
                                  ->setSkuBhan($data[3])
                                  ->setCodGroup($data[4])
                                  ->setEan($data[5]);

          $entityManager->persist($arq);
          $entityManager->flush();

          //var_dump($arq);
        }
        fclose($handle);
        //$return = $response->withJson($arq, 201)->withHeader('Content-type', 'application/json');
        //return $return;
    }


  }
}
