﻿@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>
            <a href="{{ route('inventory.reports.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">
                العودة للتقارير
            </a>
        </div>

        <!-- Filters -->
        <div class="mb-6 bg-gray-50 p-4 rounded-md">
            <form action="{{ route('inventory.reports.count_report') }}" 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="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="count_id" class="block text-sm font-medium text-gray-700 mb-1">رقم الجرد</label>
                    <select id="count_id" name="count_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($counts as $count)
                            <option value="{{ $count->id }}" {{ request('count_id') == $count->id ? 'selected' : '' }}>{{ $count->count_number }} - {{ is_string($count->date) ? $count->date : $count->date->format('Y-m-d') }}</option>
                        @endforeach
                    </select>
                </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="in_progress" {{ request('status') == 'in_progress' ? 'selected' : '' }}>قيد التنفيذ</option>
                        <option value="completed" {{ request('status') == 'completed' ? 'selected' : '' }}>مكتمل</option>
                        <option value="approved" {{ request('status') == 'approved' ? 'selected' : '' }}>معتمد</option>
                    </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">
                    <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.reports.count_report') }}" 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>

        <!-- Report Data -->
        <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($countRecords as $count)
                        <tr>
                            <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $count->count_number }}</td>
                            <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ is_string($count->date) ? $count->date : $count->date->format('Y-m-d') }}</td>
                            <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $count->warehouse->name }}</td>
                            <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                @if($count->status == 'draft')
                                    <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-gray-100 text-gray-800">
                                        مسودة
                                    </span>
                                @elseif($count->status == 'in_progress')
                                    <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-yellow-100 text-yellow-800">
                                        قيد التنفيذ
                                    </span>
                                @elseif($count->status == 'completed')
                                    <span class="p-2 inline-flex text-s leading-5 font-semibold rounded-full bg-blue-100 text-blue-800">
                                        مكتمل
                                    </span>
                                @elseif($count->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">{{ $count->items_count }}</td>
                            <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-500">
                                @if($count->total_difference > 0)
                                    <span class="text-green-600">+{{ $count->total_difference }}</span>
                                @elseif($count->total_difference < 0)
                                    <span class="text-red-600">{{ $count->total_difference }}</span>
                                @else
                                    <span>0</span>
                                @endif
                            </td>
                            <td class="p-6 py-4 whitespace-nowrap text-sm font-medium">
                                <a href="{{ route('inventory.count.show', $count->id) }}" class="text-primary-600 hover:text-primary-900">عرض التفاصيل</a>
                            </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">
            {{ $countRecords->appends(request()->query())->links() }}
        </div>
    </div>
</div>
@endsection
