@extends('layouts.admin')

@section('title', 'Gestion des Devises')

@section('content')
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 mb-0">Gestion des Devises</h1>
    </div>

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

    <div class="row">
        <div class="col-md-8">
            <div class="card mb-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0">Taux de change</h5>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-sm">
                            <thead>
                                <tr>
                                    <th>De</th>
                                    <th>Vers</th>
                                    <th>Taux</th>
                                    <th>Mis à jour</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($rates as $rate)
                                <tr>
                                    <td><strong>{{ $rate->from_currency }}</strong></td>
                                    <td><strong>{{ $rate->to_currency }}</strong></td>
                                    <td>
                                        <form action="{{ route('admin.currencies.update', $rate) }}" method="POST" class="d-flex">
                                            @csrf
                                            @method('PUT')
                                            <input type="number" name="rate" value="{{ $rate->rate }}" 
                                                   class="form-control form-control-sm" step="0.0000000001" style="width: 150px;">
                                            <button type="submit" class="btn btn-sm btn-primary ms-1">OK</button>
                                        </form>
                                    </td>
                                    <td>{{ $rate->fetched_at?->format('d/m H:i') ?? '-' }}</td>
                                    <td>
                                        <form action="{{ route('admin.currencies.destroy', $rate) }}" method="POST" class="d-inline">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-sm btn-outline-danger">Suppr</button>
                                        </form>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-md-4">
            <div class="card mb-4">
                <div class="card-header"><h5 class="mb-0">Devise par défaut</h5></div>
                <div class="card-body">
                    <form action="{{ route('admin.currencies.set-default') }}" method="POST">
                        @csrf
                        <div class="mb-3">
                            <select name="currency" class="form-select">
                                <option value="XOF" {{ $defaultCurrency === 'XOF' ? 'selected' : '' }}>XOF (Franc CFA)</option>
                                <option value="EUR" {{ $defaultCurrency === 'EUR' ? 'selected' : '' }}>EUR (Euro)</option>
                                <option value="USD" {{ $defaultCurrency === 'USD' ? 'selected' : '' }}>USD (Dollar US)</option>
                                <option value="GBP" {{ $defaultCurrency === 'GBP' ? 'selected' : '' }}>GBP (Livre Sterling)</option>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary w-100">Définir</button>
                    </form>
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h5 class="mb-0">Ajouter un taux</h5></div>
                <div class="card-body">
                    <form action="{{ route('admin.currencies.store') }}" method="POST">
                        @csrf
                        <div class="mb-3">
                            <label class="form-label">De</label>
                            <input type="text" name="from_currency" class="form-control" maxlength="3" placeholder="XOF" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Vers</label>
                            <input type="text" name="to_currency" class="form-control" maxlength="3" placeholder="EUR" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Taux</label>
                            <input type="number" name="rate" class="form-control" step="0.0000000001" required>
                        </div>
                        <button type="submit" class="btn btn-success w-100">Ajouter</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
