<?php
    include('../loginsession.php');
    error_reporting(E_ALL);
    $filename = basename(__FILE__);
    $save = mysqli_query($conn, "INSERT INTO novia_abc_user_logs (userid,filename) VALUES ('$userini_id','$filename')");
//-------------
$today = date("Y-m-d");
$Today = new DateTime($today);
$Today_dMY = $Today->format('d M Y');
$Hari = $Today->format('l');
$hari = $HARI[$Hari];
//-------------
    $ITEM = array();
    $sql3 = "SELECT * FROM novia_abc_routine_item ORDER BY urutan ASC";
        $result3 = $conn->query($sql3);
        if ($result3->num_rows > 0) {
            while($row3 = $result3->fetch_assoc()) {
                $ITEM[$row3['id']] = $row3['item'];
                //---
                $Itemid = $row3['id'];
                $Itemidcount = 0;
                
                $sql5 = "SELECT COUNT(*) as count FROM novia_abc_routine WHERE date = '$today' AND pdaid = '$Itemid' AND userid = '$userini_id'";
                $result5 = $conn->query($sql5);
                if ($result5->num_rows > 0) {
                    while($row5 = $result5->fetch_assoc()) {
                		$Itemidcount = $row5['count'];
                        if ($Itemidcount == 0){
                            $save = mysqli_query($conn, "INSERT INTO novia_abc_routine (pdaid, userid) VALUES ($Itemid, $userini_id)");
                            header("Location: routine.php");
                        }                		
                    }
                }
            }
        }

//-------------    
    $sql = "SELECT * FROM novia_abc_routine WHERE date = '$today' AND userid = '$userini_id' ORDER BY id ASC";
    $result = $conn->query($sql);

        // Mengubah status rutinitas
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            $id = $_POST['id'];
            $status = $_POST['status'];
            $new_status = ($status == 0) ? 1 : 0;
        
            $update_sql = "UPDATE novia_abc_routine SET status='$new_status' WHERE id=$id";
            $conn->query($update_sql);
            header("Location: routine.php");
        }
        
        // Mengubah status rutin berdasarkan tanggal
        $date_sql = "UPDATE novia_abc_routine SET status=0 WHERE date != CURDATE() AND userid = '$userini_id'";
        $conn->query($date_sql);
//-------------
    
?>


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

	<style>

        body {
            color : #fff;
            padding:8px;
            background-image : url('../images/bg_brown_04.jpg');
            }    

		img {
            width: 100%;
            max-width: 500px;
            height: auto;
        }
        h2 {
            color:#B61A1A;
            text-align: center;
            font-family: "Times New Roman";
            text-shadow:1px 1px 0px #CCC;
        }

	    .head th {
        	padding:10px;
        	color:#333;
        	text-shadow:1px 1px 0px #CCC;
        	font-size:14px;
        }
        .container {
            width: 90%;
            margin: 0 auto;
            padding: 10px;
            color:#eee;
            /*background-color: #f0f0f0;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);*/
            display: flex;
            justify-content: center;
        }
        .tiga {
            background-color:#333;
        }
    </style>	
</head>
<body>
    
    <table width="100%" cellpadding="5">
		<tr class="dua">
		    <td><a href="../administrasi.php" title="Navigasi"><button style="padding : 10px; width:100%; background-color:#A6B7C1;"> NAVIGASI </button></a></td>
		    <td><a href="../aero/meditation.php" title="User Log"><button style="padding : 10px; width:100%; background-color:#A6B7C1;"> MEDITATION </button></a></td>
			<td><a href="../aero/aero_main.php" title="Aerobik"><button style="padding : 10px; width:100%; background-color:#A6B7C1;">AEROBIK</button></a></td>
		</tr>
	</table>
	<br>
<h2><?php echo "Rutinitas Harian<br>".$hari." ".$Today_dMY; ?></h2>
<div class="container">
    <table border="1"  cellpadding="5">
        <tr class="head">
            <th width="160">Rutinitas Harian</th>
            <th width="90">Status</th>
        </tr>
        <?php if ($result->num_rows > 0) : ?>
            <?php while ($row = $result->fetch_assoc()) : ?>
                <tr class="tiga">
                    
                    <?php
                    $item = $row['pdaid'];
                    $Item = $ITEM[$item];
                    ?>
                    
                    <td><a style="font-size:15px;"><?= $Item ?></a></td>
                    
                    <td align="center">
                        <?php if($row['status']==0){ ?>
                        <form method="POST">
                            <input type="hidden" name="id" value="<?= $row['id'] ?>">
                            <input type="hidden" name="status" value="<?= $row['status'] ?>">
                            <button type="submit"><a style="font-size:13px;">Belum</a></button>
                        </form>
                        <?php } ?>
                        <?php if($row['status']==1){ ?>
                            <input type="hidden" name="id" value="<?= $row['id'] ?>">
                            <input type="hidden" name="status" value="<?= $row['status'] ?>">
                            <button><a style="font-size:13px;">√</a></button>
                        <?php } ?>
                        
                    </td>
                </tr>
            <?php endwhile; ?>
        <?php endif; ?>
    </table>    
</div>
<?php
if($userini_id == 3){
    ?>
    <table width="100%" cellpadding="5">
		<tr class="dua">
		    <td><a href="#" title=""><button style="padding : 10px; width:100%; background-color:#819CA9;"> Kosong </button></a></td>
		    <td><a href="routine_edit.php" title=""><button style="padding : 10px; width:100%; background-color:#546E7A;">EDIT</button></a></td>
		</tr>
	</table>
<?php
}
?>
</body>