﻿@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-2l font-bold text-gray-900">أذون الصرف</h1>
            @can('inventory.issue.create')
            <a href="{{ route('inventory.issue.create') }}" class="inline-flex items-center p-4 py-2 bg-primary-600 text-white rounded-md hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-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="M12 6v6m0 0v6m0-6h6m-6 0H6" />
                </svg>
                إضافة إذن صرف جديد
            </a>
            @endcan
        </div>

        <!-- Filters -->
        <div class="mb-6 bg-gray-50 p-4 rounded-md">
            <form action="{{ route('inventory.issue.index') }}" method="GET" class="flex flex-wrap items-end space-y-4 space-y-reverse">
                <div class="w-full md:w-auto mb-4 md:mb-0">
                    <label for="search" class="block text-sm font-medium text-gray-700 mb-1">بحث</label>
                    <input type="text" id="search" name="search" value="{{ request('search') }}" placeholder="رقم الإذن أو الملاحظات" class="block w-full rounded-md border-gray-300 shadow-sm focus:border-primary-500 focus:ring focus:ring-primary-500 focus:ring-opacity-50">
                </div>
                
                <div class="w-full md:w-auto mb-4 md:mb-0">
                    <label for="warehouse_id" class="block text-sm font-medium text-gray-700 mb-1">المخزن</label>
                    <select id="warehouse_id" name="warehouse_id" class="block w-full rounded-md border-gray-300 shadow-sm focus:border-primary-500 focus:ring focus:ring-primary-500 focus:ring-opacity-50">
                        <option value="">جميع المخازن</option>
                        @foreach($warehouses as $warehouse)
                            <option value="{{ $warehouse->id }}" {{ request('warehouse_id') == $warehouse->id ? 'selected' : '' }}>{{ $warehouse->name }}</option>
                        @endforeach
                    </select>
                </div>
                
                <div class="w-full md:w-auto mb-4 md:mb-0">
                    <label for="from_date" class="block text-sm font-medium text-gray-700 mb-1">من تاريخ</label>
                    <input type="date" id="from_date" name="from_date" value="{{ request('from_date') }}" class="block w-full rounded-md border-gray-300 shadow-sm focus:border-primary-500 focus:ring focus:ring-primary-500 focus:ring-opacity-50">
                </div>
                
                <div class="w-full md:w-auto mb-4 md:mb-0">
                    <label for="to_date" class="block text-sm font-medium text-gray-700 mb-1">إلى تاريخ</label>
                    <input type="date" id="to_date" name="to_date" value="{{ request('to_date') }}" class="block w-full rounded-md border-gray-300 shadow-sm focus:border-primary-500 focus:ring focus:ring-primary-500 focus:ring-opacity-50">
                </div>
                
                <div class="w-full md:w-auto mb-4 md:mb-0">
                    <label for="status" class="block text-sm font-medium text-gray-700 mb-1">الحالة</label>
                    <select id="status" name="status" class="block w-full rounded-md border-gray-300 shadow-sm focus:border-primary-500 focus:ring focus:ring-primary-500 focus:ring-opacity-50">
                        <option value="">جميع الحالات</option>
                        <option value="draft" {{ request('status') == 'draft' ? 'selected' : '' }}>مسودة</option>
                        <option value="approved" {{ request('status') == 'approved' ? 'selected' : '' }}>معتمد</option>
                        <option value="cancelled" {{ request('status') == 'cancelled' ? 'selected' : '' }}>ملغي</option>
                    </select>
                </div>
                
                <div class="w-full md:w-auto">
                    <button type="submit" class="inline-flex items-center p-4 py-2 bg-primary-600 text-white rounded-md hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500">
                        تطبيق الفلتر
                    </button>
                    <a href="{{ route('inventory.issue.index') }}" class="inline-flex items-center p-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 mr-2">
                        إعادة ضبط
                    </a>
                </div>
            </form>
        </div>

        <!-- Issue Documents List -->
        <div class="overflow--auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">رقم الإذن</th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">التاريخ</th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">المخزن</th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">المستلم</th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">عدد الأصناف</th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">الحالة</th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">الإجراءات</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @forelse($issueDocuments as $document)
                        <tr>
                            <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $document->document_number }}</td>
                            <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $document->date->format('Y-m-d') }}</td>
                            <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $document->warehouse->name }}</td>
                            <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $document->recipient_name }}</td>
                            <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $document->items_count }}</td>
                            <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                @if($document->status == 'draft')
                                    <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-yellow-100 text-yellow-800">
                                        مسودة
                                    </span>
                                @elseif($document->status == 'approved')
                                    <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                                        معتمد
                                    </span>
                                @elseif($document->status == 'cancelled')
                                    <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-red-100 text-red-800">
                                        ملغي
                                    </span>
                                @endif
                            </td>
                            <td class="p-6 py-4 whitespace-nowrap text-sm font-medium">
                                <div class="flex items-center space-y-3 space-y-reverse">
                                    @can('inventory.issue.show')
                                    <a href="{{ route('inventory.issue.show', $document->id) }}" class="text-blue-600 hover:text-blue-900">عرض</a>
                                    @endcan
                                    
                                    @if($document->status == 'draft')
                                        @can('inventory.issue.edit')
                                        <a href="{{ route('inventory.issue.edit', $document->id) }}" class="text-primary-600 hover:text-primary-900">تعديل</a>
                                        @endcan
                                        
                                        @can('inventory.issue.approve')
                                        <form action="{{ route('inventory.issue.approve', $document->id) }}" method="POST" onsubmit="return confirm('هل أنت متأكد من اعتماد هذا الإذن؟');">
                                            @csrf
                                            <button type="submit" class="text-green-600 hover:text-green-900">اعتماد</button>
                                        </form>
                                        @endcan
                                        
                                        @can('inventory.issue.cancel')
                                        <form action="{{ route('inventory.issue.cancel', $document->id) }}" method="POST" onsubmit="return confirm('هل أنت متأكد من إلغاء هذا الإذن؟');">
                                            @csrf
                                            <button type="submit" class="text-red-600 hover:text-red-900">إلغاء</button>
                                        </form>
                                        @endcan
                                    @endif
                                    
                                    @can('inventory.issue.print')
                                    <a href="{{ route('inventory.issue.print', $document->id) }}" target="_blank" class="text-gray-600 hover:text-gray-900">طباعة</a>
                                    @endcan
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="p-6 py-4 whitespace-nowrap text-sm text-gray-500 text-center">لا توجد أذون صرف مطابقة للفلتر</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <!-- Pagination -->
        <div class="mt-4">
            {{ $issueDocuments->appends(request()->query())->links() }}
        </div>
    </div>
</div>
@endsection
