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


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

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

		$flgIn=$db->insertAry("book",$_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("book",$_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("book","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 book 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> Add 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">Category</label>
							<div class="controls">
								<select class="span12" name="category">
									<option value="">--Select--</option>
                                    <?php
										$class=$db->getRows("select * from category where school_id='".$_SESSION["admin"]["school_id"]."' ");
										foreach($class as $cla){
											?>
											<option <?php if($cla["name"]==$aryForm["category"])echo 'selected';?> value="<?php echo $cla["name"] ?>">
												<?php echo $cla["name"] ?>
                                            </option>
											<?php
										}
									?>
								</select>
							</div>
						</div>
						
						<div class="control-group">
							<label class="control-label">Publication</label>
							<div class="controls">
								<select class="span12" name="publication">
									<option value="">--Select--</option>
                                    <?php 
										$class=$db->getRows("select * from publication");
										foreach($class as $cla){
											?>
											<option <?php if($cla["publisher"]==$aryForm["publication"])echo 'selected';?> value="<?php echo $cla["publisher"] ?>">
												<?php echo $cla["publisher"] ?>
                                            </option>
											<?php
										}
									?>
								</select>
							</div>
						</div>

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

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

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

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

						<div class="control-group">
							<label class="control-label">Book Description</label>
							<div class="controls">
								<textarea class="input-block-level" name="description"><?php echo $aryForm["description"]; ?></textarea>
							</div>
						</div>

						<div class="control-group">
							<label class="control-label">Amount</label>
							<div class="controls">
								<input type="number" name="amount" class="input-block-level" value="<?php echo $aryForm["amount"]; ?>" >
							</div>
						</div>
						
                </div>
           </div>
           <div class="span6">
				<div class="well no-padding">

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

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

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

						<div class="control-group">
							<label class="control-label">Quantities</label>
							<div class="controls">
								<input type="number" name="qty" class="input-block-level" value="<?php echo $aryForm["qty"]; ?>" >
							</div>
						</div>

                        <div class="control-group">
							<label class="control-label">Book Language</label>
							<div class="controls">
								<input type="text" name="language" class="input-block-level" value="<?php echo $aryForm["language"]; ?>" >
							</div>
						</div>
						
						
						<div class="control-group">
							<label class="control-label">Book Source</label>
							<div class="controls">
								<input type="text" name="source" class="input-block-level" value="<?php echo $aryForm["source"]; ?>" >
							</div>
						</div>
						
						<div class="control-group">
							<label class="control-label">Book Call No</label>
							<div class="controls">
								<input type="text" name="call_no" class="input-block-level" value="<?php echo $aryForm["call_no"]; ?>" >
							</div>
						</div>
						
						<div class="control-group">
							<label class="control-label">Remarks</label>
							<div class="controls">
							    <textarea class="input-block-level" name="remark"><?php echo $aryForm["remark"]; ?></textarea>
							</div>
						</div>
						
						<div class="control-group">
							<label class="control-label" for="title">Book Status</label>
							<div class="controls">
                                <select class="input-block-level" name="status">
                                    <option value="Active" <?php if($aryForm['status']=="Active")echo "selected"; ?>>Active</option>
                                    <option value="Inactive" <?php if($aryForm['status']=="Inactive")echo "selected"; ?>>Inactive</option>
                                </select>
							</div>
						</div>
                        
				</div>
				<!-- / Forms: Content -->

			</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 book ");
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>S No</th>
                    <th>Book</th>
                    <th>Category</th>
                    <th>Publication</th>
                    <th>Author</th>
                    <th>Edition</th>
                    <th>Language</th>
                    <th>ISBN No</th>
                    <th>Status</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["name"] ?></td>
                    <td><?php echo $clist["category"] ?></td>
                    <td><?php echo $clist["publication"] ?></td>
                    <td><?php echo $clist["author"] ?></td>
                    <td><?php echo $clist["edition"] ?></td>
                    <td><?php echo $clist["language"] ?></td>
                    <td><?php echo $clist["isbn"] ?></td>
                    <td><?php echo $clist["status"] ?></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
}
?>