<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Application Details | IMMIA FINANCE LIMITED</title>
    
    <!-- Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <style>
        :root {
            --primary: #1e40af;
            --primary-dark: #1e3a8a;
            --secondary: #0f172a;
            --accent: #06b6d4;
            --light: #f8fafc;
        }
        
        .sidebar {
            transition: all 0.3s ease;
        }
        
        @media (max-width: 768px) {
            .sidebar {
                transform: translateX(-100%);
            }
            
            .sidebar.active {
                transform: translateX(0);
            }
        }
        
        .card-hover {
            transition: all 0.3s ease;
        }
        
        .card-hover:hover {
            transform: translateY(-2px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
        }
        
        .notification-dot {
            animation: pulse 2s infinite;
        }
        
        @keyframes pulse {
            0% { opacity: 1; }
            50% { opacity: 0.5; }
            100% { opacity: 1; }
        }

        .text-truncate-safe {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            min-width: 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    @php
        $customer = Auth::guard('customer')->user();
        $hasProfilePicture = $customer->docpath && Storage::disk('public')->exists($customer->docpath);
        $profilePictureUrl = $hasProfilePicture ? asset('storage/' . $customer->docpath) : 'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?auto=format&fit=crop&w=500&q=60';
        
        // Status colors
        $statusColor = match($application->status) {
            'submitted' => ['bg' => 'blue', 'text' => 'blue', 'icon' => 'clock'],
            'under_review' => ['bg' => 'yellow', 'text' => 'yellow', 'icon' => 'search'],
            'approved' => ['bg' => 'green', 'text' => 'green', 'icon' => 'check-circle'],
            'rejected' => ['bg' => 'red', 'text' => 'red', 'icon' => 'times-circle'],
            'disbursed' => ['bg' => 'purple', 'text' => 'purple', 'icon' => 'money-bill-wave'],
            'withdrawn' => ['bg' => 'gray', 'text' => 'gray', 'icon' => 'undo'],
            default => ['bg' => 'gray', 'text' => 'gray', 'icon' => 'file']
        };
        
        $progress = $progress ?? $this->calculateApplicationProgress($application);
    @endphp

    <!-- Mobile Menu Button -->
    <button id="mobile-menu-button" class="lg:hidden fixed top-4 left-4 z-50 bg-blue-600 text-white p-3 rounded-lg shadow-lg">
        <i class="fas fa-bars"></i>
    </button>

    <!-- Sidebar -->
    <div id="sidebar" class="sidebar fixed inset-y-0 left-0 z-40 w-64 bg-white shadow-lg">
        <!-- Logo -->
        <div class="p-6 border-b border-gray-200">
            <div class="flex items-center space-x-3">
                <div class="w-10 h-10 bg-blue-600 rounded-lg flex items-center justify-center">
                    <i class="fas fa-hand-holding-usd text-white text-lg"></i>
                </div>
                <div>
                    <h1 class="text-xl font-bold text-blue-600">IMMIA FINANCE</h1>
                    <p class="text-gray-500 text-xs">Client Portal</p>
                </div>
            </div>
        </div>
        
        <!-- Navigation -->
        <nav class="p-4 space-y-2">
            <a href="{{ route('customer.dashboard') }}" class="flex items-center space-x-3 p-3 text-gray-700 hover:bg-gray-100 rounded-lg font-medium">
                <i class="fas fa-tachometer-alt w-5"></i>
                <span>Dashboard</span>
            </a>
            <a href="{{ route('customer.applications.index') }}" class="flex items-center space-x-3 p-3 text-gray-700 hover:bg-gray-100 rounded-lg font-medium">
                <i class="fas fa-file-invoice-dollar w-5"></i>
                <span>My Applications</span>
            </a>
            <a href="{{ route('customer.loan.application.show') }}" class="flex items-center space-x-3 p-3 text-gray-700 hover:bg-gray-100 rounded-lg font-medium">
               <i class="fas fa-paper-plane w-5"></i>
               <span>Apply for Loan</span>
            </a>
            <a href="#" class="flex items-center space-x-3 p-3 text-gray-700 hover:bg-gray-100 rounded-lg font-medium">
                <i class="fas fa-money-bill-wave w-5"></i>
                <span>Make Payment</span>
            </a>
             <a href="{{ route('customer.transactions.history') }}" 
        class="flex items-center space-x-3 p-3 text-gray-700 hover:bg-gray-100 rounded-lg font-medium">
        <i class="fas fa-history w-5"></i>
        <span>Transaction History</span>
           </a>
            <a href="#" class="flex items-center space-x-3 p-3 text-gray-700 hover:bg-gray-100 rounded-lg font-medium">
                <i class="fas fa-file-alt w-5"></i>
                <span>Documents</span>
            </a>
            <a href="{{ route('customer.profile.show') }}" class="flex items-center space-x-3 p-3 text-gray-700 hover:bg-gray-100 rounded-lg font-medium">
                <i class="fas fa-user-cog w-5"></i>
                <span>Profile Settings</span>
            </a>
            <a href="#" class="flex items-center space-x-3 p-3 text-gray-700 hover:bg-gray-100 rounded-lg font-medium">
                <i class="fas fa-question-circle w-5"></i>
                <span>Help & Support</span>
            </a>
            <form method="POST" action="{{ route('customer.logout') }}" class="mt-8">
                @csrf
                <button type="submit" class="flex items-center space-x-3 p-3 text-gray-700 hover:bg-gray-100 rounded-lg font-medium w-full">
                    <i class="fas fa-sign-out-alt w-5"></i>
                    <span>Logout</span>
                </button>
            </form>
        </nav>
    </div>

    <!-- Main Content -->
    <div class="lg:ml-64">
        <!-- Top Bar -->
        <header class="bg-white shadow-sm border-b border-gray-200">
            <div class="flex justify-between items-center px-4 sm:px-6 py-4">
                <div class="min-w-0 flex-1">
                    <h1 class="text-xl sm:text-2xl font-bold text-gray-900 text-truncate-safe">Application Details</h1>
                    <p class="text-gray-600 text-sm text-truncate-safe">Application #{{ $application->application_number }}</p>
                </div>
                
                <div class="flex items-center space-x-2 sm:space-x-4">
                    <!-- Notifications -->
                    <div class="relative">
                        <button class="p-2 text-gray-600 hover:text-blue-600 relative">
                            <i class="fas fa-bell text-lg sm:text-xl"></i>
                            <span class="absolute top-1 right-1 w-2 h-2 bg-red-500 rounded-full notification-dot"></span>
                        </button>
                    </div>
                    
                    <!-- User Menu -->
                    <div class="relative">
                        <button class="flex items-center space-x-2 sm:space-x-3 p-2 rounded-lg hover:bg-gray-100">
                            <div class="w-8 h-8 bg-blue-100 rounded-full flex items-center justify-center overflow-hidden">
                                @if($hasProfilePicture)
                                    <img src="{{ $profilePictureUrl }}" alt="Profile" class="w-full h-full object-cover">
                                @else
                                    <i class="fas fa-user text-blue-600 text-sm"></i>
                                @endif
                            </div>
                            <span class="text-gray-700 font-medium hidden sm:block">{{ $customer->customer }}</span>
                            <i class="fas fa-chevron-down text-gray-400 hidden sm:block"></i>
                        </button>
                    </div>
                </div>
            </div>
        </header>

        <!-- Main Content Area -->
        <main class="p-4 sm:p-6">
            <!-- Back Button -->
            <div class="mb-6">
                <a href="{{ route('customer.applications.index') }}" class="inline-flex items-center text-blue-600 hover:text-blue-800 transition-colors">
                    <i class="fas fa-arrow-left mr-2"></i>
                    Back to Applications
                </a>
            </div>

            <!-- Application Header -->
            <div class="bg-white rounded-xl shadow-sm border p-6 mb-6">
                <div class="flex flex-col lg:flex-row justify-between items-start lg:items-center space-y-4 lg:space-y-0">
                    <div class="flex items-center space-x-4">
                        <div class="w-16 h-16 bg-{{ $statusColor['text'] }}-100 rounded-lg flex items-center justify-center">
                            <i class="fas fa-{{ $statusColor['icon'] }} text-{{ $statusColor['text'] }}-600 text-2xl"></i>
                        </div>
                        <div>
                            <h2 class="text-xl font-bold text-gray-900">{{ $application->product->product ?? 'Loan Application' }}</h2>
                            <p class="text-gray-600">Application #{{ $application->application_number }}</p>
                            <div class="flex items-center space-x-2 mt-2">
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-{{ $statusColor['text'] }}-100 text-{{ $statusColor['text'] }}-800">
                                    {{ $statusText ?? ucfirst(str_replace('_', ' ', $application->status)) }}
                                </span>
                                <span class="text-sm text-gray-500">
                                    Applied {{ \Carbon\Carbon::parse($application->created_at)->format('F d, Y') }}
                                </span>
                            </div>
                        </div>
                    </div>
                    
                    <div class="text-right">
                        <div class="text-2xl font-bold text-gray-900">ZMW {{ number_format($application->loan_amount, 2) }}</div>
                        <div class="text-sm text-gray-600">{{ $application->loan_tenure }} months • {{ $application->interest_rate }}% interest</div>
                        <div class="text-sm text-gray-500 mt-1">Monthly: ZMW {{ number_format($application->estimated_monthly_payment, 2) }}</div>
                    </div>
                </div>
            </div>

            <!-- Progress Bar -->
            <div class="bg-white rounded-xl shadow-sm border p-6 mb-6">
                <div class="flex justify-between items-center mb-2">
                    <span class="text-sm font-medium text-gray-700">Application Progress</span>
                    <span class="text-sm font-bold text-blue-600">{{ $progress }}% Complete</span>
                </div>
                <div class="w-full bg-gray-200 rounded-full h-3">
                    <div class="bg-blue-600 h-3 rounded-full transition-all duration-500" style="width: {{ $progress }}%"></div>
                </div>
                <div class="flex justify-between text-xs text-gray-500 mt-2">
                    <span>Submitted</span>
                    <span>Under Review</span>
                    <span>Approved</span>
                    <span>Completed</span>
                </div>
            </div>

            <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                <!-- Left Column -->
                <div class="space-y-6">
                    <!-- Applicant Details -->
                    <div class="bg-white rounded-xl shadow-sm border">
                        <div class="p-4 sm:p-6 border-b border-gray-200">
                            <h3 class="text-lg font-semibold text-gray-900">Applicant Details</h3>
                        </div>
                        <div class="p-4 sm:p-6">
                            <dl class="space-y-3">
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Full Name</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->customer->full_name ?? $application->customer->customer }}</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Email</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->customer->email }}</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Phone</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->customer->phone }}</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">NRC Number</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->customer->number ?? 'N/A' }}</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Address</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->customer->address }}, {{ $application->customer->town }}</dd>
                                </div>
                            </dl>
                        </div>
                    </div>

                    <!-- Employment Details -->
                    <div class="bg-white rounded-xl shadow-sm border">
                        <div class="p-4 sm:p-6 border-b border-gray-200">
                            <h3 class="text-lg font-semibold text-gray-900">Employment Details</h3>
                        </div>
                        <div class="p-4 sm:p-6">
                            <dl class="space-y-3">
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Job Title</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->job_title ?? 'N/A' }}</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Employer</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->employer_name ?? 'N/A' }}</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Employment Type</dt>
                                    <dd class="text-sm text-gray-900">{{ ucfirst($application->employment_type) ?? 'N/A' }}</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Years in Employment</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->employment_years ?? 'N/A' }} years</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Gross Salary</dt>
                                    <dd class="text-sm text-gray-900">ZMW {{ number_format($application->gross_salary, 2) }}</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Net Salary</dt>
                                    <dd class="text-sm text-gray-900">ZMW {{ number_format($application->net_salary, 2) }}</dd>
                                </div>
                            </dl>
                        </div>
                    </div>
                </div>

                <!-- Right Column -->
                <div class="space-y-6">
                    <!-- Loan Details -->
                    <div class="bg-white rounded-xl shadow-sm border">
                        <div class="p-4 sm:p-6 border-b border-gray-200">
                            <h3 class="text-lg font-semibold text-gray-900">Loan Details</h3>
                        </div>
                        <div class="p-4 sm:p-6">
                            <dl class="space-y-3">
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Loan Product</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->product->product ?? 'N/A' }}</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Loan Amount</dt>
                                    <dd class="text-sm text-gray-900">ZMW {{ number_format($application->loan_amount, 2) }}</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Loan Tenure</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->loan_tenure }} months</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Interest Rate</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->interest_rate }}%</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Monthly Payment</dt>
                                    <dd class="text-sm text-gray-900">ZMW {{ number_format($application->estimated_monthly_payment, 2) }}</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Total Repayment</dt>
                                    <dd class="text-sm text-gray-900">ZMW {{ number_format($application->estimated_monthly_payment * $application->loan_tenure, 2) }}</dd>
                                </div>
                            </dl>
                        </div>
                    </div>

                    <!-- Next of Kin -->
                    <div class="bg-white rounded-xl shadow-sm border">
                        <div class="p-4 sm:p-6 border-b border-gray-200">
                            <h3 class="text-lg font-semibold text-gray-900">Next of Kin</h3>
                        </div>
                        <div class="p-4 sm:p-6">
                            <dl class="space-y-3">
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Full Name</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->kin_first_name }} {{ $application->kin_surname }}</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Telephone</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->kin_telephone }}</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Email</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->kin_email ?? 'N/A' }}</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Address</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->kin_physical_address }}</dd>
                                </div>
                                <div class="flex justify-between">
                                    <dt class="text-sm font-medium text-gray-500">Town/Province</dt>
                                    <dd class="text-sm text-gray-900">{{ $application->kin_town }}, {{ $application->kin_province }}</dd>
                                </div>
                            </dl>
                        </div>
                    </div>

                    <!-- Loan Purpose -->
                    <div class="bg-white rounded-xl shadow-sm border">
                        <div class="p-4 sm:p-6 border-b border-gray-200">
                            <h3 class="text-lg font-semibold text-gray-900">Loan Purpose</h3>
                        </div>
                        <div class="p-4 sm:p-6">
                            <p class="text-sm text-gray-700">{{ $application->loan_purpose }}</p>
                        </div>
                    </div>
                </div>
            </div>
            
            
            <!-- Documents Section - Add this after the grid closing div -->
<div class="mt-8">
    <div class="bg-white rounded-xl shadow-sm border">
        <div class="p-4 sm:p-6 border-b border-gray-200">
            <div class="flex justify-between items-center">
                <h3 class="text-lg font-semibold text-gray-900">Application Documents</h3>
                <span class="text-sm text-gray-500">
                    {{ $application->documents->count() }} document(s)
                </span>
            </div>
        </div>
        
        <div class="p-4 sm:p-6">
            @if($application->documents->count() > 0)
                <div class="space-y-4">
                    @foreach($application->documents as $document)
                    <div class="flex items-center justify-between p-4 bg-gray-50 rounded-lg hover:bg-gray-100 transition-colors">
                        <div class="flex items-center space-x-4">
                            <div class="w-12 h-12 bg-blue-100 rounded-lg flex items-center justify-center">
                                @php
                                    $fileExt = strtolower(pathinfo($document->file_name, PATHINFO_EXTENSION));
                                    $icon = match($fileExt) {
                                        'pdf' => 'fa-file-pdf',
                                        'doc', 'docx' => 'fa-file-word',
                                        'jpg', 'jpeg', 'png', 'gif' => 'fa-file-image',
                                        'xls', 'xlsx', 'csv' => 'fa-file-excel',
                                        default => 'fa-file'
                                    };
                                @endphp
                                <i class="fas {{ $icon }} text-blue-600 text-lg"></i>
                            </div>
                            <div>
                                <h4 class="font-medium text-gray-900">{{ $document->file_name }}</h4>
                                <div class="flex items-center space-x-3 mt-1">
                                    <span class="text-xs text-gray-500">
                                        {{ $document->getFormattedFileSizeAttribute() }}
                                    </span>
                                    <span class="text-xs text-gray-500">•</span>
                                    <span class="text-xs text-gray-500">
                                        {{ strtoupper($fileExt) }}
                                    </span>
                                    <span class="text-xs text-gray-500">•</span>
                                    <span class="text-xs px-2 py-0.5 rounded-full 
                                        {{ $document->status == 'verified' ? 'bg-green-100 text-green-800' : 
                                           ($document->status == 'rejected' ? 'bg-red-100 text-red-800' : 'bg-yellow-100 text-yellow-800') }}">
                                        {{ ucfirst($document->status) }}
                                    </span>
                                </div>
                                <p class="text-xs text-gray-500 mt-1">
                                    Uploaded: {{ $document->uploaded_at->format('M d, Y H:i') }}
                                </p>
                            </div>
                        </div>
                        
                        
                        
                        <div class="flex items-center space-x-2">
                            <a href="{{ route('customer.documents.view', $document->id) }}" 
                               target="_blank"
                               class="inline-flex items-center px-3 py-1.5 bg-white border border-blue-200 text-blue-600 rounded-lg hover:bg-blue-50 transition-colors text-sm font-medium">
                                <i class="fas fa-eye mr-1.5"></i> View
                            </a>
                            <a href="{{ route('customer.documents.download', $document->id) }}" 
                               class="inline-flex items-center px-3 py-1.5 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors text-sm font-medium">
                                <i class="fas fa-download mr-1.5"></i> Download
                            </a>
                        </div>
                    </div>
                    @endforeach
                </div>
            @else
                <div class="text-center py-8">
                    <div class="w-16 h-16 bg-gray-100 rounded-full flex items-center justify-center mx-auto mb-4">
                        <i class="fas fa-folder-open text-gray-400 text-2xl"></i>
                    </div>
                    <h4 class="text-lg font-medium text-gray-700 mb-2">No Documents Uploaded</h4>
                    <p class="text-gray-500 text-sm">No documents were uploaded with this application.</p>
                    <p class="text-gray-400 text-xs mt-1">The document upload feature was added after this application was submitted.</p>
                </div>
            @endif
        </div>
    </div>
</div>
            

            <!-- Action Buttons -->
            @if(in_array($application->status, ['draft', 'submitted']))
                <div class="mt-6 flex flex-col sm:flex-row space-y-3 sm:space-y-0 sm:space-x-4">
                    <form action="{{ route('customer.applications.withdraw', $application->id) }}" method="POST" class="w-full sm:w-auto">
                        @csrf
                        <button type="submit" 
                                class="w-full bg-red-600 text-white px-6 py-3 rounded-lg hover:bg-red-700 transition-colors font-semibold"
                                onclick="return confirm('Are you sure you want to withdraw this application? This action cannot be undone.')">
                            <i class="fas fa-times mr-2"></i>Withdraw Application
                        </button>
                    </form>
                </div>
            @endif
        </main>
    </div>

    <script>
        // Mobile menu toggle
        document.getElementById('mobile-menu-button').addEventListener('click', function() {
            document.getElementById('sidebar').classList.toggle('active');
        });

        // Close sidebar when clicking outside on mobile
        document.addEventListener('click', function(event) {
            const sidebar = document.getElementById('sidebar');
            const mobileButton = document.getElementById('mobile-menu-button');
            
            if (window.innerWidth < 1024 && 
                !sidebar.contains(event.target) && 
                !mobileButton.contains(event.target)) {
                sidebar.classList.remove('active');
            }
        });

        // Handle window resize
        window.addEventListener('resize', function() {
            if (window.innerWidth >= 1024) {
                document.getElementById('sidebar').classList.remove('active');
            }
        });

        // Simulate real-time updates
        setInterval(() => {
            // Update notification dot
            const dot = document.querySelector('.notification-dot');
            dot.style.animation = 'none';
            setTimeout(() => {
                dot.style.animation = 'pulse 2s infinite';
            }, 10);
        }, 5000);
    </script>
</body>
</html>