@extends('layouts.app')

@section('title', 'Savings Accounts')

@section('content')
<div class="container-fluid">
    <!-- 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-piggy-bank me-2 text-primary"></i>Savings Accounts</h1>
                    <p class="text-muted mb-0">Manage and track all savings accounts</p>
                </div>
            </div>
            <div class="col-md-6 text-end">
                <div class="action-buttons-nav">
                    <a href="{{ route('savings.accounts.print-all', request()->query()) }}" class="btn btn-outline-primary" target="_blank">
                        <i class="fas fa-print me-1"></i>Print All
                    </a>
                    <a href="{{ route('savings.accounts.download-all', request()->query()) }}" class="btn btn-primary">
                        <i class="fas fa-download me-1"></i>Download All
                    </a>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-12">
            <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-piggy-bank"></i>
                                    </div>
                                    <h3 class="stats-number">{{ \App\Models\SavingsAccount::count() }}</h3>
                                    <p class="stats-label">Total Accounts</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-money-bill-wave"></i>
                                    </div>
                                    <h3 class="stats-number">USD {{ number_format(\App\Models\SavingsAccount::sum('current_balance'), 2) }}</h3>
                                    <p class="stats-label">Total Balance</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-check-circle"></i>
                                    </div>
                                    <h3 class="stats-number">{{ \App\Models\SavingsAccount::active()->count() }}</h3>
                                    <p class="stats-label">Active Accounts</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-exchange-alt"></i>
                                    </div>
                                    <h3 class="stats-number">{{ \App\Models\SavingsTransaction::count() }}</h3>
                                    <p class="stats-label">Total Transactions</p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Savings Accounts Table -->
                    <div class="section-wrapper">
                        <div class="section-header">
                            <div class="icon-wrapper">
                                <i class="fas fa-list"></i>
                            </div>
                            <h5>Savings Accounts</h5>
                        </div>
                        <div class="table-responsive">
                            <table class="table table-hover client-table">
                                <thead>
                                    <tr>
                                        <th>ACCOUNT NO.</th>
                                        <th>CLIENT</th>
                                        <th>PRODUCT</th>
                                        <th>TYPE</th>
                                        <th>BALANCE</th>
                                        <th>INTEREST RATE</th>
                                        <th>STATUS</th>
                                        <th>CREATED</th>
                                        <th>ACTIONS</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse($savingsAccounts as $account)
                                    <tr>
                                        <td>
                                            <strong>{{ $account->account_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>
                                                    @if ($account->client)
                                                      <div class="fw-semibold">{{ $account->client->full_name }}</div>
                                                      <small class="text-muted">{{ $account->client->client_number }}</small>
                                                    @else
                                                      <div class="fw-semibold text-danger">Client Missing</div>
                                                    @endif
                                                </div>
                                            </div>
                                        </td>
                                        <td>{{ $account->product_name }}</td>
                                        <td>
                                            <span class="status-badge badge bg-info">{{ ucfirst($account->type) }}</span>
                                        </td>
                                        <td class="fw-semibold text-success">USD {{ number_format($account->current_balance, 2) }}</td>
                                        <td>{{ $account->interest_rate }}%</td>
                                        <td>
                                            @if($account->status == 'active')
                                                <span class="status-badge badge bg-success">
                                                    <i class="fas fa-check-circle me-1"></i>Active
                                                </span>
                                            @elseif($account->status == 'dormant')
                                                <span class="status-badge badge bg-warning">
                                                    <i class="fas fa-clock me-1"></i>Dormant
                                                </span>
                                            @elseif($account->status == 'closed')
                                                <span class="status-badge badge bg-secondary">
                                                    <i class="fas fa-times-circle me-1"></i>Closed
                                                </span>
                                            @else
                                                <span class="status-badge badge bg-danger">
                                                    <i class="fas fa-ban me-1"></i>Frozen
                                                </span>
                                            @endif
                                        </td>
                                        <td>{{ $account->created_at->format('M d, Y') }}</td>
                                        <td>
                                            <div class="action-buttons-table">
                                                <a href="{{ route('savings.accounts.show', $account) }}" class="btn-action btn-view" title="View Account">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <a href="{{ route('savings.accounts.print-single', $account) }}" class="btn-action btn-print" title="Print Account" target="_blank">
                                                    <i class="fas fa-print"></i>
                                                </a>
                                                <a href="{{ route('savings.accounts.download-single', $account) }}" class="btn-action btn-download" title="Download Account">
                                                    <i class="fas fa-download"></i>
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                    @empty
                                    <tr>
                                        <td colspan="9" class="text-center py-5">
                                            <div class="empty-state">
                                                <i class="fas fa-piggy-bank fa-3x text-muted mb-3"></i>
                                                <h4 class="text-muted">No savings accounts found</h4>
                                                <p class="text-muted mb-3">No savings accounts match your criteria.</p>
                                            </div>
                                        </td>
                                    </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>

                        <!-- Pagination -->
                        @if($savingsAccounts->hasPages())
                        <div class="d-flex justify-content-between align-items-center mt-4">
                            <div class="text-muted">
                                Showing {{ $savingsAccounts->firstItem() }} to {{ $savingsAccounts->lastItem() }} of {{ $savingsAccounts->total() }} accounts
                            </div>
                            {{ $savingsAccounts->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;
    }

    /* 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;
        position: relative;
    }

    .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-print {
        background: rgba(108, 117, 125, 0.1);
        color: #6c757d;
    }

    .btn-print:hover {
        background: #6c757d;
        color: white;
        transform: translateY(-1px);
    }

    .btn-download {
        background: rgba(25, 135, 84, 0.1);
        color: var(--primary-green);
    }

    .btn-download:hover {
        background: var(--primary-green);
        color: white;
        transform: translateY(-1px);
    }

    .dropdown-menu {
        border: none;
        box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
        border-radius: 8px;
        padding: 0.5rem;
    }

    .dropdown-item {
        padding: 0.75rem 1rem;
        border-radius: 6px;
        font-weight: 500;
        transition: all 0.2s ease;
    }

    .dropdown-item:hover {
        background: var(--gray-100);
    }

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

    .empty-state i {
        opacity: 0.5;
    }

    /* Modal Styles */
    .modal-content {
        border: none;
        border-radius: 16px;
        box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
    }

    .modal-header {
        border-bottom: 2px solid var(--gray-200);
        padding: 1.5rem;
    }

    .modal-title {
        font-weight: 700;
        color: var(--gray-900);
    }

    .modal-body {
        padding: 1.5rem;
    }

    .modal-footer {
        border-top: 2px solid var(--gray-200);
        padding: 1.5rem;
    }

    /* 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-success {
        background: var(--primary-green);
        border: none;
    }

    .btn-success:hover {
        background: var(--primary-green-dark);
    }

    /* 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>

<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
