@extends('layouts.master')
@section('title', 'DPS Installments & Payments')

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

    <div class="header-action">
        <nav class="nav">
            <a class="nav-link" href="{{ route('dps.index') }}">DPS Accounts</a>
            <a class="nav-link active" href="#">Installments & Payments</a>
        </nav>
    </div>
</header>
@endsection

@section('content')

{{-- DPS Account Info --}}
<div class="col-md-12 mb-3">
    <div class="card">
        <div class="card-header bg-primary text-white">
            <h5 class="mb-0">DPS Account Info</h5>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-sm-6 col-md-4 mb-2">
                    <strong>Customer:</strong> <br>
                    {{ optional($dps->customer)->name ?? 'N/A' }} ({{ optional($dps->customer)->mobile_no ?? '-' }}) {{ $customer->account_no }}
                </div>
                <div class="col-sm-6 col-md-4 mb-2">
                    <strong>DPS No:</strong> <br>
                    {{ $dps->id }}
                </div>
                <div class="col-sm-6 col-md-4 mb-2">
                    <strong>Duration:</strong> <br>
                    {{ $dps->duration_months }} months
                </div>
                <div class="col-sm-6 col-md-4 mb-2">
                    <strong>Interest Rate:</strong> <br>
                    {{ $dps->interest_rate }}%
                </div>
                <div class="col-sm-6 col-md-4 mb-2">
                    <strong>Start Date:</strong> <br>
                    {{ \Carbon\Carbon::parse($dps->start_date)->format('d M Y') }}
                </div>
                <div class="col-sm-6 col-md-4 mb-2">
                    <strong>Maturity Date:</strong> <br>
                    {{ \Carbon\Carbon::parse($dps->maturity_date)->format('d M Y') }}
                </div>
                <div class="col-sm-6 col-md-4 mb-2">
                    <strong>Status:</strong> <br>
                    @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
                </div>
                <div class="col-sm-6 col-md-4 mb-2">
                    <strong>Withdraw:</strong> <br>
                    @if(in_array($dps->status, ['closed', 'completed']) || now()->gte($dps->maturity_date))
                        <a href="{{ route('dps.withdraw.form', $dps->id) }}" class="btn btn-danger">
                            <i class="fa fa-hand-holding-usd"></i> Withdraw DPS
                        </a>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>

<div class="col-md-12">
    <!-- Installments Table -->
    <div class="card mb-3">
        <div class="card-header">
            <h4 class="card-title fw-400">Installments</h4>
        </div>
        <div class="card-body table-responsive">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Installment Date</th>
                        <th>Amount</th>
                        <th>Remaining</th>
                        <th>Status</th>
                        <th>Payment Date</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($dps->installments as $key => $inst)
                    <tr>
                        <td>{{ $key + 1 }}</td>
                        <td>{{ \Carbon\Carbon::parse($inst->installment_date)->format('d M Y') }}</td>
                        <td>{{ number_format($inst->amount, 2) }}</td>
                        <td>{{ number_format($inst->remaining_amount ?? $inst->amount, 2) }}</td>
                        <td>
                            @if($inst->is_paid)
                                <span class="badge badge-success">Paid</span>
                            @else
                                <span class="badge badge-warning">Due</span>
                            @endif
                        </td>
                        <td>{{ $inst->paid_at ? \Carbon\Carbon::parse($inst->paid_at)->format('d M Y') : '-' }}</td>
                        <td>
                            @if($dps->status === 'running' && !$inst->is_paid)
                                <!-- Collect Payment Button -->
                                <button type="button" class="btn btn-sm btn-success" data-toggle="modal"
                                    data-target="#collectPaymentModal{{ $inst->id }}">
                                    <i class="fa fa-money-bill"></i> Collect
                                </button>

                                <!-- Modal -->
                                <div class="modal fade" id="collectPaymentModal{{ $inst->id }}" tabindex="-1"
                                    role="dialog" aria-labelledby="collectPaymentModalLabel{{ $inst->id }}"
                                    aria-hidden="true">
                                    <div class="modal-dialog" role="document">
                                        <form action="{{ route('dps.installments.collect', $inst->id) }}" method="POST">
                                            @csrf
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <h5 class="modal-title">
                                                        Collect Payment - Installment #{{ $key + 1 }}
                                                    </h5>
                                                    <button type="button" class="close" data-dismiss="modal"
                                                        aria-label="Close">
                                                        <span aria-hidden="true">&times;</span>
                                                    </button>
                                                </div>
                                                <div class="modal-body">
                                                    <div class="form-group">
                                                        <label>Payment Amount<span class="text-danger">*</span></label>
                                                        <input type="number" step="0.01" name="amount"
                                                            class="form-control"
                                                            value="{{ $inst->remaining_amount ?? $inst->amount }}"
                                                            required>
                                                        <small class="text-muted">
                                                            Remaining: {{ number_format($inst->remaining_amount ?? $inst->amount, 2) }}
                                                        </small>
                                                    </div>
                                                    <div class="form-group">
                                                        <label>Payment Date<span class="text-danger">*</span></label>
                                                        <input type="date" name="paid_date" class="form-control"
                                                            value="{{ date('Y-m-d') }}" required>
                                                    </div>
                                                    <div class="form-group">
                                                        <label>Bank Account</label>
                                                        <select name="bank_account_id" class="form-control" required>
                                                            @foreach(App\BankAccount::all() as $account)
                                                                <option value="{{ $account->id }}">{{ $account->name }}</option>
                                                            @endforeach
                                                        </select>
                                                    </div>
                                                </div>
                                                <div class="modal-footer">
                                                    <button type="button" class="btn btn-secondary"
                                                        data-dismiss="modal">Cancel</button>
                                                    <button type="submit" class="btn btn-success">Collect Payment</button>
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            @else
                                <span class="text-muted">Done</span>
                            @endif
                        </td>
                    </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>

    <!-- Payments Table -->
    <div class="card">
        <div class="card-header">
            <h4 class="card-title fw-400">Payments</h4>
        </div>
        <div class="card-body table-responsive">
            <table class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Installment No</th>
                        <th>Paid Amount</th>
                        <th>Payment Date</th>
                        <th>Bank Account</th>
                    </tr>
                </thead>
                <tbody>
                    @php $counter = 1; @endphp
                    @forelse($dps->installments as $installment)
                        @foreach($installment->payments as $payment)
                            <tr>
                                <td>{{ $counter++ }}</td>
                                <td>{{ $installment->installment_no ?? $loop->iteration }}</td>
                                <td>{{ number_format($payment->amount, 2) }}</td>
                                <td>{{ \Carbon\Carbon::parse($payment->payment_date)->format('d-m-Y') }}</td>
                                <td>{{ optional($payment->bankAccount)->name ?? '-' }}</td>
                            </tr>
                        @endforeach
                    @empty
                        <tr>
                            <td colspan="6" class="text-center">No payments found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>

@endsection
