<?php
$pgMod="route";
$pgAct="view";
if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);

if(isset($_POST["saveroute"])){
    
    $saveAry = array(
            "name"          =>  $_POST['route'],
            "pickup_point"  =>  $_POST['pickup'], 
            "pick_drop_time"=>  "Pickup: $_POST[pickhour]:$_POST[pickminute] $_POST[picksession] Drop: $_POST[drophour]:$_POST[dropminute] $_POST[dropsession] ",
            "added_by"      =>  $_POST["added_by"]=$_SESSION["admin"]["uid"],
            "school_id"     =>  $_POST["added_by"]=$_SESSION["admin"]["school_id"]
        );

		$flgIn=$db->insertAry("location",$saveAry);
		if(!is_null($flgIn))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Transport", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}    
    
}


if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	if(!isset($_POST['name']) || trim($_POST['name'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter route name.");
	}
	if($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
        $oldname = $db->getVal("select name from location where id='".$_GET['id']."' ");
		$flgUp=$db->updateAry("location",$_POST,"where id='".$_GET['id']."'");
		$db->updateAry("location",$_POST,"where name='".$oldname."'");
		if(!is_null($flgUp))
		{
			$_SESSION['msg']='update Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Transport", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}
	}
	else
	{
        $_POST["added_by"]=$_SESSION["admin"]["uid"];
        $_POST["school_id"]=$_SESSION["admin"]["school_id"];

		$flgIn=$db->insertAry("location",$_POST);
		if(!is_null($flgIn))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Transport", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}
	}

}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("location","where id='".$_GET['id']."'");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Transport", "page_id"=>$pgMod)));
}
if(isset($_SESSION['msg']) && $_SESSION['msg']!="")
{
	?>
	<div class="alert alert-success">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-thumbs-up-alt"></i> <b>Notice</b><?php echo $_SESSION['msg'] ?></i>
	</div>
	<?php
	unset($_SESSION["msg"]);
}
if(is_array($alert_err) && count($alert_err)>0)
{
	?>
	<div class="alert alert-error">
	  <a class="close" data-dismiss="alert">×</a>
	  <i class="icon icon-warning-sign"></i> <b>Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
if($pgAct=="edit" && $_GET["id"]!="")
{
	$aryForm=$db->getRow("SELECT * FROM location WHERE id='".$_GET['id']."'");
}

?>

<style>

/* The Modal (background) */
.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 9991; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 280px;
  top: 0px;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 50%;
}

/* The Close Button */
.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}



</style>

	<div class="row-fluid">
        <form method="post" action="">

			<!-- Forms: Box -->
			<div class="span12">

				<!-- Forms: Top Bar -->
				<div class="top-bar">
					<h3><i class="icon-cog"></i> Create Route </h3>
				</div>
				<!-- / Forms: Top Bar -->
 
				<!-- Forms: Content -->
				<div class="well no-padding">

                        <div class="control-group">
							<label class="control-label" for="route">Route Name</label>
							<div class="controls">
								<input type="text" id="route" name="name" class="input-block-level" value="<?php if($_POST)echo $_POST["name"]; else  echo $aryForm["name"]; ?>" placeholder="Route name" >
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" >Description</label>
							<div class="controls">
							    <textarea name="description" rows="4" class="input-block-level"><?php if($_POST)echo $_POST["description"]; else echo $aryForm["description"]; ?></textarea>
							</div>
						</div>

						<div class="form-actions">
							<button type="submit" class="btn btn-primary"> Save</button>
							<button type="button" class="btn"> Cancel</button>
						</div>
                        
				</div>
				<!-- / Forms: Content -->

			</div>
			<!-- / Forms: Box -->

        </form>
        
        
            <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"Transport", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM location group by name "); $i=0;
		?>

        <table class="table"> 
            <thead>
                <tr>
                    <!--<th>S.No.</th>-->
                    <th>Routes</th>
                    <th>Pickup Points</th>
                    <th>Manage</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['id'];
            $aryPgAct['action']="edit";
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <!--<td><?php echo $i; ?></td>-->
                    <td><?php echo $clist["name"] ?><br><a href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>" style='font-size:20px;color:green;'>&#9745;</a></td>
                    <td><?php 
                        $pickup = $db->getRows("select * from location where name='$clist[name]' and pickup_point!='' ");
                        foreach($pickup as $point){ 
                            $name = $db->getVal("select root from bus where id='$point[pickup_point]' ");
                            echo "<p style='font-size:12px;'>$name &emsp;$point[pick_drop_time]&emsp;<span style='color:#fa1232;cursor:pointer;' onclick='deletepickup($point[id])'>X</span> </p>";
                        }
                        ?>
                    </td>
                    <td>
                        <button onclick="showmodal('<?php echo $clist['name']; ?>')" style="background:#ff9c03;color:#fff;border:none;border-radius:5px;font-size:16px;padding:5px 15px;">Add</button>
                    </td>
                    <!--<td>-->
                    <!--    <?php $aryPgAct['action']="edit"; ?>-->
                    <!--        <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Edit</button></a>-->
                    <!--    <?php $aryPgAct['action']="delete"; ?>-->
                    <!--        <a href="javascript:;" onclick="del('<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>')" class="confirmation"><button type="button" class="btn btn-danger">Delete</button></a>  -->
                    <!--</td>-->
                </tr>
            <?php } ?>
            </tbody>
        </table>

	</div>

    <!-- The Modal -->
<div id="myModal" class="modal">

  <div class="modal-content">
    <span class="close">&times;</span>
    <h4 style="width:97%;text-align:center;background:green;color:white;">Assign Pickup Points</h4>
    <form method="post" action="">
        <div class="well no-padding">
		    <?php $allpickups = $db->getRows("select * from bus "); ?>
            <input type="hidden" name="route" id="route_no" value="" />
                <div class="control-group">
					<label class="control-label" for="route">Pickup Point</label>
					<div class="controls">
					    <div style="width:100%;display:inline-block;">
					        <select name="pickup" style="width:500px; display:inline-block;">
					            <option value="">--Select  Pickup Point--</option>
					            <?php foreach($allpickups as $point){ ?>
					                <option value="<?php echo $point['id']; ?>"><?php echo $point['root']; ?></option>
					            <?php } ?>
					        </select>
				        </div>
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" >Pickup Time</label>
					<div class="controls">
					    <div>
					        <label style="display:inline-block;">Hour</label>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
					        <label style="display:inline-block;">Minute</label>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
					        <label style="display:inline-block;">Session</label>
					    </div>
					    <div>
					    <select name="pickhour" style="width:100%;display:block;">
					        <?php for($i=01;$i<=12;$i++){
					            echo "<option value='$i'>$i</option>";
    					        }
					        ?>
					    </select>
					    <select name="pickminute">
					        <?php for($i=00;$i<=59;$i++){
					            echo "<option value='$i'>$i</option>";
    					        }
					        ?>
					    </select>
					    <select name="picksession">
					            <option value='AM'>AM</option>
					            <option value='PM'>PM</option>
					    </select>
					    </div>
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" >Drop Time</label>
					<div class="controls">
					    <div>
					        <label style="display:inline-block;">Hour</label>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
					        <label style="display:inline-block;">Minute</label>&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
					        <label style="display:inline-block;">Session</label>
					    </div>
					    <div>
					    <select name="drophour" style="width:100%;display:block;">
					        <?php for($i=01;$i<=12;$i++){
					            echo "<option value='$i'>$i</option>";
    					        }
					        ?>
					    </select>
					    <select name="dropminute">
					        <?php for($i=00;$i<=59;$i++){
					            echo "<option value='$i'>$i</option>";
    					        }
					        ?>
					    </select>
					    <select name="dropsession">
					            <option value='AM'>AM</option>
					            <option value='PM'>PM</option>
					    </select>
					    </div>
					</div>
				</div>
				<div class="form-actions">
					<button type="submit" name="saveroute" class="btn btn-primary"> Save</button>
					<button type="button" class="btn"> Cancel</button>
				</div>
                
		</div>

		</form>

        </div>
  </div>

</div>

	</div>

<!-- / Forms -->



<script>
function showmodal(name){
    $("#route_no").val(name);
// Get the modal
var modal = document.getElementById("myModal");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

  modal.style.display = "block";

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}
 
// When the user clicks anywhere outside of the modal, close it
    window.onclick = function(event) {
      if (event.target == modal) {
        modal.style.display = "none";
      }
    } 
}

function deletepickup(id){
    var r = confirm("Are you sure want to Delete ?");
    if (r == true) {
        $.post( "Transport/ajax.php", { type: "delete_pickup" ,id:id} , function( data ) {
            location.reload();
        });
    }


    
}

</script>
