<?php
session_start();
require_once '../config.php';

requireAdmin();

$page_title = 'Website Content';

// Ensure website_content table exists
$conn->query("CREATE TABLE IF NOT EXISTS website_content (
    id INT PRIMARY KEY AUTO_INCREMENT,
    homepage_title VARCHAR(255) NOT NULL DEFAULT 'HealthLink',
    homepage_description TEXT,
    about_us TEXT,
    contact_email VARCHAR(100),
    contact_phone VARCHAR(20),
    contact_address TEXT,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)");

// Get website content
$website_content = $conn->query("SELECT * FROM website_content WHERE id = 1")->fetch_assoc();

if (!$website_content) {
    // Initialize default content
    $conn->query("INSERT INTO website_content (id, homepage_title, homepage_description, about_us, contact_email, contact_phone, contact_address) VALUES (1, 'HealthLink', 'Your Health, Our Priority', '', '', '', '')");
    $website_content = $conn->query("SELECT * FROM website_content WHERE id = 1")->fetch_assoc();
}

// Handle update
$message = '';
$alert_type = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $homepage_title = clean($_POST['homepage_title'] ?? '');
    $homepage_description = clean($_POST['homepage_description'] ?? '');
    $about_us = clean($_POST['about_us'] ?? '');
    $contact_email = clean($_POST['contact_email'] ?? '');
    $contact_phone = clean($_POST['contact_phone'] ?? '');
    $contact_address = clean($_POST['contact_address'] ?? '');

    $stmt = $conn->prepare("UPDATE website_content SET homepage_title = ?, homepage_description = ?, about_us = ?, contact_email = ?, contact_phone = ?, contact_address = ? WHERE id = 1");
    $stmt->bind_param("ssssss", $homepage_title, $homepage_description, $about_us, $contact_email, $contact_phone, $contact_address);

    if ($stmt->execute()) {
        $message = 'Website content updated successfully!';
        $alert_type = 'success';
        $website_content = $conn->query("SELECT * FROM website_content WHERE id = 1")->fetch_assoc();
    } else {
        $message = 'Error updating content: ' . $stmt->error;
        $alert_type = 'danger';
    }
    $stmt->close();
}

include 'header.php';
?>

                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2">
                        <i class="fas fa-globe text-primary"></i> Website Content Management
                    </h1>
                </div>

                <?php if ($message): ?>
                    <div class="alert alert-<?php echo $alert_type; ?> alert-dismissible fade show" role="alert">
                        <?php echo htmlspecialchars($message); ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                <?php endif; ?>

                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-header bg-light">
                                <h5 class="card-title mb-0">
                                    <i class="fas fa-edit"></i> Edit Website Content
                                </h5>
                            </div>
                            <div class="card-body">
                                <form method="POST">
                                    <div class="mb-3">
                                        <label for="homepage_title" class="form-label">Homepage Title</label>
                                        <input type="text" class="form-control" id="homepage_title" name="homepage_title" value="<?php echo htmlspecialchars($website_content['homepage_title']); ?>" required>
                                    </div>

                                    <div class="mb-3">
                                        <label for="homepage_description" class="form-label">Homepage Description</label>
                                        <textarea class="form-control" id="homepage_description" name="homepage_description" rows="3"><?php echo htmlspecialchars($website_content['homepage_description']); ?></textarea>
                                    </div>

                                    <div class="mb-3">
                                        <label for="about_us" class="form-label">About Us</label>
                                        <textarea class="form-control" id="about_us" name="about_us" rows="5"><?php echo htmlspecialchars($website_content['about_us']); ?></textarea>
                                    </div>

                                    <div class="row">
                                        <div class="col-md-6">
                                            <div class="mb-3">
                                                <label for="contact_email" class="form-label">Contact Email</label>
                                                <input type="email" class="form-control" id="contact_email" name="contact_email" value="<?php echo htmlspecialchars($website_content['contact_email']); ?>">
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-3">
                                                <label for="contact_phone" class="form-label">Contact Phone</label>
                                                <input type="text" class="form-control" id="contact_phone" name="contact_phone" value="<?php echo htmlspecialchars($website_content['contact_phone']); ?>">
                                            </div>
                                        </div>
                                    </div>

                                    <div class="mb-3">
                                        <label for="contact_address" class="form-label">Contact Address</label>
                                        <textarea class="form-control" id="contact_address" name="contact_address" rows="3"><?php echo htmlspecialchars($website_content['contact_address']); ?></textarea>
                                    </div>

                                    <button type="submit" class="btn btn-primary">
                                        <i class="fas fa-save"></i> Save Changes
                                    </button>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>

<?php include 'footer.php'; ?>
