<?php


$res = db ();

if ( !$res)
    echo "NO ESTOY CONECTADO"; 


$id = 0;
$orgaId = 0;
$locaId = 0;

if(isset($_POST['eventId'])){
  $id = $_POST['eventId'];
}
if(isset($_POST['orga'])){
  $orgaId = $_POST['orga'];
  
}
if(isset($_POST['loca'])){
  $locaId = $_POST['loca'];
  
}

$object = new stdClass();

$striPers = "";
$sql= "Select p.id,p.nameLast,p.nameFirst,op.position from rmscheduler_orkincr.persons as p inner join rmscheduler_orkincr.organizationPersons as op on p.Id = op.personId inner join rmscheduler_orkincr.eventPerson as ep on p.Id = ep.idPerson where ep.idEvent = ".$id." order by p.nameLast,p.nameFirst ASC";
//echo $sql;
$result = $res->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $emplIden = $row['id'];
        $nameLast = $row['nameLast'];
        $nameFirst = $row['nameFirst'];
        $posi     = substr($row['position'],0,1);
        $striPers .= "|".$nameLast.", ".$nameFirst." [".$posi."]:".$emplIden;
    }
	$striPers .= "|";
}else{
	$striPers .= "||";
}

$object->personnel = $striPers;

$striEqui = "";
$sql= "select id0,id1,make,model,year,typeId from rmscheduler_orkincr.equipment  as e inner join rmscheduler_orkincr.eventEquipment as ee on e.id0 = ee.idEquipment where  ee.idEvent = ".$id." order by make,model,year";

$result = $res->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $iden0 = $row['id0'];
		$iden1 = $row['id1'];
		$make = $row['make'];
		$mode = $row['model'];
		$year = $row['year'];
		$striEqui .= "|".$make."_".$mode."_".$year."_".$iden1.":".$iden0;
    }
	$striEqui .= "|";
}else{
	$striEqui .= "||";
}

$object->vehicle = $striEqui;

$striAct = "";
$sql= "Select oa.description,oa.duration,oa.price,oa.id from rmscheduler_orkincr.organizationActivities as oa inner join rmscheduler_orkincr.eventActivity as ea on oa.id = ea.idActivity where ea.idEvent = ".$id.";";
$result = $res->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $activity = $row['description'];
        $striAct .= "|".$activity;
    }
	$striAct .= "|";
}else{
	$striAct .= "||";
}

$object->calendar = $striAct;


$sql= "select Name FROM rmscheduler_orkincr.organizations where idPrimary = ".$orgaId." limit 1;";
$result = $res->query($sql);
$orgaName = "";
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $orgaName = $row['Name']; 
    }
}

$object->customer = $orgaName.":".$orgaId;


$sql= "select Name FROM rmscheduler_orkincr.organizationLocations where Id = ".$locaId." limit 1;";
$result = $res->query($sql);
$locaName = "";
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $locaName = $row['Name']; 
    }
}

$object->location = $locaName.":".$locaId;







echo json_encode($object,JSON_UNESCAPED_UNICODE);


function db () {
    static $conn;
    if ($conn===NULL){ 
        $conn = new mysqli('localhost','orkindb','rmschpsw1','rmscheduler_orkincr'); 
        $conn->set_charset("utf8");
    }
    return $conn;
}
 
  

?>