﻿@extends('layouts.app')

@section('title', 'أذون الوارد')

@section('content')
<div class="container m-auto p-4 py-8">
    <div class="flex justify-between items-center mb-6">
        <h1 class="text-2l font-semibold text-gray-900">أذون الوارد</h1>
        @permission('inventory.receipts.create')
        <a href="{{ route('inventory.receipts.create') }}" class="bg-primary-600 hover:bg-primary-700 text-white font-bold py-2 p-4 rounded inline-flex items-center">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 ml-1" viewBox="0 0 20 20" fill="currentColor">
                <path fill-rule="evenodd" d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z" clip-rule="evenodd" />
            </svg>
            <span>إضافة إذن وارد جديد</span>
        </a>
        @endpermission
    </div>

    <div class="bg-white rounded-lg shadow-md overflow-hidden">
        <div class="p-4 border-b">
            <form action="{{ route('inventory.receipts.index') }}" method="GET" class="flex flex-wrap items-end gap-4">
                <div class="mb-2">
                    <label for="search" class="block text-gray-700 font-bold mb-2">بحث</label>
                    <input type="text" id="search" name="search" value="{{ request('search') }}" 
                        class="shadow appearance-none border rounded py-2 p-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
                        placeholder="رقم المستند أو ملاحظات">
                </div>
                
                <div class="mb-2">
                    <label for="warehouse" class="block text-gray-700 font-bold mb-2">المخزن</label>
                    <select id="warehouse" name="warehouse" 
                        class="shadow appearance-none border rounded py-2 p-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
                        <option value="">جميع المخازن</option>
                        @foreach($warehouses as $warehouse)
                            <option value="{{ $warehouse->id }}" {{ request('warehouse') == $warehouse->id ? 'selected' : '' }}>
                                {{ $warehouse->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                
                <div class="mb-2">
                    <label for="status" class="block text-gray-700 font-bold mb-2">الحالة</label>
                    <select id="status" name="status" 
                        class="shadow appearance-none border rounded py-2 p-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
                        <option value="">جميع الحالات</option>
                        <option value="draft" {{ request('status') == 'draft' ? 'selected' : '' }}>مسودة</option>
                        <option value="approved" {{ request('status') == 'approved' ? 'selected' : '' }}>معتمد</option>
                    </select>
                </div>
                
                <div class="mb-2">
                    <label for="date_from" class="block text-gray-700 font-bold mb-2">من تاريخ</label>
                    <input type="date" id="date_from" name="date_from" value="{{ request('date_from') }}" 
                        class="shadow appearance-none border rounded py-2 p-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
                </div>
                
                <div class="mb-2">
                    <label for="date_to" class="block text-gray-700 font-bold mb-2">إلى تاريخ</label>
                    <input type="date" id="date_to" name="date_to" value="{{ request('date_to') }}" 
                        class="shadow appearance-none border rounded py-2 p-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline">
                </div>
                
                <div class="mb-2 flex items-center">
                    <button type="submit" class="bg-primary-600 hover:bg-primary-700 text-white font-bold py-2 p-4 rounded focus:outline-none focus:shadow-outline">
                        بحث
                    </button>
                    <a href="{{ route('inventory.receipts.index') }}" class="mr-2 bg-gray-200 hover:bg-gray-300 text-gray-800 font-bold py-2 p-4 rounded focus:outline-none focus:shadow-outline">
                        إعادة تعيين
                    </a>
                </div>
            </form>
        </div>

        <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>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @forelse($documents as $document)
                    <tr>
                        <td class="p-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                            {{ $document->document_number }}
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            {{ $document->document_date->format('Y-m-d') }}
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            {{ $document->toWarehouse->name }}
                        </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>
                            @endif
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                            {{ Str::limit($document->notes, 30) }}
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap text-sm font-medium">
                            <div class="flex items-center space-y-2 space-y-reverse">
                                @permission('inventory.receipts.view')
                                <a href="{{ route('inventory.receipts.show', $document) }}" class="text-primary-600 hover:text-primary-900">عرض</a>
                                @endpermission
                                
                                @if($document->status == 'draft')
                                    @permission('inventory.receipts.edit')
                                    <a href="{{ route('inventory.receipts.edit', $document) }}" class="text-indigo-600 hover:text-indigo-900 mr-2">تعديل</a>
                                    @endpermission
                                    
                                    @permission('inventory.receipts.approve')
                                    <form action="{{ route('inventory.receipts.approve', $document) }}" method="POST" class="inline">
                                        @csrf
                                        <button type="submit" class="text-green-600 hover:text-green-900 mr-2" onclick="return confirm('هل أنت متأكد من اعتماد هذا المستند؟')">
                                            اعتماد
                                        </button>
                                    </form>
                                    @endpermission
                                    
                                    @permission('inventory.receipts.delete')
                                    <form action="{{ route('inventory.receipts.destroy', $document) }}" method="POST" class="inline">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="text-red-600 hover:text-red-900 mr-2" onclick="return confirm('هل أنت متأكد من حذف هذا المستند؟')">
                                            حذف
                                        </button>
                                    </form>
                                    @endpermission
                                @endif
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="6" class="p-6 py-4 whitespace-nowrap text-sm text-gray-500 text-center">
                            لا توجد أذون وارد
                        </td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        
        <div class="p-4">
            {{ $documents->links() }}
        </div>
    </div>
</div>
@endsection
