<?php
// ============================================
// FILE: comment.php
// PURPOSE: Allow users to add new comments
// ============================================

// Turn on error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

include('mysqli_connect.php');
include('header.php');

// Initialize variables
$fname = $lname = $company = $job_title = $comment = '';
$message = '';
$message_type = '';

// ============================================
// FORM VALIDATION & INSERT
// ============================================
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Sanitize input
    $fname = trim($_POST['fname'] ?? '');
    $lname = trim($_POST['lname'] ?? '');
    $company = trim($_POST['company'] ?? '');
    $job_title = trim($_POST['job_title'] ?? '');
    $comment = trim($_POST['comment'] ?? '');
    
    // Validation - Check for required fields
    $errors = [];
    if (empty($fname)) { 
        $errors[] = "First name is required."; 
    }
    if (empty($lname)) { 
        $errors[] = "Last name is required."; 
    }
    if (empty($comment)) { 
        $errors[] = "Comment/feedback is required."; 
    }
    
    // If no errors, insert into database
    if (empty($errors)) {
        $stmt = $db_connection->prepare(
            "INSERT INTO guestbook (fname, Iname, company, job_title, comment) 
             VALUES (?, ?, ?, ?, ?)"
        );
        
        if ($stmt) {
            $stmt->bind_param("sssss", $fname, $lname, $company, $job_title, $comment);
            
            if ($stmt->execute()) {
                $message = "✅ Thank you! Your comment has been posted successfully.";
                $message_type = "success";
                
                // Clear form fields after successful submission
                $fname = $lname = $company = $job_title = $comment = '';
            } else {
                $message = "❌ Database error: " . $stmt->error;
                $message_type = "error";
            }
            $stmt->close();
        } else {
            $message = "❌ Database error: " . $db_connection->error;
            $message_type = "error";
        }
    } else {
        $message = implode("<br>", $errors);
        $message_type = "error";
    }
}
?>

<div style="max-width: 700px; margin: 0 auto;">
    <h2 style="color: #bf360c; text-align: center;">📝 Join Our Community</h2>
    <p style="text-align: center; color: #8d6e63; margin-bottom: 25px;">
        Share your story and connect with others!
    </p>

    <?php if ($message): ?>
        <div class="alert alert-<?php echo $message_type; ?>">
            <?php echo $message; ?>
        </div>
    <?php endif; ?>

    <!-- ============================================ -->
    <!-- STICKY FORM - Values are retained after errors -->
    <!-- ============================================ -->
    <form action="comment.php" method="post">
        <div class="form-row">
            <div class="form-group">
                <label for="fname">First Name <span class="required">*</span></label>
                <input type="text" id="fname" name="fname" 
                       value="<?php echo htmlspecialchars($fname); ?>" 
                       placeholder="e.g., John" required>
            </div>
            <div class="form-group">
                <label for="lname">Last Name <span class="required">*</span></label>
                <input type="text" id="lname" name="lname" 
                       value="<?php echo htmlspecialchars($lname); ?>" 
                       placeholder="e.g., Doe" required>
            </div>
        </div>

        <div class="form-row">
            <div class="form-group">
                <label for="company">Company / Organization</label>
                <input type="text" id="company" name="company" 
                       value="<?php echo htmlspecialchars($company); ?>" 
                       placeholder="e.g., Google, Microsoft">
            </div>
            <div class="form-group">
                <label for="job_title">Job Title</label>
                <input type="text" id="job_title" name="job_title" 
                       value="<?php echo htmlspecialchars($job_title); ?>" 
                       placeholder="e.g., Senior Developer">
            </div>
        </div>

        <div class="form-group">
            <label for="comment">Your Feedback / Message <span class="required">*</span></label>
            <textarea id="comment" name="comment" 
                      placeholder="Share your experience, favorite sessions, or networking connections..." 
                      required><?php echo htmlspecialchars($comment); ?></textarea>
        </div>

        <div class="form-actions" style="display: flex; gap: 15px; margin-top: 10px;">
            <button type="submit" class="btn">🚀 Submit Registration</button>
            <a href="index.php" class="btn btn-secondary">Cancel</a>
        </div>
    </form>
</div>

<?php
$db_connection->close();
include('footer.php');
?>