@extends('layouts.app')

@section('content')
<div class="row g-4">
    <div class="col-lg-4">
        <div class="card">
            <div class="card-body">
                <h1 class="h4 mb-3">Record Agent Balance</h1>
                <form method="POST" action="{{ route('agent-balances.store') }}">
                    @csrf
                    
                    <div class="mb-3">
                        <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') == $agent->id)>
                                    {{ $agent->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="mb-3">
                        <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 account</option>
                            @foreach($accounts as $account)
                                <option value="{{ $account->id }}" @selected(old('account_id') == $account->id)>
                                    {{ $account->name }} ({{ $account->currency->code }} - Balance: {{ number_format((float) $account->balance, 4) }})
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="mb-3">
                        <label class="form-label" for="quantity">Total Quantity / Currency Amount</label>
                        <input type="number" step="0.0001" min="0.0001" class="form-control" id="quantity" name="quantity" value="{{ old('quantity') }}" placeholder="0.0000" required>
                    </div>

                    <div class="mb-3">
                        <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') }}" placeholder="0.0000" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label" for="date">Transaction Date</label>
                        <input type="date" class="form-control" id="date" name="date" value="{{ old('date', now()->format('Y-m-d')) }}" required>
                    </div>

                    <div class="mb-3">
                        <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') }}</textarea>
                    </div>

                    <button class="btn btn-primary w-100" type="submit">Save Agent Balance</button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-8">
        <div class="card">
            <div class="card-body">
                <h2 class="h4 mb-3">Agent Balance Records</h2>
                
                <form class="d-flex flex-wrap flex-md-nowrap gap-2 mb-4 align-items-center" method="GET" action="{{ route('agent-balances.index') }}">
                    <div class="flex-grow-1">
                        <input type="text" class="form-control" name="search" value="{{ $filters['search'] ?? '' }}" placeholder="Search description, agent...">
                    </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('agent-balances.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>Account</th>
                                <th>Currency</th>
                                <th class="text-end">Quantity</th>
                                <th class="text-end">Buying Rate</th>
                                <th class="text-end">Total LKR</th>
                                <th>Status</th>
                                <th class="text-end">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($balances as $balance)
                                <tr>
                                    <td>{{ $balance->date->format('Y-m-d') }}</td>
                                    <td class="fw-semibold">{{ $balance->agent->name }}</td>
                                    <td>{{ $balance->account->name }}</td>
                                    <td>{{ $balance->account->currency->code }}</td>
                                    <td class="text-end fw-semibold">{{ number_format((float) $balance->quantity, 4) }}</td>
                                    <td class="text-end">{{ number_format((float) $balance->rate, 4) }}</td>
                                    <td class="text-end fw-semibold">{{ number_format((float) $balance->quantity * (float) $balance->rate, 4) }}</td>
                                    <td>
                                        @if($balance->is_received)
                                            <span style="padding: 2px 8px; border-radius: 4px; font-size: 12px; font-weight: 600; background-color: #d1fae5; color: #065f46;">
                                                Received
                                            </span>
                                        @else
                                            <span style="padding: 2px 8px; border-radius: 4px; font-size: 12px; font-weight: 600; background-color: #fee2e2; color: #991b1b;">
                                                Pending
                                            </span>
                                        @endif
                                    </td>
                                    <td class="text-end">
                                        <div class="btn-group btn-group-sm d-flex gap-1 justify-content-end">
                                            @if(!$balance->is_received)
                                                <form action="{{ route('agent-balances.received', $balance) }}" method="POST" onsubmit="return confirm('Confirm receipt of this agent balance? This will credit the target account.')" style="display:inline-block;">
                                                    @csrf
                                                    <button type="submit" class="btn btn-sm btn-outline-success" style="padding: 2px 6px; font-size: 12px;">Received</button>
                                                </form>
                                            @endif
                                            
                                            <a href="{{ route('agent-balances.edit', $balance) }}" class="btn btn-sm btn-outline-primary" style="padding: 2px 6px; font-size: 12px;">Edit</a>
                                            
                                            <form action="{{ route('agent-balances.destroy', $balance) }}" method="POST" onsubmit="return confirm('Are you sure you want to delete this agent balance record?{{ $balance->is_received ? " This will reverse the account balance adjustment." : "" }}')" style="display:inline-block;">
                                                @csrf
                                                @method('DELETE')
                                                <button type="submit" class="btn btn-sm btn-outline-danger" style="padding: 2px 6px; font-size: 12px;">Delete</button>
                                            </form>
                                        </div>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td class="text-muted text-center" colspan="9">No agent balances recorded yet.</td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
