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

try 
{
    //print_r($_POST);
    $Poids = ""; $PA = ""; $Notes = ""; $Statut="";
    $Medicament = [];
    $Radio = [];
    $Analyse = [];

    echo "<br>"."<br>";
    echo "DocEntry: ".$_POST['DocEntry']."<br>";
    if (!empty($_POST['Poids'])) { echo "Poids: ".$_POST['Poids']."<br>"; $Poids = $_POST['Poids'] ; }
    if (!empty($_POST['PA'])) { echo "PA: ".$_POST['PA']."<br>"; $PA = $_POST['PA'] ;  }
    if (!empty($_POST['Notes'])) { echo "Notes: ".$_POST['Notes']."<br>"; $Notes = $_POST['Notes'] ;  }
    if (!empty($_POST['Statut'])) { echo "Statut: ".$_POST['Statut']."<br>"; $Statut = $_POST['Statut'] ; }

    echo "<br>"."<br>";
    echo "MEDICAMENT";
    if (!empty($_POST['Medicament'])) {

        $count = count($_POST['Medicament']['ItemCode']);
        for($i=1; $i <= $count; $i++ ) {
            //echo "$i<br/>";
            /*echo "<br>"; echo "ItemCode: ".$_POST['Medicament']['ItemCode'][$i-1]." ";
            echo "<br>"; echo "ItemName: ".$_POST['Medicament']['ItemName'][$i-1]." ";
            echo "<br>"; echo "Quantity: ".$_POST['Medicament']['Quantity'][$i-1]." ";
            echo "<br>"; echo "Instruction: ".$_POST['Medicament']['Instruction'][$i-1]." ";*/
            $MedicamentLine = [];
            $MedicamentLine['ItemCode'] = $_POST['Medicament']['ItemCode'][$i-1];
            $MedicamentLine['ItemName'] = $_POST['Medicament']['ItemName'][$i-1];
            $MedicamentLine['Quantity'] = $_POST['Medicament']['Quantity'][$i-1];            
            $MedicamentLine['Instruction'] = $_POST['Medicament']['Instruction'][$i-1];            
            array_push($Medicament, $MedicamentLine);
        }
    }

    echo "<br>"."<br>";
    echo "RADIO";
    if (!empty($_POST['Radio'])) {

        $count = count($_POST['Radio']['ItemCode']);
        for($i=1; $i <= $count; $i++ ){    
            //echo "$i<br/>";
            /*echo "<br>"; echo "ItemCode: ".$_POST['Radio']['ItemCode'][$i-1]." ";
            echo "<br>"; echo "ItemName: ".$_POST['Radio']['ItemName'][$i-1]." ";
            echo "<br>"; echo "Instruction: ".$_POST['Radio']['Instruction'][$i-1]." ";*/
            $RadioLine = [];
            $RadioLine['ItemCode'] = $_POST['Radio']['ItemCode'][$i-1];
            $RadioLine['ItemName'] = $_POST['Radio']['ItemName'][$i-1];
            $RadioLine['Instruction'] = $_POST['Radio']['Instruction'][$i-1];            
            array_push($Radio, $RadioLine);
        }
        
    }

    echo "<br>"."<br>";
    echo "ANALYSE";
    if (!empty($_POST['Analyse'])) {

        $count = count($_POST['Analyse']['ItemCode']);
        for($i=1; $i <= $count; $i++ ){    
            //echo "$i<br/>";
            /*echo "<br>"; echo "ItemCode: ".$_POST['Analyse']['ItemCode'][$i-1]." ";
            echo "<br>"; echo "ItemName: ".$_POST['Analyse']['ItemName'][$i-1]." ";
            echo "<br>"; echo "Instruction: ".$_POST['Analyse']['Instruction'][$i-1]." ";*/
            $AnalyseLine = [];
            $AnalyseLine['ItemCode'] = $_POST['Analyse']['ItemCode'][$i-1];
            $AnalyseLine['ItemName'] = $_POST['Analyse']['ItemName'][$i-1];
            $AnalyseLine['Instruction'] = $_POST['Analyse']['Instruction'][$i-1];            
            array_push($Analyse, $AnalyseLine);
        }
        
    }

    echo "<br>"."<br>";
    echo json_encode($Medicament);
    echo "<br>"."<br>";
    echo json_encode($Radio);
    echo "<br>"."<br>";
    echo json_encode($Analyse);

    //UPDATE CONSULTATION

    /*$Consultation = $sap->getService("CONSULTATION");
    $result = $Consultation->create([
        "U_Poids"=> $Poids,
        "U_PA"=> $PA,
        "U_Notes"=> $Notes,
        "U_Statut"=> $Statut,
        "DETAIL CONSULTATIONCollection"=> [ [ "U_Consultation" => "", "U_Notes" => "", "U_Description" => "" ] ],
        "ORDONNANCECollection"=> [ $Medicament ],
        "ANALYSECollection"=> [ $Analyse ],
        "RADIOCollection"=> [ $Radio ]

    ]);*/



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