<?php
$requestUri = $_SERVER['REQUEST_URI'];
if (strpos($requestUri, 'index.php') !== false) {
$newUrl = str_replace('index.php', '', $requestUri);
header('Location: ' . $newUrl);
exit; }

session_start();
include "config/connect.php";
include 'pages/control_page.php';
include 'list_data/list_data.php';
include 'list_data/sweetalert.php';
include "config/routeros_api.class.php";
include "config/whatsapp_api.php";
include "config/tanggal_indo.php";

//Fungsion PHP
$List = new Listdata();
$date = date('Y-m-d H:i:s');

if (!isset($_SESSION['user']['id']) && !isset($_SESSION['master']['id'])) 
{
  echo "<script>location='login.php';</script>";
  header('Location: login.php');
  exit();
}

//Penentuan User ID
if (isset($_SESSION['user']['id'])) {
  $UserId = $_SESSION['user']['id'];
} else { $UserId = '0'; }

$armenu = array();
$stmt = $pdo->prepare("SELECT id FROM master_menu WHERE master_akses = '0' AND id != '13'");
$stmt->execute();
while($menuarr = $stmt->fetch(PDO::FETCH_ASSOC)) {
  $armenu[] = $menuarr['id'];
}
$menu_aray = implode(",", $armenu);

//Penentu data User
if (isset($_SESSION['user']['id'])) {
  $SqlSvData = 'id_data_server IN ('.$_SESSION['user']['id_data_server'].') AND';
  $SvId = $_SESSION['user']['id_data_server'];
  $SvNama = $_SESSION['user']['nama'];
  $SqlDataWH = "WHERE id IN (".$SvId.")";
  $SqlDataWHId = "WHERE id_data_server IN (".$SvId.")";
  $SqlDataWHLikeId = "WHERE id_data_server LIKE '%".$SvId."%'";
  $SqlDataId = "AND id_data_server IN (".$SvId.")";
  $menu_akses = $menu_aray.','.$_SESSION['user']['akses'];
  $profile = $_SESSION['user']['profile'];
} 
else { 
  if ($_SESSION['master']['id'] == '1') {
    $SqlSvData = '';
    $SvId = $_SESSION['master']['id'];
    $SvNama = $_SESSION['master']['nama_pemilik'];
    $SqlDataWH = "WHERE id != '1'";
    $SqlDataWHId = "";
    $SqlDataWHLikeId = "";
    $SqlDataId = "";
    $menu_akses = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45";
    $profile = $_SESSION['master']['profile'];
  }
  else {
    $SqlSvData = "id_data_server = '".$_SESSION['master']['id']."' AND";
    $SvId = $_SESSION['master']['id'];
    $SvNama = $_SESSION['master']['nama_pemilik'];
    $SqlDataWH = "WHERE id IN (".$SvId.")";
    $SqlDataWHId = "WHERE id_data_server IN (".$SvId.")";
    $SqlDataWHLikeId = "WHERE id_data_server LIKE '%".$SvId."%'";
    $SqlDataId = "AND id_data_server IN (".$SvId.")";
    $menu_akses = $menu_aray.','.$_SESSION['master']['akses'];
    $profile = $_SESSION['master']['profile'];
  }
}

//Data Pages AND POST
if ($_SERVER['QUERY_STRING'] != '') 
{
$pag = base64_decode($_SERVER['QUERY_STRING']);
$page = explode("|",$pag);

  $pages = $page[0];
}
else
{
  $pages = 'frame';
}


//DATA Page
$Page = new Pages();
$Page->set_page($pages);
//Resize Image
function resizeImage($resourceType,$image_width,$image_height) {
  $resizeWidth = 780;
  $resizeHeight = 780;
  $imageLayer = imagecreatetruecolor($resizeWidth,$resizeHeight);
  imagecopyresampled($imageLayer,$resourceType,0,0,0,0,$resizeWidth,$resizeHeight, $image_width,$image_height);
  return $imageLayer; }
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>BilsPro</title>
  <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="plugins/fontawesome-free/css/all.min.css">
  <!-- Ionicons -->
  <link rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
  <!-- Tempusdominus Bootstrap 4 -->
  <link rel="stylesheet" href="plugins/tempusdominus-bootstrap-4/css/tempusdominus-bootstrap-4.min.css">
  <!-- iCheck -->
  <link rel="stylesheet" href="plugins/icheck-bootstrap/icheck-bootstrap.min.css">
  <!-- JQVMap -->
  <!-- <link rel="stylesheet" href="plugins/jqvmap/jqvmap.min.css"> -->
  <!-- Theme style -->
  <link rel="stylesheet" href="dist/css/adminlte.min.css">
  <!-- overlayScrollbars -->
  <link rel="stylesheet" href="plugins/overlayScrollbars/css/OverlayScrollbars.min.css">
  <!-- Daterange picker -->
  <link rel="stylesheet" href="plugins/daterangepicker/daterangepicker.css">
  <!-- summernote -->
  <link rel="stylesheet" href="plugins/summernote/summernote-bs4.min.css">
  <!-- Icon Aplikasi -->
  <link rel="icon" href="dist/img/logo-sm.png" type="image/gif" />
  <!-- Data Tabel -->
  <link rel="stylesheet" href="plugins/datatables-bs4/css/dataTables.bootstrap4.min.css">
  <link rel="stylesheet" href="plugins/datatables-responsive/css/responsive.bootstrap4.min.css">
  <link rel="stylesheet" href="plugins/datatables-buttons/css/buttons.bootstrap4.min.css">
  <link rel="stylesheet" href="plugins/select2/css/select2.min.css">
  <link rel="stylesheet" href="plugins/select2-bootstrap4-theme/select2-bootstrap4.min.css">
  <!-- Bootstrap Color Picker -->
  <link rel="stylesheet" href="plugins/bootstrap-colorpicker/css/bootstrap-colorpicker.min.css">
  <!-- Bootstrap4 Duallistbox -->
  <link rel="stylesheet" href="plugins/bootstrap4-duallistbox/bootstrap-duallistbox.min.css">
  <!-- BS Stepper -->
  <link rel="stylesheet" href="plugins/bs-stepper/css/bs-stepper.min.css">
  <!-- dropzonejs -->
  <link rel="stylesheet" href="plugins/dropzone/min/dropzone.min.css">
  <!-- Custom CSS -->
  <link rel="stylesheet" href="dist/customcss/form_image_load.css">
  <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
  <!-- Load Google Maps API library -->
  <script>
  (g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=`https://maps.${c}apis.com/maps/api/js?`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
    key: "AIzaSyA1MgLuZuyqR_OGY3ob3M52N46TDBRI_9k",
    v: "weekly",
    // Use the 'v' parameter to indicate the version to use (weekly, beta, alpha, etc.).
    // Add other bootstrap parameters as needed, using camel case.
  });
</script>
</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">

  <!-- Preloader -->
  <div class="preloader flex-column justify-content-center align-items-center" style="background: rgba(128,128,128,0.2);">
    <img class="animation__shake" src="dist/img/logo-sm.png" alt="AdminLTELogo" height="180" width="180">
  </div>

  <!-- Navbar -->
  <nav class="main-header navbar navbar-expand navbar-white navbar-light">
    <!-- Left navbar links -->
    <ul class="navbar-nav">
      <li class="nav-item">
        <a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
      </li>
    </ul>
    <!-- Right navbar links -->
    <ul class="navbar-nav ml-auto">
      <!-- Navbar Search -->
      <!-- <li class="nav-item">
        <a class="nav-link" data-widget="navbar-search" href="#" role="button">
          <i class="fas fa-search"></i>
        </a>
        <div class="navbar-search-block">
          <form class="form-inline">
            <div class="input-group input-group-sm">
              <input class="form-control form-control-navbar" type="search" placeholder="Search" aria-label="Search">
              <div class="input-group-append">
                <button class="btn btn-navbar" type="submit">
                  <i class="fas fa-search"></i>
                </button>
                <button class="btn btn-navbar" type="button" data-widget="navbar-search">
                  <i class="fas fa-times"></i>
                </button>
              </div>
            </div>
          </form>
        </div>
      </li> -->
      <!-- Messages Dropdown Menu -->
      <!-- <li class="nav-item dropdown">
        <a class="nav-link" data-toggle="dropdown" href="#">
          <i class="far fa-comments"></i>
          <span class="badge badge-danger navbar-badge">3</span>
        </a>
        <div class="dropdown-menu dropdown-menu-lg dropdown-menu-right">
          <a href="#" class="dropdown-item">
            <div class="media">
              <img src="dist/img/user1-128x128.jpg" alt="User Avatar" class="img-size-50 mr-3 img-circle">
              <div class="media-body">
                <h3 class="dropdown-item-title">
                  Brad Diesel
                  <span class="float-right text-sm text-danger"><i class="fas fa-star"></i></span>
                </h3>
                <p class="text-sm">Call me whenever you can...</p>
                <p class="text-sm text-muted"><i class="far fa-clock mr-1"></i> 4 Hours Ago</p>
              </div>
            </div>
          </a>
          <div class="dropdown-divider"></div>
          <a href="#" class="dropdown-item">
            <div class="media">
              <img src="dist/img/user8-128x128.jpg" alt="User Avatar" class="img-size-50 img-circle mr-3">
              <div class="media-body">
                <h3 class="dropdown-item-title">
                  John Pierce
                  <span class="float-right text-sm text-muted"><i class="fas fa-star"></i></span>
                </h3>
                <p class="text-sm">I got your message bro</p>
                <p class="text-sm text-muted"><i class="far fa-clock mr-1"></i> 4 Hours Ago</p>
              </div>
            </div>
          </a>
          <div class="dropdown-divider"></div>
          <a href="#" class="dropdown-item">
            <div class="media">
              <img src="dist/img/user3-128x128.jpg" alt="User Avatar" class="img-size-50 img-circle mr-3">
              <div class="media-body">
                <h3 class="dropdown-item-title">
                  Nora Silvester
                  <span class="float-right text-sm text-warning"><i class="fas fa-star"></i></span>
                </h3>
                <p class="text-sm">The subject goes here</p>
                <p class="text-sm text-muted"><i class="far fa-clock mr-1"></i> 4 Hours Ago</p>
              </div>
            </div>
          </a>
          <div class="dropdown-divider"></div>
          <a href="#" class="dropdown-item dropdown-footer">See All Messages</a>
        </div>
      </li> -->
      <!-- Notifications Dropdown Menu -->
      <!-- <li class="nav-item dropdown">
        <a class="nav-link" data-toggle="dropdown" href="#">
          <i class="far fa-bell"></i>
          <span class="badge badge-warning navbar-badge">15</span>
        </a>
        <div class="dropdown-menu dropdown-menu-lg dropdown-menu-right">
          <span class="dropdown-item dropdown-header">15 Notifications</span>
          <div class="dropdown-divider"></div>
          <a href="#" class="dropdown-item">
            <i class="fas fa-envelope mr-2"></i> 4 new messages
            <span class="float-right text-muted text-sm">3 mins</span>
          </a>
          <div class="dropdown-divider"></div>
          <a href="#" class="dropdown-item">
            <i class="fas fa-users mr-2"></i> 8 friend requests
            <span class="float-right text-muted text-sm">12 hours</span>
          </a>
          <div class="dropdown-divider"></div>
          <a href="#" class="dropdown-item">
            <i class="fas fa-file mr-2"></i> 3 new reports
            <span class="float-right text-muted text-sm">2 days</span>
          </a>
          <div class="dropdown-divider"></div>
          <a href="#" class="dropdown-item dropdown-footer">See All Notifications</a>
        </div>
      </li> -->
      <li class="nav-item">
        <a class="nav-link" data-widget="fullscreen" href="#" role="button">
          <i class="fas fa-expand-arrows-alt"></i>
        </a>
      </li>
      <!-- <li class="nav-item">
        <a class="nav-link" data-widget="control-sidebar" data-slide="true" href="#" role="button">
          <i class="fas fa-th-large"></i>
        </a>
      </li> -->
    </ul>
  </nav>
  <!-- /.navbar -->

  <!-- Main Sidebar Container -->
  <aside class="main-sidebar sidebar-dark-primary elevation-4">
    <!-- Brand Logo -->
    <a href="index.php" class="brand-link">
      <img src="dist/img/logo-sm.png" alt="AdminLTE Logo" class="brand-image img-circle elevation-3" style="opacity: .8">
      <span class="brand-text font-weight-light">BilsPro Admin</span>
    </a>
    <!-- Sidebar -->
    <div class="sidebar">
      <!-- Sidebar user panel (optional) -->
      <div class="user-panel mt-3 pb-3 mb-3 d-flex">
        <div class="image">
          <img src="img/user/<?php echo $profile; ?>" class="img-circle elevation-2" alt="User Image">
        </div>
        <div class="info">
          <a href="index.php?<?php echo base64_encode('setting_profile'); ?>" class="d-block"><?php echo $SvNama; ?> </a>
        </div>
      </div>
      <!-- Sidebar Menu -->
      <nav class="mt-2">
        <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
          <!-- Add icons to the links using the .nav-icon class with font-awesome or any other icon font library -->
          <?php

          $amenu = mysqli_query($con,"SELECT * FROM master_menu WHERE master = '0' AND id IN ($menu_akses) ORDER BY urutan ASC"); 
          while($menu = mysqli_fetch_assoc($amenu))
          {
            if($menu['sub_menu'] == 'N' AND $menu['master'] =='0') {
          ?>
          <li class="nav-item">
            <a href="index.php?<?php echo base64_encode($menu['link_menu']);?>" class="nav-link <?php if($Page->get_page() == $menu['link_menu']) { echo "active"; } ?>">
              <i class="nav-icon <?php echo $menu['icon']; ?>"></i>
              <p>
                <?php echo $menu['nama']; ?>
              </p>
            </a>
          </li>
        <?php }
        elseif($menu['sub_menu'] == 'Y' AND $menu['master'] =='0'){ 
          $link_menu = $Page->get_page();
          $idmenu = mysqli_query($con,"SELECT master FROM master_menu WHERE link_menu = '$link_menu'");
          $menuid = mysqli_fetch_assoc($idmenu);
          ?>
          <li class="nav-item <?php if($menuid['master'] == $menu['id']) { echo "menu-open"; } ?>">
            <a href="#" class="nav-link <?php if($menuid['master'] == $menu['id']) { echo "active"; } ?>">
              <i class="nav-icon <?php echo $menu['icon']; ?>"></i>
              <p>
                <?php echo $menu['nama'];
                if($menu['sub_menu'] == 'Y' AND $menu['master'] =='0'){ ?>
                <i class="right fas fa-angle-left"></i> <?php } ?>
              </p>
            </a>
            <ul class="nav nav-treeview">
              <?php 
              $smenu = mysqli_query($con,"SELECT * FROM master_menu WHERE master = '$menu[id]' ORDER BY urutan ASC");
              while($submenu = mysqli_fetch_assoc($smenu)) { ?>
              <li class="nav-item">
                <a href="index.php?<?php echo base64_encode($submenu['link_menu']);?>" class="nav-link <?php if($Page->get_page() == $submenu['link_menu']) { echo "active"; } ?>">
                  <i class="<?php echo $submenu['icon']; ?> nav-icon"></i>
                  <p><?php echo $submenu['nama']; ?></p>
                </a>
              </li>
              <?php } ?>
            </ul>
          </li>
          <?php } } ?>
          <br><br><br><br>
        </ul>
      </nav>
      <!-- /.sidebar-menu -->
    </div>
    <!-- /.sidebar -->
  </aside>
  
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper iframe-mode" data-widget="iframe" data-loading-screen="750">
    <div class="nav navbar navbar-expand navbar-white navbar-light border-bottom p-0">
      <div class="nav-item dropdown">
        <a class="nav-link bg-danger dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Close</a>
        <div class="dropdown-menu mt-0">
          <a class="dropdown-item" href="#" data-widget="iframe-close" data-type="all">Close All</a>
          <a class="dropdown-item" href="#" data-widget="iframe-close" data-type="all-other">Close All Other</a>
        </div>
      </div>
      <a class="nav-link bg-light" href="#" data-widget="iframe-scrollleft"><i class="fas fa-angle-double-left"></i></a>
      <ul class="navbar-nav overflow-hidden" role="tablist"></ul>
      <a class="nav-link bg-light" href="#" data-widget="iframe-scrollright"><i class="fas fa-angle-double-right"></i></a>
      <a class="nav-link bg-light" href="#" data-widget="iframe-fullscreen"><i class="fas fa-expand"></i></a>
    </div>
    <div class="tab-content">
      <?php 
        include 'pages/'.$Page->get_page().'.php';
      ?>
    </div>
  </div>

  <!-- Control Sidebar -->
  <aside class="control-sidebar control-sidebar-dark">
    <!-- Control sidebar content goes here -->
  </aside>
  <!-- /.control-sidebar -->
</div>
<!-- ./wrapper -->

<!-- jQuery -->
<script src="plugins/jquery/jquery.min.js"></script>
<!-- jQuery UI 1.11.4 -->
<script src="plugins/jquery-ui/jquery-ui.min.js"></script>
<!-- Resolve conflict in jQuery UI tooltip with Bootstrap tooltip -->
<script>
  $.widget.bridge('uibutton', $.ui.button)
</script>
<!-- Bootstrap 4 -->
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- overlayScrollbars -->
<script src="plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/adminlte.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="dist/js/demo.js"></script>
</body>
</html>
