<?php
// Initialize the session
session_start();
 
// Check if the user is logged in, if not then redirect to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
    header("location: login.php");
    exit;
}

// Include config file
require_once "includes/config.php";

// Initialize variables
$image = [];
$error = '';

// Check if ID parameter exists
if(isset($_GET["id"]) && !empty(trim($_GET["id"]))){
    // Prepare a select statement
    $sql = "SELECT g.*, c.name as category_name 
            FROM gallery_images g 
            LEFT JOIN gallery_categories c ON g.category_id = c.id 
            WHERE g.id = ?";
    
    if($stmt = $conn->prepare($sql)){
        // Bind variables to the prepared statement as parameters
        $stmt->bind_param("i", $param_id);
        
        // Set parameters
        $param_id = trim($_GET["id"]);
        
        // Attempt to execute the prepared statement
        if($stmt->execute()){
            $result = $stmt->get_result();
            
            if($result->num_rows == 1){
                $image = $result->fetch_assoc();
            } else{
                // URL doesn't contain valid id parameter. Redirect to error page
                header("location: error.php");
                exit();
            }
            
        } else{
            $error = "Oops! Something went wrong. Please try again later.";
        }
    }
    
    // Close statement
    $stmt->close();
    
    // Close connection
    $conn->close();
} else{
    // URL doesn't contain id parameter. Redirect to error page
    header("location: error.php");
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Image - Real Estate Admin</title>
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome 6 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary-color: #4361ee;
            --secondary-color: #3f37c9;
            --dark-color: #1a1a2e;
            --light-color: #f8f9fa;
            --success-color: #4cc9f0;
            --danger-color: #f72585;
        }

        body {
            background-color: #f5f7fb;
            color: #4a4a4a;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        /* Sidebar Styles */
        .sidebar {
            background: linear-gradient(180deg, var(--dark-color) 0%, #16213e 100%);
            min-height: 100vh;
            box-shadow: 2px 0 10px rgba(0,0,0,0.1);
            transition: all 0.3s;
        }

        .sidebar .nav-link {
            color: rgba(255, 255, 255, 0.8);
            padding: 12px 20px;
            margin: 4px 10px;
            border-radius: 8px;
            transition: all 0.3s;
        }

        .sidebar .nav-link:hover,
        .sidebar .nav-link.active {
            background: rgba(255, 255, 255, 0.1);
            color: white;
            transform: translateX(5px);
        }

        .sidebar .nav-link i {
            width: 24px;
            text-align: center;
            margin-right: 10px;
        }

        /* Main Content */
        .main-content {
            padding: 2rem;
        }

        /* Header */
        .page-header {
            background: white;
            padding: 1.5rem;
            border-radius: 12px;
            box-shadow: 0 2px 15px rgba(0,0,0,0.05);
            margin-bottom: 2rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 1rem;
        }

        .page-title {
            font-weight: 700;
            color: var(--dark-color);
            margin: 0;
        }

        /* Image Container */
        .image-container {
            background: white;
            border-radius: 12px;
            box-shadow: 0 2px 15px rgba(0,0,0,0.05);
            overflow: hidden;
            margin-bottom: 2rem;
        }

        .image-preview {
            width: 100%;
            max-height: 60vh;
            object-fit: contain;
            background-color: #f8f9fa;
            padding: 1rem;
        }

        .image-details {
            padding: 2rem;
        }

        .image-title {
            font-size: 1.75rem;
            font-weight: 700;
            margin-bottom: 1rem;
            color: var(--dark-color);
        }

        .image-meta {
            margin-bottom: 1.5rem;
        }

        .meta-item {
            display: flex;
            margin-bottom: 0.75rem;
            align-items: center;
        }

        .meta-label {
            font-weight: 600;
            color: #6c757d;
            min-width: 100px;
        }

        .meta-value {
            color: #212529;
        }

        .category-badge {
            background: var(--primary-color);
            color: white;
            padding: 0.35rem 0.75rem;
            border-radius: 50px;
            font-size: 0.875rem;
            font-weight: 500;
            display: inline-block;
        }

        .image-description {
            margin-top: 1.5rem;
            padding-top: 1.5rem;
            border-top: 1px solid #e9ecef;
            color: #495057;
            line-height: 1.7;
        }

        .action-buttons {
            display: flex;
            gap: 1rem;
            margin-top: 2rem;
            flex-wrap: wrap;
        }

        .btn-edit {
            background-color: var(--primary-color);
            border: none;
            padding: 0.5rem 1.25rem;
            border-radius: 8px;
            color: white;
            font-weight: 500;
            transition: all 0.3s;
        }

        .btn-edit:hover {
            background-color: var(--secondary-color);
            transform: translateY(-2px);
            color: white;
        }

        .btn-delete {
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            padding: 0.5rem 1.25rem;
            border-radius: 8px;
            color: #dc3545;
            font-weight: 500;
            transition: all 0.3s;
        }

        .btn-delete:hover {
            background-color: #f1f3f5;
            transform: translateY(-2px);
            color: #dc3545;
        }

        .back-link {
            color: #6c757d;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            margin-bottom: 1.5rem;
            transition: color 0.2s;
        }

        .back-link:hover {
            color: var(--primary-color);
        }

        .back-link i {
            margin-right: 0.5rem;
        }

        /* Responsive Adjustments */
        @media (max-width: 768px) {
            .image-preview {
                max-height: 50vh;
            }
            
            .page-header {
                flex-direction: column;
                align-items: flex-start;
            }
        }

        @media (max-width: 576px) {
            .main-content {
                padding: 1rem;
            }
            
            .image-details {
                padding: 1.5rem;
            }
            
            .image-title {
                font-size: 1.5rem;
            }
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- Sidebar -->
            <div class="col-md-3 col-lg-2 px-0 sidebar d-md-block">
                <div class="d-flex flex-column flex-shrink-0 p-3 h-100">
                    <a href="index.php" class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-white text-decoration-none">
                        <span class="fs-4">RealEstate Pro</span>
                    </a>
                    <hr>
                    <ul class="nav nav-pills flex-column mb-auto">
                        <li class="nav-item">
                            <a href="index.php" class="nav-link">
                                <i class="fas fa-tachometer-alt me-2"></i>
                                Dashboard
                            </a>
                        </li>
                        <li>
                            <a href="properties.php" class="nav-link">
                                <i class="fas fa-home me-2"></i>
                                Properties
                            </a>
                        </li>
                        <li>
                            <a href="gallery.php" class="nav-link active">
                                <i class="fas fa-images me-2"></i>
                                Gallery
                            </a>
                        </li>
                        <li>
                            <a href="categories.php" class="nav-link">
                                <i class="fas fa-tags me-2"></i>
                                Categories
                            </a>
                        </li>
                        <li>
                            <a href="users.php" class="nav-link">
                                <i class="fas fa-users me-2"></i>
                                Users
                            </a>
                        </li>
                        <li>
                            <a href="settings.php" class="nav-link">
                                <i class="fas fa-cog me-2"></i>
                                Settings
                            </a>
                        </li>
                    </ul>
                    <hr>
                    <div class="dropdown">
                        <a href="#" class="d-flex align-items-center text-white text-decoration-none dropdown-toggle" id="dropdownUser1" data-bs-toggle="dropdown" aria-expanded="false">
                            <img src="https://ui-avatars.com/api/?name=<?php echo urlencode($_SESSION['username'] ?? 'Admin'); ?>&background=4361ee&color=fff" alt="User" width="32" height="32" class="rounded-circle me-2">
                            <strong><?php echo htmlspecialchars($_SESSION['username'] ?? 'Admin'); ?></strong>
                        </a>
                        <ul class="dropdown-menu dropdown-menu-dark text-small shadow" aria-labelledby="dropdownUser1">
                            <li><a class="dropdown-item" href="profile.php">Profile</a></li>
                            <li><a class="dropdown-item" href="settings.php">Settings</a></li>
                            <li><hr class="dropdown-divider"></li>
                            <li><a class="dropdown-item" href="logout.php">Sign out</a></li>
                        </ul>
                    </div>
                </div>
            </div>

            <!-- Main Content -->
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 main-content">
                <!-- Back Link -->
                <a href="gallery.php" class="back-link">
                    <i class="fas fa-arrow-left"></i> Back to Gallery
                </a>

                <!-- Page Header -->
                <div class="page-header">
                    <h1 class="page-title">Image Details</h1>
                    <div class="action-buttons">
                        <a href="gallery-edit.php?id=<?php echo $image['id']; ?>" class="btn btn-edit">
                            <i class="fas fa-edit me-2"></i>Edit Image
                        </a>
                        <a href="gallery.php?action=delete&id=<?php echo $image['id']; ?>" class="btn btn-delete" 
                           onclick="return confirm('Are you sure you want to delete this image? This action cannot be undone.');">
                            <i class="fas fa-trash me-2"></i>Delete Image
                        </a>
                    </div>
                </div>

                <?php if(!empty($error)): ?>
                    <div class="alert alert-danger alert-dismissible fade show" role="alert">
                        <div class="d-flex align-items-center">
                            <i class="fas fa-exclamation-circle me-2"></i>
                            <?php echo $error; ?>
                        </div>
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                <?php endif; ?>

                <div class="image-container">
                    <div class="row g-0">
                        <!-- Image Preview -->
                        <div class="col-lg-8">
                            <img src="../<?php echo htmlspecialchars($image['image_path']); ?>" alt="<?php echo htmlspecialchars($image['title'] ?? 'Gallery Image'); ?>" class="img-fluid image-preview">
                        </div>
                        
                        <!-- Image Details -->
                        <div class="col-lg-4">
                            <div class="image-details">
                                <h2 class="image-title"><?php echo htmlspecialchars($image['title'] ?? 'Untitled'); ?></h2>
                                
                                <div class="image-meta">
                                    <?php if(!empty($image['category_name'])): ?>
                                    <div class="meta-item">
                                        <span class="meta-label">Category:</span>
                                        <span class="meta-value">
                                            <span class="category-badge"><?php echo htmlspecialchars($image['category_name']); ?></span>
                                        </span>
                                    </div>
                                    <?php endif; ?>
                                    
                                    <?php if(!empty($image['uploaded_at'])): ?>
                                    <div class="meta-item">
                                        <span class="meta-label">Uploaded:</span>
                                        <span class="meta-value">
                                            <?php echo date('F j, Y', strtotime($image['uploaded_at'])); ?>
                                        </span>
                                    </div>
                                    <?php endif; ?>
                                    
                                    <?php if(!empty($image['file_size'])): 
                                        $file_size = $image['file_size'] < 1024 ? $image['file_size'] . ' B' : (($image['file_size'] < 1048576) ? round($image['file_size'] / 1024, 1) . ' KB' : round($image['file_size'] / 1048576, 1) . ' MB');
                                    ?>
                                    <div class="meta-item">
                                        <span class="meta-label">File Size:</span>
                                        <span class="meta-value"><?php echo $file_size; ?></span>
                                    </div>
                                    <?php endif; ?>
                                    
                                    <?php if(!empty($image['dimensions'])): ?>
                                    <div class="meta-item">
                                        <span class="meta-label">Dimensions:</span>
                                        <span class="meta-value"><?php echo htmlspecialchars($image['dimensions']); ?></span>
                                    </div>
                                    <?php endif; ?>
                                </div>
                                
                                <?php if(!empty($image['description'])): ?>
                                <div class="image-description">
                                    <h5>Description</h5>
                                    <p><?php echo nl2br(htmlspecialchars($image['description'])); ?></p>
                                </div>
                                <?php endif; ?>
                                
                                <div class="mt-4">
                                    <a href="../<?php echo htmlspecialchars($image['image_path']); ?>" class="btn btn-outline-primary" download>
                                        <i class="fas fa-download me-2"></i>Download
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </main>
        </div>
    </div>

    <!-- Bootstrap Bundle with Popper -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Enable tooltips
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[title]'));
        var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
            return new bootstrap.Tooltip(tooltipTriggerEl);
        });
    </script>
</body>
</html>
