@extends('layouts.app')

@section('content')
<div class="card">
    <div class="card-body">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h1 class="h4 mb-0">Borrow Transactions</h1>
            <a href="{{ route('borrows.create') }}" class="btn btn-primary">Record Borrow</a>
        </div>

        <form class="d-flex flex-wrap flex-md-nowrap gap-2 mb-4 align-items-center" method="GET" action="{{ route('borrows.index') }}">
            <div class="flex-grow-1">
                <input type="text" class="form-control" name="search" value="{{ $filters['search'] ?? '' }}" placeholder="Search description, currency...">
            </div>
            <div class="flex-grow-1">
                <select class="form-select" name="agent_id">
                    <option value="">All Agents</option>
                    @foreach($agents as $agent)
                        <option value="{{ $agent->id }}" @selected(($filters['agent_id'] ?? '') == $agent->id)>
                            {{ $agent->name }}
                        </option>
                    @endforeach
                </select>
            </div>
            <div>
                <input type="date" class="form-control" name="date_from" value="{{ $filters['date_from'] ?? '' }}" title="From Date">
            </div>
            <div>
                <input type="date" class="form-control" name="date_to" value="{{ $filters['date_to'] ?? '' }}" title="To Date">
            </div>
            <div class="d-flex gap-1">
                <button type="submit" class="btn btn-primary px-3">Filter</button>
                <a href="{{ route('borrows.index') }}" class="btn btn-outline-secondary px-3">Reset</a>
            </div>
        </form>
        
        <div class="table-responsive">
            <table class="table table-striped align-middle">
                <thead>
                    <tr>
                        <th>Date</th>
                        <th>Agent</th>
                        <th>Currency</th>
                        <th>Target Account</th>
                        <th class="text-end">Quantity</th>
                        <th class="text-end">Rate</th>
                        <th>Status</th>
                        <th class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($borrows as $borrow)
                        <tr>
                            <td>{{ $borrow->created_at->format('Y-m-d') }}</td>
                            <td>{{ optional($borrow->agent)->name }}</td>
                            <td>{{ optional($borrow->currency)->code }}</td>
                            <td>{{ optional($borrow->account)->name }}</td>
                            <td class="text-end">{{ number_format($borrow->quantity, 4) }}</td>
                            <td class="text-end">{{ number_format($borrow->rate, 4) }}</td>
                            <td>
                                @if($borrow->borrow_settled_at)
                                    <span class="badge bg-success">Settled on {{ $borrow->borrow_settled_at->format('Y-m-d') }}</span>
                                @else
                                    <span class="badge bg-warning text-dark">Unsettled</span>
                                @endif
                            </td>
                            <td class="text-end">
                                @if(!$borrow->borrow_settled_at)
                                    <a href="{{ route('borrows.settle', $borrow) }}" class="btn btn-sm btn-outline-success">Settle</a>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td class="text-muted text-center" colspan="8">No borrow transactions found.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection
