@extends('dashboard.layout')

@php
    // dd($settings)
@endphp

@section('content')
<div class="min-h-screen bg-gray-50 dark:bg-dark-primary px-3">
    <!-- Invoice Content -->
    <div class=" pb-20 md:pb-6 pt-4 md:pt-6">
        <!-- Invoice Card -->
        <div class="bg-white dark:bg-dark-secondary rounded print:shadow-none shadow-lg print:border-0 border border-gray-200 dark:border-gray-700 overflow-hidden">
            <!-- Invoice Header -->
            <div class="bg-gradient-to-r from-gray-50 to-gray-100 dark:from-gray-800 dark:to-gray-900 px-4 md:px-8 py-6">
                <div class="flex flex-col md:flex-row md:items-center md:justify-between">
                    <div class="mb-4 md:mb-0">
                        <h1 class="text-2xl md:text-3xl font-bold text-gray-900 dark:text-white">INVOICE</h1>
                        <p class="text-gray-500 dark:text-gray-400 mt-1">Payment Receipt</p>
                    </div>
                    <div class="text-left md:text-right">
                        <div class="text-xl md:text-2xl font-bold text-gray-900 dark:text-white">INV-{{ str_pad($payment->id, 6, '0', STR_PAD_LEFT) }}</div>
                        <p class="text-gray-500 dark:text-gray-400 mt-1">Issued: {{ $payment->created_at->format('M d, Y') }}</p>
                    </div>
                </div>
            </div>

            <!-- Company & Client Info -->
            <div class="px-4 md:px-8 py-6 border-b border-gray-200 dark:border-gray-700">
                <div class="space-y-6 md:space-y-0 md:grid md:grid-cols-2 gap-6 md:gap-8">
                    <!-- Company Info -->
                    <div>
                        <h3 class="text-base md:text-lg font-semibold text-gray-900 dark:text-white mb-3 md:mb-4">From</h3>
                        <div class="space-y-1 md:space-y-2">
                            <p class="text-sm font-medium text-gray-900 dark:text-white">{{ $settings['company_name'] }}</p>
                            <p class="text-sm text-gray-600 dark:text-gray-400">{{ $settings['company_address'] }}</p>
                            <p class="text-sm text-gray-600 dark:text-gray-400">Email: {{ $settings['company_email'] }}</p>
                            <p class="text-sm text-gray-600 dark:text-gray-400">Phone: {{ $settings['company_phone'] }}</p>
                        </div>
                    </div>

                    <!-- Client Info -->
                    <div class="mt-6 md:mt-0">
                        <h3 class="text-base md:text-lg font-semibold text-gray-900 dark:text-white mb-3 md:mb-4">Bill To</h3>
                        <div class="space-y-1 md:space-y-2">
                            @php
                                $contactInfo = is_array($payment->project->serviceRequest->contact_info ?? []) 
                                    ? $payment->project->serviceRequest->contact_info 
                                    : json_decode($payment->project->serviceRequest->contact_info ?? '[]', true);
                            @endphp
                            <p class="text-sm font-medium text-gray-900 dark:text-white">
                                {{ $contactInfo['name'] ?? ($payment->user->name ?? 'N/A') }}
                            </p>
                            <p class="text-sm text-gray-600 dark:text-gray-400">
                                {{ $contactInfo['company'] ?? 'No Company' }}
                            </p>
                            <p class="text-sm text-gray-600 dark:text-gray-400">
                                {{ $contactInfo['email'] ?? ($payment->user->email ?? 'N/A') }}
                            </p>
                            <p class="text-sm text-gray-600 dark:text-gray-400">
                                {{ $contactInfo['phone'] ?? 'N/A' }}
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Invoice Details Grid -->
            <div class="px-4 md:px-8 py-6 border-b border-gray-200 dark:border-gray-700">
                <div class="grid grid-cols-2 md:grid-cols-4 gap-4 md:gap-6">
                    <div>
                        <p class="text-xs md:text-sm font-medium text-gray-500 dark:text-gray-400">Invoice Date</p>
                        <p class="text-base md:text-lg font-semibold text-gray-900 dark:text-white">
                            {{ $payment->created_at->format('M d, Y') }}
                        </p>
                    </div>
                    <div>
                        <p class="text-xs md:text-sm font-medium text-gray-500 dark:text-gray-400">Payment Date</p>
                        <p class="text-base md:text-lg font-semibold text-gray-900 dark:text-white">
                            {{ \Carbon\Carbon::parse($payment->payment_date)->format('M d, Y') }}
                        </p>
                    </div>
                    <div class="col-span-2 md:col-span-1">
                        <p class="text-xs md:text-sm font-medium text-gray-500 dark:text-gray-400">Payment Method</p>
                        <p class="text-base md:text-lg font-semibold text-gray-900 dark:text-white capitalize">
                            {{ str_replace('_', ' ', $payment->payment_method) }}
                        </p>
                    </div>
                    <div class="col-span-2 md:col-span-1 mt-4 md:mt-0">
                        <p class="text-xs md:text-sm font-medium text-gray-500 dark:text-gray-400 mb-2">Status</p>
                        <span class="inline-flex items-center px-3 py-1 rounded-full text-xs md:text-sm font-medium
                            @if($payment->status === 'paid') 
                                bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-300
                            @elseif($payment->status === 'pending')
                                bg-yellow-100 text-yellow-800 dark:bg-yellow-900/30 dark:text-yellow-300
                            @else
                                bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-300
                            @endif">
                            <i class="fas 
                                @if($payment->status === 'paid') fa-check-circle 
                                @elseif($payment->status === 'pending') fa-clock
                                @else fa-times-circle @endif 
                                mr-1">
                            </i>
                            {{ ucfirst($payment->status) }}
                        </span>
                    </div>
                </div>
            </div>

            <!-- Content Area -->
            <div class="px-4 md:px-8 py-6">
                <!-- Project Information -->
                <div class="mb-6 md:mb-8">
                    <h3 class="text-base md:text-lg font-semibold text-gray-900 dark:text-white mb-3 md:mb-4">Project Information</h3>
                    <div class="bg-gray-50 dark:bg-gray-800 rounded-lg p-4">
                        <div class="space-y-3 md:space-y-0 md:grid md:grid-cols-2 md:gap-4">
                            <div>
                                <p class="text-xs md:text-sm font-medium text-gray-500 dark:text-gray-400">Project Name</p>
                                <p class="text-sm md:text-base text-gray-900 dark:text-white font-medium">
                                    {{ $payment->project->name ?? 'N/A' }}
                                </p>
                            </div>
                            <div>
                                <p class="text-xs md:text-sm font-medium text-gray-500 dark:text-gray-400 mt-3 md:mt-0">Project ID</p>
                                <p class="text-sm md:text-base text-gray-900 dark:text-white font-medium">
                                    {{ $payment->project_id }}
                                </p>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Payment Details -->
                <div class="mb-6 md:mb-8">
                    <h3 class="text-base md:text-lg font-semibold text-gray-900 dark:text-white mb-3 md:mb-4">Payment Details</h3>
                    <div class="overflow-hidden rounded-lg border border-gray-200 dark:border-gray-700">
                        <!-- Mobile Card View -->
                        <div class="md:hidden bg-white dark:bg-dark-secondary p-4">
                            <div class="space-y-4">
                                <div>
                                    <p class="text-sm font-medium text-gray-500 dark:text-gray-400">Description</p>
                                    <p class="text-sm font-medium text-gray-900 dark:text-white mt-1">
                                        Project Payment - {{ $payment->project->name ?? 'N/A' }}
                                    </p>
                                    <p class="text-xs text-gray-500 dark:text-gray-400 mt-1">
                                        Payment via {{ str_replace('_', ' ', $payment->payment_method) }}
                                        @if($payment->transaction_id)
                                            • Transaction: {{ $payment->transaction_id }}
                                        @endif
                                    </p>
                                    @if($payment->notes)
                                        <p class="text-xs text-gray-500 dark:text-gray-400 mt-2">
                                            <span class="font-medium">Note:</span> {{ $payment->notes }}
                                        </p>
                                    @endif
                                </div>
                                <div class="pt-4 border-t border-gray-200 dark:border-gray-700">
                                    <div class="flex justify-between items-center">
                                        <p class="text-sm font-medium text-gray-500 dark:text-gray-400">Amount</p>
                                        <p class="text-lg font-bold text-green-600 dark:text-green-400">
                                            {{ number_format($payment->amount, 2) }} SR
                                        </p>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- Desktop Table View -->
                        <table class="hidden md:table min-w-full divide-y divide-gray-200 dark:divide-gray-700">
                            <thead class="bg-gray-50 dark:bg-gray-800">
                                <tr>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
                                        Description
                                    </th>
                                    <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
                                        Amount
                                    </th>
                                </tr>
                            </thead>
                            <tbody class="bg-white dark:bg-dark-secondary divide-y divide-gray-200 dark:divide-gray-700">
                                <tr>
                                    <td class="px-6 py-4">
                                        <div>
                                            <p class="text-sm font-medium text-gray-900 dark:text-white">
                                                Project Payment - {{ $payment->project->name ?? 'N/A' }}
                                            </p>
                                            <p class="text-xs text-gray-500 dark:text-gray-400">
                                                Payment via {{ str_replace('_', ' ', $payment->payment_method) }}
                                                @if($payment->transaction_id)
                                                    • Transaction: {{ $payment->transaction_id }}
                                                @endif
                                            </p>
                                            @if($payment->notes)
                                                <p class="text-xs text-gray-500 dark:text-gray-400 mt-1">
                                                    Note: {{ $payment->notes }}
                                                </p>
                                            @endif
                                        </div>
                                    </td>
                                    <td class="px-6 py-4 text-right">
                                        <p class="text-lg font-bold text-gray-900 dark:text-white">
                                            {{ number_format($payment->amount, 2) }} SR
                                        </p>
                                    </td>
                                </tr>
                            </tbody>
                        </table>

                        <!-- Total Amount Section -->
                        <div class="border-t border-gray-200 dark:border-gray-700">
                            <div class="p-4 md:px-6 md:py-4 bg-gray-50 dark:bg-gray-800">
                                <div class="flex justify-between items-center">
                                    <p class="text-sm md:text-base font-medium text-gray-900 dark:text-white">
                                        Total Amount:
                                    </p>
                                    <p class="text-xl md:text-2xl font-bold text-green-600 dark:text-green-400">
                                        {{ number_format($payment->amount, 2) }} SR
                                    </p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Payment Summary -->
                <div class="mb-6 md:mb-8">
                    <h3 class="text-base md:text-lg font-semibold text-gray-900 dark:text-white mb-3 md:mb-4">Payment Summary</h3>
                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4 md:gap-6">
                        <div class="bg-gray-50 dark:bg-gray-800 rounded-lg p-4">
                            <div class="flex items-center">
                                <div class="flex-shrink-0 h-10 w-10 bg-blue-100 dark:bg-blue-900/30 rounded-lg flex items-center justify-center">
                                    <i class="fas fa-money-bill text-blue-600 dark:text-blue-400"></i>
                                </div>
                                <div class="ml-3">
                                    <p class="text-xs md:text-sm font-medium text-gray-500 dark:text-gray-400">Amount Paid</p>
                                    <p class="text-lg md:text-xl font-bold text-gray-900 dark:text-white">
                                        {{ number_format($payment->amount, 2) }} SR
                                    </p>
                                </div>
                            </div>
                        </div>
                        
                        <div class="bg-gray-50 dark:bg-gray-800 rounded-lg p-4">
                            <div class="flex items-center">
                                <div class="flex-shrink-0 h-10 w-10 bg-purple-100 dark:bg-purple-900/30 rounded-lg flex items-center justify-center">
                                    <i class="fas fa-calendar-alt text-purple-600 dark:text-purple-400"></i>
                                </div>
                                <div class="ml-3">
                                    <p class="text-xs md:text-sm font-medium text-gray-500 dark:text-gray-400">Payment Date</p>
                                    <p class="text-base md:text-lg font-semibold text-gray-900 dark:text-white">
                                        {{ \Carbon\Carbon::parse($payment->payment_date)->format('M d, Y') }}
                                    </p>
                                </div>
                            </div>
                        </div>
                        
                        <div class="bg-gray-50 dark:bg-gray-800 rounded-lg p-4">
                            <div class="flex items-center">
                                @php
                                    $methodColors = [
                                        'cash' => 'bg-green-100 text-green-800',
                                        'bank_transfer' => 'bg-blue-100 text-blue-800',
                                        'check' => 'bg-purple-100 text-purple-800',
                                        'online' => 'bg-indigo-100 text-indigo-800',
                                        'mobile_banking' => 'bg-orange-100 text-orange-800',
                                        'card' => 'bg-red-100 text-red-800',
                                    ];
                                    $methodIconColors = [
                                        'cash' => 'text-green-600',
                                        'bank_transfer' => 'text-blue-600',
                                        'check' => 'text-purple-600',
                                        'online' => 'text-indigo-600',
                                        'mobile_banking' => 'text-orange-600',
                                        'card' => 'text-red-600',
                                    ];
                                    $methodIcons = [
                                        'cash' => 'fa-money-bill',
                                        'bank_transfer' => 'fa-university',
                                        'check' => 'fa-file-invoice',
                                        'online' => 'fa-globe',
                                        'mobile_banking' => 'fa-mobile-alt',
                                        'card' => 'fa-credit-card',
                                    ];
                                @endphp
                                <div class="flex-shrink-0 h-10 w-10 {{ $methodColors[$payment->payment_method] ?? 'bg-gray-100' }} dark:bg-gray-700 rounded-lg flex items-center justify-center">
                                    <i class="fas {{ $methodIcons[$payment->payment_method] ?? 'fa-money-bill' }} {{ $methodIconColors[$payment->payment_method] ?? 'text-gray-600' }} dark:text-gray-400"></i>
                                </div>
                                <div class="ml-3">
                                    <p class="text-xs md:text-sm font-medium text-gray-500 dark:text-gray-400">Payment Method</p>
                                    <p class="text-base md:text-lg font-semibold text-gray-900 dark:text-white capitalize">
                                        {{ str_replace('_', ' ', $payment->payment_method) }}
                                    </p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Notes & QR Code -->
                <div class="mt-6 md:mt-8 pt-6 border-t border-gray-200 dark:border-gray-700">
                    <div class="space-y-6 md:space-y-0 md:grid md:grid-cols-2 md:gap-6">
                        <div>
                            <h4 class="text-sm font-semibold text-gray-900 dark:text-white mb-2">Notes</h4>
                            @if($payment->notes)
                                <div class="bg-gray-50 dark:bg-gray-800 rounded-lg p-3 md:p-4">
                                    <p class="text-sm text-gray-600 dark:text-gray-400">{{ $payment->notes }}</p>
                                </div>
                            @else
                                <p class="text-sm text-gray-400 dark:text-gray-500 italic">No additional notes</p>
                            @endif
                        </div>
                        <div class="flex flex-col items-center md:items-end md:text-right">
                            <div class="inline-flex items-center px-4 py-2 bg-gray-100 dark:bg-gray-800 rounded-lg">
                                <i class="fas fa-qrcode text-gray-400 text-xl mr-3"></i>
                                <div>
                                    <p class="text-xs text-gray-500 dark:text-gray-400">Scan to verify</p>
                                    <p class="text-xs font-mono text-gray-600 dark:text-gray-300">INV-{{ str_pad($payment->id, 6, '0', STR_PAD_LEFT) }}</p>
                                </div>
                            </div>
                            <p class="text-xs text-gray-500 dark:text-gray-400 mt-3">
                                ID: {{ $payment->id }}
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Invoice Footer -->
            <div class="bg-gray-50 dark:bg-gray-800 px-4 md:px-8 py-4 border-t border-gray-200 dark:border-gray-700">
                <div class="flex flex-col space-y-2 md:space-y-0 md:flex-row md:items-center md:justify-between text-center md:text-left">
                    <div>
                        <p class="text-xs text-gray-500 dark:text-gray-400">
                            Thank you for your business
                        </p>
                    </div>
                    <div>
                        <p class="text-xs text-gray-500 dark:text-gray-400">
                            This is a computer-generated invoice. No signature required.
                        </p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Mobile Quick Actions -->
        <div class="mt-6 md:hidden">
            <div class="grid grid-cols-2 gap-3">
                <a href="{{ route('dashboard.invoices') }}" 
                   class="px-4 py-3 border border-gray-300 dark:border-gray-600 rounded-lg text-center text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-dark-secondary hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors">
                    <i class="fas fa-list mr-2"></i>
                    All Invoices
                </a>
                <button onclick="shareInvoice()" 
                        class="px-4 py-3 border border-gray-300 dark:border-gray-600 rounded-lg text-center text-sm font-medium text-gray-700 dark:text-gray-300 bg-white dark:bg-dark-secondary hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors">
                    <i class="fas fa-share mr-2"></i>
                    Share
                </button>
            </div>
        </div>
    </div>
</div>

<style>
    @media print {
        .no-print {
            display: none !important;
        }
        
        body {
            background: white !important;
            font-size: 12pt !important;
        }
        
        .dark\:bg-dark-secondary {
            background: white !important;
        }
        
        .dark\:text-white {
            color: black !important;
        }
        
        .dark\:text-gray-400 {
            color: #6B7280 !important;
        }
        
        .shadow-lg {
            box-shadow: none !important;
        }
        
        .border {
            border-color: #e5e7eb !important;
        }
    }

    @media (max-width: 768px) {
        .invoice-card {
            margin: 0.5rem;
        }
        
        h1 {
            font-size: 1.5rem;
        }
        
        h2 {
            font-size: 1.25rem;
        }
        
        h3 {
            font-size: 1.1rem;
        }
    }
</style>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        console.log('Invoice view page loaded');
        
        // Add swipe gesture for mobile navigation
        let touchStartX = 0;
        let touchEndX = 0;
        
        document.addEventListener('touchstart', function(e) {
            touchStartX = e.changedTouches[0].screenX;
        });
        
        document.addEventListener('touchend', function(e) {
            touchEndX = e.changedTouches[0].screenX;
            handleSwipe();
        });
        
        function handleSwipe() {
            const swipeThreshold = 100;
            const swipeLength = touchEndX - touchStartX;
            
            if (Math.abs(swipeLength) > swipeThreshold) {
                if (swipeLength < 0) {
                    // Swipe left - could be used for next invoice
                    console.log('Swiped left');
                } else {
                    // Swipe right - go back
                    window.history.back();
                }
            }
        }
    });
    
    function shareInvoice() {
        const invoiceNumber = 'INV-{{ str_pad($payment->id, 6, '0', STR_PAD_LEFT) }}';
        const invoiceDetails = `Invoice ${invoiceNumber} - {{ $payment->project->name ?? 'N/A' }} - {{ number_format($payment->amount, 2) }} SR`;
        const url = window.location.href;
        
        if (navigator.share) {
            navigator.share({
                title: `Invoice ${invoiceNumber}`,
                text: invoiceDetails,
                url: url
            })
            .then(() => console.log('Invoice shared successfully'))
            .catch((error) => console.log('Error sharing:', error));
        } else {
            // Fallback for browsers that don't support Web Share API
            const textArea = document.createElement('textarea');
            textArea.value = `${invoiceDetails}\n\nView at: ${url}`;
            document.body.appendChild(textArea);
            textArea.select();
            document.execCommand('copy');
            document.body.removeChild(textArea);
            
            // Show copied message
            const msg = document.createElement('div');
            msg.className = 'fixed bottom-20 left-1/2 transform -translate-x-1/2 bg-gray-800 text-white px-4 py-2 rounded-lg text-sm z-50';
            msg.textContent = 'Invoice link copied to clipboard';
            document.body.appendChild(msg);
            
            setTimeout(() => {
                msg.remove();
            }, 3000);
        }
    }
</script>
@endsection