<?php
include 'connect/cms-config.php' ;
include 'requires/function.php' ;
include 'requires/session.php' ;

// include the class
include 'requires/class_resize.php' ;

$array_permission2 = [];

$array_permission2['dashboard'] = [
	"dashboard" => 
	[ 
		"dashboard-view" => "View Dashboard"
	]
];
$array_permission2['visitor'] = [
	"visitor" => [ 
		"visitor-list-view" => "View Visitor"
	]
] ;
$array_permission2['hr'] = [
	"staff-application" => [ 
		"application-form-view" => "View Application Form",
		"application-form-new" => "New Application Form", 
		"application-list-view" => "View Application List", 
		"application-list-edit" => "Edit Application", 
		"application-list-update" => "Update Status Application Form"
	],
	"staff" => [ 
		"staff-list-view" => "View Staff List",
		"staff-resign-list-view" => "View Staff Resign List",
		"staff-run-away-list-view" => "View Staff Run Away List",


		"staff-list-new" => "New Staff",
		"staff-list-edit" => "Edit Staff",
		"staff-list-update" => "Update Status Staff",
		"staff-list-trash" => "Trash Staff",
		"staff-adjustment-view" => "View Point Adjustment List",
		"staff-adjustment-new" => "New Point Adjustment",
		"staff-adjustment-wallet-view" => "View Wallet Adjustment List",
		"staff-adjustment-wallet-new" => "New Wallet Adjustment"
	],
	"merit-points" => [ 
		"hr-merit-points-movement-view" => "View Merit Points Movement", 
		"hr-merit-points-adjustment-view" => "View Merit Points Adjustment", 
		"hr-merit-points-task-view" => "View Merit Points Task", 
	],
	"attendance" => [ 
		"attendance-list-view" => "View Attendance List",
		"attendance-list-edit" => "Edit Attendance",
		"attendance-list-report" => "View Attendance Report",
		"attendance-attendance-reprocessing" => "Attendance Reprocessing",
		"attendance-list-qrcode" => "View Qrcode",
		"attendance-list-qrcode-topunch" => "Punch Card",
		"attendance-health-view" => "View Health",
		"attendance-health-new" => "New Health",
		"attendance-health-update" => "Edit Health"
	],
	"leave" => [ 
		"leave-view" => "View Leave List",
		"leave-new" => "New Leave",
		"leave-update" => "Edit Leave",
	]
] ;

$array_permission2['salary'] = [
	"salary-list" => [ 
		"salary-view" => "View Salary List"
	]
] ;
$array_permission2['task'] = [
	"task-list" => [ 
		"task-list-view" => "View Task List",
		"task-list-trash" => "Trash Task",
		"task-report-view" => "View Report List"
	]
] ;
$array_permission2['service'] = [
	"announcement" => [ 
		"announcement-view" => "View Announcement List",
		"announcement-new" => "New Announcement",
		"announcement-trash" => "Trash Announcement"
	],
	"inbox" => [ 
		"inbox-view" => "View Inbox",
		"inbox-new" => "New Inbox",
		"inbox-trash" => "Trash Inbox"
	],
	"our-inbox" => [ 
		"our-suggestion-view" => "View Suggestion List",
		"our-suggestion-edit" => "Edit Suggestion",
		"our-suggestion-trash" => "Trash Suggestion",
		"our-request-view" => "View Request List",
		"our-request-edit" => "Edit Request",
		"our-request-trash" => "Trash Request",
		"our-category-gallery" => "View Request Gallery",
		"our-category-main-view" => "View Main Category List",
		"our-category-main-new" => "New Main Category",
		"our-category-main-edit" => "Edit Main Category",
		"our-category-main-trash" => "Trash Main Category",
		"our-category-main-stock" => "Control Main Category Stock",
		"our-category-sub-view" => "View Sub Category List",
		"our-category-sub-new" => "New Sub Category",
		"our-category-sub-edit" => "Edit Sub Category",
		"our-category-sub-trash" => "Trash Sub Category",
		"our-category-sub-stock" => "Control Sub Category Stock",
		"our-grievance-view" => "View Grievance List",
		"our-grievance-edit" => "Edit Grievance",
		"our-grievance-trash" => "Trash Grievance"
	],
	"form-submission" => [ 
		"form-headcount-view" => "View Headcount List",
		"form-headcount-edit" => "Edit Headcount",
		"form-headcount-trash" => "Trash Headcount",
		"form-nomination-view" => "View Nomination List",
		"form-nomination-trash" => "Trash Nomination",
		"form-nomination-question-view" => "View Nomination Question",
		"form-nomination-question-new" => "New Nomination Question",
		"form-nomination-question-edit" => "Edit Nomination Question",
		"form-nomination-question-trash" => "Trash Nomination Question",
		"form-resignation-view" => "View resignation List",
		"form-resignation-edit" => "Edit resignation",
		"form-resignation-trash" => "Trash resignation",
		"form-submission-category-view" => "View Category",
		"form-submission-category-new" => "New Category",
		"form-submission-category-edit" => "Edit Category",
		"form-submission-category-trash" => "Trash Category"
	],
	"redeem" => [ 
		"redeem-list-view" => "View Redeem List",
		"redeem-list-new" => "New Redeem",
		"redeem-list-edit" => "Edit Redeem",
		"redeem-list-trash" => "Trash Redeem"
	],
	"association" => [ 
		"association-list-view" => "View Association List",
		"association-list-new" => "New Association",
		"association-list-edit" => "Edit Association",
		"association-list-trash" => "Trash Association",
		"association-list-qr" => "Qr Code Association",
		"association-list-gallery-category" => "View Association Gallery Category",
		"association-list-gallery" => "View Association Gallery",
		"association-category-view" => "View Category",
		"association-category-new" => "New Category",
		"association-category-edit" => "Edit Category",
		"association-category-trash" => "Trash Category"
	],
	"training" => [ 
		"training-view" => "View Training List",
		"training-new" => "New Training",
		"training-edit" => "Edit Training",
		"training-trash" => "Trash Training",
		"training-qr" => "QR Code Training",
		"training-gallery-category" => "View Training Gallery Category",
		"training-gallery" => "View Training Gallery",
	],
	"form" => [ 
		"form-list-view" => "View Form List",
		"form-list-new" => "New Form",
		"form-list-edit" => "Edit Form",
		"form-list-trash" => "Trash Form"
	],
	"handbook" => [ 
		"handbook-list-view" => "View Handbook List",
		"handbook-list-new" => "New Handbook",
		"handbook-list-edit" => "Edit Handbook",
		"handbook-list-trash" => "Trash Handbook"
	]
] ;
$array_permission2['import'] = [
	"import" => [ 
		"import-full-attendance-view" 			=> "View Import Full Attendance List",
		"import-full-attendance-upload" 		=> "Upload Full Attendance List",
		"import-full-attendance-trash" 		    => "Trash Full Attendance List",
		"import-outstanding-employee-view" 		=> "View Import Outstanding Employee List",
		"import-outstanding-employee-upload" 	=> "Upload Outstanding Employee List",
		"import-outstanding-employee-trash" 	=> "Trash Outstanding Employee List",
		"import-lateness-board-view" 			=> "View Import Lateness Board List",
		"import-lateness-board-upload" 			=> "Upload Lateness Board List",
		"import-lateness-board-trash" 			=> "Trash Lateness Board List",
		"import-point-view" 					=> "View Import Point List",
		"import-point-upload" 					=> "Upload Point List",
	]
] ;
$array_permission2['report'] = [
	"year-end-cut-off" => [ 
		"year-end-cut-off-view" 			=> "View Year End Cut Off Report"
	]
] ;
$array_permission2['setting'] = [
	"user-setting" => [ 
		"user-user-view" => "User",
		"user-new-user-new" => "New User",
		"user-user-edit" => "Edit User",
		"user-user-update" => "Update User"
	],
	"service-annoucment" => [ 
		"user-notification-view" => "View Notification List", 
		"user-notification-edit" => "Edit Notification", 
		"user-notification-trash" => "Trash Notification",
		"user-letterhead-view" => "View Letterhead List",
		"user-letterhead-new" => "New Letterhead",
		"user-letterhead-edit" => "Edit Letterhead",
		"user-letterhead-trash" => "Trash Letterhead"
	],
	"hr-setting" => [ 
		"hr-branch-view" => "View Branch List",
		"hr-branch-new" => "New Branch",
		"hr-branch-edit" => "Edit Branch",
		"hr-branch-trash" => "Trash Branch",
		"hr-working-hours-view" => "View Working Hours List",
		"hr-working-hours-new" => "New Working Hours",
		"hr-working-hours-edit" => "Edit Working Hours",
		"hr-working-hours-trash" => "Trash Working Hours",
		"hr-department-list-View" => "ViewDepartment List",
		"hr-department-list-new" => "New Department",
		"hr-department-list-edit" => "Edit Department",
		"hr-department-list-trash" => "Trash Department",
		"hr-section-list-view" => "View Section List",
		"hr-section-list-new" => "New Section",
		"hr-section-list-edit" => "Edit Section",
		"hr-section-list-trash" => "Trash Section",
		"hr-position-list-view" => "View Designation List",
		"hr-position-list-new" => "New Designation",
		"hr-position-list-edit" => "Edit Designation",
		"hr-position-list-trash" => "Trash Designation"
	],
	"app-setting" => [ 
		"app-welcome-screen-view" => "View Welcome Screen List",
		"app-welcome-screen-new" => "New Welcome Screen",
		"app-welcome-screen-edit" => "Edit Welcome Screen",
		"app-welcome-screen-trash" => "Trash Welcome Screen",
		"app-pop-up-view" => "View Pop Up",
		"app-pop-up-edit" => "Edit Pop Up",
		"app-service-view" => "View Sevice List",
		"app-service-edit" => "Edit Sevice",
		"app-service-trash" => "Trash Sevice",
		"app-page-view" => "View Page List",
		"app-page-new" => "New Page",
		"app-page-edit" => "Edit Page",
		"app-page-trash" => "Trash Page",
		"app-menu-view" => "View Menu List",
		"app-menu-new" => "New Menu",
		"app-menu-edit" => "Edit Menu",
		"app-menu-trash" => "Trash Menu",
		"app-support-view" => "View Support List",
		"app-support-new" => "New Support",
		"app-support-edit" => "Edit Support",
		"app-support-trash" => "Trash Support",
		"app-pasword-view" => "View Password List",
		"app-pasword-edit" => "Edit Password",
		"app-difficulty-view" => "View Difficulty List",
		"app-difficulty-new" => "New Difficulty",
		"app-difficulty-edit" => "Edit Difficulty",
		"app-difficulty-trash" => "Trash Difficulty",
		"app-adjustment-view" => "View Adjustment List",
		"app-adjustment-new" => "New Adjustment",
		"app-adjustment-edit" => "Edit Adjustment",
		"app-adjustment-trash" => "Trash Adjustment",
		"app-point-view" => "View Point Adjustment List",
		"app-point-new" => "New Point Adjustment",
		"app-point-edit" => "Edit Point Adjustment",
		"app-adjustment-group-view" => "View Adjustment Group List",
		"app-adjustment-group-new" => "New Adjustment Group",
		"app-adjustment-group-edit" => "Edit Adjustment Group",
		"app-adjustment-group-trash" => "Trash Adjustment Group",
		"profile-star-view" => "View Star List",
		"profile-star-edit" => "Edit Star",
		"profile-point-view" => "View Point List",
		"profile-point-edit" => "Edit Point",
		"profile-achievement-view" => "View Achievement List",
		"profile-achievement-edit" => "Edit Achievement",
		"profile-tier-view" => "Tier List",
		"profile-tier-edit" => "Edit Tier"
	]
] ;

// keep parameter in value
$page = escapeString($_GET['page']) ;
$page_mode = escapeString($_GET['page_mode']) ;
$order = escapeString($_GET['order']) ;
$type = escapeString($_GET['type']) ;
$search = escapeString($_GET['search']) ;

// get all branch
$branch_all     = [] ;
$get_branch = $mysqli->query("SELECT * FROM branch
                              WHERE deleted_at IS NULL") ;
if ( $get_branch->num_rows > 0 ){
    while ( $row_branch = $get_branch->fetch_assoc() ){
        $branch_all[$row_branch['branch_id']] = $row_branch['branch_name'] ;
    }
}

// get all requires 
$tier_list    = [] ;
$tier_list_id = [] ;
$mysqli_tier  = $mysqli->query("SELECT a.tier_id, b.title FROM profile_tier a
                        	    LEFT JOIN profile_tier_translation b ON ( a.tier_id = b.tier_id )
							    WHERE a.deleted_at IS NULL AND b.lang = 'en' ORDER BY a.sortable DESC") ;
if ( $mysqli_tier->num_rows > 0 ){
	while ( $row_tier = $mysqli_tier->fetch_assoc() ){
		$tier_list[]  = $row_tier ;
		$tier_list_id[$row_tier['tier_id']] = $row_tier['title'] ;
	}
}

// form submit
if ($_POST['hide'] == 1){

	$call = escapeString($_POST['call']) ;
	$username = escapeString($_POST['username']) ;
	$email = escapeString($_POST['email']) ;
	$fullname = escapeString($_POST['fullname']) ;
	$password = escapeString($_POST['password']) ;
	$permission = escapeString($_POST['permission']) ;
	$branch = escapeString($_POST['branch']) ;
	$colour = escapeString($_POST['colour']) ;
	$verification_code = escapeString($_POST['verification_code']) ;
	$user_is_interview_by = escapeString($_POST['user_is_interview_by']) ;
	$hide_user_id = escapeString($_POST['hide_user_id']) ;
	$branch_permission = $_POST['branch_permission'] ;
	$branch_permission = json_encode($branch_permission) ;
	
	$permission2 = ($_POST['permission2']) ;
	$temp_permission2 = multipleArrayTo($permission2) ;
	
	$boolean_redirect = false ;

	if ($username != '' && $email != '' && $fullname != ''){

		// query for user
		$user_query = ($hide_user_id != '' ? " AND user_id != '".$hide_user_id."'" : '') ;

		// if not admin
		if ( $row_user['user_permission'] != 'admin' ){
			$user_query       .= " AND user_permission != 'admin'" ;
			$permission        = 'user' ;
			$verification_code = 'no' ;
			$user_is_interview_by   = 'no' ;
		}

		// check user exsits
		$mysqli_check_user = $mysqli->query("SELECT * FROM system_user
											 WHERE user_name = '".$username."' AND user_trash = '0' ".$user_query." LIMIT 1") ;
		// check if username exsits
		if ($mysqli_check_user->num_rows > 0){
			$boolean_user = false ;
		}else{
			$boolean_user = true ;
		}
		// password
		$boolean_password = false ;
		if (strlen($password) >= 6){
			$boolean_password = true ;
		}
		// check user status
		if ($boolean_user){
			
			// set image in variable
			$image = $_FILES["image"]["name"] ;
			// remove photo
			$remove_photo = $_POST['remove_photo'] ;
			if ($remove_photo == 1){
				$image = '' ;	
				$image_query = "user_signature = ''," ;
			}

			$temp_user_tier       = [] ;
			foreach ( $_POST['user_tier'] as $kusertier => $vusertier ){
				$temp_user_tier[] = escapeString($vusertier) ;
			}
			$user_tier 			  = implode(',', $temp_user_tier) ;


			// check status
			switch($_POST['hide_status']){
				case 'new' :

					// check permission
					if ( !permissionCheck($row_user, 'user-new') ){
						header('Location: index.php') ;
						exit ;
					}
					
					// reset password
					$code = rand(0, 9999) ;
					$password = md5(md5($password).$code) ;
					
					// check password
					if ($boolean_password){
						// check password
						$mysqli->query( "INSERT INTO system_user
										(user_tier, user_call, user_name, user_email, user_password, user_code, user_fullname, user_permission, user_permission2, user_branch, user_verification_type, user_colour, user_last_login, user_date, user_modified, user_trash, user_permission_branch) VALUES
										('".$user_tier."', '".$call."', '".$username."', '".$email."', '".$password."', '".$code."', '".$fullname."', '".$permission."', '".$temp_permission2."', '".$branch."', '".$verification_code."', '".$colour."', '".TODAYDATE."', '".TODAYDATE."', '".TODAYDATE."', '0', '".$branch_permission."')") ;
						$page = $mysqli->insert_id ;
						$boolean_redirect = true ;
					}else{
						$boolean_password = false ;	
					}

				break ;
				case 'edit' :

					// check permission
					if ( !permissionCheck($row_user, 'user-update') ){
						header('Location: index.php') ;
						exit ;
					}

					// password null
					if (strlen($password) == 0){
						// set boolean = true
						$boolean_password = true ;	
					}else{
						if (strlen($password) >= 6){
							// set boolean = true
							$boolean_password = true ;
							// reset password
							$code = rand(0, 9999) ;
							$password = md5(md5($password).$code) ;
							// query for password
							$password_query = "
							user_password = '".$password."',
							user_code = '".$code."'," ;
						}	
					}

					// check boolean status
					if ($boolean_password){

						// customer join company
						$mysqli_page = $mysqli->query("SELECT * FROM system_user
													   WHERE user_id = '".$page."' AND user_trash = '0' LIMIT 1") ;
						// set query as array
						$row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;

						// check is admin can edit permission
						if ( $row_user['user_permission'] == 'admin' ){
							$admin_query = "user_verification_type = '".$verification_code."',
										    user_is_interview_by = '".$user_is_interview_by."',
											user_colour = '".$colour."',
											user_tier = '".$user_tier."',
											user_permission = '".$permission."',
											user_permission_branch = '".$branch_permission."',
											user_permission2 = '".$temp_permission2."'," ;
						}

						$mysqli->query("UPDATE system_user SET
										user_call = '".$call."',
										user_name = '".$username."',
										user_email = '".$email."',
										".$password_query."
										".$admin_query."
										".$image_query."
										user_fullname = '".$fullname."',
										user_modified = '".TODAYDATE."'
										WHERE user_id = '".$page."'") ;
					}
				break ;
				
			}
			// resize image
			$create_image = reCreateImage('User', $page, $page, '', $image, $_FILES["image"]["type"], $_FILES['image']['tmp_name']) ;
			// Image uploads when exists
			if ($create_image['result'] && is_array($create_image['crop']) && count($create_image['result']) > 0){
				$resizeObj = new resize($create_image['original']) ; // Initialise load image
				foreach($create_image['crop'] as $value){
					// Resize image (options: exact, portrait, landscape, auto, crop)
					$resizeObj -> resizeImage($value['width'], $value['height'], $value['type']) ;
					$resizeObj -> saveImage($value['source']) ; // Save image	
				}
				// update database
				$mysqli->query("UPDATE system_user SET
								user_signature = '".$create_image['image']."'
								WHERE user_id = '".$page."'");
			}
			// new user
			if ($boolean_redirect){
				// redirect to main page
				header("Location: user.php?page_mode=all") ;
				exit ;	
			}
		}else{
			$boolean_password = true ;	
		}
	}
}

// mode type | all list | new | edit
switch($page_mode){
	
	// new customer
	case 'new' :

		// check permission
		if ( !permissionCheck($row_user, 'user-new-user-new') ){
			header('Location: index.php') ;
			exit ;
		}
		
		// active menu bar
		$active_main_menu = 'setting' ;
		$active_sub_menu = 'setting-user' ;
		$active_menu = 'user-new' ;
		
		// start header here
		include 'requires/page_header.php' ;
		include 'requires/page_top.php' ;
		
		?>
		<div class="warper container-fluid">
		<div class='container' style="background-color: white; border-radius: 10px;">
			<div class="page-header" style="margin: 30px 0px 0px 0px;padding: 0px;">
				<div class="row">
		            <div class="pull-left col"><h1><?= $lang['User']?> <small><?= $lang['new']?></small></h1>
		            </div>
		        </div>
				<div class="row">
					<div class="col-md-12">
	                	<div class="error_message">
							<?php
	                        if (!$boolean_user && $_POST['hide'] == 1){
	                            echo $lang['sorry_username_exsits'] .'<br />' ;
	                        }
							if (!$boolean_password && $_POST['hide'] == 1){
	                            echo $lang['sorry_password_must_at_least_6_digits'] .'<br />' ;
	                        }
	                        ?>
	                    </div>
	                </div>
	            </div>
	        </div>
			<div class="panel panel-default">
				<div class="panel-heading"></div>
                <div class="panel-body">

                <form method="post" class="form-horizontal" id="quotationForm" action="user.php?page_mode=new&page=&type=edit" enctype="multipart/form-data" novalidate="novalidate">
                                <div class="form-group">
									<div class="col-sm-2 control-label"><?= $lang['call']?></div>
									<div class="col-sm-9">
                                    	<select name="call" class="form-control ui-search-input" required>
                                        	<option value=""><?= $lang['select_a_call']?></option>
                                            <option value="Miss"><?= $lang['miss']?></option>
                                            <option value="Mrs"><?= $lang['mrs']?> </option>
                                            <option value="Mr"><?= $lang['mr']?></option>
                                            <option value="Ms"><?= $lang['ms']?></option>
                                        </select>
									</div>
								</div>
                                <div class="form-group">
                                    <div class="col-sm-2 control-label"><?= $lang['fullname']?></div>
                                    <div class="col-sm-9">
                                        <input type="text" name="fullname" class="form-control ui-search-input" value="<?= $fullname ?>" required placeholder="<?= $lang['fullname']?>" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-2 control-label"><?= $lang['email']?></div>
                                    <div class="col-sm-9">
                                        <input type="email" name="email" class="form-control ui-search-input" value="<?= $email ?>" required placeholder="<?= $lang['email']?>" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-2 control-label"><?= $lang['user_name']?></div>
                                    <div class="col-sm-9">
                                        <input type="text" name="username" class="form-control ui-search-input" value="<?= $username ?>" required placeholder="<?= $lang['user_name']?>" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-2 control-label"><?= $lang['password']?></div>
                                    <div class="col-sm-9">
                                        <input type="password" name="password" class="form-control ui-search-input" required placeholder="<?= $lang['password']?>" />
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-2 control-label"><?= $lang['signature']?></div>
                                    <div class="col-sm-9">
                                        <div class="file_upload">
                                            <div class="file_form">
                                                <input type="hidden" name="hide_image" value="" />
                                                <input type="hidden" name="remove_photo" value="">
                                                <input type="file" name="image" class="file_button control-label" tabindex="16" />
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-2 control-label"><?= $lang['user_colour']?></div>
                                    <div class="col-sm-9">
                                        <input type="color" name="colour" class="form-control ui-search-input" required placeholder="<?= $lang['user_colour']?>" />
                                    </div>
                                </div>
                                <div class="form-group">
                            		<div class="col-sm-2 control-label"><?= $lang['Tier'] ?></div>
							        <div class="col-sm-9">
							        	<select name="user_tier[]" id="user_tier" multiple class="chosen-select select2-basic-single form-control group_2"  >
							        		<?php foreach( $tier_list as $k => $v ){ $tier_id = '|'.$v['tier_id'].'|' ; ?>
							        			<option value="<?= $tier_id ?>" ><?= $v['title'] ?></option>
							        		<?php } ?>
							            </select>
							        </div>
							    </div>

                                <?php if ( $row_user['user_permission'] == 'admin' ){ ?>
	                                <div class="form-group">
	                                    <div class="col-sm-2 control-label"><?= $lang['permission']?></div>
	                                    <div class="col-sm-9">
	                                        <select name="permission" class="form-control ui-search-input" id="select_user_permission">
	                                            <option value="admin"><?= $lang['super_admin']?></option>
	                                            <option value="user"><?= $lang['User']?></option>
	                                            <option value="request"><?= $lang['request']?></option>
	                                        </select>
	                                    </div>
	                                </div>

	                                <script type="text/javascript">
	                                	$(document).ready(function(){
	                                		$('#select_user_permission').change(function() {
											    if ($(this).val() === 'user') {
											        $(".show_branch_action").show();
											    }else{
											    	$(".show_branch_action").hide();
											    }
											});
										});
	                                </script>

	                                <div class="form-group show_branch_action display_none">
	                                    <div class="col-sm-2 control-label"><?= $lang['branch']?></div>
	                                    <div class="col-sm-9">
	                                        <select name="branch_permission[]" class="form-control ui-search-input chosen-select select2-basic-single" id="" multiple>
	                                        	<?
	                                        		foreach ($branch_all as $key => $value) {
	                                        			echo '<option value="'.$key.'">'.$value.'</option>';
	                                        		}
	                                        	?>
	                                        </select>
	                                    </div>
	                                </div>
									<?php
									$permission2 = '' ;
									foreach ( $array_permission2 as $key1 => $value1 ){

										$permission2 .= '
										<div style="margin-bottom:10px;">
											<div class="custom_label" style="text-transform: capitalize; width: 100%; display: block; background: #eaeaea; padding: 5px;" >'.str_replace('-', ' ', $key1).'</div>' ;

											foreach ( $value1 as $key2 => $value2 ){

												$permission2 .= '
												<div>
													<div style="text-transform: capitalize; width: 100%; display: block; background: #fbfbfb; padding: 5px;" >'.str_replace('-', ' ', $key2).'</div>
													<div class="clearfix">' ;

														foreach ( $value2 as $key3 => $value3 ){

															$permission2 .= '
															<label style="text-transform: capitalize; display:block; float:left; width:33.33%; margin:0; padding:6px 0;" >
																<input type="checkbox" name="permission2[]" value="'.$key3.'" '.(strpos($row_page['user_permission2'], $key3) !== false ? 'checked' : '').' class="custom_checkbox permission2_checkbox" /> '.$value3.'
															</label>' ;

														}

													$permission2 .= '
													</div>
												</div>' ;

											}

										$permission2 .= '
										</div>' ;

									}
									?>
	                                <div class="form-group">
	                                    <div class="col-sm-2 control-label"><?= $lang['permission']?>2</div>
	                                    <div class="col-sm-9">
											<?= $permission2 ?>
	                                    </div>
	                                </div>
	                                <div class="form-group">
	                                    <div class="col-sm-2 control-label"><?= $lang['verification_code']?></div>
	                                    <div class="col-sm-9">
	                                    	<input type="radio" name="verification_code" class="ui-search-input" value="yes" checked /> <?= $lang['yes']?>
	                                    	<input type="radio" name="verification_code" class="ui-search-input" value="no" /> <?= $lang['no']?>
	                                    </div>
	                                </div>
	                                <div class="form-group">
	                                    <div class="col-sm-2 control-label">Is Interview By</div>
	                                    <div class="col-sm-9">
	                                    	<input type="radio" name="user_is_interview_by" class="ui-search-input" value="yes" checked /> <?= $lang['yes']?>
	                                    	<input type="radio" name="user_is_interview_by" class="ui-search-input" value="no" /> <?= $lang['no']?>
	                                    </div>
	                                </div>
	                            <?php } ?>

                                <div class="form-group">
                                    <div class="col-sm-2 control-label"></div>
                                    <div class="col-sm-9">
                                        <input type="hidden" name="hide" value="1" />
                                        <input type="hidden" name="hide_status" value="new" />
                                        <button type="submit" class="btn" style="color:white;background-color: #5e5bd0;float:right; margin-top: 5px;width: 100px;"><?= $lang['submit']?></button>
                                        <input type="hidden" name="hide_user_id" value="" />
                                    </div>
                                </div>   
                            </form>
                        </div>
					</div>
				</div>
			</div>
		<?php
	break ;

	// edit user
	case 'edit' :
	
		// set boolean
		$boolean_user_admin = false ;
		
		// check trash permission
		if ( $row_user['user_permission'] == 'admin' ){
			// check id is same
			if ( $row_user['user_id'] != $page ){
				// set to false
				$boolean_user_admin = true ;
			}
			// admin query
			$mysqli_admin = $mysqli->query("SELECT * FROM system_user
											WHERE user_permission = 'admin' AND user_trash = '0'") ;
			// check admin person
			if ($mysqli_admin->num_rows > 1){
				// set to false
				$boolean_user_admin = true ;
			}
		}
		
		// form submit
		if ($_POST['hide'] == '2' && $_POST['hide_status'] == 'trash' && $boolean_user_admin){
			switch($_POST['page_action']){
				case 'trash':
					$mysqli_query = "UPDATE " . system_user . " SET
									user_trash = '1'
									WHERE user_id = " ;
					$trash_page = trashPage('user', $mysqli, $mysqli_query, $_POST['multiple_trash']) ;
				break;
			}
		}
		
		// active page
		$active_main_menu = 'setting' ;
		$active_sub_menu = 'setting-user' ;
		$active_menu = 'user' ;
		
		// if not admin
		$admin_query = '';
		if ($row_user['user_permission'] != 'admin'){
			$admin_query = " AND user_permission != 'admin'" ;	
		}
		
		// customer join company
		$mysqli_page = $mysqli->query("SELECT * FROM system_user
									   WHERE user_id = '".$page."' AND user_trash = '0' ".$admin_query." LIMIT 1") ;
									   
		// check table exsits
		if ($mysqli_page->num_rows == 0){
			header("Location: user.php?page_mode=all") ;
			exit ;
		}else{
			// set query as array
			$row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC) ;	
		}
		
		// start header here
		include 'requires/page_header.php';
		include 'requires/page_top.php';
		?>
		<div class="warper container-fluid">   	
			<div class="page-header"><h1><?= $lang['User']?> <small><?= $lang['profile']?></small></h1></div>
			<div class="row">
				<div class="col-md-12">

                    <!-- trash -->
					<?php if ( $boolean_user_admin ){ ?>
                        <div class="panel panel-default">
                            <div class="panel-body">
                                <form method="post" action="user.php?page_mode=edit&page=<?= $page ?>">
                                    <select name="page_action" class="form-control selectpicker">
                                        <option value=""><?= $lang['select']?></option>
                                        <option value="trash"><?= $lang['move_to_trash']?></option>
                                    </select>
                                    <input type="hidden" name="hide" value="2" />
                                    <input type="hidden" name="hide_status" value="trash" />
                                    <input type="hidden" name="multiple_trash[<?= $row_page['user_id'] ?>]" class="trash_button" value="1">
                                    <input type="submit" class="btn" style="color:white; background-color:#5e5bd0; width:100px;" value="<?= $lang['submit']?>" />
                                </form>
                            </div>
                        </div>
                	<?php } ?>
                    <!-- end trash -->

                    <div class="error_message">
						<?php
						if ($_POST['hide'] == 1 && (!$boolean_user || !$boolean_password)){
							echo '
							<div class="result_error">' ;
								if (!$boolean_user){
									echo $lang['sorry_username_exsits'] .'<br />' ;
								}
								if (!$boolean_password){
									echo $lang['sorry_password_must_at_least_6_digits'] .'<br />' ;
								}
							echo '
							</div>' ;
						}
                        ?>
                    </div>
					<div class="panel panel-default">
						<div class="panel-heading"></div>
						<div class="panel-body">
							<form method="post" class="form-horizontal" id="quotationForm" action="user.php?page_mode=edit&page=<?= $page ?>&type=edit&hide=1" enctype="multipart/form-data" novalidate="novalidate">
                                <div class="form-group">
									<div class="col-sm-2 control-label"><?= $lang['call']?></div>
									<div class="col-sm-9">
                                    	<select name="call" class="form-control ui-search-input" required>
                                        	<option value=""><?= $lang['select_a_call']?></option>
                                            <option value="Miss" <?= $row_page['user_call'] == 'Miss' ? 'selected' : '' ?>><?= $lang['miss']?></option>
                                            <option value="Mrs" <?= $row_page['user_call'] == 'Mrs' ? 'selected' : '' ?>><?= $lang['mrs']?> </option>
                                            <option value="Mr" <?= $row_page['user_call'] == 'Mr' ? 'selected' : '' ?>><?= $lang['mr']?></option>
                                            <option value="Ms" <?= $row_page['user_call'] == 'Ms' ? 'selected' : '' ?>><?= $lang['ms']?></option>
                                        </select>
									</div>
								</div>
                                <div class="form-group">
									<div class="col-sm-2 control-label"><?= $lang['fullname']?></div>
									<div class="col-sm-9">
										<input type="text" name="fullname" class="form-control ui-search-input" required value="<?= dataFilter($row_page['user_fullname']) ?>" placeholder="<?= $lang['fullname']?>" />
									</div>
								</div>
                                <div class="form-group">
									<div class="col-sm-2 control-label"><?= $lang['email']?></div>
									<div class="col-sm-9">
										<input type="email" name="email" class="form-control ui-search-input" required value="<?= dataFilter($row_page['user_email']) ?>" placeholder="<?= $lang['email']?>" />
									</div>
								</div>
                                <div class="form-group">
									<div class="col-sm-2 control-label"><?= $lang['user_name']?></div>
									<div class="col-sm-9">
										<input type="text" name="username" class="form-control ui-search-input" required value="<?= dataFilter($row_page['user_name']) ?>" placeholder="<?= $lang['user_name']?>" />
									</div>
								</div>
								<div class="form-group">
									<div class="col-sm-2 control-label"><?= $lang['password']?></div>
									<div class="col-sm-9">
										<input type="password" name="password" class="form-control ui-search-input" placeholder="<?= $lang['password']?>" />
									</div>
								</div>
                                <div class="form-group">
                                    <div class="col-sm-2 control-label"><?= $lang['photo']?></div>
                                    <div class="col-sm-9">
                                        <div class="file_upload">
                                            <div class="file_form">
                                                <input type="hidden" name="hide_image" value="<?= dataFilter($row_page['post_file']) ?>" />
                                                <input type="hidden" name="remove_photo" value="">
                                                <input type="file" name="image" class="file_button control-label" tabindex="16" />
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-2 control-label"><?= $lang['preview']?></div>
                                    <div class="col-sm-9">
                                        <?php
                                        if ($row_page['user_signature'] != ''){
                                            echo '
                                            <label class="remove_photo"><input type="checkbox" name="remove_photo" class="ui-checkbox tick" value="1">&nbsp;'.$lang['remove_photo'].'</label>
                                            <a href="'.PATH.'uploads/User/b/'.dataFilter($row_page['user_signature']).'" class="fancybox">
                                                <img src="'.PATH.'uploads/User/'.dataFilter($row_page['user_signature']).'" width="180" />
                                            </a>' ;
                                        }else{
                                            echo '
                                            <input type="hidden" name="remove_photo" value="">
                                            <input type="text" class="form-control ui-search-input" value="'.$lang['no_image'].'" disabled />' ;	
                                        }
                                        ?>
                                    </div>
                                </div>

                                <?php if ( $row_user['user_permission'] == 'admin' ){ ?>
                                    <div class="form-group">
                                        <div class="col-sm-2 control-label"><?= $lang['user_colour']?></div>
                                        <div class="col-sm-9">
                                            <input type="color" name="colour" class="form-control ui-search-input" value="<?= $row_page['user_colour'] ?>" required placeholder="<?= $lang['user_colour']?>" />
                                        </div>
                                    </div>
	                                <div class="form-group">
	                            		<div class="col-sm-2 control-label"><?= $lang['Tier'] ?></div>
								        <div class="col-sm-9">
								        	<select name="user_tier[]" id="user_tier" multiple class="chosen-select select2-basic-single form-control group_2"  >
								        		<?php foreach( $tier_list as $k => $v ){ $tier_id = '|'.$v['tier_id'].'|' ; ?>
								        			<option value="<?= $tier_id ?>" <?= ( strpos($row_page['user_tier'], $tier_id) !== false ? 'selected' : '' ) ?> ><?= $v['title'] ?></option>
								        		<?php } ?>
								            </select>
								        </div>
								    </div>
									<div class="form-group">
										<div class="col-sm-2 control-label"><?= $lang['permission']?></div>
										<div class="col-sm-9">
											<select name="permission" class="form-control ui-search-input" id="select_user_permission">
                                                <option value="admin" <?= $row_page['user_permission'] == 'admin' ? 'selected' : '' ?> ><?= $lang['super_admin']?></option>
                                                <option value="user" <?= $row_page['user_permission'] == 'user' ? 'selected' : '' ?> ><?= $lang['User']?></option>
                                                <option value="request" <?= $row_page['user_permission'] == 'request' ? 'selected' : '' ?> ><?= $lang['request']?></option>
											</select>
										</div>
									</div>

	                                <script type="text/javascript">
	                                	$(document).ready(function(){
	                                		$('#select_user_permission').change(function() {
											    if ($(this).val() === 'user') {
											        $(".show_branch_action").show();
											    }else{
											    	$(".show_branch_action").hide();
											    }
											});
										});
	                                </script>

	                                <div class="form-group show_branch_action <?= $row_page['user_permission'] == 'user' ? '' : 'display_none' ?>">
	                                    <div class="col-sm-2 control-label"><?= $lang['branch']?></div>
	                                    <div class="col-sm-9">
	                                        <select name="branch_permission[]" class="form-control ui-search-input chosen-select select2-basic-single" id="" multiple>
	                                        	<?
	                                        		foreach ($branch_all as $key => $value) {
	                                        			echo '<option value="'.$key.'" '. (strpos($row_page['user_permission_branch'], '"'.$key.'"') !== false ? 'selected' : '') .' >'.$value.'</option>';
	                                        		}
	                                        	?>
	                                        </select>
	                                    </div>
	                                </div>
									<?php
									$permission2 = '' ;
									foreach ( $array_permission2 as $key1 => $value1 ){

										$permission2 .= '
										<div style="margin-bottom:10px;">
											<div class="custom_label" style="text-transform: capitalize; width: 100%; display: block; background: #eaeaea; padding: 5px;" >'.str_replace('-', ' ', $key1).'</div>' ;

											foreach ( $value1 as $key2 => $value2 ){

												$permission2 .= '
												<div>
													<div style="text-transform: capitalize; width: 100%; display: block; background: #fbfbfb; padding: 5px;" >'.str_replace('-', ' ', $key2).'</div>
													<div class="clearfix">' ;

														foreach ( $value2 as $key3 => $value3 ){

															$permission2 .= '
															<label style="text-transform: capitalize; display:block; float:left; width:33.33%; margin:0; padding:6px 0;" >
																<input type="checkbox" name="permission2[]" value="'.$key3.'" '.(strpos($row_page['user_permission2'], $key3) !== false ? 'checked' : '').' class="custom_checkbox permission2_checkbox" /> '.$value3.'
															</label>' ;

														}

													$permission2 .= '
													</div>
												</div>' ;

											}

										$permission2 .= '
										</div>' ;

									}
									?>
									<div class="form-group">
										<div class="col-sm-2 control-label"><?= $lang['permission']?>2</div>
										<div class="col-sm-9">
											<?= $permission2 ?>
										</div>
									</div>
	                                <div class="form-group">
	                                    <div class="col-sm-2 control-label"><?= $lang['verification_code']?></div>
	                                    <div class="col-sm-9">
	                                    	<input type="radio" name="verification_code" class="ui-search-input" value="yes" <?= ( $row_page['user_verification_type'] == 'yes' ? 'checked' : '' ) ?> /> <?= $lang['yes']?>
	                                    	<input type="radio" name="verification_code" class="ui-search-input" value="no" <?= ( $row_page['user_verification_type'] == 'no' ? 'checked' : '' ) ?> /> <?= $lang['no']?>
	                                    </div>
	                                </div>
	                                <div class="form-group">
	                                    <div class="col-sm-2 control-label">Is Interview By</div>
	                                    <div class="col-sm-9">
	                                    	<input type="radio" name="user_is_interview_by" class="ui-search-input" value="yes" <?= ( $row_page['user_is_interview_by'] == 'yes' ? 'checked' : '' ) ?> /> <?= $lang['yes']?>
	                                    	<input type="radio" name="user_is_interview_by" class="ui-search-input" value="no" <?= ( $row_page['user_is_interview_by'] == 'no' ? 'checked' : '' ) ?> /> <?= $lang['no']?>
	                                    </div>
	                                </div>
	                            <?php } ?>

								<?php if ( permissionCheck($row_user, 'user-user-edit') ){ ?>
									<div class="form-group">
										<div class="col-sm-2 control-label"></div>
										<div class="col-sm-9">
	                                    	<input type="hidden" name="hide" value="1" />
	                                        <input type="hidden" name="hide_status" value="edit" />
											<button type="submit" class="btn" style="color:white;background-color: #5e5bd0;float:right; margin-top: 5px;width: 100px;"><?= $lang['submit'] ?></button>
											<input type="hidden" name="hide_user_id" value="<?= $row_page['user_id'] ?>" />
										</div>
									</div> 
								<?php } ?>

							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
		<?php
	break ;
	// all customer list
	case 'all' :
	default :

		// check permission
		if ( !permissionCheck($row_user, 'user-user-view') ){
			header('Location: index.php') ;
			exit ;
		}
	
		$search_name = escapeString($_GET['search_name']) ;
		$search_mail = escapeString($_GET['search_mail']) ;
		$search_date = ( $_GET['search_date']!= '' ? date('Y-m-d', strtotime($_GET['search_date'])) : '' ) ;

		// query type
		$search_query = '' ;

		if( $search_name != ''){
			$search_query .= " AND user_name LIKE '%".$search_name."%'" ;
		}
		if( $search_mail != ''){
			$search_query .= " AND user_email LIKE '%".$search_mail."%'" ;
		}
		if ( $search_date != '' ){
		    $search_query .= " AND user_date like '%".$search_date."%' " ;
		}

		// search query
		if ($search != ''){
			$search_query .= " AND (user_name LIKE '%".$search."%' OR user_email LIKE '%".$search."%' OR user_fullname LIKE '%".$search."%')" ;
		}
		
		// if not admin
		if ( $row_user['user_permission'] != 'admin' ){
			$search_query .= " AND user_permission != 'admin'" ;	
		}
		
		// active page
		$active_main_menu = 'setting' ;
		$active_sub_menu = 'setting-user' ;
		$active_menu = 'user' ;
		
		// pagination
		if (isset($page) && !empty($page)) { $product_page = $page ; } else { $product_page = 1 ; } // next and prev page (5 thing need to change)
		$start_from = ($product_page - 1) * LIMIT ; //end next and prev page
		
		// page query
		$mysqli_query = "SELECT * FROM system_user
						 WHERE user_email NOT LIKE '%ips.com.my%' AND user_permission != 'employment' AND user_trash = '0'".$search_query ;
		$mysqli_page = $mysqli->query($mysqli_query." ORDER BY user_id LIMIT $start_from, " . LIMIT) ;
		
		// set search url
		$search_url = 'search='.$search ;
		
		// load pagination
		$page_pagination = nextPrevious($product_page, LIMIT, $search_url, $mysqli_query);
		

		if ($_POST['hide'] == '1' && $_POST['hide_status'] == 'action'){
			 switch($_POST['page_action']){
			 	case 'export-excel-sql' :
		            require('PHPExcel/Classes/PHPExcel.php');
		            // Create new PHPExcel object
		            $objPHPExcel = new PHPExcel();

		            // set letter
		            $letters = array();
		            $letter = 'A';
		            while ($letter !== 'AAA') {
		                $letters[] = $letter++;
		            }

		             // get array header
		            $HeaderArray = array(
		                                'Code(20)',
		                                'Description'
		                                );
		            // Set document properties
		            $objPHPExcel->getProperties()->setCreator("IPS")
		                                         ->setLastModifiedBy("CMS")
		                                         ->setTitle("System Export Excel")
		                                         ->setSubject("System Export Excel")
		                                         ->setDescription("System Export Excel")
		                                         ->setKeywords("System Excel")
		                                         ->setCategory("System Excel");

		            // Add some data
		            if (arrayCheck($HeaderArray)){
		                $cound_header = 1;
		                $count = 0;
		                foreach($HeaderArray as $key => $header_name){              
		                    // if sub exist
		                    if (arrayCheck($header_name)){
		                        
		                        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($letters[$count].$cound_header, $key);
		                        $count_sub_header = $cound_header;
		                        $sub_count = $count;
		                        $count_sub_header++;
		                        foreach($header_name as $header_name_sub){
		                            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($letters[$sub_count].$count_sub_header, $header_name_sub);
		                            // continue first layer
		                            $count = $sub_count;
		                            // add second layer
		                            $sub_count++;                       
		                        }
		                    }else{
		                        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($letters[$count].$cound_header, $header_name);
		                    }
		                    // merge value 
		                    $begin = $count;
		                    //$end = $count+15;
		                    $end = $count;
		                    
		                    $count++;
		                }
		            }

		            $mysqli_page = $mysqli->query($mysqli_query."  ORDER BY user_id ") ;
		            if ($mysqli_page->num_rows > 0){
		                
		                $array_customer = array() ;
		                $count = 2 ;
		                while ($row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC)){
		                    if($row_page['user_name']!= '' ){
		                          $objPHPExcel->setActiveSheetIndex(0)
		                                        ->setCellValue('A'.$count, 'A'.$row_page['user_id'])
		                                        ->setCellValue('B'.$count, dataFilterDash($row_page['user_name']));
		                                        $count++;
		                    }
		                }   
		                               
		            }
		            // file name
		            $fileName = "User_" .time();       

		            // Rename worksheet
		            $objPHPExcel->getActiveSheet()->setTitle($fileName);

		            // Set active sheet index to the first sheet, so Excel opens this as the first sheet
		            $objPHPExcel->setActiveSheetIndex(0);

		            // Save Excel 2007 file
		            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');

		            //Setting the header type
		            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		            header('Content-Disposition: attachment;filename="'.$fileName.'.xlsx"');
		            header('Cache-Control: max-age=0');

		            // save to pc
		            $objWriter->save('php://output');
		            header("Refresh: 0") ;
		            exit ;
		        break ;
			 }
		}

		// start header here
		include 'requires/page_header.php' ;
		include 'requires/page_top.php' ;
		
		?>
		<!-- Header Ends -->
		<div class="warper container-fluid">
		<div class='container' style="background-color: white; border-radius: 10px;">
			<div class="page-header" style="margin: 30px 0px 0px 0px;padding: 0px;">
				<div class="row">
                	<div class="pull-left col">
                		<h1><?= $lang['User'] ?> <small><?= $lang['list'] ?></small></h1>
                	</div>
                	<div class="pull-right col">
                		<!--any button aliogn right-->
                	</div>
                </div>
	            <div class="result_success display_none" id="result_quantity_warning"><?= $lang['all_devices_for_the_selected_user_were_disconnected'] ?></div>
	        </div>

	        <div class="panel panel-default" id="basic-table-title">
	            <div class="panel-heading">question</div>
	            <div class="panel-body">
	                <form method="get" class="form-horizontal">
	                    <div class="form-group">
	                        <label class="col-sm-2 control-label"><?= $lang['Name'] ?></label>
	                        <div class="col-sm-9">
	                            <input type="text" name="search_name" value="<?= $search_name ?>" class="form-control" />
	                        </div>
	                    </div>
	                    <div class="form-group">
	                        <label class="col-sm-2 control-label"><?= $lang['email'] ?></label>
	                        <div class="col-sm-9">
	                            <input type="text" name="search_mail" value="<?= $search_mail ?>" class="form-control" />
	                        </div>
	                    </div>
	                    <div class="form-group">
	                        <label class="col-sm-2 control-label"><?= $lang['date'] ?></label>
	                        <div class="col-sm-9">
	                            <input class="form-control" name="search_date" type="date" value="<?= $search_date ?>" placeholder="Date Resigned">
	                        </div>
	                    </div>
	                    <div class="form-group">
	                        <div class="col-sm-11">
	                            <input type="hidden" name="page_mode" value="<?= $page_mode ?>" />
	                            <input type="hidden" name="search" value="<?= $search ?>" />
	                            <button type="submit" class="btn" style="color:white;background-color: #5e5bd0;float:right; margin-top: 5px;width: 100px;"><?= $lang['submit'] ?></button>
	                        </div>
	                    </div>
	                </form>
	            </div>
	        </div>

			<div class="panel panel-default">
				<div class="panel-heading">listing</div>
				<div class="panel-body">

					<!-- <form method="post">
                    	<select name="page_action" class="form-control selectpicker" id="page_action_form">
			                <option value=""><?= $lang['select'] ?></option>
			                <option value="export-excel-sql"><?= $lang['export_as'] ?>Excel Sql</option>
			            </select>
			            <input type="hidden" name="hide" value="1" />
			            <input type="hidden" name="hide_status" value="action" />
			            <input type="submit" class="btn btn-purple" value="<?= $lang['submit']?>" />
			        </form> -->

					<form method="post">
						<table cellpadding="0" cellspacing="0" border="0" class="responsive table table-striped table-bordered" id="basic-datatable">
							<thead>
								<tr>
									<th><?= $lang['user_name'] ?></th>
									<th><?= $lang['email'] ?></th>
									<th><?= $lang['fullname'] ?></th>
									<th><?= $lang['level'] ?></th>
                                    <th><?= $lang['login_code'] ?></th>
                                    <th><?= $lang['date_request'] ?></th>
									<th><?= $lang['last_login'] ?></th>
                                    <th><?= $lang['last_login_location'] ?></th>
                                    <th><?= $lang['last_login_IP'] ?></th>
                                    <th><?= $lang['last_login_device'] ?></th>
                                    <th><?= $lang['modified_date'] ?></th>
                                    <th><?= $lang['status'] ?></th>
								</tr>
							</thead>
							<tbody>
								<?php
								if ($mysqli_page->num_rows > 0){
									while ($row_page = $mysqli_page->fetch_array(MYSQLI_ASSOC)){
										
										$lat = dataFilter($row_page['user_last_latitude']) ;
										$lot = dataFilter($row_page['user_last_longtitude']) ;
										$coordinates = ($lat != '' && $lot != '' ? true : false) ;
										
										echo '
										 <tr class="odd gradeX">
											<td><a href="user.php?page_mode=edit&page='.$row_page['user_id'].'">'.dataFilter($row_page['user_name']).'</a></td>
											<td>'.dataFilter($row_page['user_email']).'</td>
											<td>'.dataFilter($row_page['user_fullname']).'</td>
											<td>' ;
												$level = '' ;
												switch($row_page['user_permission']){
													case 'admin' : $level = 'Super Admin' ; break ;
													case 'user' : $level = 'User' ; break ;
													case 'request' : $level = 'Request' ; break ;
												}
												echo $level.'
											</td>
											<td>'.dataFilterDash($row_page['user_verification']).'</td>
											<td>'.resetDateTimeFormat($row_page['user_verification_date']).'</td>
											<td>'.resetDateFormat($row_page['user_last_login']).'</td>
											<td class="align_center">' ;
												if ($coordinates){
													echo '
													<span class="display_none">
														<span class="lat">'.$lat.'</span>,
														<span class="lon">'.$lot.'</span>
													</span>
													<a href="https://maps.google.com/maps?&amp;z=10&amp;q='.$lat.'+'.$lot.'&amp;ll='.$lat.'+'.$lot.'" target="_blank"><i class="fa fa-map-marker"></i></a>' ;
												}else{
													echo '-' ;	
												}
											echo '
											</td>
											<td>'.dataFilterDash($row_page['user_last_ip']).'</td>
											<td>'.dataFilterDash($row_page['user_last_device']).'</td>
											<td>'.date('Y-m-d H:i:s', strtotime($row_page['user_modified'])).'</td>
											<td class="text_center">'.( ($row_page['user_login_cookies'] != '' && permissionCheck($row_user, 'user-user-edit') ) ? '<button class="btn btn-purple user_logout_device" id="'.$row_page['user_id'].'">'.$lang['disconnect'].'</button>' : '-').'</td>
										</tr>';
									}
								}else{
									echo '
									<tr class="odd gradeX">
										<td class="border_none">'.$lang['no_data'].'</td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
										<td class="border_none"></td>
									</tr>' ;
								}
								?>
							</tbody>
						</table>
					</form>
					<?= $page_pagination['page_pagination'] ?>
				</div>
			</div>
		</div>
		</div>
        <?php
	break ;
}

// footer
include 'requires/page_footer.php' ;
?>