<?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')");
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//ID" "http://www.w3.org/TR/html4/frameset.dtd">
<html lang="id">
<head>
   <title>AEROBIK</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>
	
<?php
$Total_Harian = 0;
$Total_Mingguan = 0;
$Total_Bulanan = 0;

//$Total_Harian_Meditasi = 0;
//$Total_Mingguan_Meditasi = 0;
//$Total_Bulanan_Meditasi = 0;


$sa = date("Y-m-d");
$se = date("Y-m-d H:i:s");
$dt1 = new DateTime($se);
//$timeU1 = $dt1->format('U');
//$minggu = $timeU1 - 604800;
//$bulan = $timeU1 - 2592000;

$sse = date('Y-m-d');
$ssa = new DateTime($sse);
$ssa->modify('-7 day');
$ssa_formatted = $ssa->format('Y-m-d');

$ssab = new DateTime($sse);
$ssab->modify('-30 day');
$ssab_formatted = $ssab->format('Y-m-d');

$aero = "SELECT * FROM novia_abc_user_aero where userid=$userini_id ORDER BY tanggal DESC";
$resultan = $conn->query($aero);
    while($baris = $resultan->fetch_assoc()) {
		
		
		$s = $baris["tanggal"];
        $dt = new DateTime($s);
        $currdate = $dt->format('Y-m-d');
        //$detik = $dt->format('U');
        $detik = $dt->format('Y-m-d');
		if ( $currdate == $sa ){
			$Total_Harian += $baris["aero"];
			//$Total_Harian_Meditasi += $baris["meditation"];
		}
		if ( $detik > $ssa_formatted ){
			$Total_Mingguan += $baris["aero"];
			//$Total_Mingguan_Meditasi += $baris["meditation"];
		}
		if ( $detik > $ssab_formatted ){
			$Total_Bulanan += $baris["aero"];
			//$Total_Bulanan_Meditasi += $baris["meditation"];
		}
	}


?>
    <table width="100%" cellpadding="5">
		<tr class="satu"><td colspan="4"></td></tr>
		<tr class="dua">
			<td><a href="../administrasi.php" title="Reset Data Kelurahan"><button style="padding : 10px; width:100%; background-color:#A6B7C1;">PUSAT NAVIGASI</button></a></td>
			<td><a href="../todo/routine.php" title="User Log"><button style="padding : 10px; width:100%; background-color:#A6B7C1;"> PDA </button></a></td>
			<td><a href="meditation.php" title="Meditasi"><button style="padding : 10px; width:100%; background-color:#A6B7C1;">MEDITASI</button></a></td>
		</tr>
		<tr class="satu"><td colspan="4"></td></tr>
	</table>

    <table width="100%" cellpadding="6">
		<tr class="dua">
		    <td Height=66px align="center"; style="background-color:#29434E; color:#bbb;"><a style = "font-size:20px;">AEROBIK <?php echo $Total_Bulanan; ?><a>
		        <a style = "font-size:18px;"><br><?php echo $userini_name; ?><a></td>
		</tr>
	</table>

    <div><img src="../images/Lari_01.jpg" alt="Deskripsi Gambar" width="100%" height="auto"></div>


<table width="100%" max-width="600px"" cellpadding="3" align="center">
	<tbody>
		<tr>
			<td><a href="../index.php" title="Halaman Awal"><button style="padding : 10px; width:100%; background-color:#819CA9;">BULANAN<br><?php echo $Total_Bulanan; ?></button></a></td>
			<td><a href="../index.php" title="Halaman Awal"><button style="padding : 10px; width:100%; background-color:#A6B7C1;">MINGGUAN<br><?php echo $Total_Mingguan; ?></button></a></td>
			<td><a href="../index.php" title="Halaman Awal"><button style="padding : 10px; width:100%; background-color:#C7D2D9;">HARIAN<br><?php echo $Total_Harian; ?></button></a></td>
		</tr>
	</tbody>
</table>

<form  method="post"  action="aero-insert.php?id=<?php echo $userini_id; ?>">
	<table width="100%" max-width="600px" cellpadding="5" align="center">
    <tbody>
    <tr class="satu"><td colspan="4"></td></tr>
	

    <tr class="dua">
		<td><input type="text" name="tgl_aerobik" placeholder="tgl_aerobik" size="13" maxlength="30" autocomplete="off" value="<?php echo date("Y-m-d H:i:s")?>"/></td>
        <td><input type="text" name="nilai" placeholder="nilai" size="1" maxlength="30" autocomplete="off" value="10"/></td>
        <td><button type="submit" name="simpan" style="padding: 10px; width: 100%; background-color:#29434E; color:#ccc;">TAMBAH NILAI AEROBIK</button</td>
    </tr>
	<tr class="satu"><td colspan="4"></td></tr>
	<tr class="satu"><td colspan="4"></td></tr>
    </tbody>
    </table>
</form>

<table width="100%" cellpadding="5">
        <tr class="satu">
            <td colspan="4"><button style="padding : 10px; width:100%; background-color:#B6B7C1;">Catatan AEROBIK dalam 30 hari terakhir</button></td>
        </tr>
		<tr class="satu">
    		<th>Hari</th>
    		<th>Tanggal</th>
    		<th>Waktu (Menit)</th>
    		<th>Grafik - Statistik</th>
		</tr>

<?php
$se = date('Y-m-d');
$sa = new DateTime($se);
$sa->modify('-29 day');
$sa_formatted = $sa->format('Y-m-d');

// Membuat array tanggal untuk 30 hari terakhir
        /*
        $start = new DateTime('2023-01-01');
        $end = new DateTime('2023-01-10');
        $interval = new DateInterval('P1D');
        
        $period = new DatePeriod($start, $interval, $end);
        
        foreach ($period as $date) {
            echo $date->format('Y-m-d') . PHP_EOL;
        }
        Kode di atas akan mencetak tanggal dari 1 Januari 2023 hingga 9 Januari 2023 (end date tidak termasuk).
        */

$period = new DatePeriod(
    new DateTime($sa_formatted),
    new DateInterval('P1D'), // Interval 1 hari
    new DateTime($se . ' +1 day')
);

$tanggalArray = [];
foreach ($period as $date) {
    $tanggalArray[$date->format('Y-m-d')] = 0; // Default nilai 0
}

// Query data dari database
$query = "SELECT DATE(tanggal) as tanggal, SUM(aero) as total_aero 
          FROM novia_abc_user_aero 
          WHERE userid=$userini_id AND tanggal >= '$sa_formatted'
          GROUP BY DATE(tanggal)
          ORDER BY tanggal DESC";
$result = $conn->query($query);

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $tgl = $row['tanggal'];
        $tanggalArray[$tgl] = $row['total_aero']; // Update nilai berdasarkan hasil query
    }
}
// Balik urutan array agar sesuai dengan DESC
$tanggalArray = array_reverse($tanggalArray);
// Tampilkan data dalam tabel
$bulan_aero = 0;
$count = 0;
$sty = "satu";
foreach ($tanggalArray as $tgl => $total_aero) {
    $bulan = new DateTime($tgl);
    $Hari = $bulan->format('l');
    $hari = $HARI[$Hari]; // Konversi nama hari (misalnya ke Bahasa Indonesia)
    $bulan_aero += $total_aero;
    $count ++;
    if ($count > 0) $sty = "satu";
    if ($count > 7) $sty = "dua";
    
    ?>
    <tr class=<?php echo $sty; ?>>
        <td>
            <a href="edit-aeromed.php?id=<?php echo $userini_id; ?>&tgl=<?php echo $tgl; ?>&aero=aero">
            <?php echo $hari; ?>
            </a>
        </td>
        <td align="center">
            <a href="edit-aeromed.php?id=<?php echo $userini_id; ?>&tgl=<?php echo $tgl; ?>&aero=aero">
            <?php echo $tgl; ?>
            </a>
        </td>
        <td align="center"><?php echo $total_aero; ?></td>
        <td></td>
    </tr>
    <?php
}
?>
    <tr class="satu">
        <td>TOTAL</td>
        <td align="center">30 Hari Terakhir</td>
        <td align="center"><?php echo $bulan_aero; ?></td>
        <td></td>
    </tr>
    
    <tr class="satu">
            <td colspan="4"><button style="padding : 10px; width:100%; background-color:#B6B7C1;">Catatan AEROBIK per Bulan</button></td>
    </tr>    
	<tr class="satu">
		<th colspan="2" align="left">Tahun - Bulan</th>
		
		<th>Waktu (Menit)</th>
		<th>Grafik - Statistik</th>
	</tr>
    
    
    <?php
    // Tentukan rentang bulan (misalnya 12 bulan terakhir)
    $tanggal_awal = new DateTime('2025-01-01');
    $tanggal_sekarang = new DateTime(date('Y-m-d'));
    $bulan_terakhir = date('Y-m');
    $bulan_array = [];
    
    // Hitung selisih bulan (tahun * 12 + bulan)
    $selisih_bulan = 
        (($tanggal_sekarang->format('Y') - $tanggal_awal->format('Y')) * 12)
        + ($tanggal_sekarang->format('m') - $tanggal_awal->format('m'));
    
    for ($i = 0; $i <= $selisih_bulan; $i++) {
        $bulan = date('Y-m', strtotime("-$i months", strtotime($bulan_terakhir)));
        $bulan_array[$bulan] = 0; // Default nilai 0
    }
    
    // Query untuk mengambil total aero per bulan
    $query = "SELECT DATE_FORMAT(tanggal, '%Y-%m') as bulan, SUM(aero) as total_aero_bulan
              FROM novia_abc_user_aero
              WHERE userid = $userini_id
              GROUP BY DATE_FORMAT(tanggal, '%Y-%m')";
    
    $result = $conn->query($query);
    
    // Masukkan hasil query ke array bulan
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $bulan_array[$row['bulan']] = $row['total_aero_bulan'];
        }
    }
    
    // Tampilkan semua bulan, termasuk yang totalnya 0
    foreach ($bulan_array as $bulan => $total) {
        ?>
        <tr class="dua">
            <td colspan="2"><?php echo $bulan; ?></td>
            <td align="center"><?php echo $total; ?></td>
            <td></td>
        </tr>
        <?php
    }
    ?>
    
</table>
</body>
</html>