@extends('layouts.app')

@section('title', 'Credencial')

@section('content')
<div class="max-w-7xl mx-auto">
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between mb-8">
        <div>
            <h2 class="text-4xl font-bold text-white mb-2 tracking-tight">Credenciais</h2>
            <p class="text-slate-400">Gerencie seus tokens da Utmify</p>
        </div>
        <button onclick="openModal()" class="mt-4 sm:mt-0 px-6 py-3 bg-green-600 hover:bg-green-700 text-white rounded-lg transition-all duration-200 font-semibold flex items-center space-x-2">
            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
            </svg>
            <span>Adicionar Chave</span>
        </button>
    </div>

    @if(isset($credenciais) && $credenciais->count() > 0)
        <div class="grid gap-5">
            @foreach($credenciais as $credencial)
                <div class="bg-slate-900 border border-slate-800 rounded-lg p-6 hover:border-slate-700 transition-all">
                    <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
                        <div class="flex-1">
                            <h3 class="text-xl font-bold text-white mb-3">{{ $credencial->nome }}</h3>
                            <div class="flex items-center gap-3 bg-slate-900/50 p-3 rounded-xl">
                                <p class="text-slate-300 text-sm font-mono break-all flex-1">{{ $credencial->token_umtify }}</p>
                                <button onclick="copyToken('{{ $credencial->token_umtify }}')" class="text-green-400 hover:text-green-300 transition flex-shrink-0">
                                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"/>
                                    </svg>
                                </button>
                            </div>
                            <p class="text-slate-500 text-xs mt-3">Criado em {{ $credencial->created_at->format('d/m/Y H:i') }}</p>
                        </div>
                        <div class="flex gap-2">
                            <button onclick="openEditModal({{ $credencial->id }}, '{{ $credencial->nome }}', '{{ $credencial->token_umtify }}')" class="px-4 py-2.5 bg-blue-600/90 hover:bg-blue-600 text-white rounded-xl transition-all text-sm font-semibold shadow-lg">
                                Editar
                            </button>
                            <form method="POST" action="{{ route('credencial.destroy', $credencial) }}" onsubmit="return confirm('Tem certeza que deseja excluir esta credencial?')">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="px-4 py-2.5 bg-red-600/90 hover:bg-red-600 text-white rounded-xl transition-all text-sm font-semibold shadow-lg">
                                    Excluir
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            @endforeach
        </div>
    @else
        <div class="bg-slate-900 border border-slate-800 rounded-xl p-16 text-center">
            <div class="w-24 h-24 bg-slate-800 rounded-xl flex items-center justify-center mx-auto mb-6">
                <svg class="w-12 h-12 text-slate-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 7a2 2 0 012 2m4 0a6 6 0 01-7.743 5.743L11 17H9v2H7v2H4a1 1 0 01-1-1v-2.586a1 1 0 01.293-.707l5.964-5.964A6 6 0 1121 9z"/>
                </svg>
            </div>
            <h3 class="text-2xl font-bold text-white mb-3">Nenhuma credencial cadastrada</h3>
            <p class="text-slate-400 mb-8 max-w-md mx-auto">Adicione sua primeira chave da Utmify para começar a disparar notificações</p>
            <button onclick="openModal()" class="px-8 py-3 bg-green-600 hover:bg-green-700 text-white rounded-lg transition-all duration-200 font-semibold inline-flex items-center space-x-2">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
                </svg>
                <span>Adicionar Primeira Chave</span>
            </button>
        </div>
    @endif
</div>

<div id="credencialModal" class="hidden fixed inset-0 bg-black/60 backdrop-blur-sm z-50 flex items-center justify-center p-4">
    <div class="bg-slate-900 rounded-xl max-w-md w-full p-8 border border-slate-800 animate-slide-in">
        <div class="flex items-center justify-between mb-8">
            <div>
                <h3 class="text-2xl font-bold text-white mb-1" id="modalTitle">Adicionar Chave</h3>
                <p class="text-slate-400 text-sm">Configure sua credencial da Utmify</p>
            </div>
            <button onclick="closeModal()" class="text-gray-400 hover:text-white transition p-2 hover:bg-slate-700/50 rounded-lg">
                <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                </svg>
            </button>
        </div>

        <form id="credencialForm" method="POST" action="{{ route('credencial.store') }}">
            @csrf
            <input type="hidden" id="credencialId" name="credencial_id" value="">
            <input type="hidden" id="formMethod" name="_method" value="">

            <div class="space-y-6">
                <div>
                    <label for="nome" class="block text-sm font-semibold text-slate-300 mb-2">Nome</label>
                    <input
                        type="text"
                        id="nome"
                        name="nome"
                        required
                        class="w-full px-4 py-3 bg-slate-700/50 border border-slate-600 text-white rounded-xl focus:ring-2 focus:ring-green-500 focus:border-green-500 outline-none transition"
                        placeholder="Ex: Chave de Produção"
                    >
                </div>

                <div>
                    <label for="token_umtify" class="block text-sm font-semibold text-slate-300 mb-2">Token Umtify</label>
                    <textarea
                        id="token_umtify"
                        name="token_umtify"
                        required
                        rows="4"
                        class="w-full px-4 py-3 bg-slate-700/50 border border-slate-600 text-white rounded-xl focus:ring-2 focus:ring-green-500 focus:border-green-500 outline-none transition resize-none"
                        placeholder="Cole seu token aqui"
                    ></textarea>
                </div>

                <div class="flex gap-4 pt-6">
                    <button
                        type="button"
                        onclick="closeModal()"
                        class="flex-1 px-6 py-3 bg-slate-800 text-white rounded-lg hover:bg-slate-700 transition-all duration-200 font-semibold"
                    >
                        Cancelar
                    </button>
                    <button
                        type="submit"
                        class="flex-1 px-6 py-3 bg-green-600 hover:bg-green-700 text-white rounded-lg transition-all duration-200 font-semibold"
                    >
                        Cadastrar
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<script>
    function openModal() {
        document.getElementById('modalTitle').textContent = 'Adicionar Chave';
        document.getElementById('credencialForm').action = '{{ route("credencial.store") }}';
        document.getElementById('formMethod').value = '';
        document.getElementById('credencialId').value = '';
        document.getElementById('nome').value = '';
        document.getElementById('token_umtify').value = '';
        document.getElementById('credencialModal').classList.remove('hidden');
        document.querySelector('#credencialForm button[type="submit"]').textContent = 'Cadastrar';
    }

    function openEditModal(id, nome, token) {
        document.getElementById('modalTitle').textContent = 'Editar Chave';
        document.getElementById('credencialForm').action = '/credencial/' + id;
        document.getElementById('formMethod').value = 'PUT';
        document.getElementById('credencialId').value = id;
        document.getElementById('nome').value = nome;
        document.getElementById('token_umtify').value = token;
        document.getElementById('credencialModal').classList.remove('hidden');
        document.querySelector('#credencialForm button[type="submit"]').textContent = 'Atualizar';
    }

    function closeModal() {
        document.getElementById('credencialModal').classList.add('hidden');
    }

    function copyToken(token) {
        navigator.clipboard.writeText(token).then(() => {
            alert('Token copiado para a área de transferência!');
        });
    }

    document.getElementById('credencialModal').addEventListener('click', function(e) {
        if (e.target === this) {
            closeModal();
        }
    });
</script>
@endsection
