<?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')");
    
    if(isset($_GET['id'])){
        $id = $_GET['id'];
    }
    if(isset($_GET['check'])){
        $check = $_GET['check'];
    }
    $query = mysqli_query($conn, "SELECT * FROM novia_abc_diary where id='$id'");
    $userData = mysqli_fetch_array($query);
    $note = $userData['note'];
    $TglJam = $userData['tanggal'];
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <title>CATATAN PERSONAL</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes">
    <link href="../images/balance.ico" rel="icon" type="image/icon" />
    <!-- SweetAlert2 CSS dan JS -->
    <link href="https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>    
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            padding: 20px;
            min-height: 100vh;
        }

        /* Container utama */
        .container {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            overflow: hidden;
            animation: slideIn 0.5s ease-out;
        }

        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateY(-30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        /* Header */
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            padding: 30px 20px;
            text-align: center;
        }

        .header h1 {
            color: white;
            font-size: 2.5em;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
            margin-bottom: 5px;
        }

        .header h2 {
            color: rgba(255,255,255,0.9);
            font-size: 1.3em;
            font-weight: normal;
        }

        .header h3 {
            color: rgba(255,255,255,0.8);
            font-size: 1em;
            margin-top: 10px;
            font-weight: normal;
        }

        /* Konten */
        .content {
            padding: 30px;
        }

        /* Tombol navigasi */
        .nav-buttons {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-bottom: 30px;
        }

        .btn {
            padding: 12px 20px;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            text-align: center;
            display: inline-block;
            text-decoration: none;
        }

        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102,126,234,0.4);
        }

        .btn-secondary {
            background: #f0f0f0;
            color: #333;
        }

        .btn-secondary:hover {
            background: #e0e0e0;
            transform: translateY(-2px);
        }

        .btn-danger {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
        }

        .btn-danger:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(245,87,108,0.4);
        }

        /* Form */
        .form-group {
            margin-bottom: 25px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #333;
        }

        textarea {
            width: 100%;
            padding: 15px;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            font-size: 14px;
            font-family: inherit;
            resize: vertical;
            transition: all 0.3s ease;
        }

        textarea:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102,126,234,0.1);
        }

        input[type="text"] {
            width: 100%;
            padding: 12px 15px;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            font-size: 14px;
            transition: all 0.3s ease;
        }

        input[type="text"]:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102,126,234,0.1);
        }

        .form-row {
            display: grid;
            grid-template-columns: 1fr auto;
            gap: 15px;
            align-items: end;
        }

        /* Hapus item section */
        .delete-section {
            margin-top: 30px;
            padding-top: 30px;
            border-top: 2px solid #f0f0f0;
            text-align: center;
        }

        .delete-item {
            display: inline-flex;
            flex-direction: column;
            align-items: center;
            text-decoration: none;
            transition: all 0.3s ease;
        }

        .delete-item:hover {
            transform: scale(1.05);
        }

        .delete-item img {
            width: 80px;
            height: 80px;
            margin-bottom: 10px;
        }

        .delete-text {
            color: #f5576c;
            font-weight: 600;
            font-size: 14px;
        }

        /* Responsive */
        @media (max-width: 768px) {
            body {
                padding: 10px;
            }

            .content {
                padding: 20px;
            }

            .header h1 {
                font-size: 1.8em;
            }

            .header h2 {
                font-size: 1.1em;
            }

            .nav-buttons {
                grid-template-columns: 1fr;
                gap: 10px;
            }

            .form-row {
                grid-template-columns: 1fr;
                gap: 10px;
            }

            .btn {
                padding: 10px 15px;
                font-size: 14px;
            }
        }

        @media (max-width: 480px) {
            .header {
                padding: 20px 15px;
            }

            .header h1 {
                font-size: 1.5em;
            }

            .content {
                padding: 15px;
            }

            textarea {
                padding: 10px;
                font-size: 13px;
            }

            input[type="text"] {
                padding: 10px;
                font-size: 13px;
            }
        }

        /* Animasi hover */
        .btn, .delete-item {
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        }

        /* Loading effect */
        .btn:active {
            transform: scale(0.98);
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="header">
            <h1>📔 CATATAN</h1>
            <h2>HARIAN</h2>
            <h3>✏️ Edit & Hapus Catatan</h3>
        </div>

        <div class="content">
            <!-- Tombol navigasi -->
            <div class="nav-buttons">
                <a href="../index.php" class="btn btn-secondary">🏡 HOME</a>
                <a href="../administrasi.php" class="btn btn-secondary">🏠 PUSAT NAVIGASI</a>
                <a href="index.php?check=<?php echo $check; ?>" class="btn btn-primary">↩️ BATALKAN EDIT</a>
            </div>

            <!-- Form edit -->
            <form method="post" action="">
                <div class="form-group">
                    <label>📝 Isi Catatan</label>
                    <textarea name="note" rows="6" placeholder="Tulis catatan Anda di sini..."><?php echo htmlspecialchars($note); ?></textarea>
                </div>

                <div class="form-row">
                    <div style="flex:1">
                        <label>📅 Tanggal & Waktu</label>
                        <input type="text" name="TglJam" value="<?php echo htmlspecialchars($TglJam); ?>" placeholder="YYYY-MM-DD HH:MM:SS">
                    </div>
                    <div>
                        <button type="submit" name="submit" class="btn btn-primary">💾 SIMPAN PERUBAHAN</button>
                    </div>
                </div>
            </form>

            <!-- Hapus item -->
            <br>
            <br>
            <br>
            <br>
            <br>
            <br>
            <br>
            <br>
            <br>
            <br>
            <div class="delete-section">
                <a href="javascript:void(0);" onclick="confirmDeleteSweet(<?php echo $id;?>, '<?php echo $check;?>')" class="delete-item">
                    <img src="../images/greenmap.png" alt="Hapus catatan">
                    <span class="delete-text">🗑️ HAPUS CATATAN</span>
                </a>
            </div>
            <br>
            <br>
            <br>
            
        </div>
    </div>

    <?php    
    if (isset($_POST['submit'])) {
        $note1 = mysqli_real_escape_string($conn, $_POST['note']);
        $TglJam1 = mysqli_real_escape_string($conn, $_POST['TglJam']);
        $save = mysqli_query($conn, "UPDATE novia_abc_diary SET note='$note1', tanggal='$TglJam1' where id='$id'");
        
        if($save && $check == "0"){
            echo "<script>document.location='index.php?check=0'</script>";
        }
        if($save && $check == "1"){
            echo "<script>document.location='index.php?check=1'</script>";
        }
    }
    ?>
    
    <script>
    function confirmDeleteSweet(id, check) {
        Swal.fire({
            title: 'Apakah Anda yakin?',
            text: "Catatan yang dihapus tidak dapat dikembalikan lagi!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#d33',
            cancelButtonColor: '#3085d6',
            confirmButtonText: 'Ya, hapus!',
            cancelButtonText: 'Batal',
            reverseButtons: true
        }).then((result) => {
            if (result.isConfirmed) {
                // Jika user klik Ya, redirect ke halaman hapus
                window.location.href = "note_hapus.php?id=" + id + "&check=" + check;
            }
            // Jika user klik Batal, tidak melakukan apa-apa (tetap di halaman)
        });
    }
    </script>

</body>
</html>