<?php

namespace Modules\Additional\Specifique\Client30\API\SharePoint\Traits;

use GuzzleHttp\Utils;
use Helpers\Log\LogSysteme;

trait DA
{

    public function createDA($DAnum, $DAChantierNum)
    {

        // echo fopen('C:\\Users\\bba\\Pictures\\eliquide-tabac-virginia-10ml.jpg.png', 'r');
 
        $curl = curl_init();
        
        curl_setopt_array($curl, array(
          CURLOPT_URL => $this->urlApi."api/da",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_PROXY => $this->proxy,
          CURLOPT_PROXYPORT => $this->proxyPort,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "POST",
          CURLOPT_POSTFIELDS =>"{\n    \"DAChantierNum\": \"".$DAChantierNum."\",\n    \"DANum\": \"".$DAnum."\"\n}",
          CURLOPT_HTTPHEADER => array(
            "Content-Type: application/json",
            "x-functions-key: ".$this->XFUNCTIONSKEY
          ),
        ));
        
        $response = curl_exec($curl);
        
        $httpcode = curl_getinfo($curl);
        
        curl_close($curl);
        if ($httpcode['http_code']!= 200) {
            $this->code  = $httpcode['http_code'].'|'.$response;
        } else {
            $this->code  = $httpcode['http_code'];
        }
        $this->setData($response);

        return $this->code;


        
        $body ='{
        
            "DANum": "'.$DAnum.'",
            "DAChantierNum": "'.$DAChantierNum.'"
        }';

        try {
            $response = $this->client->post($this->urlApi.'api/da', [
                'headers' => [
                    'x-functions-key' => $this->XFUNCTIONSKEY,
                    'Content-Type' => 'application/json',
                ],'body' => $body
            ]);

            $this->code = $response->getStatusCode();

            $this->setData(json_decode($response->getBody()->getContents(), true));
            return $this->code;
        } catch (\Throwable $th) {
            // echo $th->getCode();
            // echo $th->getMessage();
            return $th->getCode().'|'.$th->getMessage();
        }
    }

    public function updateDA(
        string $DAnum,
        $files = [],
        string $purchaser = '',
        string $expedition = '',
        string $country = '',
        string $center = ''
    ) :mixed {
        $count = 0;
        $response = '';

        $body = Utils::jsonEncode([
            "DANum" => $DAnum,
            "Purchaser" => $purchaser,
            "Expedition" => $expedition,
            "Country" => $country,
            "Center" => $center,
        ]);
        foreach ($files as $key => $value) {
            if (!is_string($value)) {
                LogSysteme::prefixErrorLog('Invalid value '.var_export($value, true));
                continue;
            }
            $count++;
            $multipartFile = '$arrayFiles = array(';
            $a = explode('/', $value);
            $endArray = end($a);
            $a = explode('\\', $endArray);
            $nomFichier = end($a);
            $multipartFile.=" 'files' =>  new \\CURLFILE(".var_export($value, true).", '',".var_export($nomFichier, true)."),";
            
            $multipartFile .="'da' => ".var_export($body, true).");";

            eval($multipartFile);
            
            $curl = curl_init();
    
            curl_setopt_array($curl, array(
            CURLOPT_URL => $this->urlApi."api/da",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_PROXY => $this->proxy,
            CURLOPT_PROXYPORT => $this->proxyPort,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "PUT",
            // @phpstan-ignore-next-line
            CURLOPT_POSTFIELDS => $arrayFiles,
            CURLOPT_HTTPHEADER => array(
                "x-functions-key: ".$this->XFUNCTIONSKEY,
            ),
            ));
            
            $response = curl_exec($curl);
            $httpcode = curl_getinfo($curl);
    
            curl_close($curl);
        }
        if ($count == 0) {
            $multipartFile = '$arrayFiles = array(';
            
            $multipartFile .="'da' => ".var_export($body, true).");";
            eval($multipartFile);
            $curl = curl_init();
    
            curl_setopt_array($curl, array(
            CURLOPT_URL => $this->urlApi."api/da",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_PROXY => $this->proxy,
            CURLOPT_PROXYPORT => $this->proxyPort,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "PUT",
            // @phpstan-ignore-next-line
            CURLOPT_POSTFIELDS => $arrayFiles,
            CURLOPT_HTTPHEADER => array(
                "x-functions-key: ".$this->XFUNCTIONSKEY,
            ),
            ));
            
            $response .= curl_exec($curl);
    
            $httpcode = curl_getinfo($curl);
            curl_close($curl);
        }

        if ($httpcode['http_code']!= 200) {
            $this->code  = $httpcode['http_code'].'|'.$response;
        } else {
            $this->code  = $httpcode['http_code'];
        }
        $this->setData($response);

        return $this->code;

        // Debut du OLD
        foreach ($files as $key => $value) {
            $multipartFile[] = [
                'name'     =>'files',
                'contents' => fopen($value, 'r')
            ];
        }
        $multipartFile[] =[
            'name'     => 'da',
            'contents' => $body
        ];
        
        try {
            $response = $this->client->put($this->urlApi.'api/da', [
        

                'headers' => [
                    'x-functions-key' => $this->XFUNCTIONSKEY
                ],
                'multipart' => $multipartFile
                
            ]);

            $this->code = $response->getStatusCode();
            $this->setData(json_decode($response->getBody()->getContents(), true));
            return $this->code;
        } catch (\Throwable $th) {
            echo $th->getCode();
            echo $th->getMessage();
            return $th->getCode().'|'.$th->getMessage();
        }
    }
    
    public function deactivateDA($DAnum)
    {
        
        $curl = curl_init();
        
        curl_setopt_array($curl, array(
          CURLOPT_URL => $this->urlApi."api/da/".$DAnum,
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_PROXY => $this->proxy,
          CURLOPT_PROXYPORT => $this->proxyPort,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "PATCH",
          CURLOPT_HTTPHEADER => array(
            "Content-Type: application/json",
            "x-functions-key: ".$this->XFUNCTIONSKEY,
            "Content-Type: application/json"
          ),
        ));
        
        $response = curl_exec($curl);
        
        $httpcode = curl_getinfo($curl);
        
        curl_close($curl);
        if ($httpcode['http_code']!= 200) {
            echo $this->code  = $httpcode['http_code'].'|'.$response;
        } else {
            echo $this->code  = $httpcode['http_code'];
        }
        $this->setData($response);

        return $this->code;



        try {
            $response = $this->client->patch($this->urlApi.'api/da/'.$DAnum, [
        

                'headers' => [
                    'x-functions-key' => $this->XFUNCTIONSKEY,
                    'Content-Type' => 'application/json',
                ]
            ]);

            $this->code = $response->getStatusCode();
            $this->setData(json_decode($response->getBody()->getContents(), true));

            return $this->code;
        } catch (\Throwable $th) {
            echo $th->getCode();
            echo $th->getMessage();
            return $th->getCode().'|'.$th->getMessage();
        }
    }
}
