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

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

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

@section('content')
<div class="col-md-12">
    <div class="card card-body mb-2">
        <form action="{{ route('dps.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 }}"
                            {{ old(key: 'customer_id') == $customer->id ? 'selected' : '' }}>
                            {{ $customer->name }} ({{ $customer->mobile_no }}) {{ $customer->account_no }}
                        </option>
                        @endforeach
                    </select>
                </div>

                <!-- DPS ID Filter -->
                <div class="col-md-2">
                    <input type="text" name="dps_id" class="form-control" placeholder="DPS ID"
                        value="{{ request('dps_id') }}">
                </div>

                <!-- Status Filter -->
                <div class="col-md-2">
                    <select name="status" class="form-control">
                        <option value="">All Status</option>
                        <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>Pending</option>
                        <option value="active" {{ request('status') == 'active' ? 'selected' : '' }}>Active</option>
                        <option value="completed" {{ request('status') == 'completed' ? 'selected' : '' }}>Completed
                        </option>
                    </select>
                </div>

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

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

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

            </div>
        </form>


    </div>

    <div class="card">
        <h4 class="card-title fw-400"> DPS Accounts </h4>

        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead class="thead-light">
                        <tr>
                            <th>#</th>
                            <th>DPS No</th>
                            <th>Customer</th>
                            <th>Monthly Amount</th>
                            <th>Duration</th>
                            <th>Duration (Years)</th>
                            <th>Total Deposit</th>
                            <th>Interest</th>
                            <th>Maturity Amount</th>
                            <th>Status</th>
                            <th width="140">Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($dpsAccounts as $key => $dps)
                        <tr>
                            <td>{{ $dpsAccounts->firstItem() + $key }}</td>
                            <td>{{ $dps->id }}</td>
                            <td>
                                {{ $dps->customer->name }} <br>
                                <small class="text-muted">{{ $dps->customer->mobile_no }}</small>
                            </td>
                            <td>{{ number_format($dps->monthly_amount, 2) }}</td>
                            <td>{{ $dps->duration_months }} Months</td>
                            <td>{{ number_format($dps->duration_months / 12, 1) }}</td>
                            <td>{{ number_format($dps->total_deposit, 2) }}</td>
                            <td>
                                {{ $dps->interest_rate }}%
                                <small class="text-muted d-block">
                                    ({{ ucfirst($dps->interest_type) }})
                                </small>
                            </td>
                            <td>{{ number_format($dps->maturity_amount, 2) }}</td>
                            <td>
                                @if($dps->status == 'running')
                                <span class="badge badge-warning">Running</span>
                                @elseif($dps->status == 'completed')
                                <span class="badge badge-success">Completed</span>
                                @elseif($dps->status == 'withdrawn')
                                <span class="badge badge-primary">Withdrawn</span>
                                @else
                                <span class="badge badge-danger">Closed</span>
                                @endif
                            </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">
                                        <!-- View DPS -->
                                          @can('show-dps')
                                        <a class="dropdown-item" href="{{ route('dps.show', $dps->id) }}">
                                            <i class="fa fa-eye"></i> View
                                        </a>
                                        @endcan
                                        <!-- Installments / Collection -->
                                         @can('dps_payment')
                                        <a class="dropdown-item" href="{{ route('dps.installments', $dps->id) }}">
                                            <i class="fa fa-list"></i> Collection
                                        </a>
                                        @endcan

                                        <!-- Close DPS if running -->
                                          @can('dps_status')
                                        @if($dps->status == 'running')
                                        <form action="{{ route('dps.close', $dps->id) }}" method="POST" class="d-inline"
                                            onsubmit="return confirm('Are you sure you want to close this DPS?')">
                                            @csrf
                                            <button class="dropdown-item text-danger" type="submit">
                                                <i class="fa fa-times"></i> Close DPS
                                            </button>
                                        </form>
                                        @endif
                                        @endcan

                                        <!-- Withdraw DPS if eligible -->
                                        <!-- @if(in_array($dps->status, ['closed', 'completed']) ||
                                        now()->gte($dps->maturity_date))
                                        <a class="dropdown-item" href="{{ route('dps.withdraw.form', $dps->id) }}">
                                            <i class="fa fa-hand-holding-usd"></i> Withdraw DPS
                                        </a>
                                        @endif -->
                                    </div>
                                </div>
                            </td>

                        </tr>
                        @empty
                        <tr>
                            <td colspan="10" class="text-center text-muted">
                                No DPS accounts found.
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

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

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

</style>
@endsection

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

</script>
@endsection
