@extends('layouts.app')

@section('title', 'Loans Management')

@section('content')
<div class="container-fluid p-0">
    <!-- Navigation Bar -->
    <div class="navigation-bar">
        <div class="row align-items-center">
            <div class="col-md-6">
                <div class="page-title">
                    <h1><i class="fas fa-money-bill-wave me-2 text-primary"></i>Loans Management</h1>
                    <p class="text-muted mb-0">Manage and track all loan applications</p>
                </div>
            </div>
            <div class="col-md-6 text-end">
                <div class="action-buttons-nav">
                    @if(Auth::user()->hasPermission('loans.view'))
                    <a href="{{ route('loans.print-all', request()->query()) }}" class="btn btn-outline-primary">
                        <i class="fas fa-print me-1"></i>Print All
                    </a>
                    <a href="{{ route('loans.download-all', request()->query()) }}" class="btn btn-outline-success">
                        <i class="fas fa-download me-1"></i>Download All
                    </a>
                    @endif
                </div>
            </div>
        </div>
    </div>

    <div class="row mx-0">
        <div class="col-12 px-0">
            <div class="form-card card">
                <div class="card-body">
                    <!-- Statistics Cards -->
                    <div class="row mb-4">
                        <div class="col-md-3">
                            <div class="stats-card card bg-primary text-white">
                                <div class="card-body text-center">
                                    <div class="stats-icon">
                                        <i class="fas fa-file-invoice-dollar"></i>
                                    </div>
                                    <h3 class="stats-number">{{ $stats['total'] }}</h3>
                                    <p class="stats-label">Total Loans</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stats-card card bg-warning text-dark">
                                <div class="card-body text-center">
                                    <div class="stats-icon">
                                        <i class="fas fa-clock"></i>
                                    </div>
                                    <h3 class="stats-number">{{ $stats['pending'] }}</h3>
                                    <p class="stats-label">Pending Loans</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stats-card card bg-success text-white">
                                <div class="card-body text-center">
                                    <div class="stats-icon">
                                        <i class="fas fa-check-circle"></i>
                                    </div>
                                    <h3 class="stats-number">{{ $stats['approved'] }}</h3>
                                    <p class="stats-label">Approved Loans</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="stats-card card bg-info text-white">
                                <div class="card-body text-center">
                                    <div class="stats-icon">
                                        <i class="fas fa-hand-holding-usd"></i>
                                    </div>
                                    <h3 class="stats-number">{{ $stats['disbursed'] }}</h3>
                                    <p class="stats-label">Disbursed Loans</p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Filters -->
                    <div class="section-wrapper mb-4 mx-0">
                        <div class="section-header">
                            <div class="icon-wrapper">
                                <i class="fas fa-filter"></i>
                            </div>
                            <h5>Filter Loans</h5>
                        </div>
                        <form method="GET" class="row g-3">
                            <div class="col-md-3">
                                <label for="search" class="form-label">Search</label>
                                <input type="text" name="search" id="search" class="form-control" placeholder="Search loans..." value="{{ request('search') }}">
                            </div>
                            <div class="col-md-2">
                                <label for="status" class="form-label">Status</label>
                                <select name="status" id="status" class="form-select">
                                    <option value="">All Status</option>
                                    <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>Pending</option>
                                    <option value="approved" {{ request('status') == 'approved' ? 'selected' : '' }}>Approved</option>
                                    <option value="disbursed" {{ request('status') == 'disbursed' ? 'selected' : '' }}>Disbursed</option>
                                    <option value="rejected" {{ request('status') == 'rejected' ? 'selected' : '' }}>Rejected</option>
                                    <option value="active" {{ request('status') == 'active' ? 'selected' : '' }}>Active</option>
                                    <option value="completed" {{ request('status') == 'completed' ? 'selected' : '' }}>Completed</option>
                                </select>
                            </div>
                            <div class="col-md-2">
                                <label for="loan_type" class="form-label">Loan Type</label>
                                <select name="loan_type" id="loan_type" class="form-select">
                                    <option value="">All Types</option>
                                    <option value="cash" {{ request('loan_type') == 'cash' ? 'selected' : '' }}>Cash Loan</option>
                                    <option value="product" {{ request('loan_type') == 'product' ? 'selected' : '' }}>Product Loan</option>
                                </select>
                            </div>
                            <div class="col-md-3">
                                <label class="form-label d-block">&nbsp;</label>
                                <div class="d-flex gap-2">
                                    <button type="submit" class="btn btn-primary">
                                        <i class="fas fa-search me-1"></i>Filter
                                    </button>
                                    <a href="{{ route('loans.index') }}" class="btn btn-outline-secondary">
                                        <i class="fas fa-times me-1"></i>Clear
                                    </a>
                                </div>
                            </div>
                        </form>
                    </div>

                    <!-- Loans Table -->
                    <div class="section-wrapper">
                        <div class="section-header">
                            <div class="icon-wrapper">
                                <i class="fas fa-list"></i>
                            </div>
                            <h5>Loan Applications</h5>
                        </div>
                        <div class="table-responsive">
                            <table class="table table-hover client-table">
                                <thead>
                                    <tr>
                                        <th>LOAN NUMBER</th>
                                        <th>CLIENT</th>
                                        <th>AMOUNT</th>
                                        <th>INTEREST RATE</th>
                                        <th>STATUS</th>
                                        <th>APPLICATION DATE</th>
                                        <th>ACTIONS</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse($loans as $loan)
                                    <tr>
                                        <td>
                                            <strong>{{ $loan->loan_number }}</strong>
                                        </td>
                                        <td>
                                            <div class="d-flex align-items-center">
                                                <div class="client-avatar me-2">
                                                    <i class="fas fa-user"></i>
                                                </div>
                                                <div>
                                                    <div class="fw-semibold">{{ $loan->client->first_name }} {{ $loan->client->last_name }}</div>
                                                    <small class="text-muted">{{ $loan->client->client_number }}</small>
                                                </div>
                                            </div>
                                        </td>
                                        <td class="fw-semibold">${{ number_format($loan->amount, 2) }}</td>
                                        <td>{{ $loan->interest_rate }}%</td>
                                        <td>
                                            @if($loan->status == 'pending')
                                                <span class="status-badge badge bg-warning">
                                                    <i class="fas fa-clock me-1"></i>Pending
                                                </span>
                                            @elseif($loan->status == 'approved')
                                                <span class="status-badge badge bg-success">
                                                    <i class="fas fa-check-circle me-1"></i>Approved
                                                </span>
                                            @elseif($loan->status == 'disbursed')
                                                <span class="status-badge badge bg-info">
                                                    <i class="fas fa-hand-holding-usd me-1"></i>Disbursed
                                                </span>
                                            @elseif($loan->status == 'rejected')
                                                <span class="status-badge badge bg-danger">
                                                    <i class="fas fa-times-circle me-1"></i>Rejected
                                                </span>
                                            @else
                                                <span class="status-badge badge bg-secondary">
                                                    <i class="fas fa-circle me-1"></i>{{ ucfirst($loan->status) }}
                                                </span>
                                            @endif
                                        </td>
                                        <td>{{ $loan->application_date->format('M d, Y') }}</td>
                                        <td>
                                            <div class="action-buttons-table">
                                                <a href="{{ route('loans.show', $loan) }}" class="btn-action btn-view" title="View Loan">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <a href="{{ route('loans.print-single', $loan) }}" class="btn-action btn-print" title="Print Loan" target="_blank">
                                                    <i class="fas fa-print"></i>
                                                </a>
                                                <a href="{{ route('loans.download-single', $loan) }}" class="btn-action btn-download" title="Download Loan">
                                                    <i class="fas fa-download"></i>
                                                </a>
                                                @if(Auth::user()->hasPermission('loans.approve'))
                                                    @if($loan->status == 'pending')
                                                    <a href="{{ route('loans.edit', $loan) }}" class="btn-action btn-edit" title="Edit Loan">
                                                        <i class="fas fa-edit"></i>
                                                    </a>
                                                    @endif
                                                @endif
                                            </div>
                                        </td>
                                    </tr>
                                    @empty
                                    <tr>
                                        <td colspan="7" class="text-center py-5">
                                            <div class="empty-state">
                                                <i class="fas fa-file-invoice-dollar fa-3x text-muted mb-3"></i>
                                                <h4 class="text-muted">No loans found</h4>
                                            </div>
                                        </td>
                                    </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>

                        <!-- Pagination -->
                        @if($loans->hasPages())
                        <div class="d-flex justify-content-center mt-4">
                            {{ $loans->links() }}
                        </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    :root {
        --primary-green: #198754;
        --primary-green-dark: #146c43;
        --accent-yellow: #fbbf24;
        --accent-yellow-light: #fef3c7;
        --success-green: #198754;
        --white: #ffffff;
        --gray-50: #f9fafb;
        --gray-100: #f3f4f6;
        --gray-200: #e5e7eb;
        --gray-300: #d1d5db;
        --gray-600: #4b5563;
        --gray-700: #374151;
        --gray-900: #111827;
        --danger: #ef4444;
    }

    /* Navigation Bar Styles */
    .navigation-bar {
        background: white;
        padding: 1.5rem 0;
        margin-bottom: 2rem;
        border-bottom: 2px solid var(--gray-200);
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
    }

    .page-title h1 {
        color: var(--gray-900);
        font-weight: 700;
        font-size: 1.75rem;
        margin-bottom: 0.25rem;
        display: flex;
        align-items: center;
    }

    .page-title h1 i {
        color: var(--primary-green);
    }

    .page-title p {
        color: var(--gray-600);
        margin-bottom: 0;
    }

    .action-buttons-nav .btn {
        padding: 0.625rem 1.25rem;
        border-radius: 8px;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.025em;
        font-size: 0.875rem;
        transition: all 0.3s ease;
    }

    .action-buttons-nav .btn-primary {
        background: var(--primary-green);
        border: none;
        box-shadow: 0 4px 12px rgba(25, 135, 84, 0.25);
    }

    .action-buttons-nav .btn-primary:hover {
        background: var(--primary-green-dark);
        transform: translateY(-1px);
        box-shadow: 0 6px 16px rgba(25, 135, 84, 0.35);
    }

    /* Form Card */
    .form-card {
        background: white;
        border-radius: 20px;
        box-shadow: 0 15px 40px rgba(0, 0, 0, 0.08);
        border: none;
        overflow: hidden;
    }

    .form-card .card-body {
        padding: 2rem;
    }

    /* Statistics Cards */
    .stats-card {
        border: none;
        border-radius: 16px;
        transition: all 0.3s ease;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }

    .stats-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
    }

    .stats-icon {
        font-size: 2rem;
        margin-bottom: 1rem;
        opacity: 0.8;
    }

    .stats-number {
        font-size: 2.5rem;
        font-weight: 700;
        margin-bottom: 0.5rem;
        line-height: 1;
    }

    .stats-label {
        font-size: 0.9rem;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        font-weight: 600;
        opacity: 0.9;
        margin-bottom: 0;
    }

    /* Section Styles */
    .section-wrapper {
        background: var(--gray-50);
        padding: 1.5rem;
        border-radius: 14px;
        border: 2px solid var(--gray-200);
        transition: all 0.3s ease;
        margin-bottom: 1.5rem;
    }

    .section-wrapper:hover {
        border-color: var(--primary-green);
        box-shadow: 0 6px 20px rgba(25, 135, 84, 0.12);
    }

    .section-header {
        display: flex;
        align-items: center;
        margin-bottom: 1.5rem;
        padding-bottom: 0.75rem;
        border-bottom: 3px solid transparent;
        background: linear-gradient(90deg, var(--primary-green) 0%, var(--accent-yellow) 100%);
        background-clip: text;
        -webkit-background-clip: text;
        border-image: linear-gradient(90deg, var(--primary-green) 0%, var(--accent-yellow) 100%);
        border-image-slice: 1;
        border-bottom: 3px solid;
    }

    .section-header h5 {
        margin: 0;
        font-weight: 700;
        font-size: 1.1rem;
        color: var(--primary-green);
        display: flex;
        align-items: center;
        gap: 0.75rem;
    }

    .section-header .icon-wrapper {
        width: 36px;
        height: 36px;
        background: var(--primary-green);
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-size: 1rem;
    }

    /* Form Controls */
    .form-label {
        font-weight: 600;
        color: var(--gray-700);
        margin-bottom: 0.5rem;
        font-size: 0.9rem;
    }

    .form-control, .form-select {
        border: 2px solid var(--gray-300);
        border-radius: 8px;
        padding: 0.75rem 1rem;
        font-size: 0.95rem;
        transition: all 0.3s ease;
    }

    .form-control:focus, .form-select:focus {
        border-color: var(--primary-green);
        box-shadow: 0 0 0 0.2rem rgba(25, 135, 84, 0.25);
    }

    /* Table Styles */
    .client-table {
        margin-bottom: 0;
    }

    .client-table thead th {
        background: var(--gray-50);
        border-bottom: 2px solid var(--gray-200);
        padding: 1rem 0.75rem;
        font-weight: 700;
        color: var(--gray-700);
        font-size: 0.8rem;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        vertical-align: middle;
    }

    .client-table tbody td {
        padding: 1rem 0.75rem;
        vertical-align: middle;
        border-bottom: 1px solid var(--gray-100);
    }

    .client-table tbody tr {
        transition: all 0.2s ease;
    }

    .client-table tbody tr:hover {
        background: var(--gray-50);
        transform: translateY(-1px);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    }

    /* Client Avatar */
    .client-avatar {
        width: 36px;
        height: 36px;
        background: var(--primary-green);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-size: 0.875rem;
    }

    /* Status Badge */
    .status-badge {
        font-size: 0.75rem;
        padding: 0.5rem 0.75rem;
        border-radius: 20px;
        font-weight: 600;
        display: inline-flex;
        align-items: center;
    }

    .status-badge i {
        font-size: 0.5rem;
    }

    /* Action Buttons Table */
    .action-buttons-table {
        display: flex;
        gap: 0.5rem;
    }

    .btn-action {
        width: 32px;
        height: 32px;
        border: none;
        border-radius: 6px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 0.875rem;
        transition: all 0.3s ease;
        text-decoration: none;
    }

    .btn-view {
        background: rgba(59, 130, 246, 0.1);
        color: #3b82f6;
    }

    .btn-view:hover {
        background: #3b82f6;
        color: white;
        transform: translateY(-1px);
    }

    .btn-edit {
        background: rgba(245, 158, 11, 0.1);
        color: #f59e0b;
    }

    .btn-edit:hover {
        background: #f59e0b;
        color: white;
        transform: translateY(-1px);
    }

    /* Empty State */
    .empty-state {
        padding: 2rem 0;
    }

    .empty-state i {
        opacity: 0.5;
    }

    /* Buttons */
    .btn {
        padding: 0.75rem 1.5rem;
        border-radius: 8px;
        font-weight: 600;
        text-transform: uppercase;
        letter-spacing: 0.025em;
        font-size: 0.875rem;
        transition: all 0.3s ease;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .btn-primary {
        background: var(--primary-green);
        border: none;
        box-shadow: 0 4px 12px rgba(25, 135, 84, 0.25);
    }

    .btn-primary:hover {
        background: var(--primary-green-dark);
        transform: translateY(-1px);
        box-shadow: 0 6px 16px rgba(25, 135, 84, 0.35);
    }

    .btn-outline-secondary {
        border: 2px solid var(--gray-300);
        color: var(--gray-700);
    }

    .btn-outline-secondary:hover {
        background: var(--gray-100);
        border-color: var(--gray-400);
        transform: translateY(-1px);
    }

    /* Responsive Design */
    @media (max-width: 768px) {
        .navigation-bar {
            padding: 1rem 0;
        }

        .page-title h1 {
            font-size: 1.5rem;
        }

        .action-buttons-nav {
            margin-top: 1rem;
        }

        .action-buttons-nav .btn {
            width: 100%;
            margin-bottom: 0.5rem;
        }

        .form-card .card-body {
            padding: 1.5rem;
        }

        .section-wrapper {
            padding: 1rem;
        }

        .stats-number {
            font-size: 2rem;
        }

        .client-table thead {
            display: none;
        }

        .client-table tbody tr {
            display: block;
            margin-bottom: 1rem;
            border: 1px solid var(--gray-200);
            border-radius: 8px;
            padding: 1rem;
        }

        .client-table tbody td {
            display: block;
            text-align: right;
            padding: 0.5rem 0;
            border: none;
        }

        .client-table tbody td::before {
            content: attr(data-label);
            float: left;
            font-weight: 600;
            text-transform: uppercase;
            font-size: 0.75rem;
            color: var(--gray-600);
        }

        .action-buttons-table {
            justify-content: flex-end;
        }
    }
</style>

<!-- Reject Loan Modals -->
@foreach($loans as $loan)
<div class="modal fade" id="rejectModal{{$loan->id}}" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">
                    <i class="fas fa-times-circle text-danger me-2"></i>
                    Reject Loan Application
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form action="{{ route('loans.reject', $loan) }}" method="POST">
                @csrf
                <div class="modal-body">
                    <div class="alert alert-warning">
                        <i class="fas fa-exclamation-triangle me-2"></i>
                        Are you sure you want to reject this loan application? This action cannot be undone.
                    </div>
                    <div class="mb-3">
                        <label for="rejection_reason{{$loan->id}}" class="form-label">Reason for Rejection <span class="text-danger">*</span></label>
                        <textarea class="form-control" id="rejection_reason{{$loan->id}}" name="rejection_reason" rows="4" required
                                  placeholder="Please provide a detailed reason for rejecting this loan application..."></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">
                        <i class="fas fa-times me-1"></i>Cancel
                    </button>
                    <button type="submit" class="btn btn-danger">
                        <i class="fas fa-times-circle me-1"></i>Reject Loan
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
@endforeach

<style>
    /* Additional styles for action buttons */
    .btn-approve {
        background: rgba(25, 135, 84, 0.1);
        color: #198754;
        border: none;
    }

    .btn-approve:hover {
        background: #198754;
        color: white;
    }

    .btn-reject {
        background: rgba(220, 53, 69, 0.1);
        color: #dc3545;
        border: none;
    }

    .btn-reject:hover {
        background: #dc3545;
        color: white;
    }

    .btn-delete {
        background: rgba(220, 53, 69, 0.1);
        color: #dc3545;
        border: none;
    }

    .btn-delete:hover {
        background: #dc3545;
        color: white;
    }

    /* Pagination Styles */
    .pagination {
        margin-bottom: 0;
    }

    .pagination .page-link {
        padding: 0.375rem 0.75rem;
        font-size: 0.5rem;
        line-height: 0.6;
        border-radius: 0.375rem;
        margin: 0 0.125rem;
        border: 1px solid var(--gray-300);
        color: var(--gray-700);
        font-weight: 500;
        transition: all 0.2s ease;
    }

    .pagination .page-link:hover {
        background-color: var(--primary-green);
        border-color: var(--primary-green);
        color: white;
        transform: translateY(-1px);
    }

    .pagination .page-item.active .page-link {
        background-color: var(--primary-green);
        border-color: var(--primary-green);
        color: white;
        box-shadow: 0 2px 4px rgba(25, 135, 84, 0.2);
    }

    .pagination .page-item.disabled .page-link {
        color: var(--gray-400);
        background-color: transparent;
        border-color: var(--gray-300);
    }
</style>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Add data labels for mobile responsive table
    const tableCells = document.querySelectorAll('.client-table tbody td');
    const tableHeaders = document.querySelectorAll('.client-table thead th');

    tableCells.forEach((cell, index) => {
        const headerIndex = index % tableHeaders.length;
        const label = tableHeaders[headerIndex].textContent;
        cell.setAttribute('data-label', label);
    });
});
</script>
@endsection
