<?php
$pgMod="gps_user";
$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.");
	}
//     elseif(!isset($_POST['school_url']) || trim($_POST['school_url'])=='')
// 	{
// 		$flgEr=TRUE;
// 		array_push($alert_err,"Please enter school URL.");
// 	}
	 
	

    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=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
// 	{
// 		$flgUp=$db->updateAry("student_notice",$aryData,"where added_by='".$_SESSION["admin"]["uid"]."' and 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']."'");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{

 ?>
<!-- Forms --> 


<style>
.top-bar-minimize{display:none;}
thead, tfoot {display: table-header-group;}
.top-bar+.well,.accordion .accordion-heading+.accordion-body{display: inline-block !important;width: 96.7% !important;overflow: inherit !important;} 
</style>

	<div class="row-fluid">
        <form method="post" enctype="multipart/form-data" action="">
			<!-- Forms: Box -->
			<div class="span12">

				<!-- Forms: Top Bar -->
				<div class="top-bar">
					<h3><i class="icon-cog"></i> Tracking User</h3>
				</div>
				<!-- / Forms: Top Bar -->

				<!-- Forms: Content -->
				<div class="well no-padding">

                        <div class="control-group">
							<label class="control-label" for="Name">School Code</label>
							<div class="controls">
								<!--<input type="text" id="upload_notice" name="code" class="input-block-level" value="<?php echo $aryForm['code']; ?>" required>-->
								<select name="school_code" required>
								    <option value="">Select school</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($aryForm['school_code'] == $school['code']){echo "selected"; }?> ><?php echo $school['code']." - ". $name; ?></option>
								            <?php
								        } ?>
								</select>
							</div>
						</div>
 
                        <div class="control-group">
							<label class="control-label" for="detail">Bus number</label>
							<div class="controls">
								<input type="text" name="bus_number" class="input-block-level" value="<?php echo $aryForm['bus_number']; ?>" >
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label" for="detail">Bus name</label>
							<div class="controls">
								<input type="text" name="bus_name" class="input-block-level" value="<?php echo $aryForm['bus_name']; ?>" >
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label" for="detail">Driver name</label>
							<div class="controls">
								<input type="text" name="driver_name" class="input-block-level" value="<?php echo $aryForm['driver_name']; ?>" >
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label" for="detail">Driver mobile</label>
							<div class="controls">
								<input type="text" name="driver_mobile" class="input-block-level" value="<?php echo $aryForm['driver_mobile']; ?>" >
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label" for="detail">Password</label>
							<div class="controls">
								<input type="text" name="password" class="input-block-level" value="<?php echo $aryForm['password']; ?>" >
							</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>
<?php   
}else{
?>
<style>
.hoverclass{
hover:hoverclass;
}
</style>    

	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> <?php echo ucwords($pgAct." ".$pgMod); ?></h3>

    <button style="float:right; margin-top:5px" onClick="window.location='./?page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add <?php echo $pgMod; ?></button>

    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM gps_user"); $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>Password</th>
                    <th>Action</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]' ");
								        // foreach($allschools as $school){
								            $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><?php echo $clist['password']; ?> </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>
		<?php
	}
	else{
		?>
        <div class="alert alert-info">
          <a class="close" data-dismiss="alert">×</a>
          <i class="icon icon-warning-sign"></i> <b>complain</b> Record not found </i>
        </div>
        <?php
	} 
	?>
	</div></div></div>
	<?php
}
?>
<!-- / Forms -->