{% extends 'list-crud.html' %}
{% load static %}

{% block extra_styles %}
    <link rel="stylesheet" href="{% static 'styles/details.css' %}">
     <style>
        .card-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .modal {
            display: none;
            position: fixed;
            z-index: 1000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.9);
        }

        .modal-content {
            position: relative;
            margin: auto;
            padding: 20px;
            width: 90%;
            max-width: 1200px;
            height: 90vh;
            display: flex;
            flex-direction: column;
        }

        .close-modal {
            color: #fff;
            position: absolute;
            right: 25px;
            top: 10px;
            font-size: 35px;
            cursor: pointer;
        }


    
        .gallery-item iframe {
            width: 100%;
            height: 100%;
            border: none;
            background: white;
        }
    
    .gallery-grid {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
        gap: 1rem;
        padding: 1rem;
        overflow-y: auto;
        background: rgba(255, 255, 255, 0.1);
        border-radius: 8px;
    }

    .gallery-item {
        position: relative;
        aspect-ratio: 1;
        background: white;
        border-radius: 4px;
        overflow: hidden;
    }

    .gallery-item img {
        width: 100%;
        height: 100%;
        object-fit: contain;
        background: white;
    }        
    .photo-list {
        display: flex;
        flex-direction: column;
        gap: 1rem;
        padding: 1rem;
        background: rgba(255, 255, 255, 0.1);
        border-radius: 8px;
    }

    .photo-link {
        background: #E1C6A8;
        padding: 1rem;
        border-radius: 4px;
        text-decoration: none;
        color: #000;
        display: flex;
        align-items: center;
        justify-content: space-between;
    }

    .photo-link:hover {
        background: #d4b08c;
    }        
    </style>
{% endblock %}

{% block content_header %}
    <div class="search-card">
        <div class="search-body">
            <form method="post">
                {% csrf_token %}
                <div class="search-group">
                    {{ form.container_number }}
                    <div class="search-button">
                        <button type="submit" class="btn-primary">Search</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    {% if show_results and container %}
        <div class="details-card">
            <div class="card-header">
                <h5>Container Details</h5>
                {% if container.photos.exists %}
                    <button type="button" class="btn-primary" onclick="openGallery()">
                        <i class="fas fa-images"></i> View Photos
                    </button>
                {% endif %}
            </div>
            <div class="details-body">
                <div class="details-grid">
                    <div class="details-column">
                        <div class="detail-row">
                            <div class="detail-label">Container Type:</div>
                            <div class="detail-value">{{ container.container_type }}</div>
                        </div>
                        <div class="detail-row">
                            <div class="detail-label">Line:</div>
                            <div class="detail-value">{{ container.line }}</div>
                        </div>
                        <div class="detail-row">
                            <div class="detail-label">Preinfo:</div>
                            <div class="detail-value">{{ container.preinfo }}</div>
                        </div>
                        <div class="detail-row">
                            <div class="detail-label">Received On:</div>
                            <div class="detail-value">{{ container.received_on|date:"Y-m-d H:i" }}</div>
                        </div>
                        <div class="detail-row">
                            <div class="detail-label">Receive Vehicle:</div>
                            <div class="detail-value">{{ container.receive_vehicle }}</div>
                        </div>
                    </div>
                    <div class="details-column">
                        <div class="detail-row">
                            <div class="detail-label">Swept:</div>
                            <div class="detail-value">{{ container.swept|yesno:"Yes,No" }}</div>
                        </div>
                        <div class="detail-row">
                            <div class="detail-label">Washed:</div>
                            <div class="detail-value">{{ container.washed|yesno:"Yes,No" }}</div>
                        </div>
                        <div class="detail-row">
                            <div class="detail-label">Booking:</div>
                            <div class="detail-value">
                                {% if container.booking %}
                                    {{ container.booking.name }}
                                {% else %}
                                    -
                                {% endif %}
                            </div>
                        </div>
                        <div class="detail-row">
                            <div class="detail-label">Expedited On:</div>
                            <div class="detail-value">{{ container.expedited_on|date:"Y-m-d H:i"|default:"-" }}</div>
                        </div>
                        <div class="detail-row">
                            <div class="detail-label">Expedition Vehicle:</div>
                            <div class="detail-value">{{ container.expedition_vehicle|default:"-" }}</div>
                        </div>
                        <div class="detail-row">
                            <div class="detail-label">Damages:</div>
                            <div class="detail-value">{{ container.damages|default:"-" }}</div>
                        </div>
                        <div class="detail-row">
                            <div class="detail-label">Heavy Damaged:</div>
                            <div class="detail-value">{{ container.heavy_damaged|yesno:"Yes,No" }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
<div id="galleryModal" class="modal">
    <div class="modal-content">
        <span class="close-modal" onclick="closeGallery()">&times;</span>
        <h3 style="color: white; margin-bottom: 1rem;">Photos for Container {{ container.number }}</h3>
        <div class="gallery-grid">
        </div>
    </div>
</div>
        
    {% endif %}
{% endblock content_header %}

{% block table_header %}
        <th style="display: none;">Select</th>
        <th>Company name</th>
        <th>Company short name</th>
        <th>Description</th>
{% endblock table_header %}

{% block table_data %}
    <td>{{ object.number }}</td>
    <td>{{ object.number }}</td>
    <td>{{ object.number }}</td>
{% endblock %}

{% block buttons %}
{% endblock buttons %}

{% block crud_js %}
    <script src="{% static 'js/container-details.js' %}"></script>

    <script>
function loadPhotos(containerId) {
    const photoGrid = document.querySelector('.gallery-grid');
    photoGrid.innerHTML = '<div class="loading">Loading photos...</div>';

    fetch(`/api/damages/${containerId}/`)
        .then(response => response.json())
        .then(data => {
            photoGrid.innerHTML = '';
            if (data.length === 0) {
                photoGrid.innerHTML = '<div style="color: white;">No photos available</div>';
                return;
            }

            data.forEach(photo => {
                const photoDiv = document.createElement('div');
                photoDiv.className = 'gallery-item';

                const img = document.createElement('img');
                img.src = photo.url;  // Use the presigned URL directly
                img.alt = `Container photo`;
                
                img.onerror = function() {
                    console.error('Failed to load image:', img.src);
                    this.style.display = 'none';
                    photoDiv.innerHTML += '<div style="color: red;">Failed to load image</div>';
                };

                photoDiv.appendChild(img);
                photoGrid.appendChild(photoDiv);
            });
        })
        .catch(error => {
            console.error('Error loading photos:', error);
            photoGrid.innerHTML = '<div style="color: white;">Error loading photos</div>';
        });
}

function openGallery() {
    const container = {{ container.id|default:'null' }};
    if (container) {
        loadPhotos(container);
    }

    document.getElementById('galleryModal').style.display = 'block';
    document.body.style.overflow = 'hidden';
}

function closeGallery() {
    document.getElementById('galleryModal').style.display = 'none';
    document.body.style.overflow = 'auto';
    document.querySelector('.gallery-grid').innerHTML = '';
}

// Close modal when clicking outside
window.onclick = function(event) {
    const modal = document.getElementById('galleryModal');
    if (event.target === modal) {
        closeGallery();
    }
}

// Close modal on escape key
document.addEventListener('keydown', function(event) {
    if (event.key === 'Escape') {
        closeGallery();
    }
});

// Add loading indicator styles
const style = document.createElement('style');
style.textContent = `
    .loading {
        color: white;
        text-align: center;
        padding: 2rem;
    }
`;
document.head.appendChild(style);       
    </script>    
{% endblock %}
