<?php

session_start();

if (empty($_SESSION['user']) && empty($_SESSION['pass'])) {

    echo "<script>window.location.replace('../index.php')</script>";

}



//koneksi ke database MariaDb

include'../assets/func.php';

$air = new klas_air;

$koneksi = $air->koneksi();

$dt_user = $air->dt_user($_SESSION['user']);

$level = $dt_user[2];
$_SESSION['level'] = $level;

// biar bisa ngatur akses

$level_login = $dt_user[2];



?>

<!DOCTYPE html>

<html lang="en">

    <head>

        <meta charset="utf-8" />

        <meta http-equiv="X-UA-Compatible" content="IE=edge" />

        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />

        <meta name="description" content="" />

        <meta name="author" content="" />

        <title>Dashboard - SB Admin</title>

        <link href="https://cdn.jsdelivr.net/npm/simple-datatables@7.1.2/dist/style.min.css" rel="stylesheet" />

        <link href="../css/styles.css" rel="stylesheet" />

        <link href="../css/styles-animasi.css" rel="stylesheet" />

        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" crossorigin="anonymous">

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>

        <script>
            const level_login = <?php echo json_encode($level); ?>;
            const user_login = <?php echo json_encode($_SESSION['user']); ?>;
        </script>

        <script src="../js/air.js"></script>

        

    </head>

    <body class="sb-nav-fixed">

        <nav class="sb-topnav navbar navbar-expand navbar-dark bg-dark">

            <!-- Navbar Brand-->

            <a class="navbar-brand ps-3 d-flex align-items-center" href="index.php"><i class="fas fa-users me-2 fa-lg"></i>AIR</a>

            <!-- Sidebar Toggle-->

            <button class="btn btn-link btn-sm order-1 order-lg-0 me-4 me-lg-0" id="sidebarToggle" href="#!"><i class="fas fa-bars"></i></button>

            <!-- Navbar Search-->

            <form class="d-none d-md-inline-block form-inline ms-auto me-0 me-md-3 my-2 my-md-0">

                <div class="input-group">

                    <input class="form-control" type="text" placeholder="Search for..." aria-label="Search for..." aria-describedby="btnNavbarSearch" />

                    <button class="btn btn-primary" id="btnNavbarSearch" type="button"><i class="fas fa-search"></i></button>

                </div>

            </form>

            <!-- Navbar-->

            <ul class="navbar-nav ms-auto ms-md-0 me-3 me-lg-4">

                <li class="nav-item dropdown">

                    <a class="nav-link dropdown-toggle" id="navbarDropdown" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false"><i class="fas fa-user fa-fw"></i></a>

                    <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">

                        <li><a class="dropdown-item" href="#!">Settings</a></li>

                        <li><a class="dropdown-item" href="#!">Activity Log</a></li>

                        <li><hr class="dropdown-divider" /></li>

                        <li><a class="dropdown-item" href="./logout.php">Logout</a></li>

                    </ul>

                </li>

            </ul>

        </nav>

        <div id="layoutSidenav">

            <div id="layoutSidenav_nav">

                <nav class="sb-sidenav accordion sb-sidenav-dark" id="sidenavAccordion">

                    <div class="sb-sidenav-menu">

                        <div class="nav">

                            <div class="sb-sidenav-menu-heading">Core</div>

                            <a class="nav-link" href="index.php">

                                <div class="sb-nav-link-icon"><i class="fa-solid fa-chart-column text-danger"></i></div>

                                Dashboard

                            </a>

                            <?php

                            if($level == 'admin') {

                            ?>

                            <a class="nav-link" href="index.php?p=user">

                                <div class="sb-nav-link-icon"><i class="fa-solid fa-people-group text-success"></i></div>

                                Manajemen User

                            </a>

                            <a class="nav-link" href="index.php?p=catat_meter">

                                <div class="sb-nav-link-icon"><i class="fa-solid fa-hand-holding-droplet text-warning"></i></div>

                                Lihat Pemakaian Air

                            </a>

                            <a class="nav-link" href="index.php?p=tarif">

                                <div class="sb-nav-link-icon"><i class="fa-solid fa-hand-holding-water text-primary"></i></div>

                                Manajemen Tarif

                            </a>

                            <?php

                            }

                            elseif($level == 'bendahara') {

                                //bendahara

                            ?>

                            <a class="nav-link" href="index.php?p=transaksi_pembayaran">

                                <div class="sb-nav-link-icon"><i class="fas fa-tachometer-alt fa-spin text-success"></i></div>

                                Transaksi Pembayaran

                            </a>

                            <a class="nav-link" href="index.php?p=tarif">

                                <div class="sb-nav-link-icon"><i class="fa-duotone fa-solid fa-house-flood-water text-primary fa-blink"></i></div>

                                Manajemen Tarif 

                            </a>

                            <a class="nav-link" href="index.php?p=lihat_komplain">

                                <div class="sb-nav-link-icon"><i class="fas fa-tachometer-alt fa-spin text-success"></i></div>

                                Lihat Komplain

                            </a>

                            <a class="nav-link" href="index.php?p=catat_meter">

                                <div class="sb-nav-link-icon"><i class="fa-solid fa-house-flood-water-circle-arrow-right fa-shake text-warning"></i></div>

                                Lihat Pemakaian Air

                            </a>

                            <?php

                            }

                            elseif($level == 'petugas') {

                            ?>

                            <a class="nav-link" href="index.php?p=catat_meter">

                                <div class="sb-nav-link-icon"><i class="fa-solid fa-house-flood-water-circle-arrow-right fa-shake text-warning"></i></div>

                                Catat Meter

                            </a>

                            <a class="nav-link" href="index.php?p=lihat_jumlah_pelanggan">

                                <div class="sb-nav-link-icon"><i class="fas fa-tachometer-alt fa-spin text-success"></i></div>

                                Lihat Jumlah Pelanggan

                            </a>

                            <a class="nav-link" href="index.php?p=lihat_jumlah_pemakaian_air_seluruh_warga">

                                <div class="sb-nav-link-icon"><i class="fas fa-tachometer-alt fa-spin text-success"></i></div>

                                Lihat Jumlah Pemakaian Air Seluruh Warga

                            </a>

                            <a class="nav-link" href="index.php?p=lihat_infografis_pemakaian_air_warga">

                                <div class="sb-nav-link-icon"><i class="fas fa-tachometer-alt fa-spin text-success"></i></div>

                                Lihat Infografis Pemakaian Air Warga

                            </a>

                            <?php

                            }

                            elseif($level == 'warga') {

                            ?>

                            <a class="nav-link" href="index.php?p=lihat_pemakaian">

                                <div class="sb-nav-link-icon"><i class="fa-solid fa-house-flood-water-circle-arrow-right fa-shake text-warning"></i></div>

                                Lihat Pemakaian

                            </a>

                            <a class="nav-link" href="index.php?p=lihat_tagihan_tiap_bulan">

                                <div class="sb-nav-link-icon"><i class="fas fa-tachometer-alt fa-spin text-success"></i></div>

                                Lihat Tagihan Tiap Bulan

                            </a>

                            <a class="nav-link" href="index.php?p=lihat_infografis_pemakaian_air_warga">

                                <div class="sb-nav-link-icon"><i class="fas fa-tachometer-alt fa-spin text-success"></i></div>

                                Lihat Infografis Pemakaian dan Tagihan Tiap Bulan

                            </a>

                            <?php

                            }

                            ?>

                        </div>

                    </div>

                    <div class="sb-sidenav-footer">

                        <div class="small"><i class="fa-regular fa-user fa-flip text-warning"></i> Logged in as: <?php echo $dt_user[2] ?></div>

                        <?php echo $dt_user[0] . ' (' . $dt_user[1] . ')'; ?>

                    </div>

                </nav>

            </div>

            <div id="layoutSidenav_content">

                <main>

                    <div class="container-fluid px-4">

                        <?php

                        // echo $_SERVER['REQUEST_URI'];

                        $e = explode("=", $_SERVER['REQUEST_URI']);

                        //  echo "<BR>[0]: $e[0] --> [1]: $e[1]";

                        if(!empty($e[1])) {

                            if($e[1] == "user" || $e[1] == "user_edit&user") {

                                $h1 = "Manajemen User";

                                $li = "Menu untuk CRUD User";

                            }

                            elseif($e[1] == "manajemen_pemakaian_warga") {

                                $h1 = "Manajemen Pemakaian Air Warga";

                                $li = "Manajemen Data Pemakaian Air Warga";

                            }

                            elseif($e[1] == "pemakaian") {

                                $h1 = "Lihat Data Pemakaian Air Warga";

                                $li = "Manajemen Data Pemakaian Air Warga";

                            }

                            elseif($e[1] == "tagihan_warga") {

                                $h1 = "Lihat Tagihan Warga";

                                $li = "Lihat Data Tagihan Air Warga";

                            }

                            elseif($e[1] == "infografis_tagihan_warga") {

                                $h1 = "Infografis Tagihan Warga";

                                $li = "Infografis Data Tagihan Air Warga";

                            }

                            elseif($e[1] == "transaksi_pembayaran") {

                                $h1 = "Manajemen Transaksi Pembayaran";

                                $li = "Manajemen Transaksi Pembayaran";

                            }

                            elseif($e[1] == "tarif" || $e[1] == "tarif_edit&id_tarif") {

                                $h1 = "Manajemen Tarif Air Warga";

                                $li = "Manajemen Tarif Air Warga";

                            }

                            elseif($e[1] == "lihat_komplain") {

                                $h1 = "Lihat Komplain";

                                $li = "Lihat Data Komplain";

                            }

                            elseif($e[1] == "lihat_pemakaian") {

                                $h1 = "Lihat Pemakaian Air";

                                $li = "Lihat Data Pemakaian Air";

                            }

                            elseif($e[1] == "catat_meter" || $e[1] == "meter_edit&no") {

                                $h1 = "Pencatatan Meter";

                                $li = "Pencatatan Meter Air Warga";

                            }

                            elseif($e[1] == "ubah_datameter_warga_1bulan") {

                                $h1 = "Ubah Datameter Warga (1 Bulan)";

                                $li = "Ubah Data Datameter Air Warga (1 Bulan)";

                            }

                            elseif($e[1] == "lihat_jumlah_pelanggan") {

                                $h1 = "Lihat Jumlah Pelanggan";

                                $li = "Lihat Data Jumlah Pelanggan";

                            }

                            elseif($e[1] == "lihat_jumlah_pemakaian_air_seluruh_warga") {

                                $h1 = "Lihat Jumlah Pemakaian Air Seluruh Warga";

                                $li = "Lihat Data Jumlah Pemakaian Air Seluruh Warga";

                            }

                            elseif($e[1] == "lihat_infografis_pemakaian_air_warga") {

                                $h1 = "Lihat Infografis Pemakaian Air Warga";

                                $li = "Lihat Data Infografis Pemakaian Air Warga";

                            }

                            elseif($e[1] == "lihat_tagihan_tiap_bulan") {

                                $h1 = "Lihat Infografis Tagihan Tiap Bulan";

                                $li = "Lihat Data Infografis Tagihan Tiap Bulan";

                            }

                        } else {

                            $h1 = "Dashboard";

                            $li = "Dashboard";

                        }

                        ?>

                        <h1 class="mt-4"><?php echo $h1 ?></h1>

                        <ol class="breadcrumb mb-4">

                            <li class="breadcrumb-item active"><?php echo $li ?></li>

                        </ol>

                        <?php

                            // echo "sesi user:" . $_SESSION['user'] . "sesi pass:" . $_SESSION['pass'];

                            

                            // session_destroy();

                            // echo "<BR> setelah session destroy: sesi: " . $_SESSION['user'] . "sesi pass:" . $_SESSION['pass'];

                            ?>
                        <?php if (empty($e[1]) || $e[1] == 'dashboard') : ?>
                        <div class="row mb-3" id="pilih_waktu">
                            <div class="col-xl-3 col-md-12">

                            </div>
                             <label for="sel1" class="form-label">Pilih Waktu:</label>
                              <select class="form-select w-auto" id="sel1" name="pilih_waktu">
                                <option value="">Bulan</option>
                                    <?php
                                    for($i=1;$i<=12;$i++) {
                                        if($i<10) $i='0'.$i;
                                    // option untuk bulan
                                        echo "<option value=".date("Y")."-".$i.">".$air->bln($i)." 2026</option>";
                                    }
                                    ?>
                             </select>
                        </div>
                        <?php endif; ?>
                        <div class="row" id="summary"> 

                            <div class="col-xl-3 col-md-6">

                                <div class="card bg-primary text-white mb-4">

                                    <div class="card-body d-flex justify-content-center">
                                        <h1></h1> 
                                        <div class="ms-3" id="summary-unit-1">orang</div>
                                        </div>

                                    <div class="card-footer d-flex align-items-center justify-content-center">

                                        <div class="small text-white" id="summary-label-1"><?php echo ($level == 'warga') ? 'Waktu Pencatatan' : 'Pelanggan'; ?></div>

                                    </div>

                                </div>

                            </div>

                            <div class="col-xl-3 col-md-6">

                                <div class="card bg-warning text-white mb-4">

                                  <div class="card-body d-flex justify-content-center">
                                        <h1></h1> 
                                        <div class="ms-3" id="summary-unit-2">m<sup>3</sup></div>
                                        </div>
                                    <div class="card-footer d-flex align-items-center justify-content-center">

                                        <!-- <a class="small text-white stretched-link" href="#">View Details</a> -->

                                       <div class="small text-white" id="summary-label-2">Pemakaian Air</i></div>
                                    </div>

                                </div>

                            </div>

                            <div class="col-xl-3 col-md-6">

                                <div class="card bg-success text-white mb-4">

                                    <div class="card-body d-flex justify-content-center">
                                        <h1></h1> 
                                        <div class="ms-3" id="summary-unit-3">warga</div>
                                        </div>

                                    <div class="card-footer d-flex align-items-center justify-content-center">

                                        <div class="small text-white" id="summary-label-3"><?php echo ($level == 'warga') ? 'Tagihan' : 'Sudah Dicatat'; ?></div>

                                    </div>

                                </div>

                            </div>

                            <div class="col-xl-3 col-md-6">

                                <div class="card bg-danger text-white mb-4">

                                    <div class="card-body d-flex justify-content-center">
                                        <h1></h1> 
                                        <div class="ms-3" id="summary-unit-4">warga</div>
                                        </div>
                                    <div class="card-footer d-flex align-items-center justify-content-center">

                                        <div class="small text-white" id="summary-label-4"><?php echo ($level == 'warga') ? 'Status Tagihan' : 'Belum Dicatat'; ?></div>

                                    </div>

                                </div>

                            </div>

                        </div>

                        <div class="row" id="chart">

                            <div class="col-xl-6">

                                <div class="card mb-4">

                                    <div class="card-header">

                                        <i class="fas fa-chart-bar me-1"></i>

                                        Grafik Pemakaian Air (m<sup>3</sup>)

                                    </div>

                                    <div class="card-body"><canvas id="myBarChart" width="100%" height="40"></canvas></div>

                                </div>

                            </div>

                             <div class="col-xl-6">

                                <div class="card mb-4">

                                    <div class="card-header">

                                        <i class="fas fa-chart-area me-1"></i>

                                        Grafik Tagihan Air (Rp)

                                    </div>

                                    <div class="card-body"><canvas id="myAreaChart" width="100%" height="40"></canvas></div>

                                </div>

                            </div>

                        </div>

                       <?php

$user = $pass = $nama = $alamat = $kota = $tlp = $level = $tipe = $status = '';
$username = $meter_awal = $meter_akhir = '';

                        $id_tarif = $tarif = '';

                        if(isset($_POST['tombol'])) {

                            $t = $_POST['tombol'];

                            //Table user

                            if($t == "user_add") {

                                $user = $_POST['yuser'];

                                $pass = password_hash($_POST['paswet'], PASSWORD_DEFAULT);

                                $nama = $_POST['nama'];

                                $alamat = $_POST['alamat'];

                                $kota = $_POST['kota'];

                                $tlp = $_POST['tlp'];

                                $level = $_POST['level'];

                                $tipe = $_POST['tipe'];

                                $status = $_POST['status'];



                                $qc = mysqli_query($koneksi, "SELECT username FROM login WHERE username='$user'");

                                $qj = mysqli_num_rows($qc);

                                if (empty($qj)) {

                                    mysqli_query($koneksi, "INSERT INTO login (username, password, nama, alamat, kota, tlp, level, tipe, status) VALUES ('$user','$pass','$nama','$alamat','$kota','$tlp','$level','$tipe','$status')");



                                    if (mysqli_affected_rows($koneksi) > 0) {

                                        echo "<div class='alert alert-success alert-dismissible fade show' id=alert-user>

                                                <button type=button class=btn-close data-bs-dismiss=alert></button>

                                                <strong>Data</strong> berhasil dimasukkan...

                                            </div>

                                            <script>setTimeout(function(){ window.location.href='index.php?p=user'; }, 1500);</script>";

                                    } else {

                                        echo "<div class='alert alert-danger alert-dismissible fade show' id=alert-user>

                                                <button type=button class=btn-close data-bs-dismiss=alert></button>

                                                <strong>Data</strong> GAGAL dimasukkan...

                                            </div>";

                                    }

                                } else {

                                    echo "<div class='alert alert-danger alert-dismissible fade show' id=alert-user>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Username</strong> sudah terdaftar...

                                        </div>";

                                }

                            }

                            elseif($t == "user_edit") {

                                $user = $_POST['yuser'];

                                $pass = $_POST['paswet'];

                                $nama = $_POST['nama'];

                                $alamat = $_POST['alamat'];

                                $kota = $_POST['kota'];

                                $tlp = $_POST['tlp'];

                                $level = $_POST['level'];

                                $tipe = $_POST['tipe'];

                                $status = $_POST['status'];



                                //cek password yang ada di tabel login

                                $qcp = mysqli_query($koneksi, "SELECT password FROM login WHERE username='$user'");

                                $dcp = mysqli_fetch_row($qcp);

                                $pass_db = $dcp[0];



                                if ($pass == $pass_db) {

                                    //tdk ada perubahan data password

                                    mysqli_query($koneksi, "UPDATE login SET nama=\"$nama\", alamat='$alamat', kota='$kota', tlp='$tlp', level='$level', tipe='$tipe', status='$status' WHERE username='$user'");

                                }

                                else {

                                    //ada perubahan password

                                    $pass2 = password_hash($pass, PASSWORD_DEFAULT);

                                    mysqli_query($koneksi, "UPDATE login SET password='$pass2', nama=\"$nama\", alamat='$alamat', kota='$kota', tlp='$tlp', level='$level', tipe='$tipe', status='$status' WHERE username='$user'");

                                }



                                if (mysqli_affected_rows($koneksi) > 0) {

                                    echo "<div class='alert alert-success alert-dismissible fade show' id=alert-user>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Data</strong> berhasil diupdate...

                                        </div>

                                        <script>setTimeout(function(){ window.location.href='index.php?p=user'; }, 1500);</script>";

                                } else {

                                    echo "<div class='alert alert-primary alert-dismissible fade show' id=alert-user>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Data</strong> Tidak ada perubahan...

                                        </div>";

                                }

                            }

                            elseif($t == "user_hapus") {

                                $user = $_POST['user'] ?? '';

                                if (!empty($user)) {
                                    mysqli_query($koneksi,"DELETE FROM login WHERE username='$user'");
                                }



                                if (!empty($user) && mysqli_affected_rows($koneksi) > 0) {

                                    echo "<div class='alert alert-success alert-dismissible fade show' id=alert-user>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Data</strong> berhasil dihapus...

                                        </div>

                                        <script>setTimeout(function(){ window.location.href='index.php?p=user'; }, 1500);</script>";

                                } else {

                                    echo "<div class='alert alert-danger alert-dismissible fade show' id=alert-user>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Data</strong> gagal dihapus...

                                        </div>";

                                }

                            }

                            //Tabel Tarif

                            elseif($t == "tarif_add") {

                                $id_tarif = $_POST['id_tarif'];

                                $tarif = $_POST['tarif'];

                                $tipe = $_POST['tipe'];

                                $status = $_POST['status'];



                                $qc = mysqli_query($koneksi, "SELECT id_tarif FROM tarif WHERE id_tarif='$id_tarif'");

                                $qj = mysqli_num_rows($qc);

                                if (empty($qj)) {

                                    mysqli_query($koneksi, "INSERT INTO tarif (id_tarif, tarif, tipe, status) VALUES ('$id_tarif','$tarif','$tipe','$status')");



                                    if (mysqli_affected_rows($koneksi) > 0) {

                                        echo "<div class='alert alert-success alert-dismissible fade show' id=alert-tarif>

                                                <button type=button class=btn-close data-bs-dismiss=alert></button>

                                                <strong>Data</strong> berhasil dimasukkan...

                                            </div>

                                            <script>setTimeout(function(){ window.location.href='index.php?p=tarif'; }, 1500);</script>";

                                    } else {

                                        echo "<div class='alert alert-danger alert-dismissible fade show' id=alert-tarif>

                                                <button type=button class=btn-close data-bs-dismiss=alert></button>

                                                <strong>Data</strong> GAGAL dimasukkan...

                                            </div>";

                                    }

                                } else {

                                    echo "<div class='alert alert-danger alert-dismissible fade show' id=alert-tarif>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>ID Tarif</strong> sudah terdaftar...

                                        </div>";

                                }

                            }

                            elseif($t == "tarif_edit") {

                                $id_tarif = $_POST['id_tarif'];

                                $tarif = $_POST['tarif'];

                                $tipe = $_POST['tipe'];

                                $status = $_POST['status'];



                                mysqli_query($koneksi, "UPDATE tarif SET tarif='$tarif', tipe='$tipe', status='$status' WHERE id_tarif='$id_tarif'");



                                // script timeout untuk redirect ke halaman tabel utama

                                if (mysqli_affected_rows($koneksi) > 0) {

                                    echo "<div class='alert alert-success alert-dismissible fade show' id=alert-tarif>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Data</strong> berhasil diupdate...

                                        </div>

                                        <script>setTimeout(function(){ window.location.href='index.php?p=tarif'; }, 1500);</script>";

                                } else {

                                    echo "<div class='alert alert-primary alert-dismissible fade show' id=alert-tarif>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Data</strong> Tidak ada perubahan...

                                        </div>";

                                }

                            }

                            elseif($t == "tarif_hapus") {

                                $id_tarif = $_POST['id_tarif'];



                                mysqli_query($koneksi, "DELETE FROM tarif WHERE id_tarif='$id_tarif'");



                                if (mysqli_affected_rows($koneksi) > 0) {

                                    echo "<div class='alert alert-success alert-dismissible fade show' id=alert-tarif>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Data</strong> berhasil dihapus...

                                        </div>

                                        <script>setTimeout(function(){ window.location.href='index.php?p=tarif'; }, 1500);</script>";

                                } else {

                                    echo "<div class='alert alert-danger alert-dismissible fade show'>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Data</strong> gagal dihapus...

                                        </div>";

                                }

                            }

                            //tabel suntuk menambah add

                            elseif($t == "meter_add") {

                                $username = $_POST['username'];

                                $meter_awal = $_POST['meter_awal'];

                                $meter_akhir = $_POST['meter_akhir'];

                                $status = $_POST['status'] ?? '';
                                $kd_tarif = $air->user_to_idtarif($username);

                                $tarif = $air->kdtarif_to_tarif($kd_tarif);



                                // validasi meter dan aturan per bulan

                                $meter_awal_num = floatval($meter_awal);
                                $meter_akhir_num = floatval($meter_akhir);
        
                                //mengambil meter akhir data terakhir warga tersebut
                                $q_last = mysqli_query($koneksi, "SELECT meter_akhir FROM pemakaian WHERE username='$username' ORDER BY tgl DESC, waktu DESC LIMIT 1");
                                $last_data = mysqli_fetch_row($q_last);
                                $last_meter_akhir = $last_data ? floatval($last_data[0]) : null;

                                //tidak boleh input 2x dibulan yang sama
                                $bulan_ini = date('m'); 
                                $tahun_ini = date('Y');

                                //mengecek apakah warga itu sudah punya data di tabel pemakaian pada bulan dan tahun yang sama
                                $q_same_month = mysqli_query($koneksi, "SELECT no FROM pemakaian WHERE username='$username' AND MONTH(tgl)='$bulan_ini' AND YEAR(tgl)='$tahun_ini' LIMIT 1");

                                //apabila ketemu data yang sama,
                                if ($q_same_month && mysqli_num_rows($q_same_month) > 0) {

                                //maka data ditolak dan muncul pesan
                                    echo "<div class='alert alert-danger alert-dismissible fade show' id=alert-meter>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Data</strong> warga bulan ini sudah dicatat. Hanya boleh satu catatan per bulan.

                                        </div>";

                                }

                                //validasi, apabila tidak sama akan ditolak dan danger
                                elseif ($last_meter_akhir !== null && $meter_awal_num != $last_meter_akhir) {

                                    echo "<div class='alert alert-danger alert-dismissible fade show' id=alert-meter>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Meter Awal</strong> harus sama dengan Meter Akhir terakhir ($last_meter_akhir). Jika kurang atau beda, data tidak diterima.

                                        </div>";

                                }

                                //meter akhir harus lebih besar dari meter awal
                                elseif ($meter_akhir_num <= $meter_awal_num) {

                                    echo "<div class='alert alert-danger alert-dismissible fade show' id=alert-meter>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Meter Akhir</strong> harus lebih besar dari Meter Awal...

                                        </div>";

                                }

                                elseif (empty($kd_tarif) || !is_numeric($tarif)) {

                                    echo "<div class='alert alert-danger alert-dismissible fade show' id=alert-meter>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Tarif</strong> untuk warga ini belum ditemukan atau tidak aktif...

                                        </div>";

                                }

                                else {

                                    $pemakaian = $meter_akhir_num - $meter_awal_num;
                                    $tagihan = $pemakaian * $tarif;
                                    
                                    //data yang diadd masuk ke pemakaian
                                    mysqli_query($koneksi, "INSERT INTO pemakaian (username,meter_awal,meter_akhir,pemakaian,tgl,waktu,kd_tarif,tagihan,status) VALUES ('$username','$meter_awal','$meter_akhir','$pemakaian',CURRENT_DATE(),CURRENT_TIME(),'$kd_tarif','$tagihan','$status')");

                                     if (mysqli_affected_rows($koneksi) > 0) {

                                        echo "<div class='alert alert-success alert-dismissible fade show' id=alert-meter>

                                                <button type=button class=btn-close data-bs-dismiss=alert></button>

                                                <strong>Data</strong> berhasil dimasukkan...

                                            </div>";

                                    } else {

                                        echo "<div class='alert alert-danger alert-dismissible fade show' id=alert-meter>

                                                <button type=button class=btn-close data-bs-dismiss=alert></button>

                                                <strong>Data</strong> GAGAL dimasukkan...

                                            </div>";

                                    }

                                }

                            }

                            elseif($t == "meter_edit") {

                                $no = $_POST['no'];

                                $meter_awal = $_POST['meter_awal'];

                                $meter_akhir = $_POST['meter_akhir'];

                                $status = $_POST['status'] ?? null;



                                $username = $air->no_to_user($no);

                                $kd_tarif = $air->user_to_idtarif($username);

                                $tarif = $air->kdtarif_to_tarif($kd_tarif);

                                $pemakaian = $meter_akhir - $meter_awal;

                                $tagihan = $pemakaian * $tarif;

                                

                                // cek meter awal harus lebih kecil dari meter akhir

                                if ($pemakaian <= 0) { // meter awal >= meter akhir

                                    echo "<div class='alert alert-danger alert-dismissible fade show' id=alert-meter>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Meter Akhir</strong> harus lebih besar dari Meter Awal...

                                        </div>";

                                }

                                elseif (empty($kd_tarif) || !is_numeric($tarif)) {

                                    echo "<div class='alert alert-danger alert-dismissible fade show' id=alert-meter>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Tarif</strong> untuk warga ini belum ditemukan atau tidak aktif...

                                        </div>";

                                }

                                else {

                                    // ini biar bendahara gabisa ubah tanggal atau jam nya jadi tanggal nya tetep

                                    if($level_login == "bendahara") {

                                        $updateStatus = $status ?? '';
                                        mysqli_query($koneksi, "UPDATE pemakaian SET status='$updateStatus' WHERE no='$no'");

                                    }

                                    else {

                                        if ($status === null) {
                                            mysqli_query($koneksi, "UPDATE pemakaian SET meter_awal='$meter_awal', meter_akhir='$meter_akhir', pemakaian='$pemakaian', kd_tarif='$kd_tarif', tagihan='$tagihan' WHERE no='$no'");
                                        } else {
                                            mysqli_query($koneksi, "UPDATE pemakaian SET meter_awal='$meter_awal', meter_akhir='$meter_akhir', pemakaian='$pemakaian', kd_tarif='$kd_tarif', tagihan='$tagihan', status='$status' WHERE no='$no'");
                                        }

                                    }

                                }



                                // script timeout untuk redirect ke halaman tabel utama

                                if (mysqli_affected_rows($koneksi) > 0) {

                                    echo "<div class='alert alert-success alert-dismissible fade show' id=alert-meter>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Data</strong> berhasil diupdate...

                                        </div>";

                                } else {

                                    echo "<div class='alert alert-success alert-dismissible fade show' id=alert-meter>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Data</strong> Tidak ada perubahan...

                                        </div>";

                                }

                            }

                            elseif($t == "meter_hapus") {

                                $no = $_POST['no'];



                                mysqli_query($koneksi, "DELETE FROM pemakaian WHERE no='$no'");



                                if (mysqli_affected_rows($koneksi) > 0) {

                                    echo "<div class='alert alert-success alert-dismissible fade show' id=alert-meter>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Data</strong> berhasil dihapus...

                                        </div>";

                                } else {

                                    echo "<div class='alert alert-danger alert-dismissible fade show' id=alert-meter>

                                            <button type=button class=btn-close data-bs-dismiss=alert></button>

                                            <strong>Data</strong> gagal dihapus...

                                        </div>";

                                }

                            }

                        }



                        // ================= GET =================

                        elseif(isset($_GET['p'])) {

                            $p = $_GET['p'];



                            if ($p == "user_edit") {

                                $user = $_GET['user'];

                                $q = mysqli_query($koneksi, "SELECT password,nama,alamat,kota,tlp,level,tipe,status FROM login WHERE username='$user'");

                                $d = mysqli_fetch_row($q);



                                $pass = $d[0];

                                $nama = $d[1];

                                $alamat = $d[2];

                                $kota = $d[3];

                                $tlp = $d[4];

                                $level = $d[5];

                                $tipe = $d[6];

                                $status = $d[7];

                            }



                            elseif($p == "tarif_edit") {

                                $id = $_GET['id_tarif'];

                                $q = mysqli_query($koneksi, "SELECT id_tarif,tarif,tipe,status FROM tarif WHERE id_tarif='$id'");

                                $d = mysqli_fetch_row($q);



                                $id_tarif = $d[0];

                                $tarif = $d[1];

                                $tipe = $d[2];

                                $status = $d[3];

                            }


                            //mencari data lama dari tabel pemakaian dimana no=
                            elseif($p == "meter_edit") {

                                $no = $_GET['no'];

                                $q = mysqli_query($koneksi, "SELECT username,meter_awal,meter_akhir,status FROM pemakaian WHERE no='$no'");

                                $d = mysqli_fetch_row($q);



                                $username = $d[0];

                                $meter_awal = $d[1];

                                $meter_akhir = $d[2];

                                $status = $d[3];

                            }

                        }

                        ?>

                        <div class="card mb-4" id="user_add">

                            <div class="card-header">

                                <i class="fa-solid fa-user-plus me-2 text-success fa-fade"></i>

                                User

                            </div>

                            <div class="card-body">

                               <form method="post" class="need-validation" id="user_form">

                                    <div class="mb-3">

                                        <label for="username" class="form-label">Username:</label>

                                        <input type="text" class="form-control" id="username" placeholder="Enter username" name="yuser" value="<?php echo $user ?>" required>

                                    </div>

                                    <div class="mb-3">

                                        <label for="pwd" class="form-label">Password:</label>

                                        <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="paswet" value="<?php echo $pass ?>" required>

                                    </div>

                                    <div class="mb-3">

                                        <label for="nama" class="form-label">Nama:</label>

                                        <input type="text" class="form-control" id="nama" placeholder="Enter nama" name="nama" value="<?php echo $nama ?>" required>

                                    </div>

                                    <div class="mb-3">

                                        <label for="alamat">Alamat:</label>

                                        <textarea class="form-control" rows="5" id="alamat" name="alamat"><?php echo $alamat ?></textarea>

                                    </div>

                                    <div class="mb-3">

                                        <label for="kota" class="form-label">Kota:</label>

                                        <input type="text" class="form-control" id="kota" placeholder="Enter kota" name="kota" value="<?php echo $kota ?>">

                                    </div>

                                    <div class="mb-3">

                                        <label for="tlp" class="form-label">Telepon:</label>

                                        <input type="text" class="form-control" id="tlp" placeholder="Enter telepon" name="tlp" value="<?php echo $tlp ?>">

                                    </div>

                                    <div class="mb-3">

                                        <label for="level" class="form-label">Level:</label>

                                        <select class="form-select" name="level">

                                            <option value="">Level</option>

                                            <?php

                                            $lv = array("admin","bendahara","petugas","warga");

                                            foreach($lv as $lv2) {

                                                if ($level == $lv2) $sel = "SELECTED";

                                                else $sel = "";

                                                echo "<option value=$lv2 $sel>" .ucwords($lv2). "</option>";

                                            }

                                            ?>

                                        </select>

                                    </div>

                                    <div class="mb-3">

                                        <label for="tipe" class="form-label">Tipe:</label>

                                        <select class="form-select" name="tipe">

                                            <option value="">Tipe</option>

                                            <?php

                                            $t = array("RT","KOS");

                                            foreach($t as $t2) {

                                                if ($tipe == $t2) $sel = "SELECTED";

                                                else $sel = "";

                                                echo "<option value=$t2 $sel>" .ucwords($t2). "</option>";

                                            }

                                            ?>

                                        </select>

                                    </div>

                                    <div class="mb-3">

                                        <label for="status" class="form-label">Status:</label>

                                        <select class="form-select" name="status">

                                            <option value="">Status</option>

                                            <?php

                                            $s = array("AKTIF","TDK AKTIF");

                                            foreach($s as $s2) {

                                                if ($status == $s2) $sel = "SELECTED";

                                                else $sel = "";

                                                echo "<option value='$s2' $sel>$s2</option>";

                                            }

                                            ?>

                                        </select>

                                    </div>

                                    <button type="submit" class="btn btn-primary" name="tombol" value="user_add">Simpan</button>

                                    <a href="index.php?p=user" class="btn btn-secondary ms-2">Batal</a>

                                </form> 

                            </div>

                        </div>

                        <div class="card mb-4" id="tarif_add">

                            <div class="card-header">

                                <i class="fa-solid fa-user-plus me-2 text-success fa-fade"></i>

                                Tarif

                            </div>

                            <div class="card-body">

                               <form method="post" class="need-validation" id="tarif_form">

                                    <div class="mb-3">

                                        <label for="id_tarif" class="form-label">ID Tarif:</label>

                                        <input type="text" class="form-control" id="id_tarif" placeholder="Enter ID Tarif" name="id_tarif" value="<?php echo $id_tarif ?>" required>

                                    </div>

                                    <div class="mb-3">

                                        <label for="tarif" class="form-label">Tarif:</label>

                                        <input type="number" class="form-control" id="tarif" placeholder="Enter Tarif" name="tarif" value="<?php echo $tarif ?>" required>

                                    </div>

                                    

                                    <div class="mb-3">

                                        <label for="tipe" class="form-label">Tipe:</label>

                                        <select class="form-select" name="tipe">

                                            <option value="">Tipe</option>

                                            <?php

                                            $t = array("KOS","RT");

                                            foreach($t as $t2) {

                                                if ($tipe == $t2) $sel = "SELECTED";

                                                else $sel = "";

                                                echo "<option value=$t2 $sel>" .ucwords($t2). "</option>";

                                            }

                                            ?>

                                        </select>

                                    </div>

                                    <?php

                                    $st = array("AKTIF", "TDK AKTIF");

                                    foreach ($st as $st2) {

                                        if ($status == $st2) $sel = "CHECKED";

                                        else $sel = "";

                                    

                                        echo "<div class='form-check form-check-inline'>

                                                <input type=radio class=form-check-input id=radio1 name=status value=\"$st2\" $sel>

                                                <label class=form-check-label for=status>$st2</label>

                                              </div>";

                                    }

                                    ?>

                                   

                                    <div class= mb-3>

                                    <button type="submit" class="btn btn-primary mt-3" name="tombol" value="tarif_add">Simpan</button>

                                    <a href="index.php?p=tarif" class="btn btn-secondary ms-2 mt-3">Batal</a>

                                    </div>

                                </form> 

                            </div>

                        </div>

                        <div class="card mb-4" id="meter_add">

                            <div class="card-header">

                                <i class="fa-solid fa-house me-2 text-success fa-fade"></i>

                                Meter

                            </div>

                            <div class="card-body">

                            <?php

                            //ini untuk disabled namanya jadi abu' gabisa diubah, trus dipasang di kode bawah

                            if ($e[1] == "meter_edit&no") $dis = 'disabled';

                            else $dis = "";

                            ?>

                               <div id="meter_error" style="display:none;"></div>

                                <form method="post" class="need-validation" id="meter_form">

                                    <div class="mb-3">
                                     
                                        <label for="username" class="form-label">Nama Warga:</label>

                                        <select class="form-select" name="username" required <?php echo $dis ?>>

                                            <option value="" disabled <?php if($username == '') echo 'selected'; ?>>Nama Warga</option>

                                            <?php
                                    //mengmbil data username dan nama,dari tabel login yang level='warga'
                                            $qw = mysqli_query($koneksi, "SELECT username,nama FROM login WHERE level='warga'");

                                            //mengambil baris nama 
                                            while($dw = mysqli_fetch_row($qw)) {

                                                if ($username == $dw[0]) $sel = "SELECTED";

                                                else $sel = "";

                                                //nama nama warga muncul pakai ini

                                                echo "<option value=\"$dw[0]\" $sel>$dw[1]</option>";

                                            }

                                            ?>

                                        </select>

                                    </div>

                                    <div class="mb-3">

                                        <label for="meter_awal" class="form-label">Meter Awal (m<sup>3</sup>):</label>

                                        <input type="text" class="form-control" id="meter_awal" placeholder="Enter Meter Awal" name="meter_awal" value="<?php echo $meter_awal ?>" required>

                                    </div>
                                        
                                    <div class="mb-3">

                                        <label for="meter_akhir" class="form-label">Meter Akhir (m<sup>3</sup>):</label>

                                        <input type="text" class="form-control" id="meter_akhir" placeholder="Enter Meter Akhir" name="meter_akhir" value="<?php echo $meter_akhir ?>" required>

                                    </div>

                                    <?php

                                    if($level_login == "admin" || $level_login == "bendahara") {

                                    ?>

                                    <div class="mb-3">

                                        <label class="form-label">Status Pembayaran</label>

                                        <select class="form-select" name="status">

                                            <option value="LUNAS"

                                            <?php if($status == "LUNAS") echo "selected"; ?>>

                                                LUNAS

                                            </option>

                                            <option value="BLM LUNAS"

                                            <?php if($status == "BLM LUNAS") echo "selected"; ?>>

                                                BLM LUNAS

                                            </option>

                                        </select>

                                    </div>

                                    <?php } ?>

                                    <div class="mb-3">

                                    <button type="submit" class="btn btn-primary mt-3" name="tombol" value="meter_add" onclick="return validateMeter()">Simpan</button>

                                    <a href="index.php?p=catat_meter" class="btn btn-secondary ms-2 mt-3">Batal</a>

                                    </div>

                                </form>

                                <script>

                                    function validateMeter() {

                                        var meterAwal = parseFloat(document.getElementById('meter_awal').value);

                                        var meterAkhir = parseFloat(document.getElementById('meter_akhir').value);

                                        var errorDiv = document.getElementById('meter_error');

                                        

                                        if (isNaN(meterAwal) || isNaN(meterAkhir)) {

                                            errorDiv.innerHTML = "<div class='alert alert-danger alert-dismissible fade show' role='alert'><strong>Error!</strong> Meter Awal dan Meter Akhir harus berupa angka!<button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button></div>";

                                            errorDiv.style.display = 'block';

                                            return false;

                                        }

                                        

                                        if (meterAkhir <= meterAwal) {

                                            errorDiv.innerHTML = "<div class='alert alert-danger alert-dismissible fade show' role='alert'><strong>Error!</strong> Meter Akhir harus lebih besar dari Meter Awal!<button type='button' class='btn-close' data-bs-dismiss='alert' aria-label='Close'></button></div>";

                                            errorDiv.style.display = 'block';

                                            return false;

                                        }

                                        errorDiv.style.display = 'none';

                                        return true;

                                    }

                                </script> 

                            </div>

                        </div>

                        <div class="modal" id="myModal">

                            <div class="modal-dialog">

                                <div class="modal-content">



                                <!-- Modal Header -->

                                <div class="modal-header">

                                    <h4 class="modal-title">Konfirmasi Hapus Data</h4>

                                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>

                                </div>



                                <div class="modal-body">

                                </div>



                                <div class="modal-footer">

                                    <form method="post">

                                        <button type="submit" name="tombol" value="user_hapus" class="btn btn-danger" data-bs-dismiss="modal">Ya</button>

                                    </form>

                                    <button type="button" class="btn btn-success" data-bs-dismiss="modal">Tidak</button>

                                </div>



                                </div>

                            </div>

                        </div>

                        <div class="card mb-4" id="user_list">

                            <div class="card-header">

                                <i class="fa-solid fa-users me-2 text-success fa-fade"></i>

                                Data User

                            </div>

                            <div class="card-body">

                                <table id="datatablesSimple">

                                    <thead>

                                        <tr>

                                            <th>Username</th>

                                            <th>Nama</th>

                                            <th>Alamat</th>

                                            <th>Kota</th>

                                            <th>Telepon</th>

                                            <th>Level</th>

                                            <th>Tipe</th>

                                            <th>Status</th>

                                            <th></th>

                                        </tr>

                                    </thead>

                                    

                                    <tbody>

                                        <?php

                                        $q = mysqli_query($koneksi, "SELECT username,nama,alamat,kota,tlp,level,tipe,status FROM login ORDER BY level ASC");

                                        while($d = mysqli_fetch_row($q)) {

                                            $user = $d[0];

                                            $nama = $d[1];

                                            $alamat = $d[2];

                                            $kota = $d[3];

                                            $telepon = $d[4];

                                            $level = $d[5];

                                            $tipe = $d[6];

                                            $status = $d[7];



                                            echo "<tr>

                                                    <td>$user</td>

                                                    <td>$nama</td>

                                                    <td>$alamat</td>

                                                    <td>$kota</td>

                                                    <td>$telepon</td>

                                                    <td>$level</td>

                                                    <td>$tipe</td>

                                                    <td>$status</td>

                                                    <td>

                                                        <a href='index.php?p=user_edit&user=$user'><button type='button' class='btn btn-outline-success btn-sm'>Ubah</button></a>

                                                        <button type='button' class='btn btn-outline-danger btn-sm' data-bs-toggle='modal' data-bs-target='#myModal' data-user='$user'>Hapus</button>

                                                    </td>



                                                </tr>";

                                        }

                                        ?>

                                        

                                    </tbody>

                                </table>

                            </div>

                        </div>

                        <div class="card mb-4" id="tarif_list">

                            <div class="card-header">

                                <i class="fa-solid fa-table me-1"></i>

                                Data Tarif

                            </div>

                            <div class="card-body">

                                <table id="table_tarif">

                                    <thead>

                                        <tr>

                                            <th>ID Tarif</th>

                                            <th>Tarif</th>

                                            <th>Tipe</th>

                                            <th>Status</th>

                                            <th>Aksi</th>

                                        </tr>

                                    </thead>

                                    

                                    <tbody>

                                        <?php

                                        $q = mysqli_query($koneksi, "SELECT id_tarif,tarif,tipe,status FROM tarif ORDER BY id_tarif ASC");

                                        while($d = mysqli_fetch_row($q)) {

                                            $id_tarif = $d[0];

                                            $tarif = $d[1];

                                            $tipe = $d[2];

                                            $status = $d[3];



                                            echo "<tr>

                                                    <td>$id_tarif</td>

                                                    <td>$tarif</td>

                                                    <td>$tipe</td>

                                                    <td>$status</td>

                                                    <td>

                                                        <a href='index.php?p=tarif_edit&id_tarif=$id_tarif'><button type='button' class='btn btn-outline-success btn-sm'>Ubah</button></a>

                                                        <button type='button' class='btn btn-outline-danger btn-sm' data-bs-toggle='modal' data-bs-target='#myModal' data-id_tarif='$id_tarif'>Hapus</button>

                                                    </td>



                                                </tr>";

                                        }

                                        ?>

                                        

                                    </tbody>

                                </table>

                            </div>

                        </div>

                        <div class="card mb-4" id="meter_list">

                            <div class="card-header text-primary">

                                <i class="fa-solid text-primary fa-house-flood-water me-1"></i>

                                Data Meter Warga

                            </div>

                            <div class="card-body">

                                <table id="table_meter">

                                    <thead>

                                        <tr>

                                            <th>Nama Warga</th>

                                            <th>Tipe</th>

                                            <th>Tanggal & Waktu</th>

                                            <th>Meter Awal</th>

                                            <th>Meter Akhir</th>

                                            <th>Pemakaian</th>

                                            <th>Tagihan</th>

                                            <?php

                                            if($level_login == "admin" || $level_login == "bendahara") {

                                                echo "<th>Status</th>";

                                            }

                                            ?>

                                            <th>Aksi</th>

                                        </tr>

                                    </thead>

                                    

                                    <tbody>

                                        <?php

                                        $q = mysqli_query($koneksi, "SELECT no,username,meter_awal,meter_akhir,pemakaian,tgl,waktu,tagihan,status FROM pemakaian ORDER BY tgl DESC, waktu DESC");

                                        while($d = mysqli_fetch_row($q)) {

                                            $no = $d[0];

                                            $data_user2 = $air->dt_user($d[1]); 

                                            $nama = $data_user2[0];

                                            $meter_awal = $d[2];

                                            $meter_akhir = $d[3];

                                            $pemakaian = is_numeric($d[4]) ? (int)$d[4] : 0;

                                            $tgl = $air->tgl_walik($d[5]);

                                            $waktu = $d[6];

                                            $tagihan = $d[7];

                                            $status = $d[8];

                                            

                                            $qt = mysqli_query($koneksi, "SELECT tipe FROM login WHERE username='".$d[1]."'");

                                            $dt = mysqli_fetch_row($qt);

                                            $tipe = $dt[0];

                                            $tgl_tabel = date_create($d[5]);
                                            $tgl_sekarang = date_create();
                                            $diff = date_diff($tgl_tabel, $tgl_sekarang);
                                            $selisih = $diff->days;

                                            $bulan_id = array(
                                                1 => "Januari",
                                                "Februari",
                                                "Maret",
                                                "April",
                                                "Mei",
                                                "Juni",
                                                "Juli",
                                                "Agustus",
                                                "September",
                                                "Oktober",
                                                "November",
                                                "Desember"
                                            );
                                            $tanggal_hari_ini = date("d") . " " . $bulan_id[intval(date("m"))] . " " . date("Y");

                                            echo "<tr>

                                                    <td>$nama</td>

                                                    <td>$tipe</td>

                                                    <td>$tgl $waktu |  " . $tanggal_hari_ini . " $selisih hari</td>

                                                    <td>$meter_awal</td>

                                                    <td>$meter_akhir</td>

                                                    <td>$pemakaian</td>

                                                    <td>Rp. " . number_format($tagihan, 0, ',', '.') . "</td>";

                                                    

                                            if($level_login == "admin" || $level_login == "bendahara") {

                                                if($status == "LUNAS") {

                                                    echo "<td><span class='badge bg-success'>LUNAS</span></td>";

                                                }

                                                else {

                                                    echo "<td><span class='badge bg-danger'>BLM LUNAS</span></td>";

                                                }

                                                //ubah di admin, dan button untuk tombol hapus, next ke 1219
                                                echo"<td>

                                                        <a href='index.php?p=meter_edit&no=$no'><button type='button' class='btn btn-outline-success btn-sm'>Ubah</button></a>
                                                
                                                        <button type='button' class='btn btn-outline-danger btn-sm' data-bs-toggle='modal' data-bs-target='#myModal' data-no='$no'>Hapus</button>

                                                    </td>";

                                            }

                                            else {

                                                // berlaku untuk petugas

                                                if (!isset($selisih)) {
                                                    $tgl_tabel = date_create($d[5]);
                                                    $tgl_sekarang = date_create();
                                                    $diff = date_diff($tgl_tabel, $tgl_sekarang);
                                                    $selisih = $diff->days;
                                                }

                                                if($selisih <= 30) {

                                                echo"<td>

                                                        <a href='index.php?p=meter_edit&no=$no'><button type='button' class='btn btn-outline-success btn-sm'>Ubah</button></a>

                                                        <button type='button' class='btn btn-outline-danger btn-sm' data-bs-toggle='modal' data-bs-target='#myModal' data-no='$no'>Hapus</button>

                                                    </td>";

                                                }

                                                else {

                                                    echo"<td></td>";

                                                }

                                            }

                                             

                                            

                                            echo"</tr>";

                                        }

                                        ?>

                                        

                                    </tbody>

                                </table>

                            </div>

                        </div>

                        <div class="card mb-4" id="pemakaian_sendiri">

                            <div class="card-header">

                                <i class="fa-solid fa-table me-1"></i>

                                Data Pemakaian

                            </div>

                            <div class="card-body">

                                <table id="table_pemakaian">

                                    <thead>

                                        <tr>

                                            <th>Tanggal & Waktu</th>

                                            <th>Kode Tarif</th>

                                            <th>Meter Awal</th>

                                            <th>Meter Akhir</th>

                                            <th>Pemakaian</th>

                                            <th>Tagihan</th>

                                            <th>Status</th>

                                        </tr>

                                    </thead>

                                    

                                    <tbody>

                                        <?php

                                        $q = mysqli_query($koneksi, "SELECT kd_tarif,meter_awal,meter_akhir,pemakaian,tagihan,status,tgl,waktu FROM pemakaian WHERE username='$_SESSION[user]' ORDER BY tgl DESC, waktu DESC");

                                        while($d = mysqli_fetch_row($q)) {

                                            $kd_tarif = $d[0];

                                            $meter_awal = $d[1];

                                            $meter_akhir = $d[2];

                                            $pemakaian = is_numeric($d[3]) ? (int)$d[3] : 0;

                                            $tagihan = $d[4];

                                            $status = $d[5];

                                            $tgl = $air->tgl_walik($d[6]);

                                            $waktu = $d[7];



                                            echo "

                                            <tr>

                                                <td>$tgl $waktu</td>

                                                <td>$kd_tarif</td>

                                                <td>$meter_awal</td>

                                                <td>$meter_akhir</td>

                                                <td>$pemakaian</td>

                                                <td>Rp. " . number_format($tagihan, 0, ',', '.') . "</td>";



                                            if($status == "LUNAS") {

                                                echo "

                                                <td><span class='badge bg-success'>LUNAS</span></td>";

                                            }

                                            else {

                                                echo "

                                                <td><span class='badge bg-danger'>BLM LUNAS</span></td>";

                                            }

                                            echo "</tr>";

                                        }

                                        ?>

                                        

                                    </tbody>

                                </table>

                            </div>

                        </div>

                        

                    </div>

                </main>

                <footer class="py-4 bg-light mt-auto">

                    <div class="container-fluid px-4">

                        <div class="d-flex align-items-center justify-content-between small">

                            <div class="text-muted">Copyright &copy; Your Website 2026</div>

                            <div>

                                <a href="#">Privacy Policy</a>

                                &middot;

                                <a href="#">Terms &amp; Conditions</a>

                            </div>

                        </div>

                    </div>

                </footer>

            </div>

        </div>

        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>

        <script src="../js/scripts.js"></script>

        <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart.min.js" crossorigin="anonymous"></script>

        <!-- <script src="../assets/demo/chart-area-demo.js"></script>

        <script src="../assets/demo/chart-bar-demo.js"></script> -->

        <script src="https://cdn.jsdelivr.net/npm/simple-datatables@7.1.2/dist/umd/simple-datatables.min.js" crossorigin="anonymous"></script>

        <script src="../js/datatables-simple-demo.js"></script>

    </body>

</html>
