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


if(isset($_GET['type']) && $_GET['type']=="selectstudent" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$totalRoom=$db->getVal("select seat from hostelroom where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET["roomid"]."'");
	$totalAllocatedRoom=$db->getVal("select count(id) from roomallocation where school_id='".$_SESSION["admin"]["school_id"]."' and roomid='".$_GET["roomid"]."'");
	$flgEr=FALSE;
	if($totalRoom <= $totalAllocatedRoom)
	{
		$flgEr=TRUE;
		array_push($alert_err,"There are no space ho new student.".$totalRoom."<=". $totalAllocatedRoom);
	}
	if($flgEr!=TRUE)
	{
		$insAry=array("roomid"=>$_GET["roomid"],
		              "school_id"=>$_SESSION["admin"]["school_id"],"added_by"=>$_SESSION["admin"]["uid"],
					  "idno"=>$_GET["id"]);
		$res=$db->insertAry("roomallocation",$insAry);
		if(!is_null($res)) 
		{ 
			$_SESSION['msg']='Allocate Successfully'; //&action=allocateroom&id=1
			$hdetail=$db->getRow("select * from hostelroom where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET["roomid"]."'");
			sms(mydetail("mobile",$_GET["id"],"student"),"Dear Parent, Your child is successfully Alloted in room no. ".$hdetail["noofroom"]." in Hostel '".$hdetail["hostel"]."' at ".$LinksDetails["site_name"],"hostel_room_allortment");
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Hostel", "page_id"=>$pgMod,"action"=>$_GET["action"],"id"=>$_GET["id"])));
		}
		else { array_push($alert_err,$db->getErMsg().$db->getLastQuery()); }
	}
	else { array_push($alert_err,$db->getErMsg()); }
} elseif(isset($_GET['type']) && $_GET['type']=="delete" && isset($_GET['id']) && trim($_GET['id'])!=''){
	$res=$db->delete("roomallocation","where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."'");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; //&action=viewallocate&id=1
		$hdetail=$db->getRow("select * from hostelroom where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET["roomid"]."'");
			sms(mydetail("mobile",$_GET["id"],"student"),"Dear Parent, Your child is De-Allocated from room no. ".$hdetail["noofroom"]." in Hostel '".$hdetail["hostel"]."' at ".$LinksDetails["site_name"],"hostel_room_allortment");
		redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Hostel", "page_id"=>$pgMod,"action"=>"viewallocate","id"=>$_GET["roomid"])));
	}
	else { array_push($alert_err,$db->getErMsg()); }
	
}
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 hostelroom WHERE id='".$_GET['id']."'");
}else{
	$aryForm=$_POST;
}
if($pgAct=="allocateroom"){
	
	?>
	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> Allocate room to student</h3>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"Hostel", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and status=1 and issue_TC=0 and idno not in (select idno from roomallocation) order by name");
	//echo $db->getLastQuery().$db->getErMsg();
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>S.No</th>
                    <th>Name</th>
                    <th>Class</th>
                    <th>Medium</th>
                    <th>Father</th>
                    <th>Mobile</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                	<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 $i; ?></td>
                    <td><?php echo $clist["name"] ?></td>
                    <td><?php echo $clist["class"] ?></td>
                    <td><?php echo $clist["medium"] ?></td>
                    <td><?php echo $clist["fname"] ?></td>
                    <td><?php echo $clist["mobile"] ?></td>
                    <td>
                     <?php 
					 $aryPgAct['roomid']=$_GET["id"];
					 $aryPgAct['action']="allocateroom";
					 $aryPgAct['type']="selectstudent"; ?>
                         <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Select Student</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
}elseif($pgAct=="viewallocate"){
	?>
	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i> Allocate room to student</h3>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"Hostel", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and status=1 and idno in (select idno from roomallocation where roomid='".$_GET["id"]."')");
	//echo $db->getLastQuery().$db->getErMsg();
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>Id No</th>
                    <th>Name</th>
                    <th>Class</th>
                    <th>Medium</th>
                    <th>Father</th>
                    <th>Mobile</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                	<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["class"] ?></td>
                    <td><?php echo $clist["medium"] ?></td>
                    <td><?php echo $clist["fname"] ?></td>
                    <td><?php echo $clist["mobile"] ?></td>
                    <td>
                     <?php //&action=viewallocate&=1
					 $aryPgAct['roomid']=$_GET["id"];
					 $aryPgAct['type']="delete"; ?>
                         <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-danger">Remove Student</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
}else{
	?>
	<div class="row-fluid"><div class="span12"><div class="top-bar">
    <h3><i class="icon-eye-open"></i>View All Room</h3>
    </div>
    <div class="well no-padding">
	<?php
	$aryPgAct=array("src"=>"Hostel", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("SELECT * FROM hostelroom  where school_id='".$_SESSION["admin"]["school_id"]."' "); $i=0;
	if(is_array($aryList) && count($aryList)>0)
	{
		?>

        <table class="table" id="example">
            <thead>
                <tr>
                    <th>Hostel Name</th>
                    <th>Room No</th>
                    <th>No of Seat</th>
                    <th>Ablaible Seat</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['id'];
			$totalAllocatedRoom=$db->getVal("select count(id) from roomallocation where school_id='".$_SESSION["admin"]["school_id"]."' and roomid='".$clist["id"]."'");
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $clist["hostel"] ?></td>
                    <td><?php echo $clist["noofroom"] ?></td>
                    <td><?php echo $clist["seat"] ?></td>
                    <td><?php echo $balanceRoom=($clist["seat"]-$totalAllocatedRoom) ?></td>
                    <td>
                    <?php $aryPgAct['action']="viewallocate"; ?>
                         <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">View Student</button></a>
                    <?php if($balanceRoom!=0){$aryPgAct['action']="allocateroom"; ?>
                         <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Allocate student</button></a><?php } ?>
                     
                    </td>
                </tr>
            <?php } ?>
            </tbody>
            <tfoot>
                <tr>
                	<th></th>
                    <th colspan="4"></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 -->