<?php

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

use Helpers\Log\LogSysteme;

trait OP
{

    public function createOP($DAnum, $files = [])
    {
        
        $count = 0;
        $response = '';
        

        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 .=");";

            eval($multipartFile);
            
            $curl = curl_init();
    
            curl_setopt_array($curl, array(
            CURLOPT_URL => $this->urlApi.'api/op/'.$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 => "POST",
            // @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' => \"".addslashes($body)."\");";
            // eval($multipartFile);
            $curl = curl_init();
    
            curl_setopt_array($curl, array(
            CURLOPT_URL => $this->urlApi.'api/op/'.$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 => "POST",
            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;


        foreach ($files as $key => $value) {
            $multipartFile[] = [
                'name'     =>'files',
                'contents' => fopen($value, 'r')
            ];
        }

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

                '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->getMessage();
        }
    }
}
