@extends('layouts.app')

@section('content')
<div class="container">
    <h2>Investments</h2>
    <a href="{{ route('investments.create') }}" class="btn btn-primary mb-3">Add Investment</a>

    {{-- Filter Form --}}
    <form method="GET" action="{{ route('investments.index') }}" class="mb-4">
        <div class="row">
            <div class="col-md-3">
                <label for="investor_id">Investor</label>
                <select name="investor_id" id="investor_id" class="form-control">
                    <option value="">All Investors</option>
                    @foreach($investors as $investor)
                        <option value="{{ $investor->id }}" {{ request('investor_id') == $investor->id ? 'selected' : '' }}>
                            {{ $investor->name }}
                        </option>
                    @endforeach
                </select>
            </div>

            <div class="col-md-3">
                <label for="type">Type</label>
                <select name="type" id="type" class="form-control">
                    <option value="">All Types</option>
                    <option value="IN" {{ request('type') == 'IN' ? 'selected' : '' }}>IN</option>
                    <option value="OUT" {{ request('type') == 'OUT' ? 'selected' : '' }}>OUT</option>
                    <option value="CAR" {{ request('type') == 'CAR' ? 'selected' : '' }}>CAR</option>
                </select>
            </div>

            <div class="col-md-3">
                <label for="date">Date</label>
                <input type="date" name="date" id="date" value="{{ request('date') }}" class="form-control">
            </div>

            <div class="col-md-3 d-flex align-items-end">
                <button type="submit" class="btn btn-success me-2">Filter</button>
                <a href="{{ route('investments.index') }}" class="btn btn-secondary">Reset</a>
            </div>
        </div>
    </form>

    {{-- Success Message --}}
    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif

    {{-- Investments Table --}}
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>ID</th>
                <th>Investor</th>
                <th>Type</th>
                <th>Amount</th>
                <th>Date</th>
                <th>Remarks</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            @forelse($investments as $investment)
            <tr>
                <td>{{ $investment->id }}</td>
                <td>{{ $investment->investor->name ?? 'N/A' }}</td>
                <td>{{ ucfirst($investment->type) }}</td>
                <td>{{ number_format($investment->amount, 2) }}</td>
                <td>{{ $investment->date }}</td>
                <td>{{ $investment->remarks }}</td>
                <td>
                    <a href="{{ route('investments.edit', $investment->id) }}" class="btn btn-sm btn-warning">Edit</a>
                    <form action="{{ route('investments.destroy', $investment->id) }}" method="POST" style="display:inline;">
                        @csrf
                        @method('DELETE')
                        <button type="submit" onclick="return confirm('Are you sure?')" class="btn btn-sm btn-danger">
                            Delete
                        </button>
                    </form>
                </td>
            </tr>
            @empty
            <tr>
                <td colspan="7" class="text-center">No investments found</td>
            </tr>
            @endforelse
        </tbody>
    </table>

</div>
@endsection
