<?php
session_start();
require 'function.php';
require 'config.php';
$id = $_SESSION['id'];

if (isset($_POST['form_update'])) {
    $lama = $_POST['password'];
    $newpass = md5($lama);
    mysqli_query($koneksi, "UPDATE `users_id` SET password='$newpass' WHERE id = '$id'");
    echo '<script language="JavaScript">document.location="?info=valid"</script>';
}

if (isset($_POST['form_update_wa'])) {
    $pesan = mysqli_real_escape_string($koneksi, $_POST['pesan']);
    mysqli_query($koneksi, "UPDATE `users_id` SET whatsapp='$pesan' WHERE id = '$id'");
    echo '<script language="JavaScript">document.location="?info=valid"</script>';
}

$user_data_res = mysqli_query($koneksi, "SELECT * FROM users_id WHERE id = '$id'");
$user_data = mysqli_fetch_assoc($user_data_res);
$pesan = $user_data['whatsapp'];

require 'header.php';
?>

<div class="container py-4">
    <div class="row mb-4">
        <div class="col-md-12">
             <h3 class="fw-bold text-gradient d-inline-block">User Profile</h3>
             <p class="text-muted">Manage your personal settings and invitation message.</p>
        </div>
    </div>

    <div class="row g-4 justify-content-center">
        <?php if (isset($_SESSION['role']) && $_SESSION['role'] == 'admin') : ?>
        <!-- Default Message -->
        <div class="col-lg-6">
            <div class="glass-card h-100">
                <h5 class="fw-bold mb-3"><i class="bi bi-whatsapp"></i> Default Message Text</h5>
                <p class="small text-muted mb-4">Set your default invitation message for all events.</p>
                <form action="" method="POST">
                    <div class="mb-3">
                        <label class="form-label">Default Message Text</label>
                        <?php $fallback_text = "Terima kasih banyak atas kehadiran Bapak/Ibu/Saudara/i [nama] di Hari Bahagia Kami, pada hari [tgl] jam [jam].\nTerima kasih atas doa restu terbaik yang Anda berikan kepada Kami dan Keluarga."; ?>
                        <textarea class="form-control" name="pesan" rows="6" placeholder="<?= htmlspecialchars($fallback_text) ?>"><?= htmlspecialchars($pesan ?? '') ?></textarea>
                        <div class="form-text small">Gunakan <code>[nama]</code> untuk Nama Tamu, <code>[tgl],[jam]</code> untuk tanggal dan waktu hadir, <code>[link-undangan]</code> untuk Link Undangan.</div>
                    </div>
                    <input type="hidden" name="form_update_wa" value="1" />
                    <button class="btn btn-primary w-100" type="submit">Update Default Message</button>
                </form>
            </div>
        </div>
        <?php endif; ?>

        <!-- Change Password -->
        <div class="col-lg-6">
            <div class="glass-card h-100">
                <h5 class="fw-bold mb-3"><i class="bi bi-shield-lock"></i> Change Password</h5>
                <p class="small text-muted mb-4">Ensure your account is secure by using a strong password.</p>
                <form action="" method="POST">
                    <div class="mb-3">
                        <label class="form-label">New Password</label>
                        <div class="input-group">
                            <input type="password" class="form-control" name="password" id="profilePassword" placeholder="Enter new password" required>
                            <button class="btn btn-outline-secondary" type="button" onclick="togglePasswordVisibility('profilePassword', 'eyeIconProfile')">
                                <i class="bi bi-eye-slash" id="eyeIconProfile"></i>
                            </button>
                        </div>
                    </div>
                    <input type="hidden" name="form_update" value="1" />
                    <button class="btn btn-primary w-100" type="submit">Update Password</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    function togglePasswordVisibility(inputId, iconId) {
        const passwordInput = document.getElementById(inputId);
        const icon = document.getElementById(iconId);
        if (passwordInput.type === 'password') {
            passwordInput.type = 'text';
            icon.classList.remove('bi-eye-slash');
            icon.classList.add('bi-eye');
        } else {
            passwordInput.type = 'password';
            icon.classList.remove('bi-eye');
            icon.classList.add('bi-eye-slash');
        }
    }
</script>

<?php

if (isset($_GET['info']) && $_GET['info'] == 'valid')
    echo '<script>swal("Success", "Profile updated successfully!", "success");</script>';

if (isset($_GET['info']) && $_GET['info'] == 'expired')
    echo '<script>swal("Access Denied", "Your subscription is inactive or has expired. Please contact Admin.", "warning");</script>';

?>

</body>
</html>