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


if($_POST)
{
	$_SESSION['form']=$_POST;
	$flgEr=FALSE;
	$_POST["session"]=$_SESSION["admin"]["session"];
	$_POST["school_id"]=$_SESSION["admin"]["school_id"];
	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"];
		$pass = new password();
		$_POST["password"]=$pass->password($_POST["password"]);
		$flgIn=$db->insertAry("users",$_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("src"=>"Promote", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		}
	}
	elseif($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id'])!='')
	{
	    $pass = new password();
	    $_POST["password"]=$pass->password($_POST["password"]);
		$flgUp=$db->updateAry("users",$_POST,"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_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("src"=>"Promote", "page_id"=>$pgMod)));
		}
		else
		{
			array_push($alert_err,$db->getErMsg());
		
		}
	} 
}		
elseif($pgAct=="changestatus" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$flgUp=$db->updateAry("users",array("status"=>0),"where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
	if(!is_null($flgUp)) { $_SESSION['msg']='Inactive Successfully';
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Promote", "page_id"=>$pgMod))); }
	else { array_push($alert_err,$db->getErMsg()); }
	
}
elseif($pgAct=="delete" && isset($_GET['id']) && trim($_GET['id'])!='')
{
	$res=$db->delete("users","where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$_GET['id']."'");
	if(!is_null($res)) { $_SESSION['msg']='Deleted Successfully';
	redirect(URL_ADMIN_HOME.getQueryString(array("src"=>"Promote", "page_id"=>$pgMod)));
	}
	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 users WHERE school_id='".$_SESSION["admin"]["school_id"]."' and 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="" id="printableDiv" enctype="multipart/form-data">
     <div class="row-fluid">
        <div class="span12">
            <div class="top-bar">
                <h3><i class="icon-cog"></i> Create New Users</h3>
                <div style="float:right; margin-top:5px">
                    <button type="submit" class="btn btn-primary"> Save</button>
                    <button type="reset" class="btn"> Cancel</button>
                </div>
            </div>
            <div class="well">
			<!-- Forms: Box -->
			<div class="span6">

				<!-- Forms: Top Bar -->
				
				<!-- / Forms: Top Bar -->

				<!-- Forms: Content -->
				<div class="well no-padding">
                        <div class="control-group">
							<label class="control-label" for="roll">Role</label>
							<div class="controls">
								<select class="span12" name="roll" id="roll">
									<option value="">Role</option>
									<?php 
										$class=$db->getRows("select * from tbl_roll where school_id='".$_SESSION["admin"]["school_id"]."'");
										foreach($class as $cla){
											?>
											<option <?php if($cla["id"]==$aryForm["roll"])echo 'selected';?> value="<?php echo $cla["id"] ?>">
												<?php echo $cla["name"] ?>
                                            </option>
											<?php
										}
									?>
								</select>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="name">Teacher Name</label>
							<div class="controls">
                                <input type="text" id="name" name="name" class="input-block-level" value="<?php echo $aryForm["name"]; ?>" >
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="userid">User ID</label>
							<div class="controls">
								<input type="text" id="userid" name="userid" class="input-block-level" value="<?php echo $aryForm["userid"]; ?>" >
							</div>
						</div>
                        
                        <div class="control-group">
							<label class="control-label" for="password">Password</label>
							<div class="controls">
								<input type="password" id="password" name="password" class="input-block-level" value="<?php echo $aryForm["password"]; ?>" >
							</div>
						</div>
						
				</div>
				<!-- / Forms: Content -->

			</div>
			<!-- / Forms: Box -->
            <!-- Forms: Box --><?php //echo "<pre>";print_r($aryForm);echo "</pre>"; ?>
			<div class="span6">

				<!-- Forms: Top Bar -->
				
				<!-- / Forms: Top Bar -->

				<!-- Forms: Content -->
				<div class="well no-padding">

					<!-- Forms: Form -->
						<!-- Forms: Normal input field -->
                        <?php
						if($pgAct=="edit" && isset($_GET['id']) && trim($_GET['id']) !=''){
							?>
                            <div class="control-group">
                                <label class="control-label" for="photo">Photo</label>
                                <div class="controls">
                                	<?php if($aryForm["photo"]!=""){
										?><img src="../media/photo/<?php echo $aryForm["photo"]; ?>" style="height:60px" /><?php
									}?>
                                    <a href="javascript:;" onclick="$('#photo').click()">Change Photo</a>
                                    <input type="file" name="photo" style="display:none" id="photo"/>
                                </div>
                            </div>
							
							<?php
						}else{?>
                        <div class="control-group">
							<label class="control-label" for="photo">Photo</label>
							<div class="controls">
								<input type="file" name="photo" id="photo"/>
							</div>
						</div>
                        <?php 
						}?>
                         <div class="control-group">
							<label class="control-label" for="email">E-mail</label>
							<div class="controls">
								<input type="text" id="email" name="email" class="input-block-level" value="<?php echo $aryForm["email"]; ?>" >
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="mobile">Mobile</label>
							<div class="controls">
								<input type="text" id="mobile" name="mobile" class="input-block-level" value="<?php echo $aryForm["mobile"]; ?>" required>
							</div>
						</div>
                        <div class="control-group">
							<label class="control-label" for="status">Status</label>
							<div class="controls">
								<select class="span12" name="status" id="status">
                                	<option <?php if("1"==$aryForm["status"])echo 'selected';?> value="1">Active</option>
                                    <option <?php if("0"==$aryForm["status"])echo 'selected';?> value="0">Inactive</option>
								</select>
							</div>
						</div>
						<!-- / Forms: Normal input field -->


						<!-- Forms: Form Actions -->
						
						<!-- / Forms: Form Actions -->
	 
					<!-- / Forms: Form -->           

				</div>
				<!-- / Forms: Content -->

			</div>
			<!-- / Forms: Box -->
            </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=Promote&page_id=<?php echo $pgMod; ?>&action=add'" class="btn" type="button">Add <?php echo $pgMod; ?></button>
    </div>
    <div class="well no-padding">
	<?php
	$pass = new password();
	$aryPgAct=array("src"=>"Promote", "page_id"=>$pgMod,"action"=>"add");
	$aryList=$db->getRows("select * from users where school_id='".$_SESSION["admin"]["school_id"]."' ");
	if(is_array($aryList) && count($aryList)>0)
	{
		?>
        <table class="table" id="example">
            <thead>
                <tr>
                    <th>S. No.</th>
                    <th>Name</th>
                    <th>User ID</th>
                    <th>Password</th>
                    <th>Email</th>
                    <th>Mobile</th>
                    <th>Role</th>
                    <!--<th>Status</th>-->
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach($aryList as $clist){ $i++; 
            $aryPgAct["id"]=$clist['id'];
            ?>
                <tr>
                    <td><?php echo $i; ?></td>
                    <td><?php echo $clist["name"] ?></td>
                    <td><?php echo $clist["userid"] ?></td>
					<td><?php 
					$salt = "CrazyassLongSALTThatMakesYourUsersPasswordVeryLong123!!312567__asdSdas";
					echo $pass->encrypt_decrypt("decrypt", $clist["password"] , $salt, "a1b2c3a1b2c3a1b2"); ?></td>
                    <td><?php echo $clist["email"] ?></td>
                    <td><?php echo $clist["mobile"] ?></td>
                    <td><?php echo $db->getVal("select name from tbl_roll where school_id='".$_SESSION["admin"]["school_id"]."' and id='".$clist["roll"]."'");?></td>
                    <!--<td><?php if($clist["status"]==1)echo '<span class="label label-success">Active</span>';else echo '<span class="label label-important">Inactive</span>'; ?></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 if($clist["status"]===1){ $aryPgAct['action']="changestatus";
                    ?>
                         <a href="javascript:;" onclick="del('<?php echo URL_ADMIN_HOME.getQueryString($aryPgAct); ?>')" class="confirmation"><button type="button" class="btn btn-warning">inactive</button></a>
                         <?php }?>  

                         <?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
}
?>