@extends('layouts.main')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <h4>Profit Report</h4>
    <div>
        <a href="{{ route('reports.profit.pdf', ['date_from' => $dateFrom, 'date_to' => $dateTo]) }}" class="btn btn-danger" target="_blank">
            <i class="bi bi-file-pdf"></i> Print PDF
        </a>
        <a href="{{ route('reports.profit.export', ['date_from' => $dateFrom, 'date_to' => $dateTo]) }}" class="btn btn-success">
            <i class="bi bi-download"></i> Export CSV
        </a>
    </div>
</div>

<div class="card mb-4">
    <div class="card-header">
        <form method="GET" action="{{ route('reports.profit') }}" class="row g-3">
            <div class="col-md-4">
                <label class="form-label">Date From</label>
                <input type="date" name="date_from" class="form-control" value="{{ $dateFrom }}">
            </div>
            <div class="col-md-4">
                <label class="form-label">Date To</label>
                <input type="date" name="date_to" class="form-control" value="{{ $dateTo }}">
            </div>
            <div class="col-md-2">
                <label class="form-label">&nbsp;</label>
                <button type="submit" class="btn btn-primary w-100">Generate</button>
            </div>
            <div class="col-md-2">
                <label class="form-label">&nbsp;</label>
                <a href="{{ route('reports.profit') }}" class="btn btn-secondary w-100">Today</a>
            </div>
        </form>
    </div>
</div>

<div class="row mb-4">
    <div class="col-md-3">
        <div class="card bg-success text-white">
            <div class="card-body">
                <h5>Total Revenue</h5>
                <h3>{{ number_format($totalRevenue, 2) }}</h3>
                <small>{{ $totalOrders }} orders | Avg: {{ number_format($averageOrderValue, 2) }}</small>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card bg-warning text-dark">
            <div class="card-body">
                <h5>Total Cost</h5>
                <h3>{{ number_format($totalCost, 2) }}</h3>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card bg-info text-white">
            <div class="card-body">
                <h5>Gross Profit</h5>
                <h3>{{ number_format($grossProfit, 2) }}</h3>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card bg-primary text-white">
            <div class="card-body">
                <h5>Net Profit</h5>
                <h3>{{ number_format($netProfit, 2) }}</h3>
                <small>After expenses: {{ number_format($expenses, 2) }}</small>
            </div>
        </div>
    </div>
</div>

<div class="row mb-4">
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">Payment Methods Breakdown</h5>
            </div>
            <div class="card-body">
                <table class="table table-sm">
                    <tr>
                        <td><strong>Cash</strong></td>
                        <td class="text-end">{{ number_format($cashPayments, 2) }}</td>
                        <td class="text-end text-muted">{{ $totalRevenue > 0 ? number_format(($cashPayments / $totalRevenue) * 100, 1) : 0 }}%</td>
                    </tr>
                    <tr>
                        <td><strong>M-Pesa</strong></td>
                        <td class="text-end">{{ number_format($mpesaPayments, 2) }}</td>
                        <td class="text-end text-muted">{{ $totalRevenue > 0 ? number_format(($mpesaPayments / $totalRevenue) * 100, 1) : 0 }}%</td>
                    </tr>
                    <tr>
                        <td><strong>Card</strong></td>
                        <td class="text-end">{{ number_format($cardPayments, 2) }}</td>
                        <td class="text-end text-muted">{{ $totalRevenue > 0 ? number_format(($cardPayments / $totalRevenue) * 100, 1) : 0 }}%</td>
                    </tr>
                    <tr>
                        <td><strong>Credit</strong></td>
                        <td class="text-end">{{ number_format($creditPayments, 2) }}</td>
                        <td class="text-end text-muted">{{ $totalRevenue > 0 ? number_format(($creditPayments / $totalRevenue) * 100, 1) : 0 }}%</td>
                    </tr>
                    <tr class="table-active">
                        <td><strong>Total</strong></td>
                        <td class="text-end"><strong>{{ number_format($totalRevenue, 2) }}</strong></td>
                        <td class="text-end"><strong>100%</strong></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0">Category Profits</h5>
            </div>
            <div class="card-body table-responsive">
                <table class="table table-sm">
                    <thead>
                        <tr>
                            <th>Category</th>
                            <th class="text-end">Revenue</th>
                            <th class="text-end">Cost</th>
                            <th class="text-end">Profit</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($categoryProfits as $cat)
                            <tr>
                                <td>{{ $cat->name }}</td>
                                <td class="text-end">{{ number_format($cat->total_revenue, 2) }}</td>
                                <td class="text-end">{{ number_format($cat->total_cost, 2) }}</td>
                                <td class="text-end"><strong>{{ number_format($cat->total_revenue - $cat->total_cost, 2) }}</strong></td>
                            </tr>
                        @empty
                            <tr><td colspan="4" class="text-center">No data</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div class="card mb-4">
    <div class="card-header">
        <h5 class="mb-0">Daily Sales</h5>
    </div>
    <div class="card-body table-responsive">
        <table class="table table-sm">
            <thead>
                <tr>
                    <th>Date</th>
                    <th class="text-end">Orders</th>
                    <th class="text-end">Revenue</th>
                </tr>
            </thead>
            <tbody>
                @forelse($dailySales as $day)
                    <tr>
                        <td>{{ \Carbon\Carbon::parse($day->date)->format('d M Y') }}</td>
                        <td class="text-end">{{ $day->orders_count }}</td>
                        <td class="text-end"><strong>{{ number_format($day->daily_revenue, 2) }}</strong></td>
                    </tr>
                @empty
                    <tr><td colspan="3" class="text-center">No data</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>

<div class="card mb-4">
    <div class="card-header">
        <h5 class="mb-0">Top Selling Products</h5>
    </div>
    <div class="card-body table-responsive">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>Product</th>
                    <th class="text-end">Qty Sold</th>
                    <th class="text-end">Revenue</th>
                    <th class="text-end">Cost</th>
                    <th class="text-end">Profit</th>
                </tr>
            </thead>
            <tbody>
                @forelse($topProducts as $product)
                    <tr>
                        <td>{{ $product->name }}</td>
                        <td class="text-end">{{ $product->total_sold }}</td>
                        <td class="text-end">{{ number_format($product->total_revenue, 2) }}</td>
                        <td class="text-end">{{ number_format($product->total_cost, 2) }}</td>
                        <td class="text-end"><strong>{{ number_format($product->total_revenue - $product->total_cost, 2) }}</strong></td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="5" class="text-center">No data found.</td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>

<div class="card mb-4">
    <div class="card-header">
        <h5 class="mb-0">Profit per Item</h5>
    </div>
    <div class="card-body table-responsive">
        <table class="table table-sm table-hover">
            <thead>
                <tr>
                    <th>Product</th>
                    <th class="text-end">SKU</th>
                    <th class="text-end">Cost/Unit</th>
                    <th class="text-end">Qty Sold</th>
                    <th class="text-end">Revenue</th>
                    <th class="text-end">Total Cost</th>
                    <th class="text-end">Profit</th>
                    <th class="text-end">Margin</th>
                </tr>
            </thead>
            <tbody>
                @forelse($itemProfits as $item)
                    <tr>
                        <td>{{ $item->name }}</td>
                        <td class="text-end text-muted">{{ $item->sku ?? '-' }}</td>
                        <td class="text-end">{{ number_format($item->cost_price, 2) }}</td>
                        <td class="text-end">{{ $item->total_quantity }}</td>
                        <td class="text-end">{{ number_format($item->total_revenue, 2) }}</td>
                        <td class="text-end">{{ number_format($item->total_cost, 2) }}</td>
                        <td class="text-end"><strong>{{ number_format($item->total_profit, 2) }}</strong></td>
                        <td class="text-end">
                            @php $margin = $item->total_revenue > 0 ? ($item->total_profit / $item->total_revenue) * 100 : 0; @endphp
                            <span class="badge {{ $margin >= 30 ? 'bg-success' : ($margin >= 15 ? 'bg-warning' : 'bg-danger') }}">
                                {{ number_format($margin, 1) }}%
                            </span>
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="8" class="text-center">No data found.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>
@endsection
