<?php
include("../db.php");
$ch=$_REQUEST["type"];

if($ch=="fetchStudent"){
	$driverList=$db->getRow("select * from driver where bus='".$_REQUEST["name"]."'");
	echo "<table class='table' border='1' id='tblData'>";
	if(is_array($driverList) && count($driverList)>0){
	    echo "<tr rowspan='2'>";
		echo "<th>Driver Name : </th><th>".$driverList["name"]."</th>";
		echo "<th>Phone No : </th><th>".$driverList["mobile"]."</th>";
		echo "<th>Address :  ".$driverList["address"]."</th> <th>";
		if($driverList["photo"]!="")
		echo "<img class='imgs' src='".URL_ROOT."media/photo/".$driverList["photo"]."'>";
		echo "</th></tr>";
	}else{
		echo $db->getErMsg();
	}
	echo "<tr><th colspan='6'>Bus Root : ".$_REQUEST["id"]."</th></tr>";
	
	echo "<tr>
	            <th>Name </th>
	            <th>Father Name </th>
	            <th>Class </th>
	            <th>Contact </th>
	            <th>Address </th>
	            <th>Photo </th>
    	  </tr>";
	$studentList=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and bus='".$_REQUEST["name"]."' and session='".$_SESSION["admin"]["session"]."'");
	if(is_array($studentList) && count($studentList)>0){

		foreach($studentList as $sList){
    		echo "<tr>";
    		echo "
    		        <td>$sList[name]</td>
    		        <td>$sList[fname]</td>
    		        <td>$sList[class] ($sList[medium])</td>
    		        <td>$sList[mobile]</td>
    		        <td>$sList[address]</td>
    		        <td>
    		    ";
			if($sList["photo"]!="")
			echo "<img class='imgs' src='".URL_ROOT."media/photo/".$sList["photo"]."' style='width:150px;'>";
			echo "</td>";
    		echo "</tr>";
		}
	}else{
		echo "<tr><td colspan='5'>No record Found...<td></tr>";
	}
	echo "</table>";
}

if($ch=="delete_pickup"){
	$id = $_POST['id'];

    $res=$db->delete("location","where id='".$id."' ");

}


if($ch=="getdriverno"){
	$id = $_POST['id'];

    echo $no = $db->getVal("select mobile from staff where idno='".$id."' ");

}

if($ch=="delete_doc"){
   $doc = $_POST['doctype'];
    $file = $db->getVal("SELECT $doc FROM bus_detail WHERE school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_POST["id"]."'");

	unlink("../media/bus/$file") or die("Failed to <strong class='highlight'>delete</strong> file");
	$name="";
   $db->updateAry("bus_detail",array("$doc"=>$name),"where id=".$_POST["id"]);
}

if($ch=="getvehicle"){
	$route = $_POST['route'];
	$vehicles = $db->getRows("select * from bus_detail where route='$route' and school_id='".$_SESSION["admin"]["school_id"]."'");
    ?>
        <select name="old_school[]"  onchange="getpickup(<?php echo $_POST['idno'] ?>)">
        	<option value="">Select Bus</option>
        	<?php if(is_array($vehicles) && count($vehicles)>0){
				foreach($vehicles as $vehicle){
					?><option  value="<?php echo $vehicle["id"] ?>"><?php echo $vehicle["name"] ?></option><?php
				}
			} ?>
        </select>
   <?php
}

if($ch=="getpickup"){
	$route = $db->getVal("select name from location where id='$_POST[route]' ");
	$routes = $db->getRows("select * from location where name='$route' and pickup_point!='' ");
    ?>
        <select name="bus[]">
        	<option value="">Select Pickup</option>
        	<?php if(is_array($routes) && count($routes)>0){
				foreach($routes as $loc){
				    $pickupname = $db->getVal("select root from bus where id='$loc[pickup_point]' ");
					?><option <?php if($loc["id"]==$stu["location"])echo 'selected'; ?>  value="<?php echo $loc["pickup_point"] ?>"><?php echo $pickupname; ?></option><?php
				}
			} ?>
        </select>
   <?php
}

if($ch=="route_pickup"){
	$route = $db->getVal("select name from location where id='$_POST[route]' ");
	$routes = $db->getRows("select * from location where name='$route' and pickup_point!='' ");
    ?>
        <select name="bus">
        	<option value="">Select Pickup</option>
        	<?php if(is_array($routes) && count($routes)>0){
				foreach($routes as $loc){
				    $pickupname = $db->getVal("select root from bus where id='$loc[pickup_point]' ");
					?><option <?php if($loc["id"]==$stu["location"])echo 'selected'; ?>  value="<?php echo $loc["pickup_point"] ?>"><?php echo $pickupname; ?></option><?php
				}
			} ?>
        </select>
   <?php
}


?>