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


if($_POST)
{
    
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	$_POST["dob"]=$_POST["byear"]."-".$_POST["bmonth"]."-".$_POST["bdate"];
	$_POST["doj"]=$_POST["year"]."-".$_POST["month"]."-".$_POST["date"];
	unset($_POST["year"]);unset($_POST["month"]);unset($_POST["date"]);
	unset($_POST["byear"]);unset($_POST["bmonth"]);unset($_POST["bdate"]);
	if(isset($_FILES["photo"]["name"]) && !empty($_FILES["photo"]["name"])){
		$lfilename = basename($_FILES['photo']['name']);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		$lnewfile=md5(microtime()).".".$lext;
		if(move_uploaded_file($_FILES['photo']['tmp_name'],PATH_MEDIA."photo".DS.$lnewfile))
		{
			$_POST["photo"]=$lnewfile;
		}
	}
	//echo "<pre>";print_r($_POST);exit;
	if($flgEr!=TRUE && $pgAct=="add")
	{
		$_POST["school_id"]=$_SESSION["admin"]["school_id"];
$_POST["added_by"]=$_SESSION["admin"]["uid"];
 $_POST["bus"]=implode(",", $_POST['bus']);
		$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("page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
			
		}
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
	    $_POST["bus"]=implode(",", $_POST['bus']);
		$flgUp=$db->updateAry("driver",$_POST,"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."'");
		if(!is_null($flgUp))
		{
		    
			//echo $db->getlastQuery();exit;
			$_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'])!='')
{
	$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("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="dateofadmission">Date of Joining</label>
                            <div class="controls">
                                    <select class="span3" name="date" id="date">
                                        <?php
									    for($m = 01;$m <= 31; $m++){ 
                                            echo "<option value='$m'>$m</option>";
                                        }
										?> 
                                    </select>
                                    <select class="span4" name="month" id="month">
                                     <?php
									    for($m = 1;$m <= 12; $m++){ 
                                            $month =  date("F", mktime(0, 0, 0, $m)); 
                                            echo "<option value='$month'>$month</option>"; 
                                        }
									 ?> 
                                    </select>
                                    <select class="span4" name="year" id="year">
                                        <?php
									    for($m = 2000;$m <= 2020; $m++){  
                                            echo "<option value='$m'>$m</option>"; 
                                        }
										?> 
                                    </select>
                                </div>
                        </div>
                        <div class="control-group">
							<label class="control-label" for="bus">Bus</label>
							<div class="controls">
								<select class="span12" name="bus[]" id="bus" multiple="multiple">
									<option value="">select bus</option>
									<?php 
										$class=$db->getRows("select * from bus where school_id='".$_SESSION["admin"]["school_id"]."' ");
										
										if($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
										{
                                      $stu_sib=$db->getval("select bus from driver where idno='".$_GET['id']."'");
									// print_r($stu_sib);
									$csvString= "$stu_sib";
									
$options = explode(",", $csvString);
//print_r($options);
foreach($options as $val) {
	//echo $val;
    ${"opt".$val} = "selected=\"selected\"";
	
	$sib_name=$db->getval("select root from bus 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["bus"] ?>(<?php echo $cla["root"] ?>)
                                            </option>
											<?php
										}
									?>
								</select>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="sex">Sex</label>
							<div class="controls">
								<select class="span12" name="sex" id="sex">
									<option <?php if("Male"==$aryForm["sex"])echo 'selected';?> value="hindi">Male</option>
									<option <?php if("FeMale"==$aryForm["sex"])echo 'selected';?> value="english">FeMale</option>
								</select>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="experience">Experience</label>
							<div class="controls">
								<input type="text" id="experience" name="experience" class="input-block-level" value="<?php echo $aryForm["experience"]; ?>" >
							</div>
						</div>
                        
                        
                     <div class="control-group">
							<label class="control-label" for="bus">Location</label>
							<div class="controls">
								<select class="span12" name="location" id="location">
									<option value="">select location</option>
									<?php 
										$class=$db->getRows("select * from location where school_id='".$_SESSION["admin"]["school_id"]."' ");
																		
										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>
				<!-- / Forms: Content -->

			</div>
			<!-- / Forms: Box -->
			<!-- Forms: Box -->
			<div class="span4">

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

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

					<!-- Forms: Form -->
                    
						<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 class="control-group">
							<label class="control-label" for="rollno">Date of Birth</label>
                            <div class="controls">
                                    <select class="span3" name="bdate" id="bdate">
                                        <?php
									    for($m = 01;$m <= 31; $m++){ 
                                            echo "<option value='$m'>$m</option>";
                                        }
										?> 
                                    </select>
                                    <select class="span4" name="bmonth" id="bmonth">
                                     <?php
									    for($m = 1;$m <= 12; $m++){ 
                                            $month =  date("F", mktime(0, 0, 0, $m)); 
                                            echo "<option value='$month'>$month</option>"; 
                                        }
									 ?> 
                                    </select>
                                    <select class="span4" name="byear" id="byear">
                                        <?php
									    for($m = 1990;$m <= 2006; $m++){  
                                            echo "<option value='$m'>$m</option>"; 
                                        }
										?> 
                                    </select>
                                </div>
                        </div>
	 					<div class="control-group">
							<label class="control-label" for="fname">Father Name</label>
							<div class="controls">
								<input type="text" id="fname" name="fname" class="input-block-level" value="<?php echo $aryForm["fname"]; ?>" required>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="mname">Mother Name</label>
							<div class="controls">
								<input type="text" id="mname" name="mname" class="input-block-level" value="<?php echo $aryForm["mname"]; ?>" required>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="foccupation">Fathers Occupation</label>
							<div class="controls">
								<input type="text" id="foccupation" name="foccupation" class="input-block-level" value="<?php echo $aryForm["foccupation"]; ?>" required>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="qualification">Qualification</label>
							<div class="controls">
								<input type="text" id="qualification" name="qualification" class="input-block-level" value="<?php echo $aryForm["qualification"]; ?>" required>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="email">E-mail</label>
							<div class="controls">
								<input type="text" id="email" name="email" class="input-block-level" value="<?php echo $aryForm["email"]; ?>">
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="mobile">Mobile</label>
							<div class="controls">
								<input type="text" id="mobile" name="mobile" class="input-block-level" value="<?php echo $aryForm["mobile"]; ?>" required>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="address">Address</label>
							<div class="controls">
							<textarea rows="3" class="span12" name="address" id="address"><?php echo $aryForm["address"]; ?></textarea>
							</div>
						</div>
						<!-- Forms: Form Actions -->
						
						<!-- / Forms: Form Actions -->
	 
					<!-- / Forms: Form -->           

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

			</div>
			<!-- / Forms: Box -->
            <!-- Forms: Box --><?php //echo "<pre>";print_r($aryForm);echo "</pre>"; ?>
			<div class="span4">

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

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

					<!-- Forms: Form -->
						<!-- Forms: Normal input field -->
                        <?php
						if($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''){
							?>
                            <div class="control-group">
                                <label class="control-label" for="photo">Photo</label>
                                <div class="controls">
                                	<?php if($aryForm["photo"]!=""){
										?><img src="../media/photo/<?php echo $aryForm["photo"]; ?>" style="height:60px" /><?php
									}?>
                                    <a href="javascript:;" onclick="$('#photo').click()">Change Photo</a>
                                    <input type="file" name="photo" style="display:none" id="photo"/>
                                </div>
                            </div>
							
							<?php
						}else{?>
                        <div class="control-group">
							<label class="control-label" for="photo">Photo</label>
							<div class="controls">
								<input type="file" name="photo" id="photo"/>
							</div>
						</div>
                        <div id="ins">
                            
                        </div>
                        <div id="busins">
                        
                        </div>
                        <?php 
						}?>
						<!-- / Forms: Normal input field -->


						<!-- Forms: Form Actions -->
						
						<!-- / Forms: Form Actions -->
	 
					<!-- / Forms: Form -->           

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

			</div>
			<!-- / Forms: Box -->
            </div>
        </div>
        
	</div>
</form>
<!-- / Forms -->
<?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='./?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 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>Father Name</th>
                    <th>D.O.B.</th>
                    <th>D.O.J</th>
                    <th>Bus</th>
                    <th>Contact</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                	<th></th>
                    <th></th>
                    <th></th>
                    <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 echo $clist["fname"] ?></td>
                    <td><?php echo $clist["dob"] ?></td>
                    <td><?php echo $clist["doj"] ?></td>
                    <td><?php 
                    
                   $stu_sib=$db->getval("select bus from driver where idno='".$clist["idno"]."'");
									
		
									$csvString= "$stu_sib";
									
$options = explode(",", $csvString);
//print_r($options);
foreach($options as $val) { 
                    
  	echo $bus=$db->getVal("SELECT root FROM bus where  id='".$val."'").",";
                    
}
                    ?></td>
                    <td><?php echo $clist["mobile"] ?></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
}
?>