<?php

namespace Modules\Additional\Specifique\Client30\Modules\API\Controller;

use Helpers\Outil\XmlHelper;
use Skeleton\Controller\Controller;
use Helpers\Session\ParseBase1;

class SharePointController extends Controller
{
    public function batchCreateDAAction($post = [])
    {
        $myfile = file_get_contents((new ParseBase1())->getRep()->tmp.$post[2], "r") ;
        $param = new \SimpleXMLElement($myfile);
        $sharePoint = new \Modules\Additional\Specifique\Client30\API\SharePoint\SharePoint($this->pdo);
        return $sharePoint->createDA($param->body->DANum, $param->body->DAChantierNum);
    }

    public function batchUpdateDAAction($post = [])
    {
        $myfile = file_get_contents((new ParseBase1())->getRep()->tmp.$post[2], "r") ;
        $param = new \SimpleXMLElement($myfile);
        
        $files = [];
        $listeDoc = XmlHelper::normalize((array)$param->listeDoc, true);
        
        $this->constructDocArray($listeDoc, $files);
      
        $sharePoint = new \Modules\Additional\Specifique\Client30\API\SharePoint\SharePoint($this->pdo);
        return $sharePoint->updateDA(
            (string)$param->body->DANum,
            $files,
            // @phpstan-ignore-next-line
            (string)$param->body->Purchaser ??'',
            // @phpstan-ignore-next-line
            (string)$param->body->Expedition ?? '',
            // @phpstan-ignore-next-line
            (string)$param->body->Country ?? '',
            // @phpstan-ignore-next-line
            (string)$param->body->Center ?? ''
        );
    }
    
    public function batchDeactivateDAAction($post = [])
    {
        $myfile = file_get_contents((new ParseBase1())->getRep()->tmp.$post[2], "r") ;
        $param = new \SimpleXMLElement($myfile);
        $sharePoint = new \Modules\Additional\Specifique\Client30\API\SharePoint\SharePoint($this->pdo);
        return $sharePoint->deactivateDA($param->body->DANum);
    }

    public function batchCreateAOAction($post = [])
    {
        $myfile = file_get_contents((new ParseBase1())->getRep()->tmp.$post[2], "r") ;
        $param = new \SimpleXMLElement($myfile);
        
        $files = [];
        $listeDoc = XmlHelper::normalize((array)$param->listeDoc, true);
       
        $this->constructDocArray($listeDoc, $files);
        
        $sharePoint = new \Modules\Additional\Specifique\Client30\API\SharePoint\SharePoint($this->pdo);
        return $sharePoint->createAO(
            $param->body->DANum,
            $param->body->AONum,
            $files,
            $param->body->Provider ??''
        );
    }

    public function batchCreateOPAction($post = [])
    {
        $myfile = file_get_contents((new ParseBase1())->getRep()->tmp.$post[2], "r") ;
        $param = new \SimpleXMLElement($myfile);
        
        $files = [];
        $listeDoc = XmlHelper::normalize((array)$param->listeDoc, true);
        $this->constructDocArray($listeDoc, $files);
        
        $sharePoint = new \Modules\Additional\Specifique\Client30\API\SharePoint\SharePoint($this->pdo);
        return $sharePoint->createOP(
            (string)$param->body->DANum,
            $files
        );
    }

    public function batchCreateCDEAction($post = [])
    {
        $myfile = file_get_contents((new ParseBase1())->getRep()->tmp.$post[2], "r") ;
        $param = new \SimpleXMLElement($myfile);
        $sharePoint = new \Modules\Additional\Specifique\Client30\API\SharePoint\SharePoint($this->pdo);
        return $sharePoint->createCDE(
            $param->body->DANum,
            $param->body->CDENum
        );
    }

    

    public function batchUpdateCDEAction($post = [])
    {
        $myfile = file_get_contents((new ParseBase1())->getRep()->tmp.$post[2], "r") ;
        $param = new \SimpleXMLElement($myfile);
        
        $files = [];
        $listeDoc = XmlHelper::normalize((array)$param->listeDoc, true);
       
        $this->constructDocArray($listeDoc, $files);
        
        $sharePoint = new \Modules\Additional\Specifique\Client30\API\SharePoint\SharePoint($this->pdo);
        return $sharePoint->updateCDE(
            (string)$param->body->DANum,
            (string)$param->body->CDENum,
            $files,
            (string)$param->body->Provider,
            (string)$param->body->Expedition
        );
    }

    public function batchCreateAVNAction($post = [])
    {
        $myfile = file_get_contents((new ParseBase1())->getRep()->tmp.$post[2], "r") ;
        $param = new \SimpleXMLElement($myfile);
        
        $files = [];
        $listeDoc = XmlHelper::normalize((array)$param->listeDoc, true);
       
        $this->constructDocArray($listeDoc, $files);
        
        $sharePoint = new \Modules\Additional\Specifique\Client30\API\SharePoint\SharePoint($this->pdo);
        return $sharePoint->createAVN(
            (string)$param->body->DANum,
            (string)$param->body->CDENum,
            (string)$param->body->Index,
            $files,
            (string)$param->body->Provider,
            (string)$param->body->Expedition
        );
    }

    public function constructDocArray($listeDoc, &$files)
    {
        if (isset($listeDoc['doc']) && is_array($listeDoc['doc']) && isset($listeDoc['doc'][0])) {
            foreach ($listeDoc['doc'] as $key => $value) {
                if ((!is_array($value['NomFichier']) && $value['NomFichier'] != '') || (is_array($value['NomFichier']) && count($value['NomFichier']))) {
                    $files[$value['NomFichier']] = $value['CheminFichier'];
                } else {
                    $files[] = $value['CheminFichier'];
                }
            }
        } elseif (isset($listeDoc['doc']) && count($listeDoc['doc'])) {
            if (is_array($listeDoc['doc']['NomFichier'])) {
                $files[$listeDoc['doc']['NomFichier'][0]] = $listeDoc['doc']['CheminFichier'];
            } else {
                $files[$listeDoc['doc']['NomFichier']] = $listeDoc['doc']['CheminFichier'];
            }
        }
    }
}
