<?php
$pgMod="gps_tracking";
$pgAct="view";


if(isset($_GET['action']) && trim($_GET['action'])!='')
		$pgAct=strtolower($_GET['action']);
			unset($_SESSION['form']);
if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;

	if(!isset($_POST['school_code']) || trim($_POST['school_code'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please select school.");
	}
    if($flgEr!=TRUE && $pgAct=="add")
	{
		$aryData=array(
					'school_code'   => $_POST['school_code'],
					'bus_number'	=> $_POST['bus_number'],
					'bus_name'      => $_POST['bus_name'],
					'driver_name'   => $_POST['driver_name'],
					'driver_mobile' => $_POST['driver_mobile'],
					'password'      => $_POST['password'],
					'school_id'     => $_SESSION['admin']['school_id'],
					'added_by'      => $_SESSION['admin']['uid'],
					'session'       => $_SESSION['admin']['session'],
    			);

	 	    $flgIn=$db->insertAry("gps_user",$aryData);	

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

	}
		elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
    		$aryData=array(
					'school_code'   => $_POST['school_code'],
					'bus_number'	=> $_POST['bus_number'],
					'bus_name'      => $_POST['bus_name'],
					'driver_name'   => $_POST['driver_name'],
					'driver_mobile' => $_POST['driver_mobile'],
					'password'      => $_POST['password'],
					'school_id'     => $_SESSION['admin']['school_id'],
					'added_by'      => $_SESSION['admin']['uid'],
					'session'       => $_SESSION['admin']['session'],
    			);
		$flgUp=$db->updateAry("gps_user",$aryData,"where id='".$_GET['id']."'");
	
		if(!is_null($flgUp)) 
		{ 
			$_SESSION['msg']='update Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		
		}
	}

}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("gps_user","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("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>complain</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>Student Notice</b> <?php echo $alert_err[0] ?></i>
	</div>
	<?php
}
if($pgAct=="edit" && $_GET["id"]!="")
{
	$aryForm=$db->getRow("SELECT * FROM gps_user WHERE  id='".$_GET['id']."'");
}

?>
<style>
.hoverclass{
hover:hoverclass;
}
</style>    
<form method="get" action="">
    <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>
    </div>
    <div class=" no-padding" style="background:white;">


    <div style="z-index: 99999;margin: 30px;">
    <form method="get" action="">
        <table style="z-index:88988989898989">
            <tr>
                <td>
                    <label class="control-label">Select school</label>
                    <select name="school_code" required>
					    <option value="">--Select code--</option>
					    <?php $allschools = $db->getRows("select * from all_school_codes");
					        foreach($allschools as $school){
					            $name = str_replace(".savarnacloud.com","","$school[school_url]");
					            ?>
					            <option value="<?php echo $school['code']; ?>" <?php if($_GET['school_code'] == $school['code']){echo "selected"; }?> ><?php echo $school['code']." - ". $name; ?></option>
					            <?php
					        } ?>
					</select>
                </td>
                <td>
                    <label class="control-label">Select Bus</label>
                    <div  class="controls" id="bus_div">

                    <select name="bus">
                        <option value="">--Select bus--</option>
                    </select>  
                    </div>
                </td>
                <td>
                    <label class="control-label">From Date</label>
                      <div  class="controls" id="sub_subject">
                        <input type="date" name="fdate" value="<?php echo $_GET['fdate']; ?>" />
                    </div>
                </td>
                <td>
                    <label class="control-label">To Date</label>
                      <div  class="controls" id="chapter_div">
                        <input type="date" name="tdate" value="<?php echo $_GET['tdate']; ?>" />
                    </select>
                    </div>
                </td>
                <td>
                    <label></label>
                    <button type="submit" class="btn btn-info" name="report" style="margin-top:20px;font-size:15px;padding:3px 8px;" >Search</button>
                </td>
            </tr>
        </table>
    </form>
    </div>

	<?php
	
			if(isset($_GET["report"])){
	$aryPgAct=array("page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM gps_user where school_code='$_GET[school_code]' "); $i=0;

	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            
            <thead>
                <tr>
                    <th>S. No.</th>
                    <th>School Code</th>
                    <th>Bus Number</th>
                    <th>Bus Name</th>
                    <th>Driver Name</th>
                    <th>Driver Mobile</th>
                    <th>Location</th>
                    <th>Date</th>
	            </tr>
            </thead>
            
            <tbody>
            <?php foreach($aryList as $clist){ $i++;
                $aryPgAct["id"]=$clist['id'];
                $schoolname = $db->getVal("select school_url from all_school_codes where code='$clist[school_code]' ");
	            $name = str_replace(".savarnacloud.com","","$schoolname");
             ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $i ?></td>
					<td><?php echo $clist['school_code']." - ".$name; ?></td>
              		<td><?php echo $clist['bus_number']; ?> </td>
              		<td><?php echo $clist['bus_name']; ?> </td>
              		<td><?php echo $clist['driver_name']; ?> </td>
              		<td><?php echo $clist['driver_mobile']; ?> </td>
              		<td></td>
                    <td>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
        </table>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>Tracking </b> Record not found </i>
        </div>
        <?php
	} 
			}
	?>
	</div></div></div>

<!-- / Forms -->