@extends('layouts.biller')

@section('title', 'Reports')
@section('page_title', 'Tier 1 Operations Reports')

@section('content')
<div class="space-y-6 max-w-7xl mx-auto">
    
    <!-- Header & Filter -->
    <div class="bg-white p-6 rounded-sm border border-zinc-200 shadow-sm flex flex-col md:flex-row justify-between items-center gap-4">
        <div>
            <h2 class="text-lg font-black uppercase tracking-tight text-zinc-900">Operations Overview</h2>
            <p class="text-[10px] font-bold text-zinc-500 uppercase tracking-widest mt-1">
                Data from {{ $startDate->format('d M Y, h:i A') }} to {{ $endDate->format('d M Y, h:i A') }}
            </p>
        </div>
        
        <form action="{{ route('biller.reports.index') }}" method="GET" class="flex flex-col sm:flex-row items-end sm:items-center gap-2" x-data="{ filter: '{{ $filter }}' }">
            <select name="date_filter" x-model="filter" @change="if(filter !== 'custom') $el.form.submit()" class="text-xs font-bold bg-zinc-50 border border-zinc-200 text-zinc-700 py-2 pl-3 pr-8 rounded-sm focus:ring-indigo-500 focus:border-indigo-500">
                <option value="today">Today</option>
                <option value="yesterday">Yesterday</option>
                <option value="last_7_days">Last 7 Days</option>
                <option value="this_month">This Month</option>
                <option value="custom">Custom Range</option>
            </select>

            <div x-show="filter === 'custom'" x-transition x-cloak class="flex items-center space-x-2">
                <input type="date" name="start_date" value="{{ request('start_date', $startDate->format('Y-m-d')) }}" required class="text-xs font-bold bg-zinc-50 border border-zinc-200 text-zinc-700 py-2 px-3 rounded-sm focus:ring-indigo-500 focus:border-indigo-500">
                <span class="text-xs font-bold text-zinc-400">to</span>
                <input type="date" name="end_date" value="{{ request('end_date', $endDate->format('Y-m-d')) }}" required class="text-xs font-bold bg-zinc-50 border border-zinc-200 text-zinc-700 py-2 px-3 rounded-sm focus:ring-indigo-500 focus:border-indigo-500">
            </div>

            <button type="submit" class="px-4 py-2 bg-indigo-600 text-white text-[10px] font-black uppercase tracking-widest rounded-sm hover:bg-indigo-700 transition-colors shadow-lg shadow-indigo-900/20 whitespace-nowrap">
                Refresh
            </button>
        </form>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        
        <!-- Left Column: Sales & Payments -->
        <div class="lg:col-span-1 space-y-6">
            
            <!-- Daily Sales Summary -->
            <div class="bg-white rounded-sm border border-zinc-200 shadow-sm overflow-hidden">
                <div class="bg-zinc-900 px-5 py-4 border-b border-zinc-800">
                    <h3 class="text-[11px] font-black uppercase tracking-widest text-white flex items-center">
                        <svg class="w-4 h-4 text-indigo-400 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                        Sales Summary
                    </h3>
                </div>
                <div class="p-5">
                    <div class="flex flex-col items-center justify-center py-6 border-b border-zinc-100 mb-4">
                        <span class="text-[10px] font-black text-zinc-400 uppercase tracking-widest mb-1">Total Revenue</span>
                        <span class="text-4xl font-black text-indigo-600">₹{{ number_format($salesSummary['total_revenue'], 2) }}</span>
                    </div>
                    
                    <div class="space-y-3">
                        <div class="flex justify-between items-center">
                            <span class="text-[11px] font-bold text-zinc-500 uppercase">Subtotal</span>
                            <span class="text-[11px] font-black text-zinc-900">₹{{ number_format($salesSummary['total_subtotal'], 2) }}</span>
                        </div>
                        <div class="flex justify-between items-center">
                            <span class="text-[11px] font-bold text-zinc-500 uppercase">Taxes (GST)</span>
                            <span class="text-[11px] font-black text-zinc-900">₹{{ number_format($salesSummary['total_gst'], 2) }}</span>
                        </div>
                        <div class="flex justify-between items-center text-rose-600">
                            <span class="text-[11px] font-bold uppercase">Discounts</span>
                            <span class="text-[11px] font-black">-₹{{ number_format($salesSummary['total_discount'], 2) }}</span>
                        </div>
                        <div class="pt-3 mt-3 border-t border-zinc-100 flex justify-between items-center">
                            <span class="text-[11px] font-bold text-zinc-500 uppercase">Total Orders</span>
                            <span class="text-xs font-black text-indigo-600">{{ $salesSummary['total_orders'] }}</span>
                        </div>
                    </div>
                </div>
                <div class="bg-zinc-50 px-5 py-3 border-t border-zinc-200 grid grid-cols-3 gap-2 text-center divide-x divide-zinc-200">
                    <div>
                        <p class="text-[9px] font-black text-zinc-400 uppercase tracking-widest mb-1">Dine-in</p>
                        <p class="text-[11px] font-black text-zinc-900">₹{{ number_format($salesSummary['by_type']['dine_in'], 2) }}</p>
                    </div>
                    <div>
                        <p class="text-[9px] font-black text-zinc-400 uppercase tracking-widest mb-1">Takeaway</p>
                        <p class="text-[11px] font-black text-zinc-900">₹{{ number_format($salesSummary['by_type']['takeaway'], 2) }}</p>
                    </div>
                    <div>
                        <p class="text-[9px] font-black text-zinc-400 uppercase tracking-widest mb-1">Delivery</p>
                        <p class="text-[11px] font-black text-zinc-900">₹{{ number_format($salesSummary['by_type']['delivery'], 2) }}</p>
                    </div>
                </div>
            </div>

            <!-- Payment Methods -->
            <div class="bg-white rounded-sm border border-zinc-200 shadow-sm overflow-hidden">
                <div class="bg-zinc-900 px-5 py-4 border-b border-zinc-800">
                    <h3 class="text-[11px] font-black uppercase tracking-widest text-white flex items-center">
                        <svg class="w-4 h-4 text-emerald-400 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z"></path></svg>
                        Collection by Tender
                    </h3>
                </div>
                <div class="p-5 space-y-4">
                    <div class="flex items-center justify-between p-3 bg-zinc-50 border border-zinc-100 rounded-sm">
                        <div class="flex items-center space-x-3">
                            <div class="w-8 h-8 bg-emerald-100 text-emerald-600 rounded-sm flex items-center justify-center">
                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 9V7a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2m2 4h10a2 2 0 002-2v-6a2 2 0 00-2-2H9a2 2 0 00-2 2v6a2 2 0 002 2zm7-5a2 2 0 11-4 0 2 2 0 014 0z"></path></svg>
                            </div>
                            <span class="text-xs font-black uppercase text-zinc-700">Cash</span>
                        </div>
                        <span class="text-sm font-black text-zinc-900">₹{{ number_format($paymentMethods['cash'], 2) }}</span>
                    </div>

                    <div class="flex items-center justify-between p-3 bg-zinc-50 border border-zinc-100 rounded-sm">
                        <div class="flex items-center space-x-3">
                            <div class="w-8 h-8 bg-indigo-100 text-indigo-600 rounded-sm flex items-center justify-center">
                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 10h18M7 15h1m4 0h1m-7 4h12a3 3 0 003-3V8a3 3 0 00-3-3H6a3 3 0 00-3 3v8a3 3 0 003 3z"></path></svg>
                            </div>
                            <span class="text-xs font-black uppercase text-zinc-700">Card</span>
                        </div>
                        <span class="text-sm font-black text-zinc-900">₹{{ number_format($paymentMethods['card'], 2) }}</span>
                    </div>

                    <div class="flex items-center justify-between p-3 bg-zinc-50 border border-zinc-100 rounded-sm">
                        <div class="flex items-center space-x-3">
                            <div class="w-8 h-8 bg-amber-100 text-amber-600 rounded-sm flex items-center justify-center">
                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 18h.01M8 21h8a2 2 0 002-2V5a2 2 0 00-2-2H8a2 2 0 00-2 2v14a2 2 0 002 2z"></path></svg>
                            </div>
                            <span class="text-xs font-black uppercase text-zinc-700">UPI / Digital</span>
                        </div>
                        <span class="text-sm font-black text-zinc-900">₹{{ number_format($paymentMethods['upi'], 2) }}</span>
                    </div>
                </div>
            </div>

        </div>

        <!-- Right Column: Item Sales Performance -->
        <div class="lg:col-span-2">
            <div class="bg-white rounded-sm border border-zinc-200 shadow-sm overflow-hidden h-full flex flex-col">
                <div class="bg-zinc-900 px-5 py-4 border-b border-zinc-800 flex justify-between items-center">
                    <h3 class="text-[11px] font-black uppercase tracking-widest text-white flex items-center">
                        <svg class="w-4 h-4 text-amber-400 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 7h8m0 0v8m0-8l-8 8-4-4-6 6"></path></svg>
                        Top Performing Items (Menu Engineering)
                    </h3>
                    <span class="text-[9px] font-black text-zinc-400 uppercase tracking-widest">Top 20</span>
                </div>
                
                <div class="flex-1 overflow-x-auto">
                    <table class="w-full text-left border-collapse">
                        <thead>
                            <tr class="bg-zinc-50 border-b border-zinc-200">
                                <th class="px-5 py-3 text-[10px] font-black text-zinc-500 uppercase tracking-widest">Item Name</th>
                                <th class="px-5 py-3 text-[10px] font-black text-zinc-500 uppercase tracking-widest text-center">Qty Sold</th>
                                <th class="px-5 py-3 text-[10px] font-black text-zinc-500 uppercase tracking-widest text-right">Revenue</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-zinc-100">
                            @forelse($itemSales as $index => $item)
                                <tr class="hover:bg-zinc-50 transition-colors">
                                    <td class="px-5 py-3">
                                        <div class="flex items-center">
                                            <span class="w-5 h-5 rounded-full {{ $index < 3 ? 'bg-amber-100 text-amber-600' : 'bg-zinc-100 text-zinc-500' }} flex items-center justify-center text-[9px] font-black mr-3">
                                                {{ $index + 1 }}
                                            </span>
                                            <span class="text-xs font-black uppercase text-zinc-900">{{ $item->name }}</span>
                                        </div>
                                    </td>
                                    <td class="px-5 py-3 text-center">
                                        <span class="px-2 py-1 bg-indigo-50 text-indigo-700 text-[10px] font-black rounded-sm">
                                            {{ $item->total_qty }}
                                        </span>
                                    </td>
                                    <td class="px-5 py-3 text-right">
                                        <span class="text-xs font-black text-zinc-900">₹{{ number_format($item->total_revenue, 2) }}</span>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="3" class="px-5 py-12 text-center">
                                        <div class="inline-flex items-center justify-center w-12 h-12 rounded-full bg-zinc-100 text-zinc-400 mb-3">
                                            <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="M20 13V6a2 2 0 00-2-2H6a2 2 0 00-2 2v7m16 0v5a2 2 0 01-2 2H6a2 2 0 01-2-2v-5m16 0h-2.586a1 1 0 00-.707.293l-2.414 2.414a1 1 0 01-.707.293h-3.172a1 1 0 01-.707-.293l-2.414-2.414A1 1 0 006.586 13H4"></path></svg>
                                        </div>
                                        <p class="text-[11px] font-black text-zinc-500 uppercase tracking-widest">No items sold in this period</p>
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- Tier 2 Reports Row -->
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        
        <!-- Category Sales -->
        <div class="lg:col-span-1 bg-white rounded-sm border border-zinc-200 shadow-sm overflow-hidden flex flex-col">
            <div class="bg-zinc-900 px-5 py-4 border-b border-zinc-800 flex justify-between items-center">
                <h3 class="text-[11px] font-black uppercase tracking-widest text-white flex items-center">
                    <svg class="w-4 h-4 text-rose-400 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 3.055A9.001 9.001 0 1020.945 13H11V3.055z"></path><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20.488 9H15V3.512A9.025 9.025 0 0120.488 9z"></path></svg>
                    Category Sales
                </h3>
            </div>
            <div class="flex-1 overflow-x-auto">
                <table class="w-full text-left border-collapse">
                    <tbody class="divide-y divide-zinc-100">
                        @forelse($categorySales as $category)
                            <tr class="hover:bg-zinc-50 transition-colors">
                                <td class="px-5 py-3">
                                    <span class="text-[11px] font-black uppercase text-zinc-900">{{ $category->name }}</span>
                                </td>
                                <td class="px-5 py-3 text-right">
                                    <span class="text-[11px] font-black text-zinc-900">₹{{ number_format($category->total_revenue, 2) }}</span>
                                    <span class="text-[9px] font-bold text-zinc-400 block">{{ $category->total_qty }} items</span>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="2" class="px-5 py-8 text-center">
                                    <p class="text-[10px] font-black text-zinc-400 uppercase tracking-widest">No data available</p>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Waiter Performance -->
        <div class="lg:col-span-2 bg-white rounded-sm border border-zinc-200 shadow-sm overflow-hidden flex flex-col">
            <div class="bg-zinc-900 px-5 py-4 border-b border-zinc-800 flex justify-between items-center">
                <h3 class="text-[11px] font-black uppercase tracking-widest text-white flex items-center">
                    <svg class="w-4 h-4 text-cyan-400 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"></path></svg>
                    Steward Performance
                </h3>
                @if($tableTurnaround)
                    <div class="flex items-center space-x-2 bg-zinc-800 px-3 py-1 rounded-sm border border-zinc-700">
                        <svg class="w-3 h-3 text-emerald-400" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>
                        <span class="text-[9px] font-black text-zinc-300 uppercase tracking-widest">Avg Table Turnaround:</span>
                        <span class="text-[10px] font-black text-white">{{ round($tableTurnaround) }} mins</span>
                    </div>
                @endif
            </div>
            <div class="flex-1 overflow-x-auto">
                <table class="w-full text-left border-collapse">
                    <thead>
                        <tr class="bg-zinc-50 border-b border-zinc-200">
                            <th class="px-5 py-3 text-[10px] font-black text-zinc-500 uppercase tracking-widest">Steward Name</th>
                            <th class="px-5 py-3 text-[10px] font-black text-zinc-500 uppercase tracking-widest text-center">Orders Handled</th>
                            <th class="px-5 py-3 text-[10px] font-black text-zinc-500 uppercase tracking-widest text-right">Total Revenue</th>
                            <th class="px-5 py-3 text-[10px] font-black text-zinc-500 uppercase tracking-widest text-right hidden sm:table-cell">Avg Order Value</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-zinc-100">
                        @forelse($waiterPerformance as $waiter)
                            <tr class="hover:bg-zinc-50 transition-colors">
                                <td class="px-5 py-3">
                                    <div class="flex items-center">
                                        <div class="w-6 h-6 rounded-sm bg-indigo-100 text-indigo-700 flex items-center justify-center text-[9px] font-black mr-3">
                                            {{ substr($waiter['name'], 0, 1) }}
                                        </div>
                                        <span class="text-[11px] font-black uppercase text-zinc-900">{{ $waiter['name'] }}</span>
                                    </div>
                                </td>
                                <td class="px-5 py-3 text-center">
                                    <span class="text-[11px] font-black text-zinc-700">{{ $waiter['total_orders'] }}</span>
                                </td>
                                <td class="px-5 py-3 text-right">
                                    <span class="text-[11px] font-black text-zinc-900">₹{{ number_format($waiter['total_revenue'], 2) }}</span>
                                </td>
                                <td class="px-5 py-3 text-right hidden sm:table-cell">
                                    <span class="text-[11px] font-black text-zinc-500">₹{{ number_format($waiter['avg_order_value'], 2) }}</span>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="4" class="px-5 py-8 text-center">
                                    <p class="text-[10px] font-black text-zinc-400 uppercase tracking-widest">No steward data available</p>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

    </div>
</div>
@endsection
