<?php
require_once __DIR__ . '/../magic/auth.php';

$auth = bdr_require_privilege(49, '../logout.php');
$user = $auth['user_id'];
$father = $auth['user_id'];
$branch = $auth['branch'];
$conn = $auth['conn'];
$db = $auth['conn'];

$user 		= $_REQUEST['id'];
$role = 3 ;
$element_id = 49;

$master = $db->prepare('SELECT role from users where user_id = ? ');
$master->bindParam(1, $father);
$master->execute();
$ro = $master->fetch(PDO::FETCH_ASSOC);
$role		  = $ro["role"];

$sitQuery = $db->prepare('SELECT realname , branch from users where user_id = ? ');
$sitQuery->bindParam(1, $user);
$sitQuery->execute();
if ($sitQuery->rowCount() < 1) {
	header("location:../logout.php");
} else {
	$row = $sitQuery->fetch(PDO::FETCH_ASSOC);
	$realname     = $row["realname"];
	$branch 	  = $row["branch"];
}
?>
<!DOCTYPE html>
<html lang="ar" dir="rtl">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="robots" content="index,follow">
	<meta name="author" content="سامح دياب">
	<link rel="icon" href="../images/perfect.ico">
	<title>تحديث صلاحيات مستخدم</title>
	<link rel="stylesheet" href="../dist/css/bootstrap.css" />
	<link rel="stylesheet" href="../css/font-awesome.min.css">
	<link rel="stylesheet" href="../hj2/humanity.calendars.picker.css">
	<link rel="stylesheet" href="../css/myCar.css" />
	<script src="../js/jquery.js"></script>
	<script src="../dist/js/umd/popper.min.js"></script>
	<script src="../dist/js/bootstrap.js"></script>
	<script src="../hj2/jquery.plugin.js"></script>
	<script src="../hj2/jquery.calendars.js"></script>
	<script src="../hj2/jquery.calendars.plus.js"></script>
	<script src="../hj2/jquery.calendars.picker.js"></script>
	<script src="../hj2/jquery.calendars.ummalqura.js"></script>
	
</head>

<body style="background-color:#3D3D3D;">
	<div class="container-fluid fixed-top" style="background-color:black;padding:10px;">
		<h6 style="color:green; font-size:12pt;"> شاشة تعديل صلاحية موظف <?php echo $realname; ?></h6>
	</div>
	<br /><br /> <br /><br /><br />
	<div class="container-fluid" style="background-color:#3D3D3D;color:#cfd2da;">
		<input type="hidden" id="user_id" value="<?php echo $user; ?>">

		<div class="form-row">
			<div class="col-2">
				<label for="branch" class="control-label " style="float:left;">الفرع</label>
			</div>
			<div class="col-2">
				<select class="form-control" id="branch" name="branch" style="padding:3px;">
					<?php
					$sql = $db->prepare("SELECT branchID , b_name from branches");
					$sql->execute();
					if ($sql->rowCount() > 0) {
						while ($row = $sql->fetch()) {
							$branchid   = $row["branchID"];
							$b_name     = $row["b_name"];
							if ($branchid == $branch) {
								echo '<option selected="selected" value="' . $branchid . '" >' . $b_name . '</option>';
							} else {
								echo "<option value= $branchid >$b_name</option>";
							}
						}
					} ?>
				</select>
			</div>
			<div class="col-2">
				<button id="branch_btn" class="btn btn-outline-primary btn-block">تعديل</button>
			</div>
		</div>
		<br /><br />
		<div class="form-row" style="padding:4px;">
			<div class="col-2">
				<label for="new_pass" class="control-label" style="float:left;">كلمة السر الجديدة</label>
			</div>
			<div class="col-3">
				<input type="password" class="form-control" id="new_pass" name="new_pass" />
			</div>
			<div class="col-1">
				<label for="new_pass2" class="control-label" style="float:left;">تأكيد</label>
			</div>
			<div class="col-3">
				<input type="password" class="form-control" id="new_pass2" name="new_pass2" placeholder="تأكيد كلمة السر" />
			</div>
			<div class="col-2">
				<button id="pass_btn" class="btn btn-outline-primary btn-block">تعديل</button>
			</div>
		</div>
		<br /><br />
		<section>
			<div class="form-row">
				<div class="col">
<?php
$oprtion = "";
if($role >2){
		$where = 	"bar_id < 9 and bar_id NOT IN(3,4,5,8)";
		$oprtion = "AND  site_element.element_id NOT IN(5 , 8 , 9 ,41)";
	}
	else{
		$where =	"bar_id < 9";
		$oprtion = "";
	}
	
	$bars = $db->prepare("SELECT bar_id , arabic , comm from bar_priv where $where");
	$bars->execute();
	if ($bars->rowCount() > 0) {
		while ($bar = $bars->fetch()) {
			$output1 = '';
			$bar_id = $bar["bar_id"];
			$arabic = $bar["arabic"];
			$comm   = $bar["comm"];
			$output1 .= '<label class="control-label"style="float:right;font-size:16px;color:#cfd2da;">' . $arabic . ' # تحتوي ' . $comm . ' </label>';
			$output1 .= '</div></div><br /><br />
							<div class="form-row">
								<div class="col-6"style="color:#cfd2da;">
									<h6>صلاحيات الموظف</h6>
										<table class="table table-bordered table-hover">
											<thead>
												<tr>
													<th class="text-center" width="10%">الحالة</th>
													<th class="text-center" width="75%">شاشة</th>
													<th class="text-center" width="15%">حذف</th>
													</tr>
												 </thead>
											<tbody>';

							?>
					<?php
							$output2 = "";
							$sql = $db->prepare("SELECT site_element.arb_name ,site_element.bar_id , privileges.user_id , privileges.element_id , privileges.yes_no ,  privileges.priv_id
												FROM privileges , site_element
												WHERE site_element.element_id = privileges.element_id
												AND privileges.user_id = ? AND site_element.bar_id = ? 
												order by element_id ");
							$sql->bindParam(1, $user);
							$sql->bindParam(2, $bar_id);
							$sql->execute();
							if ($sql->rowCount() > 0) {
								while ($row = $sql->fetch()) {
									$bb = $row['yes_no'];
									$priv_id = $row['priv_id'];
									if ($bb == 1) {
										$dd = '<button class="btn btn-success btn-sm btn-block active " act="' . $priv_id . '" style="font-weight:bold ;font-size:14px; padding:2;" >فعال </button>';
									} else {
										$dd = '<button class="btn btn-danger btn-sm btn-block cutt " cut="' . $priv_id . '" style="font-weight:bold ;font-size:14px; padding:2;" >معطل </button>';
									}
									$output2 .= '<tr>
										<td/>' . $dd . '</td>
										<td>' . $row['arb_name'] . '</td>
										<td><button del="' . $priv_id . '" style="font-weight:bold ;font-size:14px; padding:2;" class="btn btn-danger btn-sm btn-block dell ">حذف</button></td>
										</tr>  ';
								}
							} else {
								$output2 .= '<tr><td colspan="3">لا يوجد سجلات ...</td></tr>';
							}
							$output1 .= $output2;
							$output1 .= ' </tbody>
				<tfoot></tfoot>
			</table>
</div>
<div class="col-6" style="color:#cfd2da;">
<h6>الصلاحيات المتاحة</h6>
	 <table class="table table-bordered table-hover">
		 <thead>
				<tr>
						 <th class="text-center" width="25%">الشاشة</th>
						 <th class="text-center" width="65%">تعليق</th>
						 <th class="text-center" width="10%">إضافة</th>
				</tr></thead><tbody>';
							$output3 = "";
							$sql = $db->prepare("SELECT DISTINCT element_id ,arb_name ,bar_id , comm
												FROM site_element
												WHERE bar_id = ? AND ready = 1 AND element_id NOT IN (SELECT DISTINCT element_id from privileges where user_id = ?)
												$oprtion order by element_id ");
							$sql->bindParam(1, $bar_id);
							$sql->bindParam(2, $user);
							$sql->execute();
							if ($sql->rowCount() > 0) {
				while ($row = $sql->fetch()) {
							$output3 .= '<tr><td >' . $row['arb_name'] . '</td>
									<td>' . $row['comm'] . '</td>
									<td><button id="' . $row['element_id'] . '" style="font-weight:bold ;font-size:14px; padding:2;" class="btn btn-primary btn-sm btn-block add_new ">إضافة</button></td>
									</tr>  ';
								}
							} else {
								$output3 .= '<tr><td colspan="3">لا يوجد سجلات ...</td></tr>';
							}
							$output1 .= $output3;
							$output1 .= '	 </tbody>
								 <tfoot></tfoot>
								</table></div></div><br />';
							echo $output1;
						}
					}
					?>
		</section>

	</div>
	<script>
		$(document).ready(function() {
			$(".add_new").click(function(event) {
				event.preventDefault();
				if (confirm("هل تريد إضافة هذه الصلاحية للمستخدم الحالي ؟")) {
					var element_id = $(this).attr('id');
					var user_id = $("#user_id").val();
					$.ajax({
						url: "priv_action.php",
						method: "POST",
						data: {
							element_id: element_id,
							user_id: user_id
						},
						success: function(data) {
							location.reload();
						}
					});
				} else {
					return false;
				}
			}); //end of add privileges
			$(".active").click(function(event) {
				event.preventDefault();
				if (confirm("هل تريد تعديل صلاحية المستخدم الحالي ؟")) {
					var priv_id = $(this).attr('act');
					var active = 2;
					$.ajax({
						url: "priv_action.php",
						method: "POST",
						data: {
							priv_id: priv_id,
							active: active
						},
						success: function(data) {
							location.reload();
						}
					});
				} else {
					return false;
				}
			}); //end of edit privileges
			$(".cutt").click(function(event) {
				event.preventDefault();
				if (confirm("هل تريد تعديل صلاحية المستخدم الحالي ؟")) {
					var priv_id = $(this).attr('cut');
					var active = 1;
					$.ajax({
						url: "priv_action.php",
						method: "POST",
						data: {
							priv_id: priv_id,
							active: active
						},
						success: function(data) {
							location.reload();
						}
					});
				} else {
					return false;
				}
			}); //end of edit privileges
$(".dell").click(function(event) {
				event.preventDefault();
				if (confirm("هل تريد حذف الصلاحة من المستخدم الحالي ؟")) {
					var dell = $(this).attr('del');
				$.ajax({
						url: "priv_action.php",
						method: "POST",
						data: {	dell: dell },
						success: function(data) {
							location.reload();
						}
					});
				} else { return false; }
			}); //end of edit privileges

$("#branch_btn").click(function(event) {
				event.preventDefault();
				if (confirm("هل تريد نقل الموظف الحالي لفرع اخر ؟")) {
					var user_id = $("#user_id").val();
					var branch = $("#branch").val();
					$.ajax({
						url: "priv_action.php",
						method: "POST",
						data: {
							user_id: user_id,
							branch: branch
						},
					success: function(data) {
							location.reload();
						}
					});
			} else {return false;	}
			});
$("#pass_btn").click(function(event) {
				event.preventDefault();
				if (confirm("هل تريد تغيير كلمة السر ؟")) {
					var user_id = $("#user_id").val();
					var new_pass = $("#new_pass").val();
					var new_pass2 = $("#new_pass2").val();
					if (new_pass != new_pass2) {
						alert("كلمة السر غير متطابقة");
					} else {
						$.ajax({
							url: "priv_action.php",
							method: "POST",
							data: {
								user_id: user_id,
								new_pass: new_pass
							},
							success: function(data) {
								location.reload();
							}
						});
					}
				} else {return false;}
			});

		});
	</script>

</body>

</html>
