﻿@extends('layouts.app')

@section('title', 'تقرير ضريبة الدخل')

@section('content')
<div class="container m-auto p-4 sm:p-6 lg:p-8 py-8">
    <div class="flex justify-between items-center mb-6">
        <h1 class="text-2l font-bold text-gray-900">{{ \App\Helpers\TranslationHelper::transFromDatabase('Income Tax Report', 'تقرير ضريبة الدخل') }}</h1>
        
        <a href="{{ route('payroll-reports.index') }}" class="bg-gray-500 hover:bg-gray-700 text-white font-bold py-2 p-4 rounded">
            <i class="fas fa-arrow-right ml-1"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Back to Reports', 'العودة إلى التقارير') }}
        </a>
    </div>

    <!-- نموذج البحث والتصفية -->
    <div class="bg-white shadow-md rounded-lg overflow-hidden p-6 mb-6">
        <form action="{{ route('payroll-reports.ta') }}" method="GET" class="space-y-4">
            <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                <div>
                    <label for="year" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Year', 'السنة') }}</label>
                    <select id="year" name="year" class="mt-1 block w-full py-2 p-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                        @for($y = date('Y'); $y >= 2020; $y--)
                            <option value="{{ $y }}" {{ request('year', date('Y')) == $y ? 'selected' : '' }}>{{ $y }}</option>
                        @endfor
                    </select>
                </div>
                
                <div>
                    <label for="quarter" class="block text-sm font-medium text-gray-700 mb-1">{{ \App\Helpers\TranslationHelper::transFromDatabase('Quarter', 'الربع') }}</label>
                    <select id="quarter" name="quarter" class="mt-1 block w-full py-2 p-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm">
                        <option value="1" {{ request('quarter', ceil(date('m') / 3)) == 1 ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('First Quarter (Jan - Mar)', 'الربع الأول (يناير - مارس)') }}</option>
                        <option value="2" {{ request('quarter', ceil(date('m') / 3)) == 2 ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Second Quarter (Apr - Jun)', 'الربع الثاني (أبريل - يونيو)') }}</option>
                        <option value="3" {{ request('quarter', ceil(date('m') / 3)) == 3 ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Third Quarter (Jul - Sep)', 'الربع الثالث (يوليو - سبتمبر)') }}</option>
                        <option value="4" {{ request('quarter', ceil(date('m') / 3)) == 4 ? 'selected' : '' }}>{{ \App\Helpers\TranslationHelper::transFromDatabase('Fourth Quarter (Oct - Dec)', 'الربع الرابع (أكتوبر - ديسمبر)') }}</option>
                    </select>
                </div>
                
                <div class="flex items-end">
                    <button type="submit" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 p-4 rounded">
                        <i class="fas fa-search ml-1"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Search', 'بحث') }}
                    </button>
                    
                    <a href="{{ route('payroll-reports.ta') }}" class="bg-gray-500 hover:bg-gray-700 text-white font-bold py-2 p-4 rounded mr-2">
                        <i class="fas fa-redo ml-1"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Reset', 'إعادة تعيين') }}
                    </a>
                    
                    <button type="button" onclick="window.print()" class="bg-green-600 hover:bg-green-700 text-white font-bold py-2 p-4 rounded mr-2">
                        <i class="fas fa-print ml-1"></i> {{ \App\Helpers\TranslationHelper::transFromDatabase('Print', 'طباعة') }}
                    </button>
                </div>
            </div>
        </form>
    </div>

    <!-- ملخص التقرير -->
    <div class="bg-white shadow-md rounded-lg overflow-hidden p-6 mb-6">
        <h2 class="text-l font-semibold text-gray-900 mb-4">{{ \App\Helpers\TranslationHelper::transFromDatabase('Income Tax Report Summary', 'ملخص تقرير ضريبة الدخل') }} - {{ $year }} - {{ \App\Helpers\TranslationHelper::transFromDatabase('Quarter', 'الربع') }} {{ $quarter }}</h2>
        
        <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <div class="bg-blue-50 p-4 rounded-lg">
                <p class="text-sm font-medium text-blue-500">{{ \App\Helpers\TranslationHelper::transFromDatabase('Total Taxable Income', 'إجمالي الدخل الخاضع للضريبة') }}</p>
                <p class="mt-1 text-lg font-semibold text-blue-900">{{ number_format($totalTaableIncome ?? 0, 2) }} {{ \App\Helpers\TranslationHelper::transFromDatabase('SAR', 'ريال') }}</p>
            </div>
            
            <div class="bg-purple-50 p-4 rounded-lg">
                <p class="text-sm font-medium text-purple-500">{{ \App\Helpers\TranslationHelper::transFromDatabase('Total Income Tax', 'إجمالي ضريبة الدخل') }}</p>
                <p class="mt-1 text-lg font-semibold text-purple-900">{{ number_format($totalTaAmount ?? 0, 2) }} {{ \App\Helpers\TranslationHelper::transFromDatabase('SAR', 'ريال') }}</p>
            </div>
        </div>
    </div>

    <!-- تفاصيل التقرير -->
    <div class="bg-white shadow-md rounded-lg overflow-hidden">
        <div class="p-6 py-4 border-b border-gray-200">
            <h2 class="text-l font-semibold text-gray-900">{{ \App\Helpers\TranslationHelper::transFromDatabase('Employee Income Tax Details', 'تفاصيل ضريبة الدخل للموظفين') }}</h2>
        </div>
        
        @if(isset($employeeTaes) && count($employeeTaes) > 0)
        <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">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Employee Number', 'رقم الموظف') }}
                        </th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Employee Name', 'اسم الموظف') }}
                        </th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('ID/Iqama Number', 'رقم الهوية/الإقامة') }}
                        </th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Department', 'القسم') }}
                        </th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Taxable Income', 'الدخل الخاضع للضريبة') }}
                        </th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Tax Amount', 'مبلغ الضريبة') }}
                        </th>
                        <th scope="col" class="p-6 py-3 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Tax Percentage', 'نسبة الضريبة') }}
                        </th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @foreach($employeeTaes as $employeeId => $taInfo)
                    <tr>
                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            {{ $taInfo['employee']->employee_number }}
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            {{ $taInfo['employee']->name }}
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            {{ $taInfo['employee']->national_id }}
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase($taInfo['employee']->department ?? 'Not Specified', $taInfo['employee']->department ?? 'غير محدد') }}
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            {{ number_format($taInfo['taable_income'], 2) }}
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap text-sm font-medium text-blue-900">
                            {{ number_format($taInfo['ta_amount'], 2) }}
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap text-sm text-gray-900">
                            {{ number_format(($taInfo['ta_amount'] / $taInfo['taable_income']) * 100, 2) }}%
                        </td>
                    </tr>
                    @endforeach
                </tbody>
                <tfoot class="bg-gray-50">
                    <tr>
                        <td colspan="4" class="p-6 py-4 whitespace-nowrap text-sm font-bold text-gray-900">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('Total', 'الإجمالي') }}
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap text-sm font-bold text-gray-900">
                            {{ number_format($totalTaableIncome ?? 0, 2) }}
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap text-sm font-bold text-blue-900">
                            {{ number_format($totalTaAmount ?? 0, 2) }}
                        </td>
                        <td class="p-6 py-4 whitespace-nowrap text-sm font-bold text-gray-900">
                            {{ number_format(($totalTaAmount / $totalTaableIncome) * 100, 2) }}%
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
        @else
        <div class="p-6 py-4 text-center text-gray-500">
            {{ \App\Helpers\TranslationHelper::transFromDatabase('No data available for the selected period.', 'لا توجد بيانات متاحة للفترة المحددة.') }}
        </div>
        @endif
    </div>
</div>
@endsection
