<?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 StockDBController extends Container
{
  public function get(Request $request, Response $response, $args){
    // env.php define variaveis
    $token = getenv('token');
    $appKey = getenv('appkey');

    $warehouseId = 'acreSergio';

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

    foreach ($stock as $s) {
      $sku[] = $s->getSkuVtex();
      $quantityFabric[] = $s->getQuantityFabric();
    }

    //var_dump($sku);
    //var_dump(count($quantityFabric));

    $arrayCount = count($quantityFabric);

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

    for ($i=1; $i < $arrayCount; $i++) {

      $curl = curl_init();

      curl_setopt_array($curl, array(
        CURLOPT_URL => "https://logistics.myvtex.com/api/logistics/pvt/inventory/skus/$sku[$i]/warehouses/$warehouseId?an=arsdesign",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => false,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "PUT",
        CURLOPT_POSTFIELDS =>"{\n    \"unlimitedQuantity\": false,\n    \"dateUtcOnBalanceSystem\": null,\n    \"quantity\": {$quantityFabric[$i]},\n    \"timeToRefill\": 0\n}",
        CURLOPT_HTTPHEADER => array(
          "Content-Type: application/json; charset=utf-8",
          "Accept: application/json",
          "X-VTEX-API-AppToken: {$token}",
          "X-VTEX-API-AppKey: {$appKey}"
        ),
      ));

      $response = curl_exec($curl);
      $err = curl_error($curl);

      curl_close($curl);

      if ($err) {
        echo "cURL Error #:" . $err;
      } else {
        echo $response . "<br>" .$i;
      }

    }
















/*
    $multCurl = array();
    $res = array();
    $curl = curl_multi_init();
*/

/*
    foreach ($sku as $key => $item) {
      $fetchURL = "https://logistics.myvtex.com/api/logistics/pvt/inventory/skus/$item/warehouses/$warehouseId?an=arsdesign";
      $multCurl[$key] = curl_init();

      curl_setopt_array($multCurl[$key], array(
      CURLOPT_URL => $fetchURL,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => false,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "PUT",
      CURLOPT_POSTFIELDS =>"{\n    \"unlimitedQuantity\": false,\n    \"dateUtcOnBalanceSystem\": null,\n    \"quantity\": {$quantityFabric[$key]},\n    \"timeToRefill\": 0\n}",
      CURLOPT_HTTPHEADER => array(
        "Content-Type: application/json; charset=utf-8",
        "Accept: application/json",
        "X-VTEX-API-AppToken: {$token}",
        "X-VTEX-API-AppKey: {$appKey}"
      ),
    ));
    curl_multi_add_handle($curl, $multCurl[$key]);
    }

    $index = null;
    do {
      curl_multi_exec($curl,$index);
    } while ($index > 0);

    foreach ($multCurl as $k => $ch) {
      $res[$k] = curl_multi_getcontent($ch);
      curl_multi_remove_handle($curl, $ch);
      $arrSkus = json_decode($res[$k]);

      var_dump($arrSkus);
    }

*/

  }
}
