<?php
/**
 * EXAMPLE: Using Database in Your Pages
 * This file shows how to integrate database queries in your PHP pages
 */

// At the top of each PHP file that needs database:
// require_once 'config/database.php';
// require_once 'config/session.php';
// require_once 'config/helpers.php';

?>

<!-- EXAMPLE 1: Display Rooms on rooms.php -->
<?php
/*
require_once 'config/database.php';
require_once 'config/helpers.php';

// Get all available rooms
$rooms = $dbHelper->getAllRooms();
*/
?>

<!-- Then in your HTML: -->
<?php
/*
foreach ($rooms as $room):
    ?>
    <div class="room-card">
        <h3><?php echo htmlspecialchars($room['room_type']); ?></h3>
        <p><?php echo htmlspecialchars($room['description']); ?></p>
        <p>Guests: <?php echo $room['max_guests']; ?></p>
        <p>Price: $<?php echo number_format($room['price_per_night'], 2); ?>/night</p>
        <img src="<?php echo htmlspecialchars($room['image_url']); ?>" alt="">
        <a href="room-details.php?id=<?php echo $room['id']; ?>">View Details</a>
    </div>
    <?php
endforeach;
*/
?>


<!-- EXAMPLE 2: Display News on news.php -->
<?php
/*
require_once 'config/database.php';
require_once 'config/helpers.php';

// Get published news
$news_articles = $dbHelper->getPublishedNews(6);
*/
?>

<?php
/*
foreach ($news_articles as $article):
    ?>
    <article class="news-item">
        <img src="<?php echo htmlspecialchars($article['featured_image']); ?>" alt="">
        <h3><?php echo htmlspecialchars($article['title']); ?></h3>
        <p><?php echo htmlspecialchars($article['summary']); ?></p>
        <span class="date"><?php echo date('F j, Y', strtotime($article['published_at'])); ?></span>
        <a href="news-details.php?slug=<?php echo htmlspecialchars($article['slug']); ?>">Read More</a>
    </article>
    <?php
endforeach;
*/
?>


<!-- EXAMPLE 3: Single News Article on news-details.php -->
<?php
/*
require_once 'config/database.php';
require_once 'config/helpers.php';

$slug = isset($_GET['slug']) ? sanitize($_GET['slug']) : '';
$article = $dbHelper->getNewsBySlug($slug);

if (!$article):
    header("HTTP/1.1 404 Not Found");
    echo "Article not found";
    exit();
endif;
*/
?>

<?php
/*
?>
<article>
    <h1><?php echo htmlspecialchars($article['title']); ?></h1>
    <img src="<?php echo htmlspecialchars($article['featured_image']); ?>" alt="">
    <div class="content">
        <?php echo $article['content']; ?>
    </div>
    <footer>
        <span class="views">Views: <?php echo $article['views']; ?></span>
        <span class="date"><?php echo date('F j, Y', strtotime($article['published_at'])); ?></span>
    </footer>
</article>
<?php
*/
?>


<!-- EXAMPLE 4: Display Facilities on facilities.php -->
<?php
/*
require_once 'config/database.php';
require_once 'config/helpers.php';

$facilities = $dbHelper->getFacilities();
*/
?>

<?php
/*
foreach ($facilities as $facility):
    ?>
    <div class="facility-card">
        <div class="icon">
            <img src="<?php echo htmlspecialchars($facility['icon']); ?>" alt="">
        </div>
        <h3><?php echo htmlspecialchars($facility['name']); ?></h3>
        <p><?php echo htmlspecialchars($facility['description']); ?></p>
    </div>
    <?php
endforeach;
*/
?>


<!-- EXAMPLE 5: Display Gallery on gallery.php -->
<?php
/*
require_once 'config/database.php';
require_once 'config/helpers.php';

// Get all gallery images
$gallery = $dbHelper->getGalleryImages();

// Or filter by category
$rooms_gallery = $dbHelper->getGalleryImages('rooms', 12);
*/
?>

<?php
/*
foreach ($gallery as $image):
    ?>
    <div class="gallery-item">
        <img src="<?php echo htmlspecialchars($image['image_url']); ?>" alt="<?php echo htmlspecialchars($image['title']); ?>">
        <h4><?php echo htmlspecialchars($image['title']); ?></h4>
    </div>
    <?php
endforeach;
*/
?>


<!-- EXAMPLE 6: User Dashboard (after login) -->
<?php
/*
require_once 'config/database.php';
require_once 'config/session.php';
require_once 'config/helpers.php';

// Check if user is logged in
if (!isLoggedIn()) {
    header("Location: login.php");
    exit();
}

$user = getCurrentUser();
$user_id = getCurrentUserId();

// Get user reservations
$reservations = $dbHelper->getUserReservations($user_id);
*/
?>

<div class="user-dashboard">
    <h2>Welcome, <?php echo htmlspecialchars($user['first_name']); ?>!</h2>
    
    <?php
    /*
    if (empty($reservations)):
        ?>
        <p>You have no reservations yet. <a href="rooms.php">Browse rooms</a></p>
        <?php
    else:
        foreach ($reservations as $res):
            ?>
            <div class="reservation-card">
                <img src="<?php echo htmlspecialchars($res['image_url']); ?>" alt="">
                <h3><?php echo htmlspecialchars($res['room_type']); ?> - Room <?php echo htmlspecialchars($res['room_number']); ?></h3>
                <p>Check-in: <?php echo date('M d, Y', strtotime($res['check_in_date'])); ?></p>
                <p>Check-out: <?php echo date('M d, Y', strtotime($res['check_out_date'])); ?></p>
                <p>Status: <span class="status-<?php echo $res['status']; ?>"><?php echo ucfirst($res['status']); ?></span></p>
                <p>Total: $<?php echo number_format($res['total_price'], 2); ?></p>
            </div>
            <?php
        endforeach;
    endif;
    */
    ?>
</div>


<!-- EXAMPLE 7: Add to index.php for Special Offers -->
<?php
/*
require_once 'config/database.php';
require_once 'config/helpers.php';

$offers = $dbHelper->getActiveOffers();
*/
?>

<?php
/*
if (!empty($offers)):
    foreach ($offers as $offer):
        ?>
        <div class="offer-banner">
            <h3><?php echo htmlspecialchars($offer['title']); ?></h3>
            <p><?php echo htmlspecialchars($offer['description']); ?></p>
            <?php if ($offer['discount_type'] === 'percentage'): ?>
                <span class="discount">-<?php echo $offer['discount_value']; ?>%</span>
            <?php else: ?>
                <span class="discount">-$<?php echo number_format($offer['discount_value'], 2); ?></span>
            <?php endif; ?>
        </div>
        <?php
    endforeach;
endif;
*/
?>


<!-- EXAMPLE 8: Update User Profile -->
<?php
/*
require_once 'config/database.php';
require_once 'config/session.php';
require_once 'config/helpers.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isLoggedIn()) {
    $user_id = getCurrentUserId();
    
    $update_data = [
        'first_name' => $_POST['first_name'] ?? '',
        'last_name' => $_POST['last_name'] ?? '',
        'phone' => $_POST['phone'] ?? '',
        'address' => $_POST['address'] ?? '',
        'city' => $_POST['city'] ?? '',
        'country' => $_POST['country'] ?? '',
    ];
    
    if ($dbHelper->updateUserProfile($user_id, $update_data)) {
        echo "Profile updated successfully!";
    }
}
*/
?>


<!-- UTILITY FUNCTIONS -->
<?php
/*
// Sanitize input to prevent XSS
function sanitize($input) {
    return htmlspecialchars(trim($input), ENT_QUOTES, 'UTF-8');
}

// Sanitize array (for POST/GET)
function sanitizeArray($array) {
    $sanitized = [];
    foreach ($array as $key => $value) {
        $sanitized[$key] = sanitize($value);
    }
    return $sanitized;
}

// Format currency
function formatCurrency($amount) {
    return '$' . number_format($amount, 2);
}

// Format date
function formatDate($date, $format = 'F j, Y') {
    return date($format, strtotime($date));
}
*/
?>

<!-- =============================================
     INTEGRATION CHECKLIST
     ============================================= -->

QUICK SETUP CHECKLIST:
[ ] 1. Create database and import SQL file
[ ] 2. Update config/database.php with your credentials
[ ] 3. Add requires to top of pages that need database:
        require_once 'config/database.php';
        require_once 'config/session.php';
        require_once 'config/helpers.php';
[ ] 4. Update form actions to point to API handlers
[ ] 5. Use examples above to display dynamic content
[ ] 6. Test registration and login
[ ] 7. Test making reservations
[ ] 8. Test contact form submissions
[ ] 9. Verify data appears in database
[ ] 10. Deploy to production

API ENDPOINTS AVAILABLE:
- POST api/register-handler.php          (Register new user)
- POST api/login-handler.php              (User login)
- GET  api/get-rooms.php                  (Fetch available rooms)
- GET  api/get-news.php                   (Fetch published news)
- POST api/contact-handler.php            (Submit contact form)
- POST api/reservation-handler.php        (Create reservation)

HELPER FUNCTIONS AVAILABLE:
- getAllRooms()                    (Get all available rooms)
- getRoomById($id)                 (Get single room details)
- getPublishedNews($limit)         (Get news articles)
- getNewsBySlug($slug)             (Get single article)
- getFacilities()                  (Get hotel facilities)
- getGalleryImages($category)      (Get gallery images)
- getActiveOffers()                (Get active promotions)
- checkRoomAvailability($room_id, $check_in, $check_out)
- getUserReservations($user_id)    (Get user bookings)
- getUserById($user_id)            (Get user profile)
- updateUserProfile($user_id, $data) (Update profile)
- getReservationDetails($res_id)   (Get booking details)

For more help, refer to DATABASE_SETUP.md
