<?php 
    include('loginsession.php');
    error_reporting(E_ALL);
    $filename = __FILE__;
    if (strlen(__FILE__) > 48) {$filename = substr(__FILE__, 48);}
    $save = mysqli_query($conn, "INSERT INTO novia_abc_user_logs (userid,filename) VALUES ('$userini_id','$filename')");
    
    $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 PASIEN BULANAN</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>

    </style>	
</head>

<body>
    
	<table width="100%" cellpadding="5">
		<tr class="dua">
			<td><a href="index.php" title="Kembali ke halaman awal"><button style="padding : 10px; width:100%">HOME</button></a></td>
			<td><a href="visit-yearly.php?tanggal=<?php echo $today;?>" title="Lihat Pasien Bulan ini"><button style="padding : 10px; width:100%">PASIEN TAHUNAN</button></a></td>
			<td><a href="visit-daily.php?tanggal=<?php echo $today;?>" title="Lihat Pasien Bulan ini"><button style="padding : 10px; width:100%">PASIEN HARIAN</button></a></td>
			<td><a href="administrasi.php" title="Pusat Navigasi Data"><button style="padding : 10px; width:100%; background-color:#A6B7C1;"> NAVIGASI </button></a></td>
		</tr>
	</table<>
	<table width="100%" cellpadding="5">
	    <tr class="satu">
        <td colspan="5"><button style="padding : 10px; width:100%; background-color:#B6B7C1;">TOTAL OMSET dan PASIEN per Bulan</button></td>
        </tr>    
    	<tr class="satu">
    		<th align="left">Tahun - Bulan</th>
    		<th align="right">OMSET</th>
    		<th align="center">Jumlah Pasien</th>
    		<th align="right">Jumlah Jarum</th>
    		<th align="center">Kisaran Tarif</th>
    	</tr>

    <?php 

    // Tentukan tanggal awal (misalnya September 2024)
    $start_month = '2024-09-01';

    // Query untuk mengambil total aero per bulan
    $query = "SELECT DATE_FORMAT(date_, '%Y-%m') as bulan, 
              SUM(biaya) as total_biaya_bulan,
              SUM(pin) as total_jarum_bulan,
              COUNT(*) as jumlah_baris
              FROM novia_abc_visit
              WHERE date_ >= '$start_month'
              GROUP BY DATE_FORMAT(date_, '%Y-%m')
              ORDER BY bulan DESC";
    
    $result = $conn->query($query);

    // Masukkan hasil query ke array bulan
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
        $Rata = $row['total_biaya_bulan'] / $row['jumlah_baris'];
        
    ?>
    
        <tr class="dua">
            <td>
                <a href="<?php 
                    echo 'visit-monthly.php?tanggal=' . $row["bulan"];
                    ?>"
                    title="Lihat Kunjungan dalam Bulan ini ">
                    <?php echo $row['bulan']; ?>
                </a>
            </td>
            <td align="right">
                <a href="<?php 
                    echo 'visit-monthly.php?tanggal=' . $row["bulan"];
                    ?>"
                    title="Lihat Kunjungan dalam Bulan ini ">
                    <?php echo $row['total_biaya_bulan']; ?>
                </a>
            </td>
            <td align="center"><?php echo $row['jumlah_baris']; ?></td>
            <td align="right"><?php echo $row['total_jarum_bulan']; ?></td>
            <td align="center"><?php echo number_format($Rata, 1); ?></td>
        </tr>
        
    <?php
    }
    } else {
    ?>
        <tr>
            <td colspan="5" align="center">Tidak ada data</td>
        </tr>
    <?php
    }
    ?>
</table>
</body>
</html>    