@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 Agent Balance</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('agent-balances.index') }}">Agent Balance</a></li>
                    <li class="breadcrumb-item active" aria-current="page">Edit</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 Agent Balance</h5>
            </div>
            <div class="card-body">
                @if($agentBalance->is_received)
                    <div class="alert alert-warning mb-4">
                        <strong>Important:</strong> This agent balance was marked as <strong>Received</strong> on {{ $agentBalance->received_at->format('Y-m-d H:i') }}. Updating it will automatically reverse the old balance adjustment on the target account and apply the new details.
                    </div>
                @endif

                <form method="POST" action="{{ route('agent-balances.update', $agentBalance) }}">
                    @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', $agentBalance->agent_id) == $agent->id)>
                                        {{ $agent->name }}
                                    </option>
                                @endforeach
                            </select>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label" for="account_id">Target Account (To receive currency)</label>
                            <select class="form-select" id="account_id" name="account_id" required>
                                <option value="">Select target account</option>
                                @foreach($accounts as $account)
                                    <option value="{{ $account->id }}" @selected(old('account_id', $agentBalance->account_id) == $account->id)>
                                        {{ $account->name }} ({{ $account->currency->code }} - Balance: {{ number_format((float) $account->balance, 4) }})
                                    </option>
                                @endforeach
                            </select>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label" for="quantity">Quantity</label>
                            <input type="number" step="0.0001" min="0.0001" class="form-control" id="quantity" name="quantity" value="{{ old('quantity', (float) $agentBalance->quantity) }}" required>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label" for="rate">Buying Rate</label>
                            <input type="number" step="0.0001" min="0.0001" class="form-control" id="rate" name="rate" value="{{ old('rate', (float) $agentBalance->rate) }}" required>
                        </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', $agentBalance->date->format('Y-m-d')) }}" required>
                        </div>

                        <div class="col-md-12">
                            <label class="form-label" for="description">Description / Reason</label>
                            <textarea class="form-control" id="description" name="description" rows="3" placeholder="e.g. Balance from manual transactions before system usage">{{ old('description', $agentBalance->description) }}</textarea>
                        </div>
                    </div>

                    <div class="d-flex justify-content-end gap-2 mt-4">
                        <a href="{{ route('agent-balances.index') }}" class="btn btn-outline-primary">Cancel</a>
                        <button class="btn btn-primary" type="submit">Update Agent Balance</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection
