@extends('layouts.admin')

@section('title', 'Vouchers - Centipid ISP CRM')
@section('breadcrumb_parent', 'FINANCE')
@section('breadcrumb_current', 'Vouchers')

@section('content')
<div class="space-y-6">
    <div class="flex items-center justify-between">
        <div>
            <h1 class="text-3xl font-extrabold text-white tracking-tight">Hotspot <span class="text-orange-500">vouchers.</span></h1>
            <p class="text-xs text-slate-400 mt-1">Batch code generator, printable templates & reseller agent float management.</p>
        </div>
        <div class="flex items-center gap-3">
            <button onclick="openAddAgentModal()" class="flex items-center gap-2 bg-[#1c1c1f] hover:bg-[#26262a] text-slate-300 font-semibold text-xs px-4 py-2.5 rounded-xl border border-[#2a2a2e] transition-all">
                <i data-lucide="user-check" class="w-4 h-4 text-orange-500"></i>
                <span>Add agent</span>
            </button>
            <button onclick="openGenerateBatchModal()" class="flex items-center gap-2 btn-orange font-bold text-xs px-4 py-2.5 rounded-xl shadow-lg shadow-orange-500/20">
                <i data-lucide="ticket" class="w-4 h-4"></i>
                <span>Generate vouchers</span>
            </button>
        </div>
    </div>

    <!-- Voucher KPI Cards -->
    <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
        <div class="custom-card p-4 rounded-2xl">
            <p class="text-[10px] font-extrabold uppercase tracking-widest text-slate-400">Total Batches</p>
            <p class="text-2xl font-extrabold text-white mt-1">{{ $totalBatchCount }}</p>
            <p class="text-[11px] text-slate-500 mt-1">batches generated</p>
        </div>

        <div class="custom-card p-4 rounded-2xl">
            <p class="text-[10px] font-extrabold uppercase tracking-widest text-slate-400">Unused Stock</p>
            <p class="text-2xl font-extrabold text-emerald-400 mt-1">{{ $unusedCount }}</p>
            <p class="text-[11px] text-slate-500 mt-1">ready for sale</p>
        </div>

        <div class="custom-card p-4 rounded-2xl">
            <p class="text-[10px] font-extrabold uppercase tracking-widest text-slate-400">Redeemed</p>
            <p class="text-2xl font-extrabold text-orange-500 mt-1">{{ $redeemedCount }}</p>
            <p class="text-[11px] text-slate-500 mt-1">used on network</p>
        </div>

        <div class="custom-card p-4 rounded-2xl">
            <p class="text-[10px] font-extrabold uppercase tracking-widest text-slate-400">Active Agents</p>
            <p class="text-2xl font-extrabold text-white mt-1">{{ count($agents) }}</p>
            <p class="text-[11px] text-slate-500 mt-1">reseller partners</p>
        </div>
    </div>

    <!-- Voucher Reseller Agents Cards -->
    <div class="space-y-3">
        <h3 class="text-xs font-extrabold uppercase text-slate-400 tracking-wider">Voucher Agents & Float Balances</h3>
        <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
            @foreach($agents as $ag)
                <div class="custom-card p-4 rounded-2xl space-y-3">
                    <div class="flex items-center justify-between">
                        <div class="flex items-center gap-2.5">
                            <div class="w-8 h-8 rounded-full bg-orange-500/10 text-orange-500 font-extrabold flex items-center justify-center text-xs">
                                {{ strtoupper(substr($ag->name, 0, 2)) }}
                            </div>
                            <div>
                                <h4 class="text-xs font-bold text-white">{{ $ag->name }}</h4>
                                <p class="text-[10px] text-slate-400">{{ $ag->phone ?? 'No phone' }}</p>
                            </div>
                        </div>
                        <span class="text-[10px] font-mono text-emerald-400 bg-emerald-500/10 px-2 py-0.5 rounded border border-emerald-500/20">
                            10% Comm.
                        </span>
                    </div>

                    <div class="grid grid-cols-2 gap-2 bg-[#111113] p-2.5 rounded-xl text-center text-xs">
                        <div>
                            <p class="text-slate-500 text-[10px]">CURRENT FLOAT</p>
                            <p class="font-bold text-emerald-400">Rs {{ number_format($ag->current_float, 2) }}</p>
                        </div>
                        <div>
                            <p class="text-slate-500 text-[10px]">SOLD VOUCHERS</p>
                            <p class="font-bold text-white">{{ $ag->sold_vouchers_count }} / {{ $ag->assigned_vouchers_count }}</p>
                        </div>
                    </div>
                </div>
            @endforeach
        </div>
    </div>

    <!-- Vouchers Table -->
    <div class="custom-card rounded-2xl overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-left text-xs text-slate-300">
                <thead class="bg-[#1c1c1f] text-slate-400 uppercase text-[10px] font-extrabold tracking-wider border-b border-[#2a2a2e]">
                    <tr>
                        <th class="py-4 px-6">BATCH CODE</th>
                        <th class="py-4 px-6">VOUCHER CODE</th>
                        <th class="py-4 px-6">PACKAGE</th>
                        <th class="py-4 px-6">PRICE</th>
                        <th class="py-4 px-6">AGENT</th>
                        <th class="py-4 px-6">STATUS</th>
                        <th class="py-4 px-6 text-right">REDEEMED AT</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-[#2a2a2e] font-medium">
                    @forelse($vouchers as $v)
                        <tr class="hover:bg-[#1f1f23] transition-colors">
                            <td class="py-4 px-6 font-mono text-slate-400">{{ $v->batch_code }}</td>
                            <td class="py-4 px-6 font-mono text-orange-500 font-bold text-sm">{{ $v->voucher_code }}</td>
                            <td class="py-4 px-6 text-white font-bold">{{ $v->package_name }}</td>
                            <td class="py-4 px-6 font-bold text-emerald-400">Rs {{ number_format($v->price, 2) }}</td>
                            <td class="py-4 px-6 text-slate-300">{{ $v->assigned_agent ?? 'Direct Admin' }}</td>
                            <td class="py-4 px-6">
                                @if($v->status === 'Unused')
                                    <span class="px-2.5 py-1 rounded-full text-[10px] font-bold bg-emerald-500/10 text-emerald-400 border border-emerald-500/20">Unused</span>
                                @elseif($v->status === 'Redeemed')
                                    <span class="px-2.5 py-1 rounded-full text-[10px] font-bold bg-orange-500/10 text-orange-400 border border-orange-500/20">Redeemed</span>
                                @else
                                    <span class="px-2.5 py-1 rounded-full text-[10px] font-bold bg-[#222226] text-slate-400">{{ $v->status }}</span>
                                @endif
                            </td>
                            <td class="py-4 px-6 text-right font-mono text-slate-500 text-[11px]">
                                {{ $v->redeemed_at ? $v->redeemed_at->format('M d, H:i') : '-' }}
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="py-8 text-center text-slate-500">No vouchers generated yet.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        <div class="p-4 border-t border-[#2a2a2e]">
            {{ $vouchers->links() }}
        </div>
    </div>
</div>

<!-- Modal 1: Generate Voucher Batch (2-Step Modal) -->
<div id="generateBatchModal" class="fixed inset-0 z-[9999] hidden bg-black/80 backdrop-blur-sm flex items-center justify-center p-4">
    <div class="custom-panel rounded-2xl max-w-md w-full p-6 shadow-2xl space-y-4">
        <div class="flex items-center justify-between border-b border-[#2a2a2e] pb-3">
            <h3 class="text-base font-bold text-white flex items-center gap-2">
                <i data-lucide="ticket" class="w-5 h-5 text-orange-500"></i>
                <span>Generate Vouchers</span>
            </h3>
            <button onclick="closeGenerateBatchModal()" class="text-slate-400 hover:text-white">&times;</button>
        </div>

        <form action="{{ route('admin.finance.generateVouchers') }}" method="POST" class="space-y-4">
            @csrf
            <div>
                <label class="block text-xs font-semibold text-slate-300 mb-1">Hotspot Package *</label>
                <select name="package_name" required class="w-full bg-[#0f0f10] border border-[#2a2a2e] text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-orange-500 focus:outline-none">
                    @foreach($hotspotPlans as $hp)
                        <option value="{{ $hp->name }}">{{ $hp->name }} &bull; Rs {{ $hp->price }}</option>
                    @endforeach
                </select>
            </div>

            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Quantity *</label>
                    <input type="number" name="quantity" value="20" min="1" max="100" required class="w-full bg-[#0f0f10] border border-[#2a2a2e] text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-orange-500 focus:outline-none">
                </div>
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Price per Voucher (Rs) *</label>
                    <input type="number" step="1" name="price" value="50" min="1" required class="w-full bg-[#0f0f10] border border-[#2a2a2e] text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-orange-500 focus:outline-none">
                </div>
            </div>

            <div>
                <label class="block text-xs font-semibold text-slate-300 mb-1">Code Prefix (e.g. WIFI)</label>
                <input type="text" name="prefix" value="WIFI" class="w-full bg-[#0f0f10] border border-[#2a2a2e] text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-orange-500 focus:outline-none">
            </div>

            <div class="flex items-center justify-end gap-3 pt-3 border-t border-[#2a2a2e]">
                <button type="button" onclick="closeGenerateBatchModal()" class="px-4 py-2 text-xs font-semibold text-slate-400 hover:text-white">Cancel</button>
                <button type="submit" class="btn-orange text-xs px-5 py-2.5 rounded-xl">Generate Codes Batch</button>
            </div>
        </form>
    </div>
</div>

<!-- Modal 2: Add Agent -->
<div id="addAgentModal" class="fixed inset-0 z-[9999] hidden bg-black/80 backdrop-blur-sm flex items-center justify-center p-4">
    <div class="custom-panel rounded-2xl max-w-md w-full p-6 shadow-2xl space-y-4">
        <div class="flex items-center justify-between border-b border-[#2a2a2e] pb-3">
            <h3 class="text-base font-bold text-white flex items-center gap-2">
                <i data-lucide="user-check" class="w-5 h-5 text-orange-500"></i>
                <span>Add Voucher Agent</span>
            </h3>
            <button onclick="closeAddAgentModal()" class="text-slate-400 hover:text-white">&times;</button>
        </div>

        <form action="{{ route('admin.finance.storeAgent') }}" method="POST" class="space-y-4">
            @csrf
            <div>
                <label class="block text-xs font-semibold text-slate-300 mb-1">Agent Name *</label>
                <input type="text" name="name" required placeholder="e.g. Rajesh Mobile Store" class="w-full bg-[#0f0f10] border border-[#2a2a2e] text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-orange-500 focus:outline-none">
            </div>

            <div class="grid grid-cols-2 gap-4">
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Phone</label>
                    <input type="text" name="phone" placeholder="+91 9988776655" class="w-full bg-[#0f0f10] border border-[#2a2a2e] text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-orange-500 focus:outline-none">
                </div>
                <div>
                    <label class="block text-xs font-semibold text-slate-300 mb-1">Opening Float (Rs)</label>
                    <input type="number" step="0.01" name="opening_float" value="3000.00" class="w-full bg-[#0f0f10] border border-[#2a2a2e] text-slate-100 text-xs rounded-xl px-3 py-2.5 focus:border-orange-500 focus:outline-none">
                </div>
            </div>

            <div class="flex items-center justify-end gap-3 pt-3 border-t border-[#2a2a2e]">
                <button type="button" onclick="closeAddAgentModal()" class="px-4 py-2 text-xs font-semibold text-slate-400 hover:text-white">Cancel</button>
                <button type="submit" class="btn-orange text-xs px-5 py-2.5 rounded-xl">Create Agent</button>
            </div>
        </form>
    </div>
</div>
@endsection

@push('scripts')
<script>
    function openGenerateBatchModal() {
        document.getElementById('generateBatchModal').classList.remove('hidden');
    }
    function closeGenerateBatchModal() {
        document.getElementById('generateBatchModal').classList.add('hidden');
    }
    function openAddAgentModal() {
        document.getElementById('addAgentModal').classList.remove('hidden');
    }
    function closeAddAgentModal() {
        document.getElementById('addAgentModal').classList.add('hidden');
    }
</script>
@endpush
