{% extends 'base.html' %}
{% load static %}

{% block title %}All Customer Documents - HAVEN GRAZURI ADVANCE{% endblock %}

{% block content %}
<div class="min-h-screen bg-gray-50">
    <!-- Page Header -->
    <div class="bg-white shadow">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-6">
            <div class="md:flex md:items-center md:justify-between">
                <div class="flex-1 min-w-0">
                    <h1 class="text-2xl font-bold leading-7 text-gray-900 sm:text-3xl sm:truncate">
                        All Customer Documents
                    </h1>
                    <p class="mt-1 text-sm text-gray-500">
                        View and manage all customer documents uploaded during registration
                    </p>
                </div>
                <div class="mt-4 flex md:mt-0 md:ml-4 space-x-3">
                    <div class="flex items-center space-x-2">
                        <span class="text-sm text-gray-500">Total Documents:</span>
                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-blue-100 text-blue-800">
                            {{ total_documents }}
                        </span>
                    </div>
                    <div class="flex items-center space-x-2">
                        <span class="text-sm text-gray-500">Clients:</span>
                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800">
                            {{ total_clients }}
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters Section -->
    <div class="bg-white border-b border-gray-200">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4">
            <form method="GET" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-6 gap-4">
                <!-- Search -->
                <div>
                    <label for="search" class="block text-sm font-medium text-gray-700">Search Client</label>
                    <input type="text" name="search" id="search" value="{{ filters.search }}" 
                           placeholder="Search by client name..." 
                           class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-primary focus:ring-primary sm:text-sm">
                </div>

                <!-- Document Type -->
                <div>
                    <label for="document_type" class="block text-sm font-medium text-gray-700">Document Type</label>
                    <select name="document_type" id="document_type" 
                            class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-primary focus:ring-primary sm:text-sm">
                        <option value="">All Types</option>
                        {% for doc_type, doc_name in document_types %}
                        <option value="{{ doc_type }}" {% if filters.document_type == doc_type %}selected{% endif %}>
                            {{ doc_name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>

                <!-- Client -->
                <div>
                    <label for="client_id" class="block text-sm font-medium text-gray-700">Client</label>
                    <select name="client_id" id="client_id" 
                            class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-primary focus:ring-primary sm:text-sm">
                        <option value="">All Clients</option>
                        {% for client in clients %}
                        <option value="{{ client.id }}" {% if filters.client_id == client.id|stringformat:"s" %}selected{% endif %}>
                            {{ client.get_full_name }}
                        </option>
                        {% endfor %}
                    </select>
                </div>

                <!-- Date From -->
                <div>
                    <label for="date_from" class="block text-sm font-medium text-gray-700">From Date</label>
                    <input type="date" name="date_from" id="date_from" value="{{ filters.date_from }}"
                           class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-primary focus:ring-primary sm:text-sm">
                </div>

                <!-- Date To -->
                <div>
                    <label for="date_to" class="block text-sm font-medium text-gray-700">To Date</label>
                    <input type="date" name="date_to" id="date_to" value="{{ filters.date_to }}"
                           class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-primary focus:ring-primary sm:text-sm">
                </div>

                <!-- Status -->
                <div>
                    <label for="status" class="block text-sm font-medium text-gray-700">Status</label>
                    <select name="status" id="status" 
                            class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-primary focus:ring-primary sm:text-sm">
                        <option value="">All Status</option>
                        <option value="active" {% if filters.status == 'active' %}selected{% endif %}>Active</option>
                        <option value="inactive" {% if filters.status == 'inactive' %}selected{% endif %}>Inactive</option>
                        <option value="suspended" {% if filters.status == 'suspended' %}selected{% endif %}>Suspended</option>
                    </select>
                </div>

                <!-- Filter Buttons -->
                <div class="lg:col-span-6 flex justify-end space-x-3">
                    <button type="submit" 
                            class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-primary hover:bg-opacity-90 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary">
                        <span>🔍</span>
                        Apply Filters
                    </button>
                    <a href="{% url 'utils:all_customer_documents' %}" 
                       class="inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary">
                        <span>✕</span>
                        Clear
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Documents Content -->
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
        {% if customer_documents %}
            <div class="space-y-8">
                {% for client_docs in customer_documents %}
                <div class="bg-white rounded-lg shadow overflow-hidden">
                    <!-- Client Header -->
                    <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                        <div class="flex items-center justify-between">
                            <div class="flex items-center space-x-4">
                                <div class="flex-shrink-0">
                                    <div class="h-12 w-12 rounded-full bg-primary flex items-center justify-center">
                                        <span class="text-white font-semibold text-lg">
                                            {{ client_docs.client.get_full_name|first|upper }}
                                        </span>
                                    </div>
                                </div>
                                <div>
                                    <h3 class="text-lg font-semibold text-gray-900">
                                        {{ client_docs.client.get_full_name }}
                                    </h3>
                                    <div class="flex items-center space-x-4 text-sm text-gray-500">
                                        <span>{{ client_docs.client.phone_number }}</span>
                                        <span>{{ client_docs.client.email|default:"No email" }}</span>
                                        <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium 
                                                   {% if client_docs.client.status == 'active' %}bg-green-100 text-green-800
                                                   {% elif client_docs.client.status == 'inactive' %}bg-gray-100 text-gray-800
                                                   {% elif client_docs.client.status == 'suspended' %}bg-red-100 text-red-800
                                                   {% else %}bg-yellow-100 text-yellow-800{% endif %}">
                                            {{ client_docs.client.status|title }}
                                        </span>
                                    </div>
                                </div>
                            </div>
                            <div class="text-right">
                                <div class="text-2xl font-bold text-gray-900">{{ client_docs.total_documents }}</div>
                                <div class="text-sm text-gray-500">Total Documents</div>
                            </div>
                        </div>
                    </div>

                    <!-- Document Categories -->
                    <div class="p-6">
                        <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                            <!-- ID Documents -->
                            {% if client_docs.id_documents %}
                            <div class="bg-blue-50 rounded-lg p-4">
                                <h4 class="text-sm font-semibold text-blue-900 mb-3 flex items-center">
                                    <span>🪪</span>
                                    ID Documents ({{ client_docs.id_documents|length }})
                                </h4>
                                <div class="space-y-2">
                                    {% for doc in client_docs.id_documents %}
                                    <div class="flex items-center justify-between bg-white rounded p-3 shadow-sm">
                                        <div class="flex items-center space-x-3">
                                            <span>📄</span>
                                            <div>
                                                <div class="text-sm font-medium text-gray-900">{{ doc.name }}</div>
                                                <div class="text-xs text-gray-500">{{ doc.uploaded_at|date:"M d, Y" }}</div>
                                            </div>
                                        </div>
                                        <div class="flex items-center space-x-2">
                                            <a href="{{ doc.file.url }}" target="_blank" 
                                               class="text-blue-600 hover:text-blue-800 text-sm">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <a href="{{ doc.file.url }}" download 
                                               class="text-green-600 hover:text-green-800 text-sm">
                                                <span>⬇️</span>
                                            </a>
                                        </div>
                                    </div>
                                    {% endfor %}
                                </div>
                            </div>
                            {% endif %}

                            <!-- Selfie Documents -->
                            {% if client_docs.selfie_documents %}
                            <div class="bg-green-50 rounded-lg p-4">
                                <h4 class="text-sm font-semibold text-green-900 mb-3 flex items-center">
                                    <span>📷</span>
                                    Selfie Photos ({{ client_docs.selfie_documents|length }})
                                </h4>
                                <div class="space-y-2">
                                    {% for doc in client_docs.selfie_documents %}
                                    <div class="flex items-center justify-between bg-white rounded p-3 shadow-sm">
                                        <div class="flex items-center space-x-3">
                                            <span>📷</span>
                                            <div>
                                                <div class="text-sm font-medium text-gray-900">{{ doc.name }}</div>
                                                <div class="text-xs text-gray-500">{{ doc.uploaded_at|date:"M d, Y" }}</div>
                                            </div>
                                        </div>
                                        <div class="flex items-center space-x-2">
                                            <a href="{{ doc.file.url }}" target="_blank" 
                                               class="text-green-600 hover:text-green-800 text-sm">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <a href="{{ doc.file.url }}" download 
                                               class="text-green-600 hover:text-green-800 text-sm">
                                                <span>⬇️</span>
                                            </a>
                                        </div>
                                    </div>
                                    {% endfor %}
                                </div>
                            </div>
                            {% endif %}

                            <!-- Utility Documents -->
                            {% if client_docs.utility_documents %}
                            <div class="bg-yellow-50 rounded-lg p-4">
                                <h4 class="text-sm font-semibold text-yellow-900 mb-3 flex items-center">
                                    <span>⚡</span>
                                    Utility Bills ({{ client_docs.utility_documents|length }})
                                </h4>
                                <div class="space-y-2">
                                    {% for doc in client_docs.utility_documents %}
                                    <div class="flex items-center justify-between bg-white rounded p-3 shadow-sm">
                                        <div class="flex items-center space-x-3">
                                            <span>⚡</span>
                                            <div>
                                                <div class="text-sm font-medium text-gray-900">{{ doc.name }}</div>
                                                <div class="text-xs text-gray-500">{{ doc.uploaded_at|date:"M d, Y" }}</div>
                                            </div>
                                        </div>
                                        <div class="flex items-center space-x-2">
                                            <a href="{{ doc.file.url }}" target="_blank" 
                                               class="text-yellow-600 hover:text-yellow-800 text-sm">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <a href="{{ doc.file.url }}" download 
                                               class="text-yellow-600 hover:text-yellow-800 text-sm">
                                                <span>⬇️</span>
                                            </a>
                                        </div>
                                    </div>
                                    {% endfor %}
                                </div>
                            </div>
                            {% endif %}

                            <!-- Bank Documents -->
                            {% if client_docs.bank_documents %}
                            <div class="bg-purple-50 rounded-lg p-4">
                                <h4 class="text-sm font-semibold text-purple-900 mb-3 flex items-center">
                                    <i class="fas fa-university mr-2"></i>
                                    Bank Statements ({{ client_docs.bank_documents|length }})
                                </h4>
                                <div class="space-y-2">
                                    {% for doc in client_docs.bank_documents %}
                                    <div class="flex items-center justify-between bg-white rounded p-3 shadow-sm">
                                        <div class="flex items-center space-x-3">
                                            <i class="fas fa-university text-purple-500"></i>
                                            <div>
                                                <div class="text-sm font-medium text-gray-900">{{ doc.name }}</div>
                                                <div class="text-xs text-gray-500">{{ doc.uploaded_at|date:"M d, Y" }}</div>
                                            </div>
                                        </div>
                                        <div class="flex items-center space-x-2">
                                            <a href="{{ doc.file.url }}" target="_blank" 
                                               class="text-purple-600 hover:text-purple-800 text-sm">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <a href="{{ doc.file.url }}" download 
                                               class="text-purple-600 hover:text-purple-800 text-sm">
                                                <span>⬇️</span>
                                            </a>
                                        </div>
                                    </div>
                                    {% endfor %}
                                </div>
                            </div>
                            {% endif %}

                            <!-- Business Documents -->
                            {% if client_docs.business_documents %}
                            <div class="bg-indigo-50 rounded-lg p-4">
                                <h4 class="text-sm font-semibold text-indigo-900 mb-3 flex items-center">
                                    <i class="fas fa-briefcase mr-2"></i>
                                    Business Documents ({{ client_docs.business_documents|length }})
                                </h4>
                                <div class="space-y-2">
                                    {% for doc in client_docs.business_documents %}
                                    <div class="flex items-center justify-between bg-white rounded p-3 shadow-sm">
                                        <div class="flex items-center space-x-3">
                                            <i class="fas fa-briefcase text-indigo-500"></i>
                                            <div>
                                                <div class="text-sm font-medium text-gray-900">{{ doc.name }}</div>
                                                <div class="text-xs text-gray-500">{{ doc.uploaded_at|date:"M d, Y" }}</div>
                                            </div>
                                        </div>
                                        <div class="flex items-center space-x-2">
                                            <a href="{{ doc.file.url }}" target="_blank" 
                                               class="text-indigo-600 hover:text-indigo-800 text-sm">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <a href="{{ doc.file.url }}" download 
                                               class="text-indigo-600 hover:text-indigo-800 text-sm">
                                                <span>⬇️</span>
                                            </a>
                                        </div>
                                    </div>
                                    {% endfor %}
                                </div>
                            </div>
                            {% endif %}

                            <!-- Other Documents -->
                            {% if client_docs.other_documents %}
                            <div class="bg-gray-50 rounded-lg p-4">
                                <h4 class="text-sm font-semibold text-gray-900 mb-3 flex items-center">
                                    <span>📁</span>
                                    Other Documents ({{ client_docs.other_documents|length }})
                                </h4>
                                <div class="space-y-2">
                                    {% for doc in client_docs.other_documents %}
                                    <div class="flex items-center justify-between bg-white rounded p-3 shadow-sm">
                                        <div class="flex items-center space-x-3">
                                            <span>📄</span>
                                            <div>
                                                <div class="text-sm font-medium text-gray-900">{{ doc.name }}</div>
                                                <div class="text-xs text-gray-500">{{ doc.uploaded_at|date:"M d, Y" }}</div>
                                                {% if doc.description %}
                                                <div class="text-xs text-gray-400">{{ doc.description }}</div>
                                                {% endif %}
                                            </div>
                                        </div>
                                        <div class="flex items-center space-x-2">
                                            <a href="{{ doc.file.url }}" target="_blank" 
                                               class="text-gray-600 hover:text-gray-800 text-sm">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <a href="{{ doc.file.url }}" download 
                                               class="text-gray-600 hover:text-gray-800 text-sm">
                                                <span>⬇️</span>
                                            </a>
                                        </div>
                                    </div>
                                    {% endfor %}
                                </div>
                            </div>
                            {% endif %}
                        </div>
                    </div>
                </div>
                {% endfor %}
            </div>
        {% else %}
            <!-- Empty State -->
            <div class="text-center py-12">
                <span>📂</span>
                <h3 class="text-lg font-medium text-gray-900 mb-2">No documents found</h3>
                <p class="text-gray-500 mb-6">No customer documents match your current filters.</p>
                <a href="{% url 'utils:all_customer_documents' %}" 
                   class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-primary hover:bg-opacity-90">
                    <span>✕</span>
                    Clear Filters
                </a>
            </div>
        {% endif %}
    </div>
</div>
{% endblock %}

{% block scripts %}
<script>
// Auto-submit form when filters change
document.addEventListener('DOMContentLoaded', function() {
    const filterInputs = document.querySelectorAll('select, input[type="date"]');
    filterInputs.forEach(input => {
        input.addEventListener('change', function() {
            // Don't auto-submit for the search input to allow typing
            if (input.type !== 'text') {
                input.closest('form').submit();
            }
        });
    });

    // Add debounced search for text input
    const searchInput = document.getElementById('search');
    let searchTimeout;
    searchInput.addEventListener('input', function() {
        clearTimeout(searchTimeout);
        searchTimeout = setTimeout(() => {
            searchInput.closest('form').submit();
        }, 500);
    });
});

// Document preview functionality
function previewDocument(url) {
    window.open(url, '_blank');
}

// Download functionality
function downloadDocument(url, filename) {
    const link = document.createElement('a');
    link.href = url;
    link.download = filename;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}
</script>
{% endblock %} 