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


if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	
	if(!isset($_POST['name']) || trim($_POST['name'])=='')
	{
		$flgEr=TRUE;
		array_push($alert_err,"Please enter location Name.");
	}

	if(isset($_FILES["adharphoto"]["name"]) && !empty($_FILES["adharphoto"]["name"])){
		$lfilename = basename($_FILES['adharphoto']['name']);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		$lnewfile=md5(microtime()).".".$lext;
		if(move_uploaded_file($_FILES['adharphoto']['tmp_name'],"media/bus/".$lnewfile))
		{
			$_POST["adharphoto"]=$lnewfile;
		}
    	}
    	
//     	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'],"media/bus/".$lnewfile))
// 		{
// 			$_POST["photo"]=$lnewfile;
// 		}
//     	}
    	
    	if(isset($_FILES["driving_license"]["name"]) && !empty($_FILES["driving_license"]["name"])){
		$lfilename = basename($_FILES['driving_license']['name']);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		$lnewfile=md5(microtime()).".".$lext;
		if(move_uploaded_file($_FILES['driving_license']['tmp_name'],"media/bus/".$lnewfile))
		{
			$_POST["driving_license"]=$lnewfile;
		}
    	}

        if(isset($_FILES["insurance"]["name"]) && !empty($_FILES["insurance"]["name"])){
		$lfilename = basename($_FILES['insurance']['name']);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		$lnewfile=md5(microtime()).".".$lext;
		if(move_uploaded_file($_FILES['insurance']['tmp_name'],"media/bus/".$lnewfile))
		{
			$_POST["insurance"]=$lnewfile;
		}
    	}	

        if(isset($_FILES["road_tax"]["name"]) && !empty($_FILES["road_tax"]["name"])){
		$lfilename = basename($_FILES['road_tax']['name']);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		$lnewfile=md5(microtime()).".".$lext;
		if(move_uploaded_file($_FILES['road_tax']['tmp_name'],"media/bus/".$lnewfile))
		{
			$_POST["road_tax"]=$lnewfile;
		}
    	}

        if(isset($_FILES["pollution"]["name"]) && !empty($_FILES["pollution"]["name"])){
		$lfilename = basename($_FILES['pollution']['name']);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		$lnewfile=md5(microtime()).".".$lext;
		if(move_uploaded_file($_FILES['pollution']['tmp_name'],"media/bus/".$lnewfile))
		{
			$_POST["pollution"]=$lnewfile;
		}
    	}

        if(isset($_FILES["fitness"]["name"]) && !empty($_FILES["fitness"]["name"])){
		$lfilename = basename($_FILES['fitness']['name']);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		$lnewfile=md5(microtime()).".".$lext;
		if(move_uploaded_file($_FILES['fitness']['tmp_name'],"media/bus/".$lnewfile))
		{
			$_POST["fitness"]=$lnewfile;
		}
    	}

        if(isset($_FILES["permit"]["name"]) && !empty($_FILES["permit"]["name"])){
		$lfilename = basename($_FILES['permit']['name']);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		$lnewfile=md5(microtime()).".".$lext;
		if(move_uploaded_file($_FILES['permit']['tmp_name'],"media/bus/".$lnewfile))
		{
			$_POST["permit"]=$lnewfile;
		}
    	}

	if($flgEr!=TRUE && $pgAct=="add")
	{
       $_POST["school_id"]=$_SESSION["admin"]["school_id"];
        $_POST["added_by"]=$_SESSION["admin"]["uid"];
		$flgIn=$db->insertAry("bus_detail",$_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=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
	    
		$flgUp=$db->updateAry("bus_detail",$_POST,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_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'])!='')
{
    $files = $db->getRow("SELECT * FROM bus_detail WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET["id"]."'");

    unlink("../media/bus/$files[insurance]");
    unlink("../media/bus/$files[road_tax]");
    unlink("../media/bus/$files[pollution]");
    unlink("../media/bus/$files[fitness]");
    unlink("../media/bus/$files[permit]");
    unlink("../media/bus/$files[adharphoto]");
    unlink("../media/bus/$files[driving_license]");

	$res=$db->delete("bus_detail","where school_id='".$_SESSION["admin"]["school_id"]."' and 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 bus_detail WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_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="name">Vehicle Name</label>
							<div class="controls">
								<input type="text" id="name" name="name" class="input-block-level" required value="<?php if($_POST)echo $_POST["name"]; else echo $aryForm["name"] ?>">
							</div>
						</div>

                       <div class="control-group">
							<label class="control-label" for="vehicle_no">Vehicle No</label>
							<div class="controls">
								<input type="text" id="vehicle_no" name="vehicle_no" class="input-block-level"  value="<?php if($_POST)echo $_POST["vehicle_no"]; else echo $aryForm["vehicle_no"] ?>">
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="vehicle_type">Vehicle Type</label>
							<div class="controls">
								<input type="text" id="vehicle_type" name="vehicle_type" class="input-block-level"  value="<?php if($_POST)echo $_POST["vehicle_type"]; else echo $aryForm["vehicle_type"] ?>">
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label">Vehicle Capacity</label>
							<div class="controls">
								<input type="text" name="no_of_stduent" class="input-block-level"  value="<?php if($_POST)echo $_POST["no_of_stduent"]; else echo $aryForm["no_of_stduent"] ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label">Route</label>
							<div class="controls">
								<select class="span12" name="route" >
									<option value="">select</option>
									<?php
										$route=$db->getRows("SELECT * FROM location group by name ");
										foreach($route as $d){
											?>
											<option <?php if($d["id"]==$aryForm["route"])echo 'selected';?> value="<?php echo $d["id"] ?>">
												<?php echo $d["name"] ?>
                                            </option>
											<?php
										}
									?>
								</select>
								<!--<input type="text" id="root" name="root" class="input-block-level"  value="<?php if($_POST)echo $_POST["root"]; else echo $aryForm["root"] ?>">-->
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label">Mileage</label>
							<div class="controls">
								<input type="text" name="mileage" class="input-block-level"  value="<?php if($_POST)echo $_POST["mileage"]; else echo $aryForm["mileage"] ?>">
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="km_per_day">KM Per Day</label>
							<div class="controls">
								<input type="text" id="km_per_day" name="km_per_day" class="input-block-level"  value="<?php if($_POST)echo $_POST["km_per_day"]; else echo $aryForm["km_per_day"] ?>">
							</div>
						</div>



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

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

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

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

                        <div class="control-group">
							<label class="control-label" for="driver_name">Driver Name</label>
							<div class="controls">
								<select class="span12" name="driver_name" id="driver_name" onchange="getdriverno(this.value)">
									<option value="">select</option>
									<?php
										$driver=$db->getRows("select * from staff where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' ");
										foreach($driver as $d){
											?>
											<option <?php if($d["idno"]==$aryForm["driver_name"])echo 'selected';?> value="<?php echo $d["idno"] ?>">
												<?php echo $d["name"] ?>
                                            </option>
											<?php
										}
									?>
								</select>

							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="driver_no">Driver No</label>
							<div class="controls">
								<input type="text" id="driver_no" name="driver_no" class="input-block-level" value="<?php if($_POST)echo $_POST["driver_no"]; else echo $aryForm["driver_no"] ?>" readonly>
							</div>
						</div>

						<div class="control-group">
							<label class="control-label">Transport In. I</label>
							<div class="controls">
								<input type="text" name="conductor_name" class="input-block-level"  value="<?php if($_POST)echo $_POST["conductor_name"]; else echo $aryForm["conductor_name"] ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" >Mobile No</label>
							<div class="controls">
								<input type="text" name="conductor_no" class="input-block-level" value="<?php if($_POST)echo $_POST["conductor_no"]; else echo $aryForm["conductor_no"] ?>">
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label">Transport In. II</label>
							<div class="controls">
								<input type="text" name="incharge_name" class="input-block-level"  value="<?php if($_POST)echo $_POST["incharge_name"]; else echo $aryForm["incharge_name"] ?>">
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" >Mobile No</label>
							<div class="controls"> 
								<input type="text" name="incharge_no" class="input-block-level"  value="<?php if($_POST)echo $_POST["incharge_no"]; else echo $aryForm["incharge_no"] ?>">
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label">Insurance Validity</label>
							<div class="controls"> 
							    <input type="date" name="insurance_validity" value="<?php if($_POST)echo $_POST["insurance_validity"]; else echo $aryForm["insurance_validity"] ?>" class="input-block-level" />
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label">RoadTax Validity</label>
							<div class="controls"> 
							    <input type="date" name="tax_validity" value="<?php if($_POST)echo $_POST["tax_validity"]; else echo $aryForm["tax_validity"] ?>" class="input-block-level" />
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label">Pollution Validity</label>
							<div class="controls"> 
							    <input type="date" name="poll_validity" value="<?php if($_POST)echo $_POST["poll_validity"]; else echo $aryForm["poll_validity"] ?>" class="input-block-level" />
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label">Fitness Validity</label>
							<div class="controls"> 
							    <input type="date" name="fitness_validity" value="<?php if($_POST)echo $_POST["fitness_validity"]; else echo $aryForm["fitness_validity"] ?>" class="input-block-level" />
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label" >Permit Validity</label>
							<div class="controls"> 
							    <input type="date" name="permit_validity" value="<?php if($_POST)echo $_POST["permit_validity"]; else echo $aryForm["permit_validity"] ?>" class="input-block-level" />
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label" >DL Validity</label>
							<div class="controls"> 
							    <input type="date" name="dl_validity" value="<?php if($_POST)echo $_POST["dl_validity"]; else echo $aryForm["dl_validity"] ?>" class="input-block-level" />
							</div>
						</div>
					<!-- / Forms: Form -->           

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

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

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

       <!--                 <div class="control-group">-->
							<!--<label class="control-label" for="photo">Photo</label>-->
							<!--<div class="controls">-->
								<?php /*if($aryForm["photo"]!=""){
    										?><a href="media/bus/<?php echo $aryForm["photo"]; ?>" download>
    										<img src="media/bus/<?php echo $aryForm["photo"]; ?>"  style="width:150px;height:150px;" /></a><?php
        									}?><br>
            								<input type="file" name="photo" id="photo"/><br>
            								<?php if($aryForm["photo"]!=""){?>
        									<a href="" style="color: #00f;" onclick="deletephoto(<?= $aryForm[id] ?>)">Delete Photo</a>
        									<?php } */?>
						<!--	</div>-->
						<!--</div>-->
						
						<div class="control-group">
							<label class="control-label" for="photo">Aadhaar</label>
							<div class="controls">
							 <?php if($aryForm["adharphoto"]!=""){
        									?><a href="media/bus/<?php echo $aryForm["adharphoto"]; ?>" download>
        									<img src="media/bus/<?php echo $aryForm["adharphoto"]; ?>" style="width:150px;height:150px;" /></a><?php
        									}?><br>
            								<input type="file" name="adharphoto" id="adharphoto"/><br>
            								<?php if($aryForm["adharphoto"]!=""){?>
        									<a href="" style="color: #00f;" onclick="deletedoc(<?= $aryForm[id] ?>, 'adharphoto')">Delete Aadhaar</a>
								<?php } ?>
							</div>
						</div>
						
						<div class="control-group">
							<label class="control-label" for="photo">Insurance</label>
							<div class="controls">
								<?php if($aryForm["insurance"]!=""){
    										?><a href="media/bus/<?php echo $aryForm["insurance"]; ?>" download>
    										<img src="media/bus/<?php echo $aryForm["insurance"]; ?>"  style="width:150px;height:150px;" /></a><?php
        									}?><br>
            								<input type="file" name="insurance" id="insurance"/><br>
            								<?php if($aryForm["insurance"]!=""){?>
        									<a href="" style="color: #00f;" onclick="deletedoc(<?= $aryForm[id] ?>, 'insurance')">Delete Insurance</a>
        									<?php } ?>
							</div>
						</div>

						<div class="control-group">
							<label class="control-label" for="photo">Road Tax</label>
							<div class="controls">
								<?php if($aryForm["road_tax"]!=""){
    										?><a href="media/bus/<?php echo $aryForm["road_tax"]; ?>" download>
    										<img src="media/bus/<?php echo $aryForm["road_tax"]; ?>"  style="width:150px;height:150px;" /></a><?php
        									}?><br>
            								<input type="file" name="road_tax" id="road_tax"/><br>
            								<?php if($aryForm["road_tax"]!=""){?>
        									<a href="" style="color: #00f;" onclick="deletedoc(<?= $aryForm[id] ?>, 'road_tax')">Delete Road Tax</a>
        									<?php } ?>
							</div>
						</div>


						<div class="control-group">
							<label class="control-label" for="photo">Pollution</label>
							<div class="controls">
								<?php if($aryForm["pollution"]!=""){
    										?><a href="media/bus/<?php echo $aryForm["pollution"]; ?>" download>
    										<img src="media/bus/<?php echo $aryForm["pollution"]; ?>"  style="width:150px;height:150px;" /></a><?php
        									}?><br>
            								<input type="file" name="pollution" id="pollution"/><br>
            								<?php if($aryForm["pollution"]!=""){?>
        									<a href="" style="color: #00f;" onclick="deletedoc(<?= $aryForm[id] ?>,'pollution')">Delete Pollution</a>
        									<?php } ?>
							</div>
						</div>	

						<div class="control-group">
							<label class="control-label" for="photo">Fitness</label>
							<div class="controls">
								<?php if($aryForm["fitness"]!=""){
    										?><a href="media/bus/<?php echo $aryForm["fitness"]; ?>" download>
    										<img src="media/bus/<?php echo $aryForm["fitness"]; ?>"  style="width:150px;height:150px;" /></a><?php
        									}?><br>
            								<input type="file" name="fitness" id="fitness"/><br>
            								<?php if($aryForm["fitness"]!=""){?>
        									<a href="" style="color: #00f;" onclick="deletedoc(<?= $aryForm[id] ?>,'fitness')">Delete Fitness</a>
        									<?php } ?>
							</div>
						</div>

						<div class="control-group">
							<label class="control-label" for="photo">Road Permit</label>
							<div class="controls">
								<?php if($aryForm["permit"]!=""){
    										?><a href="media/bus/<?php echo $aryForm["permit"]; ?>" download>
    										<img src="media/bus/<?php echo $aryForm["permit"]; ?>"  style="width:150px;height:150px;" /></a><?php
        									}?><br>
            								<input type="file" name="permit" id="permit"/><br>
            								<?php if($aryForm["permit"]!=""){?>
        									<a href="" style="color: #00f;" onclick="deletedoc(<?= $aryForm[id] ?>,'permit')">Delete Permit</a>
        									<?php } ?>
							</div>
						</div>

						<div class="control-group">
							<label class="control-label" for="photo">Driving Licence</label>
							<div class="controls">
								<?php if($aryForm["driving_license"]!=""){
    										?><a href="media/bus/<?php echo $aryForm["driving_license"]; ?>" download>
    										<img src="media/bus/<?php echo $aryForm["driving_license"]; ?>"  style="width:150px;height:150px;" /></a><?php
        									}?><br>
            								<input type="file" name="driving_license" id="driving_license"/><br>
            								<?php if($aryForm["driving_license"]!=""){?>
        									<a href="" style="color: #00f;" onclick="deletedoc(<?= $aryForm[id] ?>,'driving_license')">Delete Driving License</a>
        									<?php } ?>
							</div>
						</div>
						
                        <div id="ins">
                            
                        </div>
                        <div id="busins">
                        
                        </div>
                        
					<!-- / 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='./?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 bus_detail where school_id='".$_SESSION["admin"]["school_id"]."'"); $i=0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example"> 
            <thead>
                <tr>
                    <th>S.No.</th>
                    <th>Vehicle</th>
                    <th>Vehicle No.</th>
                    <th>Route</th>
                    <th>Driver</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['id'];$aryPgAct['page_id']=$pgMod;
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $i; ?></td>
                    <td><?php echo $clist["name"] ?></td>
                    <td><?php echo $clist["vehicle_no"] ?></td>
                    <td><?php echo $db->getVal("select name from location where id='$clist[route]' "); ?></td>
                    <td><?php echo $db->getVal("select name from staff where idno='$clist[driver_name]' "); ?></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
}
?>

<script>
    function deletedoc(id, doctype){

		if (confirm("Are you sure?") == true) {
		  $.post( "Transport/ajax.php", { type: "delete_doc", id: id, doctype: doctype  } , function( data ){

	        });
		}
    }

function getdriverno(id){
    $.post( "Transport/ajax.php", { type: "getdriverno", id: id  } , function( data ){
        $("#driver_no").val(data);
    		});
}

</script>