<?php
require_once(__DIR__ . '/../../session.php');
include(__DIR__ . '/../../inc/query.dashboard.php');
?>

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

<?php include(__DIR__ . '/../../layouts/header.php'); ?>

<body>
  <div class="wrapper">
    <?php include(__DIR__ . '/../../layouts/sidebar.php'); ?>

    <div class="main-panel">
      <?php include(__DIR__ . '/../../layouts/navbar.php'); ?>

      <div class="container">
        <div class="page-inner">

          <div
            class="d-flex align-items-left align-items-md-center flex-column flex-md-row pt-2 pb-4">
            <div>
              <h3 class="fw-bold mb-3">Dashboard</h3>
              <h6 class="op-7 mb-2">Selamat datang <?= $admin['fullname']; ?> di Dashboard admin!</h6>
            </div>
            <?php if (isset($_SESSION['admin_id']) && isset($admin['level'])): ?>
            <?php if ($admin['level'] === 'developer' || $admin['level'] === 'admin'): ?>
            <div class="ms-md-auto py-2 py-md-0">
              <a href="website" class="btn btn-label-info btn-round me-2">Pengaturan</a>
              <a href="user" class="btn btn-primary btn-round">Info Pengguna</a>
            </div>
            <?php endif; ?>
            <?php endif; ?>
          </div>

          <h5 class="fw-bold mb-3">Informasi</h5>
          <div class="row">
            <div class="col-sm-6 col-md-4">
              <div class="card card-stats card-warning card-round">
                <div class="card-body">
                  <div class="row">
                    <div class="col-5">
                      <div class="icon-big text-center">
                        <i class="fas fa-users"></i>
                      </div>
                    </div>
                    <div class="col-7 col-stats">
                      <div class="numbers">
                        <p class="card-category">Total Pengguna</p>
                        <h4 class="card-title"><?= $total_users; ?></h4>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="col-sm-6 col-md-4">
              <div class="card card-stats card-primary card-round">
                <div class="card-body">
                  <div class="row">
                    <div class="col-5">
                      <div class="icon-big text-center">
                        <i class="fas fa-coins"></i>
                      </div>
                    </div>
                    <div class="col-7 col-stats">
                      <div class="numbers">
                        <p class="card-category">Total Setoran</p>
                        <h4 class="card-title">Rp <?= number_format($total_deposit, 0, ',', '.'); ?></h4>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="col-sm-6 col-md-4">
              <div class="card card-stats card-danger card-round">
                <div class="card-body">
                  <div class="row">
                    <div class="col-5">
                      <div class="icon-big text-center">
                        <i class="fas fa-hand-holding-usd"></i>
                      </div>
                    </div>
                    <div class="col-7 col-stats">
                      <div class="numbers">
                        <p class="card-category">Total Penarikan</p>
                        <h4 class="card-title">Rp <?= number_format($total_withdrawal, 0, ',', '.'); ?></h4>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <div class="row">
            <div class="col-sm-6 col-md-4">
              <div class="card card-stats card-round">
                <div class="card-body">
                  <div class="row">
                    <div class="col-5">
                      <div class="icon-big text-center">
                        <i class="fas fa-calendar"></i>
                      </div>
                    </div>
                    <div class="col-7 col-stats">
                      <div class="numbers">
                        <p class="card-category">Deposit Hari Ini</p>
                        <h4 class="card-title">Rp <?= number_format($deposit_harian, 0, ',', '.'); ?></h4>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="col-sm-6 col-md-4">
              <div class="card card-stats card-round">
                <div class="card-body">
                  <div class="row">
                    <div class="col-5">
                      <div class="icon-big text-center">
                        <i class="fas fa-calendar-check"></i>
                      </div>
                    </div>
                    <div class="col-7 col-stats">
                      <div class="numbers">
                        <p class="card-category">Deposit Minggu Ini</p>
                        <h4 class="card-title">Rp <?= number_format($deposit_mingguan, 0, ',', '.'); ?></h4>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="col-sm-6 col-md-4">
              <div class="card card-stats card-round">
                <div class="card-body">
                  <div class="row">
                    <div class="col-5">
                      <div class="icon-big text-center">
                        <i class="fas fa-calendar-alt"></i>
                      </div>
                    </div>
                    <div class="col-7 col-stats">
                      <div class="numbers">
                        <p class="card-category">Deposit Bulan Ini</p>
                        <h4 class="card-title">Rp <?= number_format($deposit_bulanan, 0, ',', '.'); ?></h4>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- Chart -->
          <div class="row">
            <div class="col-md-8">
              <div class="card card-round">
                <div class="card-header">
                  <div class="card-head-row">
                    <div class="card-title">Statistik Bulanan</div>
                    <div class="card-tools">
                    </div>
                  </div>
                </div>
                <div class="card-body">
                  <div class="chart-container" style="min-height: 375px">
                    <canvas id="statisticsChart"></canvas>
                  </div>
                  <div id="myChartLegend"></div>
                </div>
              </div>
            </div>
            <div class="col-md-4">
              <div class="card card-primary card-round">
                <div class="card-header">
                  <div class="card-head-row">
                    <div class="card-title">Setoran Hari Ini</div>
                    <div class="card-tools">
                      <div class="dropdown">
                        <div
                          class="dropdown-menu"
                          aria-labelledby="dropdownMenuButton">
                          <a class="dropdown-item" href="#">Action</a>
                          <a class="dropdown-item" href="#">Another action</a>
                          <a class="dropdown-item" href="#">Something else here</a>
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="card-category"><?= $days[date('l')] . ', ' . date('d/m/Y'); ?></div>
                </div>
                <div class="card-body pb-0">
                  <div class="mb-4 mt-2">
                    <h1><?= number_format($deposit_hari_ini, 0, ',', '.'); ?></h1>
                  </div>
                  <div class="pull-in">
                    <canvas id="dailySalesChart"></canvas>
                  </div>
                </div>
              </div>
              <div class="card card-round">
                <div class="card-body pb-0">
                  <div class="h1 fw-bold float-end text-primary">0%</div>
                  <h2 class="mb-2"><?= $pengguna_hari_ini; ?></h2>
                  <p class="text-muted">Pendaftaran Hari Ini</p>
                  <div class="pull-in sparkline-fix">
                    <div id="newuser"></div>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <div class="row">
            <div class="col-md-12">
              <div class="card card-round">
                <div class="card-header">
                  <div class="card-head-row card-tools-still-right">
                    <div class="card-title">Daftar Transaksi Tertunda</div>
                    <div class="card-tools">
                    </div>
                  </div>
                </div>
                <div class="card-body p-0">
                  <div class="table-responsive">
                    <!-- Projects table -->
                    <table class="table align-items-center mb-0">
                      <thead class="thead-light">
                        <tr>
                          <th scope="col">Invoice</th>
                          <th>Nama Lengkap</th>
                          <th>Nama Pengguna</th>
                          <th>Type</th>
                          <th>Jumlah</th>
                          <th>Status</th>
                        </tr>
                      </thead>
                      <tbody>
            </div>
                        <?php if (isset($_SESSION['admin_id']) && isset($admin['level'])): ?>
                        <?php if ($admin['level'] === 'developer' || $admin['level'] === 'admin'): ?>
                        <?php
                        foreach ($transaksi_tertunda as $daftar_transaksi):
                        ?>
                          <tr>
                            <th scope="row">
                              #<?= $daftar_transaksi['trxid']; ?>
                            </th>
                            <td><?= $daftar_transaksi['fullname']; ?></td>
                            <td><?= $daftar_transaksi['username']; ?></td>
                            <td>
                              <?php if ($daftar_transaksi['type'] == 'deposit'): ?>
                                <span class="badge badge-primary">Deposit</span>
                              <?php else: ?>
                                <span class="badge badge-danger">Withdrawal</span>
                              <?php endif; ?>
                            </td>
                            <td><?= number_format($daftar_transaksi['Amount'], 0, ',', '.'); ?></td>
                            <td>
                              <?php if ($daftar_transaksi['status'] == 0): ?>
                                <span class="badge badge-warning">Tertunda</span>
                              <?php endif; ?>
                            </td>
                          </tr>
                        <?php endforeach; ?>
                        <?php endif; ?>
                        <?php endif; ?>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <div class="row">
            <div class="col-md-4">
              <div class="card card-round">
                <div class="card-body">
                  <div class="card-head-row card-tools-still-right">
                    <div class="card-title">Daftar Pengguna Baru</div>
                    <div class="card-tools">
                    </div>
                  </div>
                  <div class="card-list py-4">
                    <?php
                    foreach ($pengguna_terbaru as $daftar_pengguna):
                      $names = explode(' ', $daftar_pengguna['fullname']);
                      $initials = strtoupper(substr($names[0], 0, 1) . substr($names[1] ?? '', 0, 1));
                    ?>
                      <div class="item-list">
                        <div class="avatar">
                          <span class="avatar-title rounded-circle border border-white"><?= $initials; ?></span>
                        </div>
                        <div class="info-user ms-3">
                          <div class="username"><?= $daftar_pengguna['fullname']; ?></div>
                          <div class="status"><?= htmlspecialchars((new DateTime($daftar_pengguna['created_at']))->setTimezone(new DateTimeZone('Asia/Jakarta'))->format('d-M-Y H:i T')); ?></div>
                        </div>
                        <button class="btn btn-icon btn-link op-8 me-1"><i class="far fa-envelope"></i></button>
                        <button class="btn btn-icon btn-link btn-danger op-8"><i class="fas fa-ban"></i></button>
                      </div>
                    <?php endforeach; ?>
                  </div>
                </div>
              </div>
            </div>
            <div class="col-md-8">
              <div class="card card-round">
                <div class="card-header">
                  <div class="card-head-row card-tools-still-right">
                    <div class="card-title">Daftar Transaksi Baru</div>
                    <div class="card-tools">
                    </div>
                  </div>
                </div>
                <div class="card-body p-0">
                  <div class="table-responsive">
                    <!-- Projects table -->
                    <table class="table align-items-center mb-0">
                      <thead class="thead-light">
                        <tr>
                          <th>Invoice</th>
                          <th>Nama Lengkap</th>
                          <th>Type</th>
                          <th>Jumlah</th>
                          <th>Status</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php if (isset($_SESSION['admin_id']) && isset($admin['level'])): ?>
                        <?php if ($admin['level'] === 'developer' || $admin['level'] === 'admin'): ?>
                        <?php
                        foreach ($transaksi_terbaru as $daftar_transaksi):
                        ?>
                          <tr>
                            <th scope="row">
                              #<?= $daftar_transaksi['trxid']; ?>
                            </th>
                            <td><?= $daftar_transaksi['fullname']; ?></td>
                            <td>
                              <?php if ($daftar_transaksi['type'] == 'deposit'): ?>
                                <span class="badge badge-primary">Deposit</span>
                              <?php else: ?>
                                <span class="badge badge-danger">Withdrawal</span>
                              <?php endif; ?>
                            </td>
                            <td><?= number_format($daftar_transaksi['Amount'], 0, ',', '.'); ?></td>
                            <td>
                              <?php if ($daftar_transaksi['status'] == 1): ?>
                                <span class="badge badge-success">Sukses</span>
                              <?php endif; ?>
                            </td>
                          </tr>
                        <?php endforeach; ?>
                        <?php endif; ?>
                        <?php endif; ?>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <?php include(__DIR__ . '/../../layouts/footer.php'); ?>

    </div>
  </div>

  <?php include(__DIR__ . '/../../layouts/script.php'); ?>

  <script>
    const notifications = <?= $notifyjson; ?>;
    const jsonData = <?= $chartjson; ?>;
  </script>

</body>

</html>