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

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

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

        <!-- Loan Selection Form -->
        <form method="GET" action="{{ route('loan.savings.withdraw.form') }}">
            <div class="form-row">
                <div class="form-group col-md-6">
                    <label>Select Loan</label>
                    <select name="loan_id" class="form-control" onchange="this.form.submit()">
                        <option value="">-- Select Loan --</option>
                        @foreach($loans as $loan)
                            <option value="{{ $loan->id }}"
                                {{ (isset($selectedLoan) && $selectedLoan->id == $loan->id) ? 'selected' : '' }}>
                                {{ $loan->customer->name ?? 'N/A' }} - Loan #{{ $loan->id }}
                            </option>
                        @endforeach
                    </select>
                </div>
            </div>
        </form>

        @if(isset($selectedLoan))
        <hr>

        <!-- Display Total Savings -->
        <p><strong>Customer:</strong> {{ $selectedLoan->customer->name ?? '-' }}</p>
        <p><strong>Total Savings:</strong> {{ number_format($totalSavings, 2) }}</p>

        <!-- Withdraw Form -->
        <form method="POST" action="{{ route('loan.savings.withdraw') }}">
            @csrf
            <input type="hidden" name="loan_id" value="{{ $selectedLoan->id }}">

            <div class="form-row">
                <div class="form-group col-md-3">
                    <label>Withdraw Amount</label>
                    <input type="number" name="amount" class="form-control" step="0.01" min="1"
                        max="{{ $totalSavings }}" required>
                    <small class="text-muted">Cannot exceed total savings.</small>
                </div>

                <div class="form-group col-md-4">
                    <label>Bank Account</label>
                    <select name="bank_account_id" class="form-control" required>
                        @foreach($bankAccounts as $bank)
                            <option value="{{ $bank->id }}">
                                {{ $bank->name }} (Balance: {{ number_format($bank->current_balance,2) }})
                            </option>
                        @endforeach
                    </select>
                </div>

                <div class="form-group col-md-3">
                    <label>Date</label>
                      <input type="text"
                        data-provide="datepicker"
                        data-date-today-highlight="true"
                        data-orientation="bottom"
                        data-date-format="yyyy-mm-dd"
                        data-date-autoclose="true"
                        class="form-control"
                        name="withdraw_date"
                        placeholder="Date"
                        autocomplete="off"
                        value="{{ old('withdraw_date', date('Y-m-d')) }}"
                        required>
                </div>
            </div>

            <button type="submit" class="btn btn-danger mt-3">Withdraw Savings</button>
        </form>
        @endif

    </div>
</div>
@endsection
