@extends('layouts.admin')

@section('title', 'Income Tax Withholding')

@section('content')
<div class="p-6 lg:p-8" x-data="{ showModal: false }" @open-modal.window="showModal = true">
    <div class="flex justify-between items-center mb-6">
        <div>
            <h1 class="text-xl sm:text-2xl lg:text-3xl font-bold text-gray-800">Income Tax Withholding</h1>
            <p class="text-sm text-gray-600">Manage and report on income tax withheld.</p>
        </div>
        <button @click="showModal = true" class="bg-blue-600 text-white px-3 sm:px-4 py-2 rounded-md text-xs sm:text-sm font-semibold hover:bg-blue-700 transition">Add Record</button>
    </div>

    <!-- Filters -->
    <form method="GET" action="{{ url('finance/taxation.income-tax') }}" class="bg-white rounded-lg shadow-md p-4 mb-6 grid grid-cols-1 md:grid-cols-3 gap-4">
        <input type="month" name="period" value="{{ request('period', date('Y-m')) }}" class="form-input">
        <select name="employee_id" class="form-select">
            <option value="">All Employees</option>
            @foreach($employees as $employee)
                <option value="{{ $employee->id }}" {{ request('employee_id') == $employee->id ? 'selected' : '' }}>{{ $employee->name }}</option>
            @endforeach
        </select>
        <button type="submit" class="bg-gray-800 text-white px-4 py-2 rounded-md">Filter</button>
    </form>

    <!-- Withholding Table -->
    <div class="bg-white rounded-lg shadow-md">
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-4 py-2 text-left text-[11px] font-medium text-gray-500 uppercase tracking-wider">Employee</th>
                        <th class="px-4 py-2 text-left text-[11px] font-medium text-gray-500 uppercase tracking-wider">Period</th>
                        <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">Gross Income</th>
                        <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase">Tax Withheld</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    @forelse($withholdings as $item)
                        <tr>
                            <td class="px-6 py-4">{{ $item->employee->name ?? 'N/A' }}</td>
                            <td class="px-6 py-4">{{ \Carbon\Carbon::parse($item->period)->format('F Y') }}</td>
                            <td class="px-6 py-4 text-right">{{ number_format($item->gross_income, 2) }}</td>
                            <td class="px-6 py-4 text-right font-semibold">{{ number_format($item->tax_withheld, 2) }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="4" class="text-center py-10">No withholding records found.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    <!-- Add Record Modal -->
    <div x-show="showModal" @click.away="showModal = false" class="fixed inset-0 bg-gray-600 bg-opacity-50 h-full w-full z-50" x-cloak>
        <div class="relative top-10 mx-auto p-5 border w-full max-w-lg shadow-lg rounded-md bg-white">
            <form action="{{ url('finance/taxation.income-tax.store') }}" method="POST">
                @csrf
                <h3 class="text-lg font-semibold mb-4">Add Withholding Record</h3>
                <div class="space-y-4">
                    <div>
                        <label class="block text-sm">Employee</label>
                        <select name="employee_id" class="form-select" required>
                            <option value="">Select Employee</option>
                            @foreach($employees as $employee)
                                <option value="{{ $employee->id }}">{{ $employee->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm">Period</label>
                        <input type="month" name="period" class="form-input" value="{{ date('Y-m') }}" required>
                    </div>
                    <div>
                        <label class="block text-sm">Gross Income</label>
                        <input type="number" name="gross_income" step="0.01" class="form-input" required>
                    </div>
                    <div>
                        <label class="block text-sm">Taxable Income</label>
                        <input type="number" name="taxable_income" step="0.01" class="form-input" required>
                    </div>
                    <div>
                        <label class="block text-sm">Tax Withheld</label>
                        <input type="number" name="tax_withheld" step="0.01" class="form-input" required>
                    </div>
                </div>
                <div class="flex justify-end space-x-2 mt-6">
                    <button type="button" @click="showModal = false" class="bg-gray-300 text-gray-700 px-4 py-2 rounded-lg">Close</button>
                    <button type="submit" class="bg-blue-600 text-white px-3 sm:px-4 py-2 rounded-md text-xs sm:text-sm font-semibold hover:bg-blue-700 transition">Save Record</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function () {
        @if($errors->any())
            window.dispatchEvent(new CustomEvent('open-modal'));
        @endif
    });
</script>
@endpush



