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


if($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->updateAry("book",array("status"=>0),"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."'");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully'; }
	else { array_push($alert_err,$db->getErMsg()); }
	redirect(URL_ADMIN_HOME.getQueryString(array("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 book WHERE school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."'");
}
if($pgAct=="issue" && isset($_GET['id']) && trim($_GET['id']) !='')
{ 
	if(isset($_SESSION['form']))
	{
		$aryForm=$_SESSION['form'];
		unset($_SESSION['form']);
	}
?>
<!-- Forms --> 

<?php  
$aryForm=$db->getRow("select * from book where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET["id"]."'");//echo $db->getLastQuery(); echo $db->getErMsg();print_r($aryForm);?>
     <div class="row-fluid">
        
        <div style="clear:both"></div>
            <div class="top-bar">
                <h3><i class="icon-cog"></i> Issue to Student</h3>
            </div>
            
			<!-- Forms: Box -->
			
            <div class="well no-padding"><div class="span12">
<table class="table">
        	<tr style="background-color:#f9f9f9">
            	<td width="100px">Book Name</td>
                <td style="border-right:2px solid #F00"><?php echo $aryForm["title"] ?></td>
                
                <td width="100px">Class</td>
                <td style="border-right:2px solid #F00"><?php echo $aryForm["class"] ?></td>
                
                <td width="100px">Price</td>
                <td style="border-right:2px solid #F00"><?php echo $aryForm["price"] ?></td>
                
                <td>Issue date</td>
                <td style="border-right:2px solid #F00"><input type="date" id="issue_day" value="<?php echo date("Y-m-d") ?>" name="issue_day" class="input-block-level" ></td>
            </tr>
            <tr style="background-color:#f9f9f9">
            	<td width="100px">Publisher</td>
                <td style="border-right:2px solid #F00"><?php echo $aryForm["pname"] ?></td>
                
                <td width="100px">Category</td>
                <td style="border-right:2px solid #F00"><?php echo $aryForm["caltegory"] ?></td>
                
                <td width="100px">Publish Date</td>
                <td style="border-right:2px solid #F00"><?php echo $aryForm["pdate"] ?></td>
                
                <td>Return Date</td>
                <td style="border-right:2px solid #F00"><input type="date" id="return_day" name="return_day" class="input-block-level" ></td>
            </tr>
        </table>
				
<?php
$aryList=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."' and status=1 and issue_TC=0 order by name");
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>Serial 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"]."(".$clist['section'].")"; ?></td>
                    <td><?php echo $clist["medium"] ?></td>
                    <td><?php echo $clist["fname"] ?></td>
                    <td><?php echo $clist["mobile"] ?></td>
                    <td>
                         <button type="button" onclick="issueBook('<?php echo $clist["idno"] ?>')" class="btn btn-info">Issue Book</button>
                    </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>
        
	</div>
<!-- / 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='./?page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add <?php echo $pgMod; ?></button>
    <button style="float:right; margin-top:5px" onclick="printDiv('printableDiv')" class="btn btn-primary" type="button">Print</button>
    </div>
    <div class="well no-padding" id="printableDiv">
	<?php
	$aryPgAct=array("page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("select * from book where school_id='".$_SESSION["admin"]["school_id"]."' and status=1");
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>Serial No</th>
                    <th>Title</th>
                    <th>Category</th>
                    <th>Class</th>
                    <th>Publisher</th>
                    <th>No of Copy</th>
                    <th>Remaining Book</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tfoot>
                <tr>
                	<th></th>
                    <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'];$totalIssue="";
			$totalIssue=(int)$db->getVal("select count(id) from issueBook where school_id='".$_SESSION["admin"]["school_id"]."' and bid='".$clist["idno"]."' and status=0");
			$remaining=(int)$clist["nocopy"]-$totalIssue;
			$style=="";echo $totalIssue;
			if($remaining==0)
				$style="style='color:#FF0000'";
			elseif($totalIssue!=0)
				$style="style='color:#009900'";
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>" <?php echo $style; ?>>
                    <td><?php echo $i; ?></td>
                    <td><?php echo $clist["title"] ?></td>
                    <td><?php echo $clist["category"] ?></td>
                    <td><?php echo $clist["class"] ?></td>
                    <td><?php echo $clist["pname"] ?></td>
                    <td><?php echo $clist["nocopy"] ?></td>
                     <td><?php echo $remaining; ?></td>
                    <td>
                    <span class="label"></span>
                     <?php if($remaining!=0){ 
					 	$aryPgAct['action']="issue"; ?>
                         <a style="float:left" href="<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>"><button type="button" class="btn btn-info">Issue</button></a><?php } ?>
                    </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 issueBook(sid){
		var idate=$("#issue_day").val();
		var rdate=$("#return_day").val();
		var bid='<?php echo $_GET["id"] ?>';
		$.post( "<?php echo URL_ROOT ?>Library/ajax.php", { type: "issuebook", idate: idate, rdate:rdate, bid:bid, sid:sid} , function( data ) {
			if(data=="")
				window.location='?page_id=issue';
			else
				alert(data)
		});
	}
</script>