@extends('layouts.app')

@section('content')
<div class="row justify-content-center">
    <div class="col-lg-6">
        <div class="card">
            <div class="card-header">
                <h5 class="card-title mb-0">Settle Borrow Transaction</h5>
            </div>
            <div class="card-body">
                <div class="alert alert-info">
                    <strong>Borrow Details:</strong><br>
                    Date: {{ $borrow->created_at->format('Y-m-d H:i') }}<br>
                    Agent: {{ optional($borrow->agent)->name }}<br>
                    Currency: {{ optional($borrow->currency)->code }}<br>
                    Quantity: {{ number_format($borrow->quantity, 4) }}<br>
                    Initial Agreed Rate: {{ number_format($borrow->rate, 4) }}
                </div>
                
                <form method="POST" action="{{ route('borrows.process-settlement', $borrow) }}">
                    @csrf
                    <div class="row g-3">
                        <div class="col-md-12">
                            <label class="form-label" for="payment_account_id">Payment Account (To pay the agent)</label>
                            <select class="form-select" id="payment_account_id" name="payment_account_id" required>
                                <option value="">Select payment account</option>
                                @foreach($accounts as $account)
                                    <option value="{{ $account->id }}" @selected(old('payment_account_id') == $account->id)>
                                        {{ $account->name }} - {{ number_format((float) $account->balance, 2) }} {{ $account->currency->code }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-12">
                            <label class="form-label" for="rate">Final Settlement Rate</label>
                            <input class="form-control text-end atm-decimal" id="rate" name="rate" inputmode="numeric" placeholder="0.00" value="{{ old('rate', $borrow->rate) }}" required>
                            <small class="text-muted">If this rate differs from the initial rate, past profits will be recalculated automatically.</small>
                        </div>
                    </div>
                    <div class="d-flex justify-content-end gap-2 mt-4">
                        <a href="{{ route('borrows.index') }}" class="btn btn-outline-secondary">Cancel</a>
                        <button class="btn btn-success" type="submit">Settle and Pay Agent</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection
