@extends('layouts.master')

@section('content')
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h3>Employee Loans</h3>
        <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addLoanModal">
            <i class="fa fa-plus"></i> Add Loan
        </button>
    </div>
    <div class="card-body">

        @if(session('success'))
            <div class="alert alert-success">{{ session('success') }}</div>
        @endif

        <table class="table table-bordered table-hover table-sm">
            <thead class="table-light">
                <tr>
                    <th>#</th>
                    <th>Employee</th>
                    <th>Total Loan</th>
                    <th>Paid</th>
                    <th>Balance</th>
                    <th>Monthly Ded.</th>
                    <th>Start Date</th>
                    <th>Status</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($loans as $loan)
                    <tr>
                        <td>{{ $loop->iteration }}</td>
                        <td>{{ $loan->employee->first_name }} {{ $loan->employee->last_name }}</td>
                        <td class="text-end">{{ number_format($loan->total_amount, 0) }}</td>
                        <td class="text-end">{{ number_format($loan->paid_amount, 0) }}</td>
                        <td class="text-end fw-bold text-danger">{{ number_format($loan->balance, 0) }}</td>
                        <td class="text-end">{{ number_format($loan->monthly_deduction, 0) }}</td>
                        <td>{{ \Carbon\Carbon::parse($loan->start_date)->format('d/m/Y') }}</td>
                        <td>
                            <span class="badge bg-{{ $loan->status === 'active' ? 'success' : ($loan->status === 'completed' ? 'secondary' : 'danger') }}">
                                {{ ucfirst($loan->status) }}
                            </span>
                        </td>
                        <td>
                            <button class="btn btn-warning btn-xs"
                                    data-bs-toggle="modal"
                                    data-bs-target="#editLoanModal{{ $loan->id }}">
                                <i class="fa fa-edit"></i>
                            </button>
                            <form method="POST" action="{{ route('loans.destroy', $loan) }}" class="d-inline"
                                  onsubmit="return confirm('Delete this loan?')">
                                @csrf @method('DELETE')
                                <button class="btn btn-danger btn-xs"><i class="fa fa-trash"></i></button>
                            </form>
                        </td>
                    </tr>

                    {{-- Edit Modal --}}
                    <div class="modal fade" id="editLoanModal{{ $loan->id }}" tabindex="-1">
                        <div class="modal-dialog">
                            <form method="POST" action="{{ route('loans.update', $loan) }}">
                                @csrf @method('PUT')
                                <div class="modal-content">
                                    <div class="modal-header"><h5>Edit Loan</h5></div>
                                    <div class="modal-body row g-3">
                                        <div class="col-6">
                                            <label>Total Amount</label>
                                            <input type="number" name="total_amount" class="form-control" value="{{ $loan->total_amount }}" required>
                                        </div>
                                        <div class="col-6">
                                            <label>Paid Amount</label>
                                            <input type="number" name="paid_amount" class="form-control" value="{{ $loan->paid_amount }}" required>
                                        </div>
                                        <div class="col-6">
                                            <label>Monthly Deduction</label>
                                            <input type="number" name="monthly_deduction" class="form-control" value="{{ $loan->monthly_deduction }}" required>
                                        </div>
                                        <div class="col-6">
                                            <label>Status</label>
                                            <select name="status" class="form-control">
                                                @foreach(['active','completed','cancelled'] as $s)
                                                    <option value="{{ $s }}" {{ $loan->status === $s ? 'selected' : '' }}>{{ ucfirst($s) }}</option>
                                                @endforeach
                                            </select>
                                        </div>
                                        <div class="col-6">
                                            <label>Start Date</label>
                                            <input type="date" name="start_date" class="form-control" value="{{ $loan->start_date->format('Y-m-d') }}" required>
                                        </div>
                                        <div class="col-6">
                                            <label>End Date</label>
                                            <input type="date" name="end_date" class="form-control" value="{{ $loan->end_date?->format('Y-m-d') }}">
                                        </div>
                                        <div class="col-12">
                                            <label>Notes</label>
                                            <textarea name="notes" class="form-control" rows="2">{{ $loan->notes }}</textarea>
                                        </div>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="submit" class="btn btn-primary">Update</button>
                                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>

                @empty
                    <tr><td colspan="9" class="text-center text-muted">No loan records found.</td></tr>
                @endforelse
            </tbody>
        </table>

        {{ $loans->links() }}

    </div>
</div>

{{-- Add Loan Modal --}}
<div class="modal fade" id="addLoanModal" tabindex="-1">
    <div class="modal-dialog">
        <form method="POST" action="{{ route('loans.store') }}">
            @csrf
            <div class="modal-content">
                <div class="modal-header"><h5>Add New Loan</h5></div>
                <div class="modal-body row g-3">
                    <div class="col-12">
                        <label>Employee</label>
                        <select name="employee_id" class="form-control" required>
                            <option value="">— Select Employee —</option>
                            @foreach($employees as $emp)
                                <option value="{{ $emp->id }}">{{ $emp->first_name }} {{ $emp->last_name }} ({{ $emp->employee_no }})</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-6">
                        <label>Total Loan Amount</label>
                        <input type="number" name="total_amount" class="form-control" step="0.01" min="1" required>
                    </div>
                    <div class="col-6">
                        <label>Monthly Deduction</label>
                        <input type="number" name="monthly_deduction" class="form-control" step="0.01" min="0" required>
                    </div>
                    <div class="col-6">
                        <label>Start Date</label>
                        <input type="date" name="start_date" class="form-control" required>
                    </div>
                    <div class="col-6">
                        <label>End Date (optional)</label>
                        <input type="date" name="end_date" class="form-control">
                    </div>
                    <div class="col-12">
                        <label>Notes</label>
                        <textarea name="notes" class="form-control" rows="2"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">Save Loan</button>
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                </div>
            </div>
        </form>
    </div>
</div>
@endsection