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

if($_POST)
{
    // print_r($_POST);
    // die();
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;

	if($flgEr!=TRUE && $pgAct=="add")
	{
        $_POST["added_by"]=$_SESSION["admin"]["uid"];

		$flgIn=$db->insertAry("issuebook",$_POST);
		if(!is_null($flgIn))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Library", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
// 		$flgUp=$db->updateAry("issuebook",$_POST,"where id='".$_GET['id']."'");
		if(!is_null($flgUp))
		{
			$_SESSION['msg']='update Successfully';
			unset($_SESSION['form']);
			redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Library", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}
	}
}
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("issuebook","where 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"=>"Library", "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 issuebook WHERE id='".$_GET['id']."'");
}
if($pgAct=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{ 
	if(isset($_SESSION['form']))
	{
		$aryForm=$_SESSION['form'];
		unset($_SESSION['form']);
	}
?>
<!-- Forms -->
<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: 96.7% !important;overflow: inherit !important;}
</style>
<form method="post" action="" enctype="multipart/form-data">
     <div class="row-fluid">
        <div class="span12">
            <div class="top-bar">
                <h3><i class="icon-cog"></i> Issue Book</h3>
                <div style="float:right; margin-top:5px">
                <button type="button" onclick="printDiv('printableDiv')" class="btn btn-primary">Print</button>
                    <button type="submit" class="btn btn-primary"> Save</button>
                    <button type="reset" class="btn"> Cancel</button>
                </div>
            </div>
            <div class="well"  id="printableDiv">
			<!-- Forms: Box -->
			<div class="span6">
				<div class="well no-padding">

                    <div class="control-group">
						<label class="control-label">Book ID / Acc No</label>
						<div class="controls">
							<input type="text" name="bid" class="input-block-level" value="<?php echo $aryForm["bid"]; ?>" >
						</div>
					</div>

					<div class="control-group">
						<label class="control-label">Book Name</label>
						<div class="controls">
							<input type="text" name="bname" class="input-block-level" value="<?php echo $aryForm["bname"]; ?>">
						</div>
					</div>

					<!--<div class="control-group">-->
					<!--	<label class="control-label">Borrower ID</label>-->
					<!--	<div class="controls">-->
					<!--		<input type="text" name="sid" class="input-block-level" value="<?php echo $aryForm["sid"]; ?>" >-->
					<!--	</div>-->
					<!--</div>-->

                    <div class="control-group">
						<label class="control-label">Lended To</label>
						<div class="controls">
							<input type="radio" name="type" value="Students" onchange="changeType(this.value)" checked /> Students &emsp;&emsp;&emsp;&emsp;
							<input type="radio" name="type" value="Teachers" onchange="changeType(this.value)" /> Teachers
						</div>
					</div>

                        <div class="control-group" id="studentdiv">
							<label class="control-label">Borrower</label>
							<div class="controls">
							    <select class="span12" name="sid">
                                	<option value="">none</option>
									<?php
										$student=$db->getRows("select * from student where school_id='".$_SESSION["admin"]["school_id"]."' and session='".$_SESSION["admin"]["session"]."'");
										foreach($student as $st){
											?>
											<option <?php if($st["idno"]==$aryForm["sid"])echo 'selected';?> value="<?php echo $st["idno"] ?>">
												 <?php echo $st["name"]." - class ".$st["class"] ?> - admno <?php echo $st["admissionno"] ?>
                                            </option>
											<?php
										}
									?>
								</select>
							</div>
						</div>

						<div class="control-group" id="teacherdiv">
							<label class="control-label">Borrower</label>
							<div class="controls">
							    <select class="span12" name="sid" style="width:100%;position:fixed;">
                                	<option value="">none</option>
									<?php
										$student=$db->getRows("select * from staff where session='".$_SESSION["admin"]["session"]."'");
										foreach($student as $st){
											?>
											<option <?php if($st["idno"]==$aryForm["sid"])echo 'selected';?> value="<?php echo $st["idno"] ?>">
												 <?php echo $st["name"]." - Mob ".$st["mobile"] ?>
                                            </option>
											<?php
										}
									?>
								</select>
							</div>
						</div>

                </div>
           </div> 
           <div class="span6">
				<div class="well no-padding">

                    <div class="control-group">
						<label class="control-label">Issue Date</label>
						<div class="controls">
							<input type="date" name="idate" id="idate" class="input-block-level" value="<?php if($aryForm["idate"]!=""){ echo $aryForm["idate"]; } else echo date('Y-m-d'); ?>" >
						</div>
					</div>

                    <div class="control-group">
						<label class="control-label">Borrowed For<br> (in days)</label>
						<div class="controls">
							<input type="number" name="days" id="days" onkeyup="getReturnDate()" class="input-block-level" value="<?php echo $aryForm["days"]; ?>" minimum="0">
						</div>
					</div>

					<div class="control-group">
						<label class="control-label">Expected Return<br> Date</label>
						<div class="controls">
							<input type="date" name="rdate" id="rdate" class="input-block-level" value="<?php echo $aryForm["rdate"]; ?>" >
						</div>
					</div>

				</div>

			</div>
            </div>
        </div>
        
	</div>
</form>
<!-- / 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='./?src=Library&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"=>"Library", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("select * from issuebook ");
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>S No</th>
                    <th>Book</th>
                    <th>Issue to</th>
                    <th>Type</th>
                    <th>Issue Date</th>
                    <th>Days</th>
                    <th>Return Date</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['id'];
            ?>
                <tr class="grade<?php if($i%2==0)echo "C";else "U";?>">
                    <td><?php echo $i; ?></td>
                    <td><?php echo $clist["bname"] ?></td>
                    <td><?php echo $clist["sname"] ?></td>
                    <td><?php echo $clist["type"] ?></td>
                    <td><?php echo $clist["idate"] ?></td>
                    <td><?php echo $clist["days"] ?></td>
                    <td><?php echo $clist["rdate"] ?></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>
        </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 getReturnDate(){
        var idate = $("#idate").val();
        var days = $("#days").val();

        $.post( "Library/ajax.php", { type: "get_return_date", idate: idate, days: days  } , function( data ){
            var temp = JSON.parse(data);
            $("#rdate").val(temp.rdate);
        });

    }
    
    $("#teacherdiv").hide();
    
    function changeType(type){

        if(type == "Students"){
            $("#studentdiv").show();
            $("#teacherdiv").hide();
        }

        if(type == "Teachers"){
            $("#studentdiv").hide();
            $("#teacherdiv").show();
        }

    }
    
</script>

 


