@extends('layouts.biller')

@section('title', 'Sales & Collections Report')
@section('page_title', 'Sales & Collections')

@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>
            <div class="flex items-center space-x-2">
                <a href="{{ route('biller.reports.index') }}" class="text-zinc-400 hover:text-indigo-600 transition-colors">
                    <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="M10 19l-7-7m0 0l7-7m-7 7h18"></path></svg>
                </a>
                <h2 class="text-lg font-black uppercase tracking-tight text-zinc-900">Sales & Collections</h2>
            </div>
            <p class="text-[10px] font-bold text-zinc-500 uppercase tracking-widest mt-1 ml-6">
                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.sales') }}" 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 md:grid-cols-2 gap-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 h-fit">
            <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>
</div>
@endsection
