@extends('layouts.app')

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

@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-minus-circle me-2 text-danger"></i>Savings Withdrawals</h1>
                    <p class="text-muted mb-0">View and manage all savings withdrawal transactions</p>
                </div>
            </div>
            <div class="col-md-6 text-end">
                <div class="action-buttons-nav">
                    <a href="{{ route('savings.withdrawals.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.withdrawals.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-4">
                            <div class="stats-card card bg-danger text-white">
                                <div class="card-body text-center">
                                    <div class="stats-icon">
                                        <i class="fas fa-minus-circle"></i>
                                    </div>
                                    <h3 class="stats-number">{{ $withdrawals->total() }}</h3>
                                    <p class="stats-label">Total Withdrawals</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="stats-card card bg-warning text-dark">
                                <div class="card-body text-center">
                                    <div class="stats-icon">
                                        <i class="fas fa-calculator"></i>
                                    </div>
                                    <h3 class="stats-number">USD {{ number_format($withdrawals->sum('amount'), 2) }}</h3>
                                    <p class="stats-label">Total Amount</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="stats-card card bg-info text-white">
                                <div class="card-body text-center">
                                    <div class="stats-icon">
                                        <i class="fas fa-calendar-alt"></i>
                                    </div>
                                    <h3 class="stats-number">{{ $withdrawals->count() > 0 ? $withdrawals->first()->transaction_date->format('M Y') : 'N/A' }}</h3>
                                    <p class="stats-label">Latest Month</p>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Filters -->
                    <div class="section-wrapper mb-4">
                        <div class="section-header">
                            <div class="icon-wrapper">
                                <i class="fas fa-filter"></i>
                            </div>
                            <h5>Filters</h5>
                        </div>
                        <form method="GET" action="{{ route('savings.withdrawals') }}" class="row g-3">
                            <div class="col-md-3">
                                <label class="form-label">From Date</label>
                                <input type="date" name="from_date" class="form-control" value="{{ request('from_date') }}">
                            </div>
                            <div class="col-md-3">
                                <label class="form-label">To Date</label>
                                <input type="date" name="to_date" class="form-control" value="{{ request('to_date') }}">
                            </div>
                            <div class="col-md-4">
                                <label class="form-label">Search</label>
                                <input type="text" name="search" class="form-control" placeholder="Transaction #, Client Name..." value="{{ request('search') }}">
                            </div>
                            <div class="col-md-2 d-flex align-items-end">
                                <button type="submit" class="btn btn-primary w-100">
                                    <i class="fas fa-search me-1"></i>Filter
                                </button>
                            </div>
                        </form>
                    </div>

                    <!-- Withdrawals Table -->
                    <div class="section-wrapper">
                        <div class="section-header">
                            <div class="icon-wrapper">
                                <i class="fas fa-list"></i>
                            </div>
                            <h5>Withdrawal Transactions</h5>
                        </div>
                        <div class="table-responsive">
                            <table class="table table-hover client-table">
                                <thead>
                                    <tr>
                                        <th>TRANSACTION #</th>
                                        <th>CLIENT</th>
                                        <th>ACCOUNT</th>
                                        <th>AMOUNT</th>
                                        <th>DESCRIPTION</th>
                                        <th>DATE</th>
                                        <th>CREATED BY</th>
                                        <th>ACTIONS</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @forelse($withdrawals as $withdrawal)
                                    <tr>
                                        <td>
                                            <strong>{{ $withdrawal->transaction_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">{{ $withdrawal->savingsAccount->client->full_name }}</div>
                                                    <small class="text-muted">{{ $withdrawal->savingsAccount->client->client_number }}</small>
                                                </div>
                                            </div>
                                        </td>
                                        <td>
                                            <div>
                                                <div class="fw-semibold">{{ $withdrawal->savingsAccount->account_number }}</div>
                                                <small class="text-muted">{{ $withdrawal->savingsAccount->product_name }}</small>
                                            </div>
                                        </td>
                                        <td class="fw-semibold text-danger">USD {{ number_format($withdrawal->amount, 2) }}</td>
                                        <td>{{ $withdrawal->description ?? 'N/A' }}</td>
                                        <td>{{ $withdrawal->transaction_date->format('M d, Y') }}</td>
                                        <td>{{ $withdrawal->creator->name ?? 'N/A' }}</td>
                                        <td>
                                            <div class="action-buttons-table">
                                                <a href="{{ route('savings.accounts.show', $withdrawal->savingsAccount) }}" class="btn-action btn-view" title="View Account">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <button type="button" class="btn-action btn-info" title="View Details" onclick="showTransactionDetails({{ $withdrawal->id }})">
                                                    <i class="fas fa-info-circle"></i>
                                                </button>
                                                <a href="{{ route('savings.withdrawals.print-single', $withdrawal) }}" class="btn-action btn-print" title="Print Receipt" target="_blank">
                                                    <i class="fas fa-print"></i>
                                                </a>
                                                <a href="{{ route('savings.withdrawals.download-single', $withdrawal) }}" class="btn-action btn-download" title="Download Receipt">
                                                    <i class="fas fa-download"></i>
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                    @empty
                                    <tr>
                                        <td colspan="8" class="text-center py-5">
                                            <div class="empty-state">
                                                <i class="fas fa-minus-circle fa-3x text-muted mb-3"></i>
                                                <h4 class="text-muted">No withdrawal transactions found</h4>
                                            </div>
                                        </td>
                                    </tr>
                                    @endforelse
                                </tbody>
                            </table>
                        </div>

                        <!-- Pagination -->
                        @if($withdrawals->hasPages())
                        <div class="d-flex justify-content-between align-items-center mt-4">
                            <div class="text-muted">
                                Showing {{ $withdrawals->firstItem() }} to {{ $withdrawals->lastItem() }} of {{ $withdrawals->total() }} withdrawals
                            </div>
                            {{ $withdrawals->appends(request()->query())->links() }}
                        </div>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Transaction Details Modal -->
<div class="modal fade" id="transactionDetailsModal" tabindex="-1">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Transaction Details</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body" id="transactionDetailsContent">
                <!-- Content will be loaded here -->
            </div>
        </div>
    </div>
</div>

<style>
    :root {
        --primary-red: #dc3545;
        --primary-red-dark: #b02a37;
        --accent-yellow: #fbbf24;
        --accent-yellow-light: #fef3c7;
        --danger-red: #dc3545;
        --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(--danger-red);
    }

    .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-outline-primary {
        border-color: var(--primary-red);
        color: var(--primary-red);
    }

    .action-buttons-nav .btn-outline-primary:hover {
        background: var(--primary-red);
        border-color: var(--primary-red);
        color: white;
    }

    /* 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-red);
        box-shadow: 0 6px 20px rgba(220, 53, 69, 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-red) 0%, var(--accent-yellow) 100%);
        background-clip: text;
        -webkit-background-clip: text;
        border-image: linear-gradient(90deg, var(--primary-red) 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-red);
        display: flex;
        align-items: center;
        gap: 0.75rem;
    }

    .section-header .icon-wrapper {
        width: 36px;
        height: 36px;
        background: var(--primary-red);
        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-red);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-size: 0.875rem;
    }

    /* 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-info {
        background: rgba(14, 165, 233, 0.1);
        color: #0ea5e9;
    }

    .btn-info:hover {
        background: #0ea5e9;
        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(220, 53, 69, 0.1);
        color: #dc3545;
    }

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

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

    /* 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-red);
        border: none;
        box-shadow: 0 4px 12px rgba(220, 53, 69, 0.25);
    }

    .btn-primary:hover {
        background: var(--primary-red-dark);
        transform: translateY(-1px);
        box-shadow: 0 6px 16px rgba(220, 53, 69, 0.35);
    }

    /* 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);
    });
});

function showTransactionDetails(transactionId) {
    // This would typically make an AJAX call to get transaction details
    // For now, we'll show a placeholder
    const modal = document.getElementById('transactionDetailsModal');
    const content = document.getElementById('transactionDetailsContent');

    content.innerHTML = `
        <div class="text-center">
            <i class="fas fa-spinner fa-spin fa-2x text-primary mb-3"></i>
            <p>Loading transaction details...</p>
        </div>
    `;

    // Show modal
    const bsModal = new bootstrap.Modal(modal);
    bsModal.show();

    // In a real implementation, you'd make an AJAX call here
    // For demo purposes, we'll just show some placeholder content
    setTimeout(() => {
        content.innerHTML = `
            <div class="row">
                <div class="col-md-6">
                    <h6>Transaction Information</h6>
                    <p><strong>ID:</strong> ${transactionId}</p>
                    <p><strong>Type:</strong> Withdrawal</p>
                    <p><strong>Status:</strong> Completed</p>
                </div>
                <div class="col-md-6">
                    <h6>Accounting Impact</h6>
                    <p><strong>Credit:</strong> Cash/Bank Account</p>
                    <p><strong>Debit:</strong> Savings Account</p>
                    <p><strong>Journal Entry:</strong> Created</p>
                </div>
            </div>
        `;
    }, 500);
}
</script>
@endsection
