@extends('layouts.admin')

@section('title', 'Treasury')

@section('content')
@php
    $treasuryCustomers = \App\Models\Customer::where('status', 'active')->orderBy('customer_no')->get(['id', 'customer_no', 'first_name', 'last_name', 'business_name']);
@endphp

<div class="fundi-page-header">
    <div>
        <h1>Treasury</h1>
        <p class="fundi-page-header__desc">Instruments, positions, and portfolio management.</p>
    </div>
</div>

<div class="fundi-card">
    <div class="fundi-card__header"><h3>Instruments</h3></div>
    <div class="fundi-card__body">
        <div class="fundi-table-wrap">
            <table class="fundi-table">
                <thead>
                    <tr>
                        <th>Code</th>
                        <th>Name</th>
                        <th>Type</th>
                        <th>Tenor</th>
                        <th class="num">Positions</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($instruments as $instrument)
                        <tr>
                            <td>{{ $instrument->code ?? '—' }}</td>
                            <td>{{ $instrument->name }}</td>
                            <td>{{ ucfirst(str_replace('_',' ',$instrument->instrument_type ?? $instrument->type ?? '—')) }}</td>
                            <td>{{ $instrument->tenor_days ? $instrument->tenor_days.' days' : '—' }}</td>
                            <td class="num">{{ $instrument->positions_count }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="5" class="fundi-empty">No instruments configured.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="fundi-grid-2">
    <div class="fundi-card">
        <div class="fundi-card__header"><h3>Open Positions</h3></div>
        <div class="fundi-card__body">
            <div class="fundi-table-wrap">
                <table class="fundi-table">
                    <thead>
                        <tr>
                            <th>Position No.</th>
                            <th>Instrument</th>
                            <th>Portfolio</th>
                            <th class="num">Cost</th>
                            <th class="num">Market Value</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($positions as $position)
                            <tr>
                                <td>{{ $position->position_no }}</td>
                                <td>{{ $position->instrument->name ?? '—' }}</td>
                                <td>{{ ucfirst(str_replace('_',' ',$position->portfolio)) }}</td>
                                <td class="num">{{ money($position->cost_amount) }}</td>
                                <td class="num">{{ money($position->market_value) }}</td>
                                <td><x-status-badge :status="$position->status" /></td>
                            </tr>
                        @empty
                            <tr><td colspan="6" class="fundi-empty">No positions recorded.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            @if($positions->hasPages())
                <div class="fundi-pagination">{{ $positions->links() }}</div>
            @endif
        </div>
    </div>

    <div class="fundi-card">
        <div class="fundi-card__header"><h3>Add Position</h3></div>
        <div class="fundi-card__body fundi-card__body--padded">
            <form method="POST" action="{{ route('admin.treasury.positions.store') }}">
                @csrf
                <div class="fundi-form-group">
                    <label for="instrument_id">Instrument *</label>
                    <select id="instrument_id" name="instrument_id" class="fundi-select" required>
                        <option value="">— Select —</option>
                        @foreach($instruments as $instrument)
                            <option value="{{ $instrument->id }}" @selected(old('instrument_id') == $instrument->id)>{{ $instrument->name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="fundi-form-group">
                    <label for="portfolio">Portfolio *</label>
                    <select id="portfolio" name="portfolio" class="fundi-select" required>
                        @foreach(['own_book','client_managed'] as $p)
                            <option value="{{ $p }}" @selected(old('portfolio') === $p)>{{ ucfirst(str_replace('_',' ',$p)) }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="fundi-form-group" id="customer_field">
                    <label for="customer_id">Customer (client-managed)</label>
                    <select id="customer_id" name="customer_id" class="fundi-select">
                        <option value="">— Select —</option>
                        @foreach($treasuryCustomers as $c)
                            <option value="{{ $c->id }}" @selected(old('customer_id') == $c->id)>{{ $c->customer_no }} — {{ $c->display_name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="fundi-form-grid">
                    <div class="fundi-form-group">
                        <label for="quantity">Quantity *</label>
                        <input type="number" step="0.01" min="0.01" id="quantity" name="quantity" class="fundi-input" value="{{ old('quantity') }}" required>
                    </div>
                    <div class="fundi-form-group">
                        <label for="cost_amount">Cost Amount (₦) *</label>
                        <input type="number" step="0.01" min="0" id="cost_amount" name="cost_amount" class="fundi-input" value="{{ old('cost_amount') }}" required>
                    </div>
                    <div class="fundi-form-group">
                        <label for="market_value">Market Value (₦)</label>
                        <input type="number" step="0.01" min="0" id="market_value" name="market_value" class="fundi-input" value="{{ old('market_value') }}">
                    </div>
                    <div class="fundi-form-group">
                        <label for="purchase_date">Purchase Date *</label>
                        <input type="date" id="purchase_date" name="purchase_date" class="fundi-input" value="{{ old('purchase_date', date('Y-m-d')) }}" required>
                    </div>
                </div>
                <button type="submit" class="fundi-btn fundi-btn--primary">Record Position</button>
            </form>
        </div>
    </div>
</div>
@endsection
