<?php

if (!isset($sortableIncluded) || $sortableIncluded != "1") :
   include_once ("../include/mcglobal.inc.php");
   include_once ("../include/auth.inc.php");
   // include_once ("../include/image.inc.php");
   // include_once ("../include/email.inc.php");   

   // Check for authentication access and granted rights
   $usrAccessArray["hq"] = "1";
   authCheckForAccess($hq_id, $usr_id, $emp_id, "1", $customerId, $cscIdRoot, $cscIdActual);
   
   // Check HTTP-Parameters
   getSecHttpVars("1",array("f_act", "statusMessage", "deactivateMenu", "objType", "objIds", "numOfContainer", "doSortArray", "content",
                            "box0", "box1", "box2", "box3", "box4", "box5"));
endif;

// Init
if (!isset($objType)) : $objType = ""; endif;
$objIdArray = array();
if ($objIds != "") : 
   $objIdArray = spliti(",", $objIds);
endif;
$numOfContainer = count($objIdArray);
if ($doSortArray == "") : $doSortArray = "0"; endif;


// Process
if ($f_act == "process_sort") :

   $newSortedArray = array();
   for ($i = 0; $i < $numOfContainer; $i++) :
       parse_str(${("box" . $i)}, $newSortedArray[$i]);
   endfor;
// print_r($newSortedArray);
endif;


// Paramteres to be defined e.g. by POST
$arrayBox = array();
if ($objType == "job") :
   include_once ("../include/inc_job.inc.php");
   
   for ($i = 0; $i < $numOfContainer; $i++) :
       getDBData("job", $objIdArray[$i]);
       getDBData("tour", $objIdArray[$i]);

       $numOfStations = count($jobData["tour"]);
       for ($j = 1; $j <= $numOfStations; $j++) :       
           $arrayBox[$i][$objIdArray[$i] . "|" . $j] = $jobData["tour"][$j]["tr_comp"] . "<br>" . $jobData["tour"][$j]["tr_comp2"] . "<br>"
                                                 . $jobData["tour"][$j]["ad_street"] . "&nbsp;" . $jobData["tour"][$j]["tr_hsno"] . "<br>"
                                                 . $jobData["tour"][$j]["ad_zipcode"] . "&nbsp;" . $jobData["tour"][$j]["ad_city"] . "<br>"
                                                 . $jobData["tour"][$j]["tr_phone"] . "<br>";
       endfor;
   endfor;
else :   
   // Example
   $arrayBox[0] = array("5" => "Test5Box1", "1" => "Test1Box1", "2" => "Test2Box1", "3" => "Test3Box1");
   $arrayBox[1] = array("6" => "Test1Box2", "7" => "Test2Box2", "8" => "Test3Box2");
   $arrayBox[2] = array("9" => "Test1Box3", "10" => "Test2Box3", "11" => "Test3Box3", "12" => "Test4Box3");
   $arrayBox[3] = array("15" => "Test3Box4", "14" => "Test2Box4", "13" => "Test1Box4");
endif;

if ($numOfContainer == "") : $numOfContainer = 0; endif;
// print_r($arrayBox);

$sortableBoxes = "";
for ($i = 0; $i < $numOfContainer; $i++) :
    // Sort array if requested
    if ($doSortArray == "1") :
       ksort($arrayBox[$i]);
    endif;

    if ($sortableBoxes != "") : $sortableBoxes .= ", "; endif;
    $sortableBoxes .= "#divBox" . $i;
endfor;


// Generate container output
function drawBox ($arrayBox, $numOfContainer) {
	global $objIdArray;
  $html_output = "";
  for ($i = 0; $i < $numOfContainer; $i++) :
      $html_output .= '<div class="panel panel-default" style="float:left; margin-left: 2%; margin-top: 2%">' . "\n";
      $html_output .= '   <div class="panel-heading">' . $objIdArray[$i] . '</div>' . "\n";
      $html_output .= '   <div class="panel-body" style="float:left; width:95%; border-color: blue; margin-left:2%; margin-top:2%;" id="divBox' . $i . '">'. "\n";
      foreach ($arrayBox[$i] as $key => $value) {
          $html_output .= '       <div class="well divBoxes" id="item_'. $key . '">'. $value .'</div>' . "\n";
      }
      $html_output .= '   </div>' . "\n";
      $html_output .= '</div>' . "\n";
   endfor;
   return $html_output;
}
?>


<!--Latest compiled and minified CSS-->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">

<!--Optional theme-->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">

<!--Latest compiled and minified JavaScript-->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>



<html>
<head>
  <title><?php echo $pageTitle ?></title>
  <style>
  #sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
  .divBoxes {	width: 100%; margin-top: 2%; margin-left: 2%; margin-right: 2%; margin-bottom: 2%; }
  .divBox { min-height: 20%; }
  </style>
  <script type="text/javascript">
    $(document).ready(function() {
        $("<?php echo $sortableBoxes ?>").sortable({
            connectWith:"<?php echo $sortableBoxes ?>"
        }).disableSelection();

        $("#submit").click(function(){
            // Returns successful data submission message when the entered information is stored in database.
            <?php for ($i = 0; $i < $numOfContainer; $i++) : ?>
              var box<?php echo $i ?> = $( "#divBox<?php echo $i ?>" ).sortable( "serialize", { key: "content[]" } );
              // console.log(box<?php echo $i ?>);
              jQuery('#box<?php echo $i ?>').val(box<?php echo $i ?>);
            <?php endfor; ?>
            jQuery('#f_act').val('process_sort');
            jQuery('#multiSortable').submit();
        });
    });
  </script>
</head>
<body>
    <form action="multiSortable.php" method="post" id="multiSortable">
    	  <input type="hidden" name="f_act" id="f_act" value="">
    	  <input type="hidden" name="objType" id="objType" value="<?php echo $objType ?>">
    	  <input type="hidden" name="objIds" id="objIds" value="<?php echo $objIds ?>">
    	  <input type="hidden" name="numOfContainer" id="numOfContainer" value="<?php echo $numOfContainer ?>">
    	  <input type="hidden" name="doSortArray" id="doSortArray" value="<?php echo $doSortArray ?>">
    	  
        <?php echo drawBox($arrayBox, $numOfContainer); ?>
        <div style="clear: both;"></div>
        <?php for ($i = 0; $i < $numOfContainer; $i++) : ?>
          <input type="hidden" name="box<?php echo $i ?>" id="box<?php echo $i ?>">
        <?php endfor; ?>
        <input style="margin-left: 0.5%; width: 10%; height:8%; position: absolute; margin-left: 2%" id="submit" type="submit" class="btn btn-primary" value="Speichern">
    </form>
</body>
</html>