@extends('layouts.master')
@section('title', 'Savings History')

@section('page-header')
<header class="header bg-ui-general">
    <div class="header-info">
        <h1 class="header-title"><strong>Savings History - {{ $customer->name }}</strong></h1>
        <small>{{ $customer->mobile_no }} | Account: {{ $customer->account_no }}</small>
        <br>
        <strong>Total Savings:</strong> {{ number_format($totalSavings, 2) }}
    </div>
</header>
@endsection

@section('content')
<div class="col-md-12">

    {{-- Deposits --}}
    <div class="card mb-3">
        <h4 class="card-title fw-400 p-3">Deposits</h4>
        <div class="card-body table-responsive">
            <table class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Amount</th>
                        <th>Bank Account</th>
                        <th>Deposit Date</th>
                        <th width="100">Action</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($deposits as $key => $deposit)
                        <tr>
                            <td>{{ $key + 1 }}</td>
                            <td>{{ number_format($deposit->amount, 2) }}</td>
                            <td>{{ $deposit->bankAccount->name ?? '-' }}</td>
                            <td>{{ $deposit->deposit_date }}</td>
                            <td>
                                @can('delete-savings')
                                <form action="{{ route('savings.destroy', $deposit->id) }}" method="POST"
                                      onsubmit="return confirm('Are you sure you want to delete this deposit?');">
                                    @csrf
                                    @method('DELETE')
                                    <button class="btn btn-sm btn-danger" type="submit">
                                        <i class="fa fa-trash"></i> Delete
                                    </button>
                                </form>
                                @endcan
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="text-center text-muted">No deposits found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    {{-- Withdrawals --}}
    <div class="card">
        <h4 class="card-title fw-400 p-3">Withdrawals</h4>
        <div class="card-body table-responsive">
            <table class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Amount</th>
                        <th>Bank Account</th>
                        <th>Withdraw Date</th>
                        <th width="100">Action</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($withdrawals as $key => $withdraw)
                        <tr>
                            <td>{{ $key + 1 }}</td>
                            <td>{{ number_format($withdraw->amount, 2) }}</td>
                            <td>{{ $withdraw->bankAccount->name ?? '-' }}</td>
                            <td>{{ $withdraw->withdraw_date }}</td>
                            <td>
                                @can('delete-savings')
                                <form action="{{ route('savings.withdrawal.delete', $withdraw->id) }}" method="POST"
                                      onsubmit="return confirm('Are you sure you want to delete this withdrawal?');">
                                    @csrf
                                    @method('DELETE')
                                    <button class="btn btn-sm btn-danger" type="submit">
                                        <i class="fa fa-trash"></i> Delete
                                    </button>
                                </form>
                                @endcan
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="5" class="text-center text-muted">No withdrawals found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

</div>
@endsection
