<?php
include 'auth.php';
include '../koneksi.php';

// Total Semua
$queryUsersTotal = "SELECT COUNT(*) AS total_users FROM users WHERE status = 'active' AND role = 'user'";
$resultUsersTotal = mysqli_query($conn, $queryUsersTotal);
$rowUsersTotal = mysqli_fetch_assoc($resultUsersTotal);
$totalUsers = $rowUsersTotal['total_users'];

// 7 Hari Terakhir
$queryUsersThisWeek = "SELECT COUNT(*) AS total_this_week FROM users WHERE status = 'active' AND role = 'user' AND created_at >= CURDATE() - INTERVAL 7 DAY";
$resultUsersThisWeek = mysqli_query($conn, $queryUsersThisWeek);
$rowUsersThisWeek = mysqli_fetch_assoc($resultUsersThisWeek);
$totalUsersThisWeek = $rowUsersThisWeek['total_this_week'];

// 7 Hari Sebelumnya
$queryUsersLastWeek = "SELECT COUNT(*) AS total_last_week FROM users WHERE status = 'active' AND role = 'user' AND created_at >= CURDATE() - INTERVAL 14 DAY AND created_at < CURDATE() - INTERVAL 7 DAY";
$resultUsersLastWeek = mysqli_query($conn, $queryUsersLastWeek);
$rowUsersLastWeek = mysqli_fetch_assoc($resultUsersLastWeek);
$totalUsersLastWeek = $rowUsersLastWeek['total_last_week'];

// Hitung persentase perubahan
$changeUsers = $totalUsersLastWeek > 0 ? (($totalUsersThisWeek - $totalUsersLastWeek) / $totalUsersLastWeek) * 100 : 0;
$iconUsers = $changeUsers >= 0 ? "bi-arrow-up text-success" : "bi-arrow-down text-danger";
$changeUsers = number_format(abs($changeUsers), 1);

// ========================== LANDING PAGE ==========================
$queryLandingPageTotal = "SELECT COUNT(*) AS total_landingpage FROM landingpage";
$resultLandingPageTotal = mysqli_query($conn, $queryLandingPageTotal);
$rowLandingPageTotal = mysqli_fetch_assoc($resultLandingPageTotal);
$totalLandingPage = $rowLandingPageTotal['total_landingpage'];

$queryLandingPageThisWeek = "SELECT COUNT(*) AS total_this_week FROM landingpage WHERE created_at >= CURDATE() - INTERVAL 7 DAY";
$resultLandingPageThisWeek = mysqli_query($conn, $queryLandingPageThisWeek);
$rowLandingPageThisWeek = mysqli_fetch_assoc($resultLandingPageThisWeek);
$totalLandingPageThisWeek = $rowLandingPageThisWeek['total_this_week'];

$queryLandingPageLastWeek = "SELECT COUNT(*) AS total_last_week FROM landingpage WHERE created_at >= CURDATE() - INTERVAL 14 DAY AND created_at < CURDATE() - INTERVAL 7 DAY";
$resultLandingPageLastWeek = mysqli_query($conn, $queryLandingPageLastWeek);
$rowLandingPageLastWeek = mysqli_fetch_assoc($resultLandingPageLastWeek);
$totalLandingPageLastWeek = $rowLandingPageLastWeek['total_last_week'];

$changeLandingPage = $totalLandingPageLastWeek > 0 ? (($totalLandingPageThisWeek - $totalLandingPageLastWeek) / $totalLandingPageLastWeek) * 100 : 0;
$iconLandingPage = $changeLandingPage >= 0 ? "bi-arrow-up text-success" : "bi-arrow-down text-danger";
$changeLandingPage = number_format(abs($changeLandingPage), 1);

// ========================== ARTIKEL ==========================
$queryArtikelTotal = "SELECT COUNT(*) AS total_artikel FROM detail_artikel";
$resultArtikelTotal = mysqli_query($conn, $queryArtikelTotal);
$rowArtikelTotal = mysqli_fetch_assoc($resultArtikelTotal);
$totalArtikel = $rowArtikelTotal['total_artikel'];

$queryArtikelThisWeek = "SELECT COUNT(*) AS total_this_week FROM detail_artikel WHERE created_at >= CURDATE() - INTERVAL 7 DAY";
$resultArtikelThisWeek = mysqli_query($conn, $queryArtikelThisWeek);
$rowArtikelThisWeek = mysqli_fetch_assoc($resultArtikelThisWeek);
$totalArtikelThisWeek = $rowArtikelThisWeek['total_this_week'];

$queryArtikelLastWeek = "SELECT COUNT(*) AS total_last_week FROM detail_artikel WHERE created_at >= CURDATE() - INTERVAL 14 DAY AND created_at < CURDATE() - INTERVAL 7 DAY";
$resultArtikelLastWeek = mysqli_query($conn, $queryArtikelLastWeek);
$rowArtikelLastWeek = mysqli_fetch_assoc($resultArtikelLastWeek);
$totalArtikelLastWeek = $rowArtikelLastWeek['total_last_week'];

$changeArtikel = $totalArtikelLastWeek > 0 ? (($totalArtikelThisWeek - $totalArtikelLastWeek) / $totalArtikelLastWeek) * 100 : 0;
$iconArtikel = $changeArtikel >= 0 ? "bi-arrow-up text-success" : "bi-arrow-down text-danger";
$changeArtikel = number_format(abs($changeArtikel), 1);
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0" name="viewport">

  <title>Halaman Dashboard | Admin</title>
  <meta content="" name="description">
  <meta content="" name="keywords">
  <meta name="robots" content="noindex">


  <!-- Favicons -->
  <link href="../assets/img/favicon.png" rel="icon">
  <link href="../assets/img/apple-touch-icon.png" rel="apple-touch-icon">

  <!-- Google Fonts -->
  <link href="https://fonts.gstatic.com" rel="preconnect">
  <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Nunito:300,300i,400,400i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">

  <!-- Vendor CSS Files -->
  <link href="../assets/template/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
  <link href="../assets/template/vendor/bootstrap-icons/bootstrap-icons.css" rel="stylesheet">
  <link href="../assets/template/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
  <link href="../assets/template/vendor/quill/quill.snow.css" rel="stylesheet">
  <link href="../assets/template/vendor/quill/quill.bubble.css" rel="stylesheet">
  <link href="../assets/template/vendor/remixicon/remixicon.css" rel="stylesheet">
  <link href="../assets/template/vendor/simple-datatables/style.css" rel="stylesheet">

  <!-- Template Main CSS File -->
  <link href="../assets/template/css/style.css" rel="stylesheet">
  <link href="../assets/css/custom.css" rel="stylesheet">

  <!-- =======================================================
  * Template Name: NiceAdmin
  * Template URL: https://bootstrapmade.com/nice-admin-bootstrap-admin-html-template/
  * Updated: Apr 20 2024 with Bootstrap v5.3.3
  * Author: BootstrapMade.com
  * License: https://bootstrapmade.com/license/
  ======================================================== -->
</head>

<body>

  <!-- ======= Header ======= -->
  <header id="header" class="header fixed-top d-flex align-items-center">

    <div class="d-flex align-items-center justify-content-between">
      <a href="" class="logo d-flex align-items-center">
        <img src="../assets/img/logo.png" alt="">
        <span class="d-none d-lg-block">Portal UMKM</span>
      </a>
      <i class="bi bi-list toggle-sidebar-btn"></i>
    </div><!-- End Logo -->

    <nav class="header-nav ms-auto">
      <ul class="d-flex align-items-center">

        <li class="nav-item dropdown pe-3">

          <a class="nav-link nav-profile d-flex align-items-center pe-0" href="#" data-bs-toggle="dropdown">
          <span class="fw-semibold dropdown-toggle ps-2">Hai, <?php echo $_SESSION['nama_lengkap']; ?>!</span>
          </a><!-- End Profile Iamge Icon -->

          <ul class="dropdown-menu dropdown-menu-end dropdown-menu-arrow profile">
            <li class="dropdown-header">
            <h6><?php echo $_SESSION['nama_lengkap']; ?></h6>
            <span><?php echo ucfirst($_SESSION['role']); ?></span>
            </li>
            <li>
              <hr class="dropdown-divider">
            </li>

            <li>
              <a class="dropdown-item d-flex align-items-center" href="update-profile">
                <i class="bi bi-gear"></i>
                <span>Pengaturan</span>
              </a>
            </li>
            <li>
              <hr class="dropdown-divider">
            </li>

            <li>
              <a class="dropdown-item d-flex align-items-center" href="#" data-bs-toggle="modal" data-bs-target="#confirmLogoutModal">
                <i class="bi bi-box-arrow-right"></i>
                <span>Keluar</span>
              </a>
            </li>

          </ul><!-- End Profile Dropdown Items -->
        </li><!-- End Profile Nav -->

      </ul>
    </nav><!-- End Icons Navigation -->

  </header><!-- End Header -->

  <!-- ======= Sidebar ======= -->
  <aside id="sidebar" class="sidebar">

    <ul class="sidebar-nav sidebar-nav">

      <li class="nav-item mb-2">
        <a class="nav-link d-flex align-items-center collapsed" href="">
          <i class="bi bi-columns-gap"></i>
          <span>Dashboard</span>
        </a>
      </li>

      <li class="nav-item mb-2">
        <a class="nav-link d-flex align-items-center" href="landingpage">
        <i class="bi bi-journal-richtext"></i>
          <span>Landing Page</span>
        </a>
      </li>

      <li class="nav-item mb-2">
        <a class="nav-link d-flex align-items-center" href="artikel">
        <i class="bi bi-newspaper"></i>
          <span>Artikel</span>
        </a>
      </li>

      <li class="nav-item mb-2">
        <a class="nav-link d-flex align-items-center" href="pengguna">
        <i class="bi bi-people"></i>
          <span>Pengguna</span>
        </a>
      </li>

    </ul>

  </aside><!-- End Sidebar-->

  <main id="main" class="main">

    <div class="pagetitle">
      <h1>Halaman Dashboard</h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="/">Beranda</a></li>
          <li class="breadcrumb-item active">Dashboard</li>
        </ol>
      </nav>
    </div><!-- End Page Title -->

    <?php if (isset($_SESSION['success'])): ?>
      <div class="alert alert-success alert-dismissible fade show" role="alert">
        <?php echo $_SESSION['success']; ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
      </div>
      <?php unset($_SESSION['success']); // Hapus pesan setelah ditampilkan 
      ?>
    <?php endif; ?>

    <?php if (isset($_SESSION['error'])): ?>
      <div class="alert alert-danger alert-dismissible fade show" role="alert">
        <?php echo $_SESSION['error']; ?>
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
      </div>
      <?php unset($_SESSION['error']); // Hapus pesan setelah ditampilkan 
      ?>
    <?php endif; ?>

    <section class="section dashboard">
      <div class="row g-0 g-md-3">

        <!-- Pengguna Aktif Card -->
        <div class="col-12 col-md-4">
          <div class="card border-0 shadow-sm p-4 d-flex flex-row align-items-center rounded-4">
            <div class="icon-container bg-primary text-white rounded-3 d-flex align-items-center justify-content-center">
              <i class="bi bi-people fs-3"></i>
            </div>
            <div class="ms-3">
              <h6 class="text-secondary mb-1">Pengguna Aktif</h6>
              <h4 class="fw-bold text-dark"><?php echo $totalUsers; ?></h4>
              <span class="text-success fw-semibold"><i class="bi bi-arrow-up"></i> <?php echo $changeUsers; ?>% (7 hari terakhir)</span>
            </div>
          </div>
        </div>

        <!-- Landing Page Dibuat Card -->
        <div class="col-12 col-md-4">
          <div class="card border-0 shadow-sm p-4 d-flex flex-row align-items-center rounded-4">
            <div class="icon-container bg-success text-white rounded-3 d-flex align-items-center justify-content-center">
              <i class="bi bi-journal-richtext fs-3"></i>
            </div>
            <div class="ms-3">
              <h6 class="text-secondary mb-1">Landing Page Dibuat</h6>
              <h4 class="fw-bold text-dark"><?php echo $totalLandingPage; ?></h4>
              <span class="text-success fw-semibold"><i class="bi bi-arrow-up"></i> <?php echo $changeLandingPage; ?>% (7 hari terakhir)</span>
            </div>
          </div>
        </div>

        <!-- Artikel Card -->
        <div class="col-12 col-md-4">
          <div class="card border-0 shadow-sm p-4 d-flex flex-row align-items-center rounded-4">
            <div class="icon-container bg-warning text-white rounded-3 d-flex align-items-center justify-content-center">
              <i class="bi bi-newspaper fs-3"></i>
            </div>
            <div class="ms-3">
              <h6 class="text-secondary mb-1">Artikel</h6>
              <h4 class="fw-bold text-dark"><?php echo $totalArtikel; ?></h4>
              <span class="text-danger fw-semibold"><i class="bi bi-arrow-down"></i> <?php echo $changeArtikel; ?>% (7 hari terakhir)</span>
            </div>
          </div>
        </div>

      </div>
    </section>

            <!-- Modal Konfirmasi Logout -->
            <div class="modal fade" id="confirmLogoutModal" tabindex="-1" aria-labelledby="logoutModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="logoutModalLabel">Konfirmasi Keluar</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>
          <div class="modal-body">
            Apakah Anda yakin ingin keluar dari halaman ini?
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
            <a href="logout.php" id="logoutConfirmBtn" class="btn btn-danger">Keluar</a>
          </div>
        </div>
      </div>
    </div>

  </main><!-- End #main -->

  <!-- ======= Footer ======= -->
  <footer id="footer" class="footer">
    <div class="copyright">
      &copy; Copyright <strong><span>Portal UMKM</span></strong>. All Rights Reserved
    </div>
    <div class="credits">
      <!-- All the links in the footer should remain intact. -->
      <!-- You can delete the links only if you purchased the pro version. -->
      <!-- Licensing information: https://bootstrapmade.com/license/ -->
      <!-- Purchase the pro version with working PHP/AJAX contact form: https://bootstrapmade.com/nice-admin-bootstrap-admin-html-template/ -->
      <!-- Designed by <a href="https://bootstrapmade.com/">BootstrapMade</a> -->
    </div>
  </footer><!-- End Footer -->

  <a href="#" class="back-to-top d-flex align-items-center justify-content-center"><i class="bi bi-arrow-up-short"></i></a>

  <!-- Vendor JS Files -->
  <script src="../assets/template/vendor/apexcharts/apexcharts.min.js"></script>
  <script src="../assets/template/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="../assets/template/vendor/chart.js/chart.umd.js"></script>
  <script src="../assets/template/vendor/echarts/echarts.min.js"></script>
  <script src="../assets/template/vendor/quill/quill.js"></script>
  <script src="../assets/template/vendor/simple-datatables/simple-datatables.js"></script>
  <script src="../assets/template/vendor/tinymce/tinymce.min.js"></script>
  <script src="../assets/template/vendor/php-email-form/validate.js"></script>

  <!-- Template Main JS File -->
  <script src="../assets/template/js/main.js"></script>

</body>

</html>