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

@section('page-header')
<header class="header bg-ui-general">
    <div class="header-info">
        <h1 class="header-title">
            <strong>Savings List</strong>
        </h1>
    </div>

    <div class="header-action">
        <nav class="nav">
            <a class="nav-link active" href="{{ route('savings.index') }}">
                Savings List
            </a>
            <a class="nav-link" href="{{ route('savings.create') }}">
                <i class="fa fa-plus"></i>
                New Savings
            </a>
        </nav>
    </div>
</header>
@endsection

@section('content')
<div class="col-md-12">
    {{-- Filters --}}
    <div class="card card-body mb-2">
        <form action="{{ route('savings.index') }}" method="GET" class="mb-3">
            <div class="row">

                <!-- Customer Filter -->
                <div class="col-md-3">
                    <select name="customer_id" class="form-control" data-provide="selectpicker" data-live-search="true">
                        <option value="">Select Customer</option>
                        @foreach($customers as $customer)
                        <option value="{{ $customer->id }}"
                            {{ request('customer_id') == $customer->id ? 'selected' : '' }}>
                            {{ $customer->name }} ({{ $customer->mobile_no }}) {{ $customer->account_no }}
                        </option>
                        @endforeach
                    </select>
                </div>


                <!-- Start Date -->
                <div class="col-md-3">
                    <input type="text" class="form-control" data-provide="datepicker" data-date-format="yyyy-mm-dd"
                        name="from_date" placeholder="From Date" value="{{ request('from_date') }}">
                </div>

                <!-- End Date -->
                <div class="col-md-3">
                    <input type="text" class="form-control" data-provide="datepicker" data-date-format="yyyy-mm-dd"
                        name="to_date" placeholder="To Date" value="{{ request('to_date') }}">
                </div>

                <!-- Buttons -->
                <div class="col-md-3">
                    <button type="submit" class="btn btn-primary btn-block">Filter</button>
                    <a href="{{ route('savings.index') }}" class="btn btn-secondary btn-block mt-1">Reset</a>
                </div>

            </div>
        </form>
    </div>

    {{-- Savings Table --}}
    <div class="card">
        <h4 class="card-title fw-400"> Customer Savings </h4>

        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead class="thead-light">
                        <tr>
                            <th>#</th>
                            <th>Savings ID</th>
                            <th>Customer</th>
                            <th>Amount</th>
                            <th>Bank Account</th>
                            <th>Deposit Date</th>
                            
                            <th width="120">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($savings as $key => $saving)
                        <tr>
                            <td>{{ $key + 1 }}</td>
                            <td>{{ $saving->id }}</td>
                            <td>
                                {{ $saving->customer->name }} <br>
                                <small class="text-muted">{{ $saving->customer->mobile_no }}</small>
                            </td>
                            <td>{{ number_format($saving->amount, 2) }}</td>
                            <td>{{ $saving->bankAccount->name ?? 'N/A' }}</td>
                            <td>{{ \Carbon\Carbon::parse($saving->paid_date)->format('Y-m-d') }}</td>
                            <td>
                                <div class="btn-group">
                                    <button class="btn btn-primary dropdown-toggle" data-toggle="dropdown"
                                        aria-expanded="false">
                                        <i class="fa fa-cogs"></i>
                                    </button>
                                    <div class="dropdown-menu">
                                        @can('show-savings')
                                         <a class="dropdown-item" href="{{ route('savings.history', $saving->customer->id) }}">
                                            <i class="fa fa-history"></i> History
                                        </a>
                                        @endcan
                                        @can('delete-savings')
                                        <form action="{{ route('savings.destroy', $saving->id) }}" method="POST" class="d-inline"
                                            onsubmit="return confirm('Are you sure you want to delete this saving?')">
                                            @csrf
                                            @method('DELETE')
                                            <button class="dropdown-item text-danger" type="submit">
                                                <i class="fa fa-trash"></i> Delete
                                            </button>
                                        </form>
                                        @endcan
                                    </div>
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="8" class="text-center text-muted">No savings found.</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            {{-- Pagination if needed --}}
            <div class="mt-3">{{ $savings->links() }}</div> 
        </div>
    </div>
</div>
@endsection

@section('styles')
<style>
.badge {
    font-size: 90%;
}
</style>
@endsection

@section('scripts')
<script>
/* Optional JS */
</script>
@endsection
