<?php 
include "functions.php";
use SAPb1\Filters\Equal;
use SAPb1\Filters\InArray;

try 
{
    $sap = Login();
    //print_r($_POST);
    $DocEntry = ""; $Poids = ""; $PA = ""; $Notes = ""; $Statut="";
    $ConsultationDetail = [];
    $Medicament = [];
    $Radio = [];
    $Analyse = [];
    
    if (!empty($_POST['DocEntry'])) { $DocEntry = $_POST['DocEntry'] ; }
    if (!empty($_POST['Poids'])) { $Poids = $_POST['Poids'] ; }
    if (!empty($_POST['PA'])) { $PA = $_POST['PA'] ;  }
    if (!empty($_POST['Notes'])) { $Notes = $_POST['Notes'] ;  }
    if (!empty($_POST['Statut'])) { $Statut = $_POST['Statut'] ; }

    if (!empty($_POST['Consultation'])) {

        $count = count($_POST['Consultation']['ItemCode']);
        for($i=1; $i <= $count; $i++ ){    
            $ConsultationLine = [];
            $ConsultationLine['DocEntry'] = $DocEntry ;
            $ConsultationLine['LineId'] = $i;
            $ConsultationLine['U_Consultation'] = $_POST['Consultation']['ItemCode'][$i-1];
            $ConsultationLine['U_Description'] = $_POST['Consultation']['ItemName'][$i-1];
            $ConsultationLine['U_Notes'] = $_POST['Consultation']['Notes'][$i-1];            
            array_push($ConsultationDetail, $ConsultationLine);
        }
    }
    if (!empty($_POST['Medicament'])) {

        $count = count($_POST['Medicament']['ItemCode']);
        for($i=1; $i <= $count; $i++ ) {
            $MedicamentLine = [];
            $MedicamentLine['DocEntry'] = $DocEntry ;
            $MedicamentLine['LineId'] = $i;
            $MedicamentLine['U_ItemCode'] = $_POST['Medicament']['ItemCode'][$i-1];
            $MedicamentLine['U_Description'] = $_POST['Medicament']['ItemName'][$i-1];
            $MedicamentLine['U_Qte'] = $_POST['Medicament']['Quantity'][$i-1];            
            $MedicamentLine['U_Instructions'] = $_POST['Medicament']['Instruction'][$i-1];            
            array_push($Medicament, $MedicamentLine);
        }
    }
    if (!empty($_POST['Radio'])) {

        $count = count($_POST['Radio']['ItemCode']);
        for($i=1; $i <= $count; $i++ ){
            $RadioLine = [];
            $RadioLine['DocEntry'] = $DocEntry ;
            $RadioLine['LineId'] = $i;
            $RadioLine['U_ItemCode'] = $_POST['Radio']['ItemCode'][$i-1];
            $RadioLine['U_Description'] = $_POST['Radio']['ItemName'][$i-1];
            $RadioLine['U_Instructions'] = $_POST['Radio']['Instruction'][$i-1];            
            array_push($Radio, $RadioLine);
        }
        
    }
    if (!empty($_POST['Analyse'])) {

        $count = count($_POST['Analyse']['ItemCode']);
        for($i=1; $i <= $count; $i++ ){    
            $AnalyseLine = [];
            $AnalyseLine['DocEntry'] = $DocEntry ;
            $AnalyseLine['LineId'] = $i;
            $AnalyseLine['U_ItemCode'] = $_POST['Analyse']['ItemCode'][$i-1];
            $AnalyseLine['U_Description'] = $_POST['Analyse']['ItemName'][$i-1];
            $AnalyseLine['U_Instructions'] = $_POST['Analyse']['Instruction'][$i-1];            
            array_push($Analyse, $AnalyseLine);
        }        
    }

    echo "DocEntry: ".$DocEntry."<br>";
    echo "Poids: ".$Poids."<br>";
    echo "PA: ".$PA."<br>";
    echo "Notes: ".$Notes."<br>";
    echo "Statut: ".$Statut."<br>";
    echo "<br>"."<br>";

    //UPDATE CONSULTATION
    echo json_encode(
        [ 
            "U_Poids"=> $Poids, "U_PA"=> $PA, "U_Notes"=> $Notes, "U_Statut"=> $Statut,
            "CONSULTATIONCollection"=> $ConsultationDetail,
            "ORDONNANCECollection"=>  $Medicament,
            "ANALYSECollection"=>   $Analyse,
            "RADIOCollection"=>  $Radio,
        ]);

    $Consultation = $sap->getService("CONSULTATION");
    $result = $Consultation->update(intval($DocEntry),
        [
            "U_Poids"=> $Poids, "U_PA"=> $PA, "U_Notes"=> $Notes, "U_Statut"=> $Statut, 
            "CONSULTATIONCollection"=> $ConsultationDetail, 
            "ORDONNANCECollection"=>  $Medicament, 
            "ANALYSECollection"=>   $Analyse, 
            "RADIOCollection"=>  $Radio,
        ]);

        echo "<br>"."<br>";
        echo "Result"."<br>";
        echo json_encode($result);
        echo "<br>"."<br>";

    if($result == "true") {
        echo "Create Bon de Livraison";
    } else {
        echo "Error";
    }


} catch (Exception $e) {
    echo $e->getMessage();    
}