<?php
if (isset($_REQUEST["panel"]))
	$_SESSION["panel"] = cleanData($_REQUEST["panel"]);
$panel = (isset($_SESSION['panel']) && $_SESSION['panel'] != "") ? $_SESSION['panel'] : "posts";
if (!$loginUser->isLogin) {
	$_SESSION['panel'] = $panel = "index";
}
?>
<!doctype html>
<html lang="en">

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=0.8">
	<title>پنل مدیریت <?= $publicSetting->name ?></title>
	<link rel="icon" type="image/*" href="<?= helper::breakByNewLine($publicSetting->icon)[0]; ?>">

	<?php require_once("$activeTheme/requires/header.php"); //<!-- افزودن ضروریات یاراکد به صفحه --> ?>
</head>

<body dir="rtl">

	<?php $loginUser->loginoutForm(); ?>

	<div class="d-flex  align-items-stretch  m-0 bg-success p-1 ">
		<div class="m-0 p-0  d-lg-none d-print-none">
			<button class="btn position-fixed btn-primary d-lg-none top-0 end-0 m-5 z-1" type="button"
				data-bs-toggle="offcanvas" data-bs-target="#offcanvasResponsive" aria-controls="offcanvasResponsive">
				<i class="fas fa-bars"></i>
			</button>
		</div>
		<div class="col-lg-2 m-0  position-relative d-print-none"
			style=" background-image: linear-gradient(to bottom, #4F46E5, #818CF8 );">
			<div class="sticky-lg-top   offcanvas-lg  offcanvas-end  ps-0 pe-3 text-light fs-6   d-print-none"
				style="  background-image: linear-gradient(to bottom, #4F46E5, #818CF8 );" tabindex="-1"
				id="offcanvasResponsive" aria-labelledby="offcanvasResponsiveLabel">
				<div class="offcanvas-header" dir="rtl">


					<button type="button" class="btn-close " data-bs-dismiss="offcanvas"
						data-bs-target="#offcanvasResponsive" aria-label="Close"></button>
				</div>

				<div class="offcanvas-body ">
					<div class=" vh-100 overflow-y-auto pt-3 ps-1" style="">
						<div class="d-flex justify-content-start align-items-center gap-2">
							<div class=" bg-body rounded-2 d-flex align-items-center justify-content-center"
								style="width: 40px; height: 40px;">
								<i class="fas fa-cube text-primary fs-4"></i>
							</div>
							<h6 class="text-nowrap ">پنل مدیریت <?= $publicSetting->name ?></h6>
						</div>

						<?php if ($loginUser->isLogin) { ?>
							<nav class="row m-0 p-0  ">
								<a href="?panel=index"
									class="d-flex align-items-center btn  py-2 rounded-3 special-button text-light ">
									<i class="fas fa-home"></i>
									<span>داشبورد</span>
								</a>

								<?php menus($adminMenus, $loginUser, $panel); ?>



							</nav>
						<?php } ?>
					</div>
				</div>
			</div>
		</div>
		<div class="col-12 col-lg-10 p-1 bg-body-tertiary">


			<?php
			require_once("$activeTheme/requires/body.php"); // <!-- افزودن ضروریات یاراکد به صفحه -->

			echo "<div class='d-flex justify-content-between align-items-center sticky-top z-1  my-5 shadow p-1 rounded text-light d-print-none'
								style='background-image: linear-gradient(to bottom, #4F46E5, #818CF8 );'
								>";
			echo '<a href="index.php?selected_id=-1" class=" btn text-light bg-body" > 
	<img class="  rounded" style="max-width:100px; height:50px;" src="' . helper::breakByNewLine($publicSetting->brandpic)[0] . '" alt="" >
	  </a>';
			echo "<span  >";
			showActiveMenuName($adminMenus, $loginUser, $panel);
			echo "</span>";
			echo "<span  class='text-light'>";
			$loginUser->loginoutButtons();
			echo "</span>";
			echo "</div>";
			switch ($panel) {
				case "profile":
					$loginUser->SET_REQUEST_RECIVER("idProfile", [BASE::SHOW_EDIT_BTN]);
					$cols = [
						"name",
						"family",
						"meli" => ['type' => "label"],
						"birthday",
						"address",
						"postalcode",
						"email",
						"pic_url",
					];
					$loginUser->formByFixedButton("idProfile", $cols, [BASE::SHOW_EDIT_BTN], ["validation" => true, "ajaxBtn" => true]);
					break;
				case "cropper":
					if (!$loginUser->hasAccess($panel))
						break;
					$files->showfileManager("bigFile");
					break;
				default:
					if (!$loginUser->hasAccess($panel))
						break;
					if (isset($_GET['panel']) && !isset($_GET['action']))
						$_SESSION['actionnumber'] = "notsended";
					if (isset($_GET['action']))
						$_SESSION['actionnumber'] = cleanData($_GET['action']);

					if (isset($_SESSION['actionnumber']) && $_SESSION['actionnumber'] != "notsended") {
						foreach ($adminMenus as $item) {
							if (isset($item["list"])) {
								foreach ($item["list"] as $subItem) {
									if ($subItem['name'] == $panel) {

										eval ($subItem["action"]);
										break;
									}
								}
							} else {
								if ($item['name'] == $panel) {

									eval ($item["action"]);
									break;
								}
							}
						}
					} else {
						$str = '$obj= new ' . strtoupper($panel) . '($conn);';
						eval ($str);
						if ($panel == "publicSetting" || $panel == "storeSetting") {
							$obj->editAsSetting();
						} else {
							$obj->editPage();
						}
					}

					break;
			}
			?>



		</div>
	</div>
	<?php require_once("$activeTheme/requires/footer.php");  //<!-- افزودن ضروریات یاراکد به صفحه --> ?>
</body>

</html>
<style>
	.special-item {
		background-color: transparent !important;
		box-shadow: none !important;
		border: none !important;
	}

	.special-button:hover {
		background-color: whitesmoke !important;
		opacity: 0.7 !important;
		color: darkblue !important;
	}

	.special-button::after {
		filter: brightness(0.7) invert(0.8) !important;
	}
</style>
<?php
function menus($adminMenus, $loginUser, $panel)
{
	$menuIndex = 0;
	$i = 1;
	foreach ($adminMenus as $menu) {
		if (!$loginUser->hasAccess($menu['name']))
			continue;
		if (isset($menu['list'])) {
			$active = false;
			foreach ($menu['list'] as $item) {
				if ($item['name'] == $panel)
					$active = true;
			}
		
			?>
			<div class="accordion w-100 m-0 p-0 mt-1" id="accordionPanels" dir="rtl">
				<div class="accordion-item    gap-3 <?php if (!$active)
					echo "special-item text-light"; ?>">
					<h2 class="accordion-header ">
						<button class="accordion-button special-button d-flex justify-content-between <?php if (!$active)
							echo "collapsed special-item text-light";
						else
							echo "active"; ?>" type="button" data-bs-toggle="collapse" data-bs-target="#panel-id-<?= $i; ?>" aria-expanded="<?php if ($active)
									echo "true";
								else
									echo "false"; ?>" aria-controls="panel-id-<?= $i; ?>">
							<span class="w-100 text-end"><?= $menu['label'] ?></span>
						</button>
					</h2>
					<div id="panel-id-<?= $i; ?>" class="accordion-collapse collapse <?php if ($active)
						  echo "show"; ?>">
						<div class="d-flex flex-column gap-1 pe-1">
							<?php
							foreach ($menu['list'] as $item) {
								if ($loginUser->hasAccess($item['name'])) {


									?>
									<a dir="rtl" href="?panel=<?= $item['name'] ?><?php if (isset($item['action']))
										  echo '&action=' . $menuIndex; ?>" class="d-flex 
					 align-items-center text-nowrap overflow-hidden text-truncate 
					 btn py-2 rounded-3
					 <?php if ($active && $item['name'] != $panel)
						 echo "btn-outline-secondary";
					 else
						 echo "special-button text-light"; ?>
					 <?php if ($item['name'] == $panel)
						 echo "btn-outline-info active"; ?>  ">
										<span><?= $item['label'] ?></span>
									</a>
									<?php
									$menuIndex++;
								}
							}
							?>
						</div>
					</div>
				</div>
			</div>
			<?php
			$i++;
		} else {
			if ($loginUser->hasAccess($menu['name'])) {
				?>
				<a href="?panel=<?= $menu['name'] ?><?php if (isset($menu['action']))
					  echo '&action=' . $menuIndex; ?>" class="d-flex align-items-center
					 btn py-2 rounded-3
					 <?php if ($menu['name'] == $panel)
						 echo "btn-outline-info active";
					 else
						 echo " text-light special-button"; ?>">
					<span><?= $menu['label'] ?></span>
				</a>
				<?php
			}
			$menuIndex++;

		}
	}
}

function showActiveMenuName($adminMenus, $loginUser, $panel)
{
	$menuIndex = 0;
	$activeBtn = "";
	$activeItem = "";

	foreach ($adminMenus as $adminMenuItem) {

		if (isset($adminMenuItem['list'])) {
			$active = false;
			foreach ($adminMenuItem['list'] as $item) {
				if ($loginUser->hasAccess($item['name'])) {
					if ($panel == $item['name']) {
						$activeItem = $item['label'];
						$active = true;
					}
					$menuIndex++;
				}
			}
			if ($active == true) {
				$activeBtn = $adminMenuItem['label'];
			}
		} else {
			if ($loginUser->hasAccess($adminMenuItem['name'])) {
				if ($panel == $adminMenuItem['name'])
					$activeBtn = $adminMenuItem['label'];
			}
			$menuIndex++;
		}
	}
	$str = "<h6 class='d-inline-block  rounded-pill p-1 '><strong>$activeBtn</strong></h6>";
	if ($activeItem != "")
		$str .= " > <h6 class='d-inline-block  rounded-pill p-1 '><strong>$activeItem</strong></h6>";
	echo $str;
}
?>