<?php
ob_start();
include("configa.php");
session_start();

if (!isset($_SESSION['mobile']) || !isset($_SESSION['otp'])) {
    header('Location: http://localhost/hamisah/frameForAll.php');
    exit;
}

// Ensure session variables are set and handle empty cases
$mobile = $_SESSION['mobile'] ?? '';
$otp = $_SESSION['otp'] ?? '';

$for_feacth = mysqli_query($conn, "SELECT * FROM login_gest WHERE mobile = '" . mysqli_real_escape_string($conn, $mobile) . "'");
$row = mysqli_fetch_object($for_feacth);

$first_name = $row->first_name ?? '';
$last_name = $row->last_name ?? '';
$division = $row->division ?? '';
$district = $row->district ?? '';
$upazila = $row->upazila ?? '';
$up = $row->up ?? '';
$address = $row->address ?? '';

// Check if the form has been submitted and the hidden input value is '1'
$hide = $_POST['hideInput'] ?? null;
?>

<style type="text/css">
    .form-container {
        padding: 50pt;
        width: 70%;
        background-color: white;
    }

    .form-group {
        padding: 10pt;
    }

    .form-group > input, textarea {
        padding: 3pt;
        width: 100%;
    }

    .btn-submit {
        width: 100%;
        background-color: #0284C7;
        padding: 5pt;
        font-family: sutonnyOMJ;
        font-size: 13pt;
        border: solid 0pt;
        color: #fff;
    }

    .btn-submit:hover {
        background-color: #164CF9;
    }
</style>

<?php if (!isset($hide) || $hide !== '1') { ?>

<div class="form-container">
    <div class="heaLine"><h2>ব্যবহারকারীর তথ্য হালনাগাদ করুন</h2></div>

    
        <div id="message" class="message" style="display: none;"></div>
        <form id="updateForm" method="POST">
            <input type="hidden" id="hideInput" name="hideInput" value="<?= htmlspecialchars($hide) ?>">

            <div class="form-group">
                <label for="mobile">মোবাইল (Mobile):</label>
                <input type="tel" id="mobile" name="mobile" value="<?= htmlspecialchars($mobile) ?>" readonly>
            </div>

            <div class="form-group">
                <label for="password">পাসওয়ার্ড (Password):</label>
                <input type="password" id="password" name="otp" placeholder="পাসওয়ার্ড পরিবর্তন না করতে চাইলে খালি রাখুন" value="<?= htmlspecialchars($otp) ?>">
            </div>

            <div class="form-group">
                <label for="first_name">প্রথম নাম (First Name):</label>
                <input type="text" id="first_name" name="first_name" value="<?= htmlspecialchars($first_name) ?>">
            </div>

            <div class="form-group">
                <label for="last_name">শেষ নাম (Last Name):</label>
                <input type="text" id="last_name" name="last_name" value="<?= htmlspecialchars($last_name) ?>">
            </div>

            <div class="form-group">
                <label for="division">বিভাগ (Division):</label>
                <input type="text" id="division" name="division" value="<?= htmlspecialchars($division) ?>">
            </div>

            <div class="form-group">
                <label for="district">জেলা (District):</label>
                <input type="text" id="district" name="district" value="<?= htmlspecialchars($district) ?>">
            </div>

            <div class="form-group">
                <label for="upazila">উপজেলা (Upazila):</label>
                <input type="text" id="upazila" name="upazila" value="<?= htmlspecialchars($upazila) ?>">
            </div>

            <div class="form-group">
                <label for="up">ইউপি (UP):</label>
                <input type="text" id="up" name="up" value="<?= htmlspecialchars($up) ?>">
            </div>

            <div class="form-group">
                <label for="address">ঠিকানা (Address):</label>
                <textarea id="address" name="address" rows="4"><?= htmlspecialchars($address) ?></textarea>
            </div>

            <button type="submit" class="btn-submit">তথ্য হালনাগাদ করুন</button>
        </form>

</div>

<?php } else { ?>
    <p>আপনার তথ্য সফলভাবে হালনাগাদ করা হয়েছে।</p>    
<?php } ?>

<script>
document.getElementById('updateForm').addEventListener('submit', function(event) {
    event.preventDefault();

    const form = event.target;
    const formData = new FormData(form);
    const messageDiv = document.getElementById('message');
    const heaLineDiv = document.querySelector('.heaLine'); // এখানে .heaLine ক্লাসটি সিলেক্ট করা হয়েছে
    
    messageDiv.style.display = 'none';

    fetch('update_user.php', {
        method: 'POST',
        body: formData
    })
    .then(response => response.json())
    .then(data => {
        messageDiv.textContent = data.message;
        messageDiv.style.display = 'block';
        if (data.status === 'success') {
            document.getElementById('hideInput').value = '1';
            document.getElementById('updateForm').style.display = 'none';

            messageDiv.classList.remove('error');
            messageDiv.classList.add('success');
            
            messageDiv.style.height = '4in';
            messageDiv.style.marginTop = '40%'; 
            
            if (heaLineDiv) { // চেক করা হচ্ছে যেন error না আসে
                heaLineDiv.style.display = 'none'; // div টি লুকানো হচ্ছে
            }
            
        } else {
            messageDiv.classList.remove('success');
            messageDiv.classList.add('error');
        }
    })
    .catch(error => {
        messageDiv.textContent = 'একটি ত্রুটি ঘটেছে। অনুগ্রহ করে আবার চেষ্টা করুন।';
        messageDiv.style.display = 'block';
        messageDiv.classList.remove('success');
        messageDiv.classList.add('error');
        console.error('Error:', error);
    });
});
</script>

<?php
// End capturing the output and store it in a variable
$orderPage = ob_get_clean();

// Now include frameForAll.php, which will use $catagory_display_content
include("frameForAll.php");

// mysqli_close($conn); // You might want to close the connection after all database operations
ob_end_flush();
?>