<script type="text/javascript" src="js/tableExport.js"></script>

<script type="text/javascript" src="js/jquery.base64.js"></script>

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


if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	if(isset($_FILES["photo"]["name"]) && !empty($_FILES["photo"]["name"])){
		$lfilename = basename($_FILES['photo']['name']);
		$lext = strtolower(substr($lfilename, strrpos($lfilename, '.')+1));
		$lnewfile=md5(microtime()).".".$lext;
		if(move_uploaded_file($_FILES['photo']['tmp_name'],PATH_MEDIA."photo".DS.$lnewfile))
		{
			$_POST["photo"]=$lnewfile;
		}
	}
	//echo "<pre>";print_r($_POST);exit;
	if($flgEr!=TRUE && $pgAct=="add")
	{
		$_POST["school_id"]=$_SESSION["admin"]["school_id"];
$_POST["added_by"]=$_SESSION["admin"]["uid"];
		$flgIn=$db->insertAry("staff",$_POST);
		if(!is_null($flgIn))
		{
			$_SESSION['msg']='Saved Successfully';
			unset($_SESSION['form']);
			sms($_POST["mobile"],"Dear ".$_POST["name"].", You are successfully joined  ".$LinksDetails["site_name"]." , heartly congrates from ".$LinksDetails["site_name"]." campus.","faculty_enrollment");
			redirect(URL_ADMIN_HOME.getQueryString(array("page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
			
		}
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
		$flgUp=$db->updateAry("staff",$_POST,"where school_id='".$_SESSION["admin"]["school_id"]."' and idno='".$_GET['id']."'");
		if(!is_null($flgUp))
		{
			//echo $db->getlastQuery();exit;
			$_SESSION['msg']='update Successfully';
			unset($_SESSION['form']);
			sms($_POST["mobile"],"Dear ".$_POST["name"].", Your detail is updated successfully in ".$LinksDetails["site_name"],"faculty_enrollment");
			redirect(URL_ADMIN_HOME.getQueryString(array("page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		
		}
	} 
}		
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$flgUp=$db->updateAry("staff",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=="add" || ($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''))
{ 
	
}
else{
	?>
	<div class="row-fluid">
	<div class="span12">
	
	<div class="span12">

<button type="button" onclick="printDiv('printDiv2')" class="btn btn-primary " style="float:right !important; margin-top:5px ">Print</button>

&nbsp;<input  class="btn btn-primary" style="margin-top: 8px;" type="button"  onclick="$('.table').tableExport({type:'excel',escape:'false'});" id="btnExport" value=" Export To Excel " />

<form method="post" action="" style="float:left; margin-top:5px; margin-left:10px;" >					
					<select class="span7" name="department" id="department" style="margin-left:20px!important;" >
									<option value="">Department</option>
									<?php 
										$class=$db->getRows("select department from staff where  school_id='".$_SESSION["admin"]["school_id"]."' and status=1 GROUP by department order by department");
										foreach($class as $cla){
											?>
											<option <?php //if($cla["name"]==$aryForm["department"])echo 'selected';?> value="<?php echo $cla["department"] ?>">
												<?php echo $cla["department"] ?>
                                            </option>
											<?php
										} ?>
									
								</select>
						
						<span sytle="width:20px;">  </span><button type="submit" class="btn btn-primary"> Search</button>
						</form>
    </div>
    <div class="well no-padding" id="printDiv2">
	<?php
	$aryPgAct=array("page_id"=>$pgMod,"action"=>"add");
	if(isset($_POST[department])){
	$aryList=$db->getRows("select * from staff where department='".$_POST[department]."' and school_id='".$_SESSION["admin"]["school_id"]."' and status=1 order by name");
	}
else {
	    $aryList=$db->getRows("select * from staff where  school_id='".$_SESSION["admin"]["school_id"]."' and status=1 order by name");
    }
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
		 
		 
        <table class="table">
            <thead>
                <tr>
                    <th>Serial No</th>
                    <th>Name</th>
                    <th>Father Name</th>
                    <th>D.O.B.</th>
                    <th>Shift</th>
                    <th>Department</th>
                    <th>Designation</th>
                    <th>Contact</th>
                    <th>User Id</th>
                    <th>Password</th>
                </tr>
            </thead>
            <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["fname"] ?></td>
                    <td><?php echo $clist["dob"] ?></td>
                    <td><?php echo $clist["shift"] ?></td>
                    <td><?php echo $clist["department"] ?></td>
                    <td><?php echo $clist["designation"] ?></td>
                    <td><?php echo $clist["mobile"] ?></td>
                    <td><?php echo $clist["userid"] ?></td>
                    <td>
                        <?php $pass = new password();
                                $salt = "CrazyassLongSALTThatMakesYourUsersPasswordVeryLong123!!312567__asdSdas";
            					$pwd = $pass->encrypt_decrypt("decrypt", $clist["password"] , $salt, "a1b2c3a1b2c3a1b2"); 
            					echo $pwd;
    					?>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
        </table>
		</div>
		<?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 type="text/javascript">
	$(document).ready(function(e) {
		var oTable = $('.table').dataTable( {
			"bPaginate": false,
			"bLengthChange": false,
			"bFilter": true,
			"bSort": true,
			"bInfo": true,
			"bAutoWidth": false
		});
	});
	
	</script>