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


if($_POST)
{
    
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	

	if($flgEr!=TRUE && $pgAct=="add")
	{
		$_POST["school_id"]=$_SESSION["admin"]["school_id"];
$_POST["added_by"]=$_SESSION["admin"]["uid"];
 
 $_POST["location"]=implode(",", $_POST['location']);
		$flgIn=$db->insertAry("driver",$_POST);
		if(!is_null($flgIn))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			sms($_POST["mobile"],"Dear ".$_POST["name"]." Your are successfully register as a post of driver in ".$LinksDetails["site_name"],"add_bus_driver");
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Transport", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
			
		}
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{

		$_POST["location"]=implode(",", $_POST['location']);
		$flgUp=$db->updateAry("driver",$_POST,"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."'");
		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());
		
		}
	} 
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
    $res=$db->delete("driver","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."' ");

	//$flgUp=$db->updateAry("driver",array("status"=>0),"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_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 driver WHERE school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."'");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{ 
	if(isset($_SESSION['form']))
	{
		$aryForm=$_SESSION['form'];
		unset($_SESSION['form']);
	}
?>
<!-- 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>
<form method="post" action="" enctype="multipart/form-data">
     <div class="row-fluid">
        <div class="span12">
            <div class="top-bar">
                <h3><i class="icon-cog"></i> Create New Admission</h3>
                <div style="float:right; margin-top:5px">
                    <button type="submit" class="btn btn-primary"> Save</button>
                    <button type="reset" class="btn"> Cancel</button>
                </div>
            </div>
            <div class="well">
			<!-- Forms: Box -->
			<div class="span4">

				<!-- Forms: Top Bar -->
				
				<!-- / Forms: Top Bar -->

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

						<div class="control-group">
							<label class="control-label" for="idno">Id no</label>
							<div class="controls">
                            	<input type="hidden" name="session" value="<?php echo $_SESSION["admin"]["session"] ?>"	/> 
								<input type="text" id="idno" name="idno" readonly class="input-block-level" value="<?php
                                if($aryForm["idno"]!="")
								{
									echo $aryForm["idno"];
								}
								else{
									echo (int)($db->getVal("select max(idno) from driver where school_id='".$_SESSION["admin"]["school_id"]."' ")+1);	
								}
								?>">
							</div>
						</div>
                        
                        
                        


                   <div class="control-group">
							<label class="control-label" for="bus">Location</label>
							<div class="controls">
								<select class="span12" name="location[]" id="location" multiple="multiple">
									<option value="">select </option>
									<?php 
										$class=$db->getRows("select * from location where school_id='".$_SESSION["admin"]["school_id"]."' ");
										
										if($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
										{
                                      $stu_sib=$db->getval("select location from driver where idno='".$_GET['id']."'");
									$csvString= "$stu_sib";
									
                                    $options = explode(",", $csvString);
                                    foreach($options as $val) {
                                        ${"opt".$val} = "selected=\"selected\"";
                                    	
                                    	$sib_name=$db->getval("select name from location where id='".$val."'");
                                    	?>
                                    	
                                    	<option value="<?php echo $val; ?>" <?php echo $opt.$val ?> selected ><?php echo $sib_name; ?></option>
                                    	<?php
                                    }
                                    ?> 
                                     <?php
						 
 									}

										foreach($class as $cla){
											?>
											<option <?php if($cla["id"]==$aryForm["id"])echo 'selected';?> value="<?php echo $cla["id"] ?>">
												<?php echo $cla["name"] ?>
                                            </option>
											<?php
										}
									?>
								</select>
							</div>
						</div>     
                        
						
				</div>
				

			</div>

			<div class="span4">

				<div class="well no-padding">


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


				</div>

			</div>

			<!--<div class="span4">-->

			<!--	<div class="well no-padding">-->
			
			<!--	</div>-->

			<!--</div>-->

            </div>
        </div>
        
	</div>
</form>

<?php
}
else{
	?>
	<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='./?src=Transport&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("src"=>"Transport", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("select * from driver where school_id='".$_SESSION["admin"]["school_id"]."' ");
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>Id No</th>
                    <th>Name</th>
                    <th>Location</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th colspan="2"></th>
                </tr>
            </tfoot>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['idno'];
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $clist["idno"] ?></td>
                    <td><?php echo $clist["name"] ?></td>

                    <td><?php 

					$csvString= $clist['location'];

                    $options = explode(",", $csvString);

                    // print_r($options);
                    foreach($options as $val) {

                      	echo $bus=$db->getVal("SELECT name FROM location where  id='".$val."'").",  ";

                    }
                    ?>
                    </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>Notice</b> Record not found </i>
        </div>
        <?php
	}
	?>
	</div></div></div>
	<?php
}
?>