<x-app-layout>
    <x-slot name="header">
        <div class="flex justify-between items-center">
            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                {{ __('Loan Requests') }}
            </h2>
            @if(Auth::user()->employee)
                <a href="{{ route('loans.create') }}" class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 transition ease-in-out duration-150">
                    {{ __('Apply for Loan') }}
                </a>
            @endif
        </div>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            @if(session('success'))
                <div class="mb-4 p-4 bg-green-100 border border-green-400 text-green-700 rounded">
                    {{ session('success') }}
                </div>
            @endif
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 text-gray-900">
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Employee</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Amount</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Term</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Monthly</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Period</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Remaining</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Status</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Actions</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            @foreach($loans as $loan)
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap">{{ $loan->employee->first_name }} {{ $loan->employee->last_name }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap">{{ number_format($loan->amount, 2) }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap">{{ $loan->term_months }} months</td>
                                    <td class="px-6 py-4 whitespace-nowrap">{{ number_format($loan->monthly_installment, 2) }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-xs">
                                        {{ $loan->start_date ? $loan->start_date->format('Y-m-d') : '' }} - {{ $loan->end_date ? $loan->end_date->format('Y-m-d') : '' }}
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-xs">
                                        {{ number_format($loan->remaining_balance, 2) }} ({{ $loan->remaining_months }} m)
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap">
                                        <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full 
                                            {{ $loan->status === 'approved' ? 'bg-green-100 text-green-800' : 
                                               ($loan->status === 'pending' ? 'bg-yellow-100 text-yellow-800' : 'bg-red-100 text-red-800') }}">
                                            {{ ucfirst($loan->status) }}
                                        </span>
                                    </td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                                        <a href="{{ route('loans.show', $loan) }}" class="text-indigo-600 hover:text-indigo-900 mr-3">View</a>
                                        @php
                                            $canApprove = false;
                                            if ($loan->status === 'pending') {
                                                $authPref = $loan->employee->loan_approval_authority ?? 'manager';
                                                if ($authPref === 'hr_admin' && (Auth::user()->is_admin || Auth::user()->hasRole('systemadmin') || Auth::user()->hasRole('superadmin'))) {
                                                    $canApprove = true;
                                                } elseif ($authPref === 'manager' && Auth::user()->employee && Auth::user()->employee->id === $loan->manager_id) {
                                                    $canApprove = true;
                                                }
                                            }
                                        @endphp
                                        @if($canApprove)
                                            <form action="{{ route('loans.approve', $loan) }}" method="POST" class="inline mr-2">
                                                @csrf
                                                <button type="submit" class="inline-flex items-center px-4 py-2 bg-green-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-green-500 active:bg-green-700 focus:outline-none focus:ring-2 focus:ring-green-500 focus:ring-offset-2 transition ease-in-out duration-150">Approve</button>
                                            </form>
                                            <form action="{{ route('loans.reject', $loan) }}" method="POST" class="inline">
                                                @csrf
                                                <button type="submit" class="inline-flex items-center px-4 py-2 bg-red-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-red-500 active:bg-red-700 focus:outline-none focus:ring-2 focus:ring-red-500 focus:ring-offset-2 transition ease-in-out duration-150">Reject</button>
                                            </form>
                                        @endif
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                    <div class="mt-4">
                        {{ $loans->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
