<?php
session_start();
//koneksi ke database MariaDb

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

$air = new klas_air;

$koneksi = $air->koneksi();

if(isset($_POST['p'])) {
    $p=$_POST['p'];

    if($p=="chart_bar"){
        $yuser=mysqli_real_escape_string($koneksi, $_POST['user'] ?? '');
        $response=array();
        $q=mysqli_query($koneksi, "SELECT MONTH(tgl) as bulan, pemakaian FROM pemakaian WHERE username='$yuser'");
        while($d=mysqli_fetch_assoc($q)){
            $response[]=$air->bln($d['bulan']);
            $response[]=$d['pemakaian'];
        }
        echo json_encode($response);
        exit;

    }

     if($p=="chart_line"){
        $yuser=mysqli_real_escape_string($koneksi, $_POST['user'] ?? '');
        $response=array();
        $q=mysqli_query($koneksi, "SELECT MONTH(tgl) as bulan, tagihan FROM pemakaian WHERE username='$yuser'");
        while($d=mysqli_fetch_assoc($q)){
            $response[]=$air->bln($d['bulan']);
            $response[]=$d['tagihan'];
        }
        echo json_encode($response);
        exit;
    }

    if($p== 'summary'){
    // data bulan diterima
        $bln=mysqli_real_escape_string($koneksi, $_POST['t'] ?? '');

        if (!empty($_SESSION['user'])) {
            $dt_user = $air->dt_user($_SESSION['user']);
            $level_login = $dt_user[2] ?? '';
        }

        $like = ($bln !== '') ? $bln . '%' : '%';
//kalau level login adalah warga, tampilkan data pemakaian dan tagihan bulan ini untuk pengguna tersebut
        if($level_login == "warga") {
            $user_session = mysqli_real_escape_string($koneksi, $_SESSION['user']);
            $query = "SELECT tgl, waktu, pemakaian, tagihan, status FROM pemakaian WHERE username='$user_session' AND tgl LIKE '$like' ORDER BY tgl DESC, waktu DESC LIMIT 1";
            $qw = mysqli_query($koneksi, $query);
            $rw = $qw ? mysqli_fetch_assoc($qw) : null;
            
//respon kalau login sebagai warga
            $resp = array(
                'role' => 'warga',
                'data' => array(
                    'waktu_pencatatan' => $rw ? date('d', strtotime($rw['tgl'])) : '',
                    'jam' => $rw ? $rw['waktu'] : '',
                    'pemakaian' => $rw ? floatval($rw['pemakaian']) : 0,
                    'tagihan' => $rw ? floatval($rw['tagihan']) : 0,
                    'status_tagihan' => $rw ? $rw['status'] : ''
                )
            );
            echo json_encode($resp);
            exit;
        }

        $q1=mysqli_query($koneksi, "SELECT COUNT(username) as jml_pelanggan FROM login WHERE level='warga'");
        $d1=mysqli_fetch_assoc($q1);

        if($level_login == "bendahara") {
            $q2=mysqli_query($koneksi, "SELECT COALESCE(SUM(tagihan), 0) as jml_pemasukan FROM pemakaian WHERE tgl LIKE '$like' AND status='LUNAS'");
            $d2=mysqli_fetch_assoc($q2);

            $q3=mysqli_query($koneksi, "SELECT COUNT(DISTINCT username) as sudah_lunas FROM pemakaian WHERE tgl LIKE '$like' AND status='LUNAS'");
            $d3=mysqli_fetch_assoc($q3);

            $belum_lunas = intval($d1['jml_pelanggan']) - intval($d3['sudah_lunas']);

            $resp = array(
                'role' => 'bendahara',
                'data' => array(
                    'jml_pelanggan' => intval($d1['jml_pelanggan']),
                    'jml_pemasukan' => floatval($d2['jml_pemasukan']),
                    'sudah_lunas' => intval($d3['sudah_lunas']),
                    'belum_lunas' => $belum_lunas
                )
            );
            echo json_encode($resp);
            exit;
        }

        $q2=mysqli_query($koneksi, "SELECT COALESCE(SUM(pemakaian),0) as jml_pemakaian FROM pemakaian WHERE tgl LIKE '$like'");
        $d2=mysqli_fetch_assoc($q2);

        $q3=mysqli_query($koneksi, "SELECT COUNT(username) as sudah_dicatat FROM pemakaian WHERE tgl LIKE '$like'");
        $d3=mysqli_fetch_assoc($q3);

        $resp = array(
            'role' => 'default',
            'data' => array(
                'jml_pelanggan' => intval($d1['jml_pelanggan']),
                'jml_pemakaian' => floatval($d2['jml_pemakaian']),
                'sudah_dicatat' => intval($d3['sudah_dicatat'])
            )
        );
        echo json_encode($resp);
    }
}
?>