@extends('layouts.admin')

@section('title', 'Profit by Order Report')

@section('content')
<x-admin.report-page title="Profit by Order Report" subtitle="Order-level revenue, COGS, profit, and margin.">
    <x-slot:actions>
        <a href="{{ route('admin.reports.profit') }}" class="ci-btn ci-btn--ghost">
            <i class="bi bi-arrow-left"></i> Back to Overview
        </a>
    </x-slot:actions>

    <form method="GET" class="ci-filters ci-filters--report">
        @include('admin.reports.partials.location-filter', ['ci' => true])
        <div class="ci-filters__field">
            <label for="start_date">Start Date</label>
            <input type="date" name="start_date" id="start_date" value="{{ $startDate }}">
        </div>
        <div class="ci-filters__field">
            <label for="end_date">End Date</label>
            <input type="date" name="end_date" id="end_date" value="{{ $endDate }}">
        </div>
        <div class="ci-filters__field">
            <label for="order_status">Order Status</label>
            <select name="order_status" id="order_status">
                <option value="">All Status</option>
                <option value="delivered" {{ $orderStatus == 'delivered' ? 'selected' : '' }}>Delivered</option>
                <option value="shipped" {{ $orderStatus == 'shipped' ? 'selected' : '' }}>Shipped</option>
                <option value="confirmed" {{ $orderStatus == 'confirmed' ? 'selected' : '' }}>Confirmed</option>
            </select>
        </div>
        <div class="ci-filters__actions">
            <button type="submit" class="ci-btn ci-btn--primary">Filter</button>
        </div>
        <div class="ci-filters__count">
            Showing <strong>{{ $orders->count() }}</strong> orders
        </div>
    </form>

    @if($orders->count() > 0)
        @php
            $avgProfit = $orders->count() > 0 ? $orders->sum('profit') / $orders->count() : 0;
        @endphp
        <div class="ci-stats">
            <div class="ci-stat">
                <div>
                    <p class="ci-stat__label">Total Orders</p>
                    <p class="ci-stat__value">{{ $orders->count() }}</p>
                </div>
                <span class="ci-stat__icon"><i class="bi bi-receipt"></i></span>
            </div>
            <div class="ci-stat">
                <div>
                    <p class="ci-stat__label">Profitable Orders</p>
                    <p class="ci-stat__value ci-stat__value--success">{{ $orders->where('profit', '>', 0)->count() }}</p>
                </div>
                <span class="ci-stat__icon"><i class="bi bi-graph-up"></i></span>
            </div>
            <div class="ci-stat">
                <div>
                    <p class="ci-stat__label">Loss-making Orders</p>
                    <p class="ci-stat__value ci-stat__value--danger">{{ $orders->where('profit', '<', 0)->count() }}</p>
                </div>
                <span class="ci-stat__icon"><i class="bi bi-graph-down"></i></span>
            </div>
            <div class="ci-stat">
                <div>
                    <p class="ci-stat__label">Avg. Profit per Order</p>
                    <p class="ci-stat__value ci-money {{ $avgProfit >= 0 ? 'ci-stat__value--success' : 'ci-stat__value--danger' }}">
                        ₹{{ number_format($avgProfit, 2) }}
                    </p>
                </div>
                <span class="ci-stat__icon"><i class="bi bi-currency-rupee"></i></span>
            </div>
        </div>
    @endif

    <div class="ci-table-card">
        <div class="ci-table-wrap">
            <table class="ci-table">
                <thead>
                    <tr>
                        <th>Order ID</th>
                        <th>Date</th>
                        <th>Customer</th>
                        <th class="ci-end">Order Total</th>
                        <th class="ci-end">Revenue</th>
                        <th class="ci-end">COGS</th>
                        <th class="ci-end">Profit</th>
                        <th class="ci-end">Margin %</th>
                        <th class="ci-end">Status</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($orders as $order)
                    <tr>
                        <td>
                            <strong>#{{ $order['order_id'] }}</strong>
                        </td>
                        <td>
                            {{ \Carbon\Carbon::parse($order['date'])->format('M d, Y') }}
                        </td>
                        <td>
                            {{ $order['customer'] }}
                        </td>
                        <td class="ci-end ci-money">
                            ₹{{ number_format($order['total_payable'], 2) }}
                        </td>
                        <td class="ci-end ci-money">
                            ₹{{ number_format($order['revenue'], 2) }}
                        </td>
                        <td class="ci-end ci-money ci-money--muted">
                            ₹{{ number_format($order['cogs'], 2) }}
                        </td>
                        <td class="ci-end ci-money {{ $order['profit'] >= 0 ? '' : 'ci-stat__value--danger' }}" @if($order['profit'] >= 0) style="color:var(--ci-success);" @endif>
                            ₹{{ number_format($order['profit'], 2) }}
                        </td>
                        <td class="ci-end">
                            <span class="ci-pill {{ $order['margin_percent'] >= 0 ? 'ci-pill--success' : 'ci-pill--danger' }}">
                                {{ number_format($order['margin_percent'], 1) }}%
                            </span>
                        </td>
                        <td class="ci-end">
                            @php
                                $statusPills = [
                                    'delivered' => 'ci-pill--success',
                                    'shipped' => 'ci-pill--info',
                                    'confirmed' => 'ci-pill--warning',
                                    'placed' => 'ci-pill--inactive',
                                    'cancelled' => 'ci-pill--danger',
                                ];
                                $statusPill = $statusPills[$order['status']] ?? 'ci-pill--inactive';
                            @endphp
                            <span class="ci-pill {{ $statusPill }}">
                                {{ ucfirst($order['status']) }}
                            </span>
                        </td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="9">
                            <div class="ci-empty">
                                <i class="bi bi-receipt"></i>
                                <h3>No Orders Found</h3>
                                <p>No orders found for the selected criteria.</p>
                                <a href="{{ route('admin.orders.index') }}" class="ci-btn ci-btn--primary">
                                    <i class="bi bi-eye"></i> View All Orders
                                </a>
                            </div>
                        </td>
                    </tr>
                    @endforelse
                </tbody>
                @if($orders->count() > 0)
                <tfoot>
                    <tr>
                        <td colspan="3">TOTALS</td>
                        <td class="ci-end ci-money">₹{{ number_format($orders->sum('total_payable'), 2) }}</td>
                        <td class="ci-end ci-money">₹{{ number_format($orders->sum('revenue'), 2) }}</td>
                        <td class="ci-end ci-money">₹{{ number_format($orders->sum('cogs'), 2) }}</td>
                        <td class="ci-end ci-money {{ $orders->sum('profit') >= 0 ? '' : 'ci-stat__value--danger' }}" @if($orders->sum('profit') >= 0) style="color:var(--ci-success);" @endif>
                            ₹{{ number_format($orders->sum('profit'), 2) }}
                        </td>
                        <td class="ci-end">
                            @php
                                $totalRevenue = $orders->sum('revenue');
                                $totalProfit = $orders->sum('profit');
                                $overallMargin = $totalRevenue > 0 ? ($totalProfit / $totalRevenue) * 100 : 0;
                            @endphp
                            <span class="ci-pill {{ $overallMargin >= 0 ? 'ci-pill--success' : 'ci-pill--danger' }}">
                                {{ number_format($overallMargin, 1) }}%
                            </span>
                        </td>
                        <td></td>
                    </tr>
                </tfoot>
                @endif
            </table>
        </div>
    </div>
</x-admin.report-page>
@endsection
