@extends('layouts.app')

@section('content')
<div class="container m-auto p-4 sm:p-6 lg:p-8">
    <div class="bg-white rounded-lg shadow-lg p-6">
        <div class="flex justify-between items-center mb-6">
            <h1 class="text-3xl font-bold text-gray-900">عرض إذن تحويل المخزن</h1>
            <div class="flex gap-2">
                <a href="{{ route('inventory.transfers.index') }}" class="inline-flex items-center px-4 py-2 bg-gray-200 text-gray-700 rounded-md hover:bg-gray-300 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500">
                    <svg class="h-5 w-5 ml-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18" />
                    </svg>
                    العودة للقائمة
                </a>
                
                @can('inventory.transfers.print')
                <a href="{{ route('inventory.transfers.print', $document->id) }}" target="_blank" class="inline-flex items-center px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                    <svg class="h-5 w-5 ml-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z" />
                    </svg>
                    طباعة
                </a>
                @endcan
            </div>
        </div>

        <!-- Document Status -->
        <div class="mb-6">
            <span class="inline-block px-4 py-2 rounded-full text-sm font-semibold 
                @if($document->status == 'draft') bg-yellow-100 text-yellow-800
                @elseif($document->status == 'approved') bg-green-100 text-green-800
                @elseif($document->status == 'cancelled') bg-red-100 text-red-800
                @endif">
                @if($document->status == 'draft') 🔄 مسودة
                @elseif($document->status == 'approved') ✅ معتمد
                @elseif($document->status == 'cancelled') ❌ ملغي
                @endif
            </span>
        </div>

        <!-- Document Basic Information -->
        <div class="bg-gradient-to-r from-blue-50 to-indigo-50 border border-blue-200 rounded-lg p-6 mb-6">
            <h2 class="text-lg font-bold text-gray-900 mb-4">بيانات المستند الأساسية</h2>
            <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
                <div class="bg-white p-4 rounded-lg shadow-sm">
                    <p class="text-xs font-semibold text-gray-500 uppercase">رقم المستند</p>
                    <p class="mt-2 text-2xl font-bold text-blue-600">{{ $document->document_number }}</p>
                </div>
                <div class="bg-white p-4 rounded-lg shadow-sm">
                    <p class="text-xs font-semibold text-gray-500 uppercase">تاريخ المستند</p>
                    <p class="mt-2 text-lg font-bold text-gray-900">{{ $document->document_date->format('Y-m-d') }}</p>
                </div>
                <div class="bg-white p-4 rounded-lg shadow-sm">
                    <p class="text-xs font-semibold text-gray-500 uppercase">منشئ المستند</p>
                    <p class="mt-2 text-lg font-bold text-gray-900">{{ $document->creator->name ?? 'غير محدد' }}</p>
                </div>
                <div class="bg-white p-4 rounded-lg shadow-sm">
                    <p class="text-xs font-semibold text-gray-500 uppercase">الكمية الإجمالية</p>
                    <p class="mt-2 text-lg font-bold text-indigo-600">{{ number_format($document->getTotalQuantity(), 2) }}</p>
                </div>
            </div>
        </div>

        <!-- Warehouse Transfer Details -->
        <div class="bg-white border border-gray-200 rounded-lg p-6 mb-6">
            <h2 class="text-lg font-bold text-gray-900 mb-6">بيانات التحويل بين المخازن</h2>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <!-- From Warehouse -->
                <div class="border-l-4 border-red-500 bg-red-50 rounded-lg p-6">
                    <div class="flex items-center mb-3">
                        <svg class="w-6 h-6 text-red-600 ml-2" fill="currentColor" viewBox="0 0 20 20">
                            <path d="M10.707 2.293a1 1 0 00-1.414 0l-7 7a1 1 0 001.414 1.414L4 10.414V17a1 1 0 001 1h2a1 1 0 001-1v-2a1 1 0 011-1h2a1 1 0 011 1v2a1 1 0 001 1h2a1 1 0 001-1v-6.586l.293.293a1 1 0 001.414-1.414l-7-7z"/>
                        </svg>
                        <p class="text-sm font-bold text-red-700 uppercase">من المخزن (المصدر)</p>
                    </div>
                    <p class="text-3xl font-bold text-red-900 mb-2">{{ $document->fromWarehouse->name ?? 'غير محدد' }}</p>
                    @if($document->fromWarehouse)
                    <div class="text-xs text-red-600 space-y-1">
                        <p><span class="font-semibold">المعرف:</span> #{{ $document->fromWarehouse->id }}</p>
                        @if($document->fromWarehouse->is_active)
                        <p><span class="font-semibold">الحالة:</span> <span class="inline-block bg-green-200 text-green-800 px-2 py-1 rounded">نشط</span></p>
                        @else
                        <p><span class="font-semibold">الحالة:</span> <span class="inline-block bg-red-200 text-red-800 px-2 py-1 rounded">غير نشط</span></p>
                        @endif
                    </div>
                    @endif
                </div>

                <!-- Arrow -->
                <div class="flex items-center justify-center">
                    <svg class="w-12 h-12 text-gray-400 transform -rotate-90 md:rotate-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 7l5 5m0 0l-5 5m5-5H6"/>
                    </svg>
                </div>

                <!-- To Warehouse -->
                <div class="border-l-4 border-green-500 bg-green-50 rounded-lg p-6 md:col-start-2">
                    <div class="flex items-center mb-3">
                        <svg class="w-6 h-6 text-green-600 ml-2" fill="currentColor" viewBox="0 0 20 20">
                            <path d="M10.707 2.293a1 1 0 00-1.414 0l-7 7a1 1 0 001.414 1.414L4 10.414V17a1 1 0 001 1h2a1 1 0 001-1v-2a1 1 0 011-1h2a1 1 0 011 1v2a1 1 0 001 1h2a1 1 0 001-1v-6.586l.293.293a1 1 0 001.414-1.414l-7-7z"/>
                        </svg>
                        <p class="text-sm font-bold text-green-700 uppercase">إلى المخزن (الوجهة)</p>
                    </div>
                    <p class="text-3xl font-bold text-green-900 mb-2">{{ $document->toWarehouse->name ?? 'غير محدد' }}</p>
                    @if($document->toWarehouse)
                    <div class="text-xs text-green-600 space-y-1">
                        <p><span class="font-semibold">المعرف:</span> #{{ $document->toWarehouse->id }}</p>
                        @if($document->toWarehouse->is_active)
                        <p><span class="font-semibold">الحالة:</span> <span class="inline-block bg-green-200 text-green-800 px-2 py-1 rounded">نشط</span></p>
                        @else
                        <p><span class="font-semibold">الحالة:</span> <span class="inline-block bg-red-200 text-red-800 px-2 py-1 rounded">غير نشط</span></p>
                        @endif
                    </div>
                    @endif
                </div>
            </div>
        </div>

        <!-- Approval Information (if approved) -->
        @if($document->status == 'approved')
        <div class="bg-green-50 border border-green-300 rounded-lg p-6 mb-6">
            <h2 class="text-lg font-semibold text-gray-900 mb-4">معلومات الاعتماد</h2>
            <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                <div>
                    <p class="text-sm font-medium text-gray-600">معتمد بواسطة</p>
                    <p class="mt-1 text-lg font-bold text-green-800">{{ $document->approver->name ?? 'غير محدد' }}</p>
                </div>
                <div>
                    <p class="text-sm font-medium text-gray-600">تاريخ الاعتماد</p>
                    <p class="mt-1 text-lg font-bold text-green-800">{{ $document->approved_at ? $document->approved_at->format('Y-m-d') : 'غير محدد' }}</p>
                </div>
                <div>
                    <p class="text-sm font-medium text-gray-600">وقت الاعتماد</p>
                    <p class="mt-1 text-lg font-bold text-green-800">{{ $document->approved_at ? $document->approved_at->format('H:i:s') : 'غير محدد' }}</p>
                </div>
            </div>
        </div>
        @endif

        <!-- Notes -->
        @if($document->notes)
        <div class="bg-blue-50 border border-blue-300 rounded-lg p-6 mb-6">
            <h2 class="text-lg font-semibold text-gray-900 mb-3">📝 ملاحظات</h2>
            <p class="text-sm text-gray-700 whitespace-pre-wrap">{{ $document->notes }}</p>
        </div>
        @endif

        <!-- Document Items Table -->
        <div class="mb-6">
            <h2 class="text-lg font-semibold text-gray-900 mb-4">الأصناف المحولة</h2>
            <div class="overflow-x-auto border border-gray-200 rounded-lg">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead class="bg-gradient-to-r from-gray-800 to-gray-900 text-white">
                        <tr>
                            <th scope="col" class="px-6 py-4 text-right text-sm font-semibold">الصنف</th>
                            <th scope="col" class="px-6 py-4 text-right text-sm font-semibold">الوحدة</th>
                            <th scope="col" class="px-6 py-4 text-center text-sm font-semibold">الكمية</th>
                            <th scope="col" class="px-6 py-4 text-center text-sm font-semibold">سعر الوحدة</th>
                            <th scope="col" class="px-6 py-4 text-center text-sm font-semibold">الإجمالي</th>
                            <th scope="col" class="px-6 py-4 text-right text-sm font-semibold">ملاحظات</th>
                        </tr>
                    </thead>
                    <tbody class="bg-white divide-y divide-gray-200">
                        @php $totalCost = 0; @endphp
                        @forelse($document->items as $item)
                            @php $totalCost += $item->total_cost; @endphp
                            <tr class="hover:bg-blue-50 transition-colors">
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">{{ $item->item->name ?? 'غير محدد' }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-600">{{ $item->unit->name ?? 'غير محدد' }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-center font-semibold text-blue-600">{{ number_format($item->quantity, 2) }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-center text-gray-600">{{ number_format($item->unit_cost, 2) }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-center font-semibold text-green-600">{{ number_format($item->total_cost, 2) }}</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-600">{{ $item->notes ?? '-' }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="px-6 py-8 text-center text-gray-500">
                                    <svg class="mx-auto h-12 w-12 text-gray-400 mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 13V6a2 2 0 00-2-2H6a2 2 0 00-2 2v7m16 0v5a2 2 0 01-2 2H6a2 2 0 01-2-2v-5m16 0h-2.586a1 1 0 00-.707.293l-2.414 2.414a1 1 0 01-.707.293h-3.172a1 1 0 01-.707-.293l-2.414-2.414A1 1 0 006.586 13H4"/>
                                    </svg>
                                    لا توجد أصناف
                                </td>
                            </tr>
                        @endforelse
                        <tr class="bg-gray-100 font-bold text-lg">
                            <td colspan="4" class="px-6 py-4 text-right">الإجمالي الكلي:</td>
                            <td class="px-6 py-4 text-center text-green-700">{{ number_format($totalCost, 2) }}</td>
                            <td></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Action Buttons -->
        @if($document->status == 'draft')
        <div class="flex justify-end gap-3">
            @can('inventory.transfers.edit')
            <a href="{{ route('inventory.transfers.edit', $document->id) }}" class="inline-flex items-center px-4 py-2 bg-blue-600 text-white font-semibold rounded-lg hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 transition-colors">
                <svg class="h-5 w-5 ml-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
                </svg>
                تعديل
            </a>
            @endcan
            
            @can('inventory.transfers.approve')
            <form action="{{ route('inventory.transfers.approve', $document->id) }}" method="POST" onsubmit="return confirm('هل أنت متأكد من اعتماد هذا التحويل؟');">
                @csrf
                @method('PATCH')
                <button type="submit" class="inline-flex items-center px-4 py-2 bg-green-600 text-white font-semibold rounded-lg hover:bg-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-green-500 transition-colors">
                    <svg class="h-5 w-5 ml-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
                    </svg>
                    اعتماد
                </button>
            </form>
            @endcan
            
            @can('inventory.transfers.delete')
            <form action="{{ route('inventory.transfers.destroy', $document->id) }}" method="POST" onsubmit="return confirm('هل أنت متأكد من حذف هذا التحويل؟');">
                @csrf
                @method('DELETE')
                <button type="submit" class="inline-flex items-center px-4 py-2 bg-red-600 text-white font-semibold rounded-lg hover:bg-red-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-red-500 transition-colors">
                    <svg class="h-5 w-5 ml-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                    </svg>
                    حذف
                </button>
            </form>
            @endcan
        </div>
        @endif
    </div>
</div>
@endsection
