@extends('layouts.app')

@section('content')
<div class="breadcrumbbar">
    <div class="row align-items-center">
        <div class="col-md-8 col-lg-8">
            <h4 class="page-title">Edit Borrow Transaction</h4>
            <div class="breadcrumb-list">
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Home</a></li>
                    <li class="breadcrumb-item"><a href="{{ route('borrows.index') }}">Borrows</a></li>
                    <li class="breadcrumb-item active" aria-current="page">Edit Borrow</li>
                </ol>
            </div>
        </div>
    </div>          
</div>
<div class="row justify-content-center">
    <div class="col-lg-7">
        <div class="card m-b-30">
            <div class="card-header">
                <h5 class="card-title mb-0">Edit Borrow Transaction</h5>
            </div>
            <div class="card-body">
                <form method="POST" action="{{ route('borrows.update', $borrow) }}">
                    @csrf
                    @method('PUT')
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="form-label" for="agent_id">Agent</label>
                            <select class="form-select" id="agent_id" name="agent_id" required>
                                <option value="">Select agent</option>
                                @foreach($agents as $agent)
                                    <option value="{{ $agent->id }}" @selected(old('agent_id', $borrow->agent_id) == $agent->id)>
                                        {{ $agent->name }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label" for="currency_id">Currency</label>
                            <select class="form-select" id="currency_id" name="currency_id" required>
                                <option value="">Select currency</option>
                                @foreach($currencies as $currency)
                                    <option value="{{ $currency->id }}" @selected(old('currency_id', $borrow->currency_id) == $currency->id)>
                                        {{ $currency->code }} - {{ $currency->name }}
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-12">
                            <label class="form-label" for="target_account_id">Target Account (To receive borrowed currency)</label>
                            <select class="form-select" id="target_account_id" name="target_account_id" required>
                                <option value="">Select target account</option>
                                @foreach($accounts as $account)
                                    <option value="{{ $account->id }}" @selected(old('target_account_id', $borrow->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="date">Transaction Date</label>
                            <input type="date" class="form-control" id="date" name="date" value="{{ old('date', $borrow->created_at->format('Y-m-d')) }}" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label" for="quantity">Quantity Borrowed</label>
                            <input class="form-control text-end atm-decimal" id="quantity" name="quantity" inputmode="numeric" placeholder="0.00" value="{{ old('quantity', $borrow->quantity) }}" required>
                        </div>
                        <div class="col-md-6">
                            <label class="form-label" for="rate">Agreed 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>
                        </div>
                    </div>
                    <div class="d-flex justify-content-end gap-2 mt-4">
                        <button class="btn btn-primary" type="submit">Update Borrow Transaction</button>
                        <button type="button" class="btn btn-danger" onclick="if(confirm('Are you sure you want to delete this borrow transaction? This will reverse the account balance addition.')) document.getElementById('delete-transaction-form').submit();">Delete Transaction</button>
                    </div>
                </form>

                <form id="delete-transaction-form" action="{{ route('transactions.destroy', $borrow) }}" method="POST" style="display:none;">
                    @csrf
                    @method('DELETE')
                </form>
            </div>
        </div>
    </div>
</div>
@endsection
