<?php
$pgMod="bus";
$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($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"=>"Student","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"=>"Student","page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		
		}
	} 
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$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"=>"Student","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']) !=''))
{
?>
<!-- Forms --> 
	<div class="row-fluid">
        <form method="post" action="" enctype="multipart/form-data">
			<!-- Forms: Box -->
			<div class="span12">

				<!-- Forms: Top Bar -->
				<div class="top-bar">
					<h3><i class="icon-cog"></i> Create a New Class</h3>
				</div>
				<!-- / Forms: Top Bar -->

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

                        <div class="control-group">
							<div class="controls">
							    
							    <table>
							        <tr style="text-align:left;color: rgb(21, 20, 5);font-weight: 900;font-size:17px;">
							            <th>Driver Aadhaar</th>
							            <th>Driver Photo</th>
							            <th>Driving Licence</th>
							        </tr>
							        <tr>
    						            <td>
        					                <?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="deleteadharphoto(<?= $aryForm[id] ?>)">Delete Aadhaar</a>
        									<?php } ?>
							            </td>
							            <td>
							                <?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 } ?>
							            </td>
							            <td>
							                <?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="deletedl(<?= $aryForm[id] ?>)">Delete Driving License</a>
        									<?php } ?>
							            </td>
							        </tr>
							    </table>
							    
							</div>
						</div>

						<div class="control-group">
							<label class="control-label" for="name">Location 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="driver_name">Driver Name</label>
							<div class="controls">
								<input type="text" id="driver_name" name="driver_name" class="input-block-level"  value="<?php if($_POST)echo $_POST["driver_name"]; else echo $aryForm["driver_name"] ?>">
							</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"] ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="root">Root</label>
							<div class="controls">
								<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" for="Incharge Name">Transport Incharge I</label>
							<div class="controls">
								<input type="text" id="category" name="category" class="input-block-level"  value="<?php if($_POST)echo $_POST["category"]; else echo $aryForm["category"] ?>">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="vehicle_sch_no">Mobile No</label>
							<div class="controls">
								<input type="text" id="vehicle_sch_no" name="vehicle_sch_no" class="input-block-level" value="<?php if($_POST)echo $_POST["vehicle_sch_no"]; else echo $aryForm["vehicle_sch_no"] ?>">
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="conductor_name">Transport Incharge II</label>
							<div class="controls">
								<input type="text" id="conductor_name" 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" for="conductor_no">Mobile No</label>
							<div class="controls">
								<input type="text" id="conductor_no" 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" 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" for="miladge">Mileage</label>
							<div class="controls">
								<input type="text" id="miladge" name="miladge" class="input-block-level"  value="<?php if($_POST)echo $_POST["miladge"]; else echo $aryForm["miladge"] ?>">
							</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 class="control-group">
							<label class="control-label" for="no_of_stduent">Max Student in Bus</label>
							<div class="controls">
								<input type="text" id="no_of_stduent" 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="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{
	?>
	<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=Student&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"=>"Student","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>Name</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 $clist["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>
            <tfoot>
                <tr>
                	<th></th>
                    <th colspan="3"></th>
                </tr>
            </tfoot>
        </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
}
?>
<!-- / Forms -->


<script>
    function deleteadharphoto(id)
          {
			if (confirm("Are you sure?") == true) {
			  $.post( "ajax.php", { type: "delete_driveradharphoto", id: id  } , function( data ){			
		    });
			}
		  }
		  
		  function deletephoto(id)
          {
			if (confirm("Are you sure?") == true) {
			  $.post( "ajax.php", { type: "delete_driverphoto", id: id  } , function( data ){			
		});
			}
		  }
		  
		  function deletedl(id)
          {
			if (confirm("Are you sure?") == true) {
			  $.post( "ajax.php", { type: "delete_drivinglicense", id: id  } , function( data ){			
		});
			}
		  }
		  
</script>