@extends('layouts.admin')

@section('title', 'Currencies')
@section('page-title', 'Currency Management')

@section('content')
<div class="grid grid-cols-3 gap-6">
    <!-- Currency List -->
    <div class="col-span-2 bg-white rounded-xl shadow-sm overflow-hidden">
        <div class="p-4 border-b flex justify-between items-center">
            <h2 class="font-bold text-gray-800">Currencies</h2>
            
            <form action="{{ route('admin.currencies.update-rates') }}" method="POST" class="inline">
                @csrf
                <button type="submit" class="text-sm text-blue-600 hover:underline">
                    Update Rates from API
                </button>
            </form>
        </div>

        <table class="w-full text-sm">
            <thead class="bg-gray-50">
                <tr class="text-gray-500">
                    <th class="text-left py-3 px-4">Code</th>
                    <th class="text-left py-3 px-4">Name</th>
                    <th class="text-left py-3 px-4">Symbol</th>
                    <th class="text-left py-3 px-4">Position</th>
                    <th class="text-left py-3 px-4">Rate</th>
                    <th class="text-left py-3 px-4">Status</th>
                    <th class="text-left py-3 px-4">Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($currencies as $currency)
                <tr class="border-b hover:bg-gray-50 {{ $currency->is_default ? 'bg-yellow-50' : '' }}">
                    <td class="py-3 px-4 font-medium">
                        {{ $currency->code }}
                        @if($currency->is_default)
                            <span class="text-xs bg-yellow-500 text-white px-2 py-0.5 rounded ml-2">Default</span>
                        @endif
                    </td>
                    <td class="py-3 px-4">{{ $currency->name }}</td>
                    <td class="py-3 px-4 text-lg">{{ $currency->symbol }}</td>
                    <td class="py-3 px-4 text-sm">
                        <span class="px-2 py-1 rounded {{ $currency->symbol_position === 'left' ? 'bg-blue-100 text-blue-700' : 'bg-green-100 text-green-700' }}">
                            {{ ucfirst($currency->symbol_position) }}
                        </span>
                    </td>
                    <td class="py-3 px-4">{{ number_format($currency->exchange_rate, 6) }}</td>
                    <td class="py-3 px-4">
                        <span class="px-2 py-1 rounded-full text-xs font-medium
                            {{ $currency->is_active ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' }}">
                            {{ $currency->is_active ? 'Active' : 'Inactive' }}
                        </span>
                    </td>
                    <td class="py-3 px-4">
                        <button onclick="toggleEditCurrency({{ $currency->id }})" 
                                class="text-blue-600 hover:underline text-sm mr-3">Edit</button>
                        
                        @if(!$currency->is_default)
                        <form action="{{ route('admin.currencies.destroy', $currency) }}" method="POST" class="inline"
                              onsubmit="return confirm('Delete this currency?')">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="text-red-600 hover:underline text-sm">Delete</button>
                        </form>
                        @endif
                    </td>
                </tr>
                <!-- Inline Edit Form -->
                <tr id="edit-currency-{{ $currency->id }}" class="hidden bg-gray-50">
                    <td colspan="7" class="py-4 px-4">
                        <form action="{{ route('admin.currencies.update', $currency) }}" method="POST">
                            @csrf
                            @method('PUT')
                            
                            <div class="grid grid-cols-3 gap-4">
                                <div>
                                    <label class="block text-sm text-gray-600 mb-1">Code (ISO 4217)</label>
                                    <input type="text" name="code" value="{{ $currency->code }}" required maxlength="3"
                                        class="w-full border border-gray-300 rounded px-3 py-2 uppercase">
                                </div>
                                
                                <div>
                                    <label class="block text-sm text-gray-600 mb-1">Name</label>
                                    <input type="text" name="name" value="{{ $currency->name }}" required
                                        class="w-full border border-gray-300 rounded px-3 py-2">
                                </div>
                                
                                <div>
                                    <label class="block text-sm text-gray-600 mb-1">Symbol</label>
                                    <input type="text" name="symbol" value="{{ $currency->symbol }}" required
                                        class="w-full border border-gray-300 rounded px-3 py-2">
                                </div>
                                
                                <div>
                                    <label class="block text-sm text-gray-600 mb-1">Symbol Position</label>
                                    <select name="symbol_position" class="w-full border border-gray-300 rounded px-3 py-2">
                                        <option value="left" {{ $currency->symbol_position === 'left' ? 'selected' : '' }}>Left ($100)</option>
                                        <option value="right" {{ $currency->symbol_position === 'right' ? 'selected' : '' }}>Right (100 €)</option>
                                    </select>
                                </div>
                                
                                <div>
                                    <label class="block text-sm text-gray-600 mb-1">Exchange Rate</label>
                                    <input type="number" name="exchange_rate" value="{{ $currency->exchange_rate }}" 
                                        step="0.000001" required
                                        class="w-full border border-gray-300 rounded px-3 py-2">
                                </div>
                                
                                <div class="flex items-center gap-4">
                                    <label class="flex items-center gap-2">
                                        <input type="checkbox" name="is_default" value="1"
                                            {{ $currency->is_default ? 'checked' : '' }}
                                            class="rounded border-gray-300">
                                        Default Currency
                                    </label>
                                    
                                    <label class="flex items-center gap-2">
                                        <input type="checkbox" name="is_active" value="1"
                                            {{ $currency->is_active ? 'checked' : '' }}
                                            class="rounded border-gray-300">
                                        Active
                                    </label>
                                </div>
                            </div>
                            
                            <div class="flex gap-2 mt-4">
                                <button type="submit" class="bg-blue-600 text-white px-4 py-2 rounded text-sm">Update</button>
                                <button type="button" onclick="toggleEditCurrency({{ $currency->id }})" 
                                        class="bg-gray-300 text-gray-700 px-4 py-2 rounded text-sm">Cancel</button>
                            </div>
                        </form>
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="7" class="py-8 text-center text-gray-500">No currencies configured.</td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <!-- Add New Currency -->
    <div class="bg-white rounded-xl shadow-sm p-6">
        <h3 class="font-bold text-gray-800 mb-4">Add New Currency</h3>
        
        <form action="{{ route('admin.currencies.store') }}" method="POST">
            @csrf
            
            <div class="space-y-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Currency Code (ISO 4217) *</label>
                    <input type="text" name="code" required maxlength="3"
                        class="w-full border border-gray-300 rounded-lg px-3 py-2 uppercase"
                        placeholder="USD">
                    <p class="text-xs text-gray-500 mt-1">3-letter code (e.g., USD, EUR, GBP)</p>
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Name *</label>
                    <input type="text" name="name" required
                        class="w-full border border-gray-300 rounded-lg px-3 py-2"
                        placeholder="US Dollar">
                </div>

                <div class="grid grid-cols-2 gap-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Symbol *</label>
                        <input type="text" name="symbol" required
                            class="w-full border border-gray-300 rounded-lg px-3 py-2"
                            placeholder="$">
                    </div>
                    
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">Symbol Position *</label>
                        <select name="symbol_position" required class="w-full border border-gray-300 rounded-lg px-3 py-2">
                            <option value="left">Left ($100)</option>
                            <option value="right">Right (100 €)</option>
                        </select>
                    </div>
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">Exchange Rate *</label>
                    <input type="number" name="exchange_rate" required step="0.000001" value="1.000000"
                        class="w-full border border-gray-300 rounded-lg px-3 py-2"
                        placeholder="1.000000">
                    <p class="text-xs text-gray-500 mt-1">Rate against base currency</p>
                </div>

                <div class="flex items-center gap-2">
                    <input type="checkbox" name="is_default" value="1" id="is_default"
                        class="rounded border-gray-300">
                    <label for="is_default" class="text-sm text-gray-700">Set as default currency</label>
                </div>

                <div class="flex items-center gap-2">
                    <input type="checkbox" name="is_active" value="1" id="is_active" checked
                        class="rounded border-gray-300">
                    <label for="is_active" class="text-sm text-gray-700">Active</label>
                </div>
            </div>

            <button type="submit" class="w-full mt-6 bg-blue-600 text-white py-2 rounded-lg hover:bg-blue-700">
                Add Currency
            </button>
        </form>
    </div>
</div>

@push('scripts')
<script>
function toggleEditCurrency(id) {
    const form = document.getElementById('edit-currency-' + id);
    form.classList.toggle('hidden');
}
</script>
@endpush
@endsection