@extends('layouts.app')

@section('title', 'تفاصيل أذن صرف المنتجات')

@section('content')
<div class="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 py-8 px-4 md:px-8">
    <div class="max-w-4xl mx-auto">
        <div class="mb-8">
            <h1 class="text-3xl font-bold text-gray-900 mb-2">تفاصيل أذن صرف المنتجات</h1>
            <p class="text-gray-600">رقم الأذن: {{ $issue->document_number }}</p>
        </div>

        <!-- البيانات الأساسية -->
        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
            <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 p-6">
                <h3 class="text-lg font-semibold text-gray-900 mb-4">المعلومات الأساسية</h3>
                
                <div class="space-y-3">
                    <div>
                        <p class="text-sm text-gray-600">رقم الأذن</p>
                        <p class="text-lg font-semibold text-gray-900">{{ $issue->document_number }}</p>
                    </div>
                    <div>
                        <p class="text-sm text-gray-600">التاريخ</p>
                        <p class="text-lg font-semibold text-gray-900">{{ $issue->document_date->format('Y-m-d') }}</p>
                    </div>
                    <div>
                        <p class="text-sm text-gray-600">المخزن</p>
                        <p class="text-lg font-semibold text-gray-900">{{ $issue->warehouse->name ?? '-' }}</p>
                    </div>
                    <div>
                        <p class="text-sm text-gray-600">الطرف المستقبل</p>
                        <p class="text-lg font-semibold text-gray-900">
                            @if($issue->recipient instanceof \App\Models\FinishedGoodsRecipient)
                                {{ $issue->recipient->name ?? '-' }}
                            @else
                                -
                            @endif
                        </p>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 p-6">
                <h3 class="text-lg font-semibold text-gray-900 mb-4">الإجماليات</h3>
                
                <div class="space-y-3">
                    <div>
                        <p class="text-sm text-gray-600">إجمالي الكمية</p>
                        <p class="text-lg font-semibold text-gray-900">{{ number_format($issue->total_quantity, 2) }}</p>
                    </div>
                    <div>
                        <p class="text-sm text-gray-600">إجمالي التكلفة</p>
                        <p class="text-lg font-semibold text-gray-900">{{ number_format($issue->total_cost, 2) }}</p>
                    </div>
                    <div>
                        <p class="text-sm text-gray-600">الحالة</p>
                        <div class="mt-1">
                            @if($issue->status === 'draft')
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-yellow-100 text-yellow-700">
                                <i class="fas fa-pause-circle ml-1"></i>
                                مسودة
                            </span>
                            @elseif($issue->status === 'approved')
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-700">
                                <span class="w-2 h-2 bg-green-500 rounded-full ml-2"></span>
                                معتمد
                            </span>
                            @endif
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- الأصناف -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 mb-6">
            <div class="p-6 border-b border-gray-200">
                <h3 class="text-lg font-semibold text-gray-900">الأصناف</h3>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-50 border-b border-gray-200">
                        <tr>
                            <th class="px-6 py-4 text-right font-semibold text-gray-700 text-sm">#</th>
                            <th class="px-6 py-4 text-right font-semibold text-gray-700 text-sm">الصنف</th>
                            <th class="px-6 py-4 text-right font-semibold text-gray-700 text-sm">الوحدة</th>
                            <th class="px-6 py-4 text-right font-semibold text-gray-700 text-sm">الكمية</th>
                            <th class="px-6 py-4 text-right font-semibold text-gray-700 text-sm">سعر الوحدة</th>
                            <th class="px-6 py-4 text-right font-semibold text-gray-700 text-sm">الإجمالي</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($issue->items as $item)
                        <tr class="hover:bg-gray-50 transition-colors">
                            <td class="px-6 py-4 text-gray-600 font-medium">{{ $loop->iteration }}</td>
                            <td class="px-6 py-4 text-gray-900 font-medium">{{ $item->item->name ?? '-' }}</td>
                            <td class="px-6 py-4 text-gray-600">{{ $item->unit->name ?? '-' }}</td>
                            <td class="px-6 py-4 text-gray-600 text-sm">{{ number_format($item->quantity, 2) }}</td>
                            <td class="px-6 py-4 text-gray-600 text-sm">{{ number_format($item->unit_cost, 2) }}</td>
                            <td class="px-6 py-4 text-gray-900 font-semibold">{{ number_format($item->quantity * $item->unit_cost, 2) }}</td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" class="px-6 py-12 text-center text-gray-600">لا توجد أصناف</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <!-- الملاحظات -->
        @if($issue->description)
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 mb-6 p-6">
            <h3 class="text-lg font-semibold text-gray-900 mb-4">الملاحظات</h3>
            <p class="text-gray-700 whitespace-pre-wrap">{{ $issue->description }}</p>
        </div>
        @endif

        <!-- الأزرار -->
        <div class="flex gap-4">
            <a href="{{ route('inventory.finished-goods-issues.index') }}" 
               class="px-6 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300 transition-colors font-medium">
                <i class="fas fa-arrow-right ml-1"></i>
                رجوع
            </a>

            @if($issue->status === 'draft')
            @can('update', $issue)
            <a href="{{ route('inventory.finished-goods-issues.edit', $issue) }}" 
               class="px-6 py-2 bg-amber-600 text-white rounded-lg hover:bg-amber-700 transition-colors font-medium">
                <i class="fas fa-edit ml-1"></i>
                تعديل
            </a>
            @endcan

            @can('delete', $issue)
            <form action="{{ route('inventory.finished-goods-issues.destroy', $issue) }}" 
                  method="POST" class="inline" onsubmit="return confirm('هل أنت متأكد؟')">
                @csrf @method('DELETE')
                <button type="submit" 
                        class="px-6 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors font-medium">
                    <i class="fas fa-trash ml-1"></i>
                    حذف
                </button>
            </form>
            @endcan

            @can('approve', $issue)
            <form action="{{ route('inventory.finished-goods-issues.approve', $issue) }}" 
                  method="POST" class="inline" onsubmit="return confirm('هل تريد اعتماد هذه الأذن؟')">
                @csrf
                <button type="submit" 
                        class="px-6 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition-colors font-medium">
                    <i class="fas fa-check ml-1"></i>
                    اعتماد
                </button>
            </form>
            @endcan
            @endif
        </div>
    </div>
</div>
@endsection
