<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<script>
$(document).ready(function(){
  $("#myInput").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $("#myTable tr").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});

$(document).ready(function(){
  $("#myInput2").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $("#myTable2 tr").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});
</script>
<?php
$pgMod = "assign_hostel";
$pgAct = "view";

if(isset($_POST["update_busroot"])){

    // print_r($_POST);
    // die();



    if(is_array($_POST["idno"]) && count($_POST["idno"])>0){
        $x = 0;
        foreach($_POST["idno"] as $idno) {

            $hid = $_POST["hostel"][$x];
            
            $checkhostel = $db->getVal("select hostel from student where idno='$idno' and session='".$_SESSION["admin"]["session"]."' ");
        
            if($checkhostel == $hid){
                
            } else {

            $saveAry = array(
                            "hostel"  => $hid,
        				);

            $flgUps=$db->updateAry("student", $saveAry, " where idno='$idno' and session='".$_SESSION["admin"]["session"]."' ");

        	$insdata = $db->getRow("select * from hostelinstallment where idno='$idno' ");

            $hostelDetail   =   $db->getRow("select fee,installment from hostel where id='$hid' ");
            $totalFee    =   $hostelDetail["fee"];
            $installment =   $hostelDetail["installment"];
            $perFee      =   $totalFee/$installment;

            if($hid == ""){
                for($i=1;$i<=12;$i++){

                    if($insdata["ip".$i] == ""){
                        $datahostel = array( "i$i"     =>  "",
                                              "ip$i"    =>  ""
                                        );
                        $flgInstall=$db->updateAry("hostelinstallment",$datahostel,"where session='".$_SESSION["admin"]["session"]."' and idno='".$idno."'");
                    }

				}

                $hosteltotal=array("total"=>"");
                $flgUp=$db->updateAry("hostelinstallment",$hosteltotal,"where idno='".$idno."' and session='".$_SESSION["admin"]["session"]."'");

            } else {
                $student = $db->getRow("select * from student where idno='$idno' and session='".$_SESSION["admin"]["session"]."' ");

                if($totalFee>0 && $installment>0 ){

                	$instalmenthostelAry = array(
                         "idno"     =>  $idno,
						 "class"    =>  $student["class"],
						 "hid"      =>  $hid,
						 "total"    =>  $totalFee,
						 "discount" =>  0,
						 'session'  =>  $_SESSION["admin"]["session"],
						 'school_id'=>  $_SESSION["admin"]["school_id"],
						 'added_by' =>  $_SESSION["admin"]["uid"],
						 "status"   =>  1
                	);
                    if(count($insdata)>1){
                        $ary = array("total"=>$totalFee);
                        $flgUp = $db->updateAry("hostelinstallment", $ary,"where session='".$_SESSION["admin"]["session"]."' and idno='".$idno."'");
                    } else {
                        $flgIn = $db->insertAry("hostelinstallment", $instalmenthostelAry);
                    }

                    for($ii=1;$ii<=$installment;$ii++){
                        $dataHostel = array( "i$ii" => number_format($perFee, 2, '.', '') );
                        $flgInstall=$db->updateAry("hostelinstallment",$dataHostel,"where session='".$_SESSION["admin"]["session"]."' and idno='".$idno."'");
    				}

    		    }
            }
            
            
            }

            $x++;
        }

        if(!is_null($flgUp)){
            $_SESSION['msg']='update Successfully';
        }
        redirect();
    }

}
?>

<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: 100% !important;overflow: inherit !important;} 
</style>
<?php   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   }   ?>
    <form method="get" action="">
    <input type="hidden" name="src" value="Hostel" />
    <input type="hidden" name="page_id" value="<?php echo $pgMod; ?>" />
	<div class="row-fluid">
            <div class="span12">
            	<div class="top-bar">
					<h3><i class="icon-cog"></i>Search Student Record</h3>
                    <div>
                        <button type="submit" name="update_root" class="btn btn-primary">Assign Hostel</button>
					</div>
				</div>
				<div class="well no-padding">
                	<div class="controls controls-row">
                        
                        <div class="control-group span4">
                            <label class="control-label" for="class">Class</label>
                            <div class="controls">
                                <select name="class" id="class">
                                    <option value="">Class</option>
                                    <?php 
                                        $class=$db->getRows("select * from class where school_id='".$_SESSION["admin"]["school_id"]."' ");
                                        foreach($class as $cla){
                                            ?>
                                            <option <?php if($cla["class"]==$_GET["class"])echo 'selected';?> value="<?php echo $cla["class"] ?>">
                                                <?php echo $cla["class"] ?>
                                            </option>
                                            <?php
                                        }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="control-group span4">
                            <label class="control-label" for="class">Section</label>
                            <div class="controls">
                                <select name="section" id="section" >
                                    <option value="">Section</option>
                                    <?php 
                                        $class=$db->getRows("select * from section where school_id='".$_SESSION["admin"]["school_id"]."'");
                                        foreach($class as $cla){
                                            ?>
                                            <option <?php if($cla["name"]==$_GET["section"])echo 'selected';?> value="<?php echo $cla["name"] ?>">
                                                <?php echo $cla["name"] ?>
                                            </option>
                                            <?php
                                
                                        }
                                    ?>
                                </select>
                            </div>
                        </div>
                       
                    </div>
                    <br>
				</div>
				<!-- / Forms: Content -->
			</div>
    </div>
    </form>
	<?php
    if(isset($_GET["update_root"])){
		?><form method="post" action="">
		<div class="row-fluid">
            <div class="span12">
            	<div class="top-bar">
					<h3><i class="icon-cog"></i>Add Result of students</h3>
					<button type="submit" name="update_busroot" class="btn btn-primary" style="float:right;"> Save</button>
				</div>
				<div class="well no-padding">
                	<div class="controls controls-row">
		<center><input class="form-control" id="myInput" type="text" placeholder="Search.."></center>
		<?php
		$whereC=array();
		if(isset($_GET["class"]) && $_GET["class"]!="")
			$whereC[]=" class='".$_GET["class"]."' ";

		if(isset($_GET["section"]) && $_GET["section"]!="")
			$whereC[]=" section='".$_GET["section"]."' ";

		$whereM=""; 
		if(is_array($whereC) && count($whereC)>0)
			$whereM=" where ".implode(" and ",$whereC);

		$allstudent=$db->getRows("select * from student $whereM and school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and issue_TC=0 and status=1 order by name ");
		
		if(is_array($allstudent) && count($allstudent)>0){
			?>
			<table class="table table-bordered table-striped">
    		    <thead> 
        			<tr>
                        <td>SNo.</td>
                        <td>Student</td>
                        <td>Father Name</td>
                        <td>Address</td>
                        <td>Hostel</td>
                    </tr>
                </thead>
            <tbody id="myTable">
			<?php $i=0;
			foreach($allstudent as $stu){ $i++;
				$aryInstall=$db->getRow("select * from hostelinstallment where idno='".$stu["idno"]."' and session='".$_SESSION["admin"]["session"]."'");
				$totalhostelpayfee=0;
				for($ii=1;$ii<=12;$ii++){
					$iip    =   "ip".$ii;
					$totalhostelpayfee+=   $aryInstall[$iip];
				}
				?>
				<tr>
                	<td><?php echo $i; ?></td>
                	<td><?php echo $stu["name"]; ?></td>
					<td><?php echo $stu["fname"]; ?></td>
					<td><?php echo $stu["address"]; ?></td>
                    <td>
                    <input type="hidden" name="idno[]" required  value="<?php echo $stu["idno"] ?>" readonly="readonly" />
                    <div id="hostel_<?php echo $stu['idno']; ?>"> 
                       <?php
                       $hostel=$db->getRows("select * from hostel where  school_id='".$_SESSION["admin"]["school_id"]."'");
                       if(empty($totalhostelpayfee)){ ?> 
                        <select name="hostel[]">
							<option value="">--Select--</option>
							<?php 
								
								foreach($hostel as $hos){
									?>
									<option <?php if($hos["id"]==$stu["hostel"])echo 'selected';?> value="<?php echo $hos["id"] ?>">
										<?php echo $hos["name"] ?>
                                    </option>
									<?php
								}
							?>
						</select>
                    <?php } else {
                        ?>
                    <select name="hostel[]" >
                    	<option value="">--Select--</option>
                    	<?php   foreach($hostel as $hos){ ?>
							<option <?php if($hos["id"]==$stu["hostel"])echo 'selected'; ?>  value="<?php echo $hos["id"] ?>"><?php echo $hos["name"]; ?></option>
						<?php } ?>
                    </select>
                    <span style="color:red;float:right;font-size:11px;">Paid</span>
                    <?php
                    }?>
                    </div>
                </td>

                </tr>
				<?php
			}
			?></tbody></table><?php
		}
		?></div></div></div></div></form>
		
		<?php
	}

	?>
</body>
</html>	
<script type="text/javascript">
// Material Select Initialization
$(document).ready(function() {
$('.mdb-select').materialSelect();
});
    
    
    
    function filterRecord(){
 alert();
 var classname = document.getElementById("class").value;
 var subject = document.getElementById("mySelect1").value;
 var subsubject = document.getElementById("subSubjectid").value;  
 var chapter = document.getElementById("chapter").value;   

  document.getElementById('filterdata').innerHTML = 'hee';

   // $.post( "Notice/ajax.php", { type: "filterRecord" ,classname: classname ,subject: subject ,subsubject: subsubject , chapter: chapter} , function( data ) {
//alert(data);
  //document.getElementById('filterdata').innerHTML = data;
  //  });
}

	//$(document).ready(function() {
    //$('#example').DataTable();
//} ); 

function getvehicle(route, idno){
    $.post( "Transport/ajax.php", { type: "getvehicle" ,route: route,  idno:idno} , function( data ) {
        $("#vehicle_"+idno).html(data);
    });
    
}

function getpickup(idno){
    var route = document.getElementById('route_'+idno).value;
 
    $.post( "Transport/ajax.php", { type: "getpickup" ,route: route,  idno:idno} , function( data ) {
        $("#pickup_"+idno).html(data);
    });

}

</script>