<?php
    include('../loginsession.php');
    error_reporting(E_ALL);
    $filename = __FILE__;  //---Nama file saat ini
    if (strlen(__FILE__) > 48) {$filename = substr(__FILE__, 48);}
    $save = mysqli_query($conn, "INSERT INTO novia_abc_user_logs (userid,filename) VALUES ('$userini_id','$filename')");

//-------------
    if(isset($_GET['item'])){
    $item = $_GET['item'];
    }

    $sql = "SELECT * FROM novia_abc_cash_category_sub WHERE id=$item";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
	while($row = $result->fetch_assoc()) {
	    $namaitem = $row["subcategory_item"];
    	}
    }
//-------------
    $today = date("Y-m-d");
//-------------

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//ID" "http://www.w3.org/TR/html4/frameset.dtd">
<html lang="id">
<head>
	<title>STATISTIK PER ITEM BELANJA</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" href="../css/style.css">
	<link href="../images/balance.ico" rel="icon" type="image/icon" />

	<style>

		input[type=text],input[type=password]
		{
		width:60%;
		padding:8px;
		margin-top:8px;
		margin-bottom:8px;
		border:1px solid #ccc;
		padding-left:5px;
		font-size:18px;
		}

		input[type=submit]
		{
		width:35%;
		background-color:#8baca4;
		color:#FFFFFF;
		border:2px solid #8baca4;
		padding:6px;
		font-size:18px;
		cursor:pointer;
		border-radius:5px;
		}
		img {
            width: 100%;
            max-width: 500px;
            height: auto;
        }
    </style>	
</head>

<body>
<br>
<a href="../coding/coding_main.php" title="Lihat Keuangan Hari ini"><button style="padding : 10px; width:100%; background-color:#546E7A;">CODING</button></a>
<br>
<br>
    <div><img src="../images/page/keuangan_statistik.png" alt="Keuangan Harian"></div>

    <table width="100%" cellpadding="5">

		<tr class="dua">
    		<td width="5%"><a href="../administrasi.php" title="NAVIGASI"><button style="padding : 10px; width:100%; background-color:#8baca4;">NAVIGASI</button></a></td>
		    <td><a href="../index.php" title="HOME"><button style="padding : 10px; width:100%; background-color:#A6B7C1;"> HOME </button></a></td>
		</tr>
	</table>
	<table width="100%" cellpadding="5">
		<tr class="dua">
		<td><a href="cash-view-monthly.php?tanggal=<?php echo date("Y-m-d");?>" title="Lihat Pengeluaran Bulan ini"><button style="padding : 10px; width:100%; background-color:#8baca4;">BELANJA BULANAN</button></a></td>
		<td><a href="cash-view-daily.php?tanggal=<?php echo date("Y-m-d");?>" title="Lihat Pengeluaran Hari ini"><button style="padding : 10px; width:100%; background-color:#8baca4;"> BELANJA HARIAN </button></a></td>
		</tr>
	</table>
	<table width="100%" cellpadding="5">
		<tr class="dua">
			<td colspan = 2; style="padding-top : 5 px;" align="center";>
				<a style = "font-family: 'Heebo' sans-serif; font-size:25px;"><?php echo "Pengeluaran ".$namaitem; ?><a>
			</td>
			<td colspan = 2;><a href="cash-stat.php?tanggal=<?php echo $today;?>" title="Lihat Statistik Keuangan"><button style="padding : 10px; width:100%; background-color:#546E7A;">Kembali</button></a></td>
		</tr>
		<tr class="satu">
    		<th align="left">Tahun - Bulan</th>
    		<th align="right">Rupiah</th>
    		<th align="center">Jumlah</th>
    		<th align="right">Keterangan</th>
    	</tr>
    	
<?php
// Tentukan rentang bulan (misalnya dari $tanggal_awal sampai bulan sekarang)
$tanggal_awal = '2024-09-01'; // contoh, bisa diganti sesuai kebutuhan
$tanggal_sekarang = date('Y-m');

// Buat array bulan dari $tanggal_awal sampai bulan sekarang
$bulan_array = [];
$start    = new DateTime($tanggal_awal);
$end      = new DateTime($tanggal_sekarang . '-01');
$interval = new DateInterval('P1M');
$period   = new DatePeriod($start, $interval, $end->modify('+1 month'));

foreach ($period as $dt) {
    $bulan = $dt->format('Y-m');
    $bulan_array[$bulan] = ['harga' => 0, 'jumlah' => 0];
}

// Tentukan order (ASC atau DESC) dari input user atau default
$order = isset($_GET['order']) ? $_GET['order'] : 'DESC'; 
// pastikan hanya ASC/DESC yang valid
$order = strtoupper($order) === 'ASC' ? 'ASC' : 'DESC';

// Query database
$query = "SELECT DATE_FORMAT(date_, '%Y-%m') AS bulan, 
                 SUM(harga) AS total_harga_bulan,
                 SUM(jumlah) AS total_jumlah_bulan
          FROM novia_abc_cash_shopping
          WHERE subcategory_id = $item
            AND date_ >= '$tanggal_awal'
          GROUP BY DATE_FORMAT(date_, '%Y-%m')
          ORDER BY DATE_FORMAT(date_, '%Y-%m') $order";

$result = $conn->query($query);

// Buat array bulan
$bulan_array = [];

// Masukkan hasil query ke array bulan
if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $bulan_array[$row['bulan']] = [
            'harga'  => $row['total_harga_bulan'],
            'jumlah' => $row['total_jumlah_bulan']
        ];
    }
}

// Tampilkan semua bulan, termasuk yang totalnya 0
foreach ($bulan_array as $bulan => $data) {
    ?>
    <tr class="dua">
        <td><?php echo $bulan; ?></td>
        <td align="center"><?php echo number_format($data['harga'], 0); ?></td>
        <td align="center"><?php echo $data['jumlah']; ?></td>
        <td></td>
    </tr>
    <?php
}
?>

    
    	
	</table>
</body>