@extends('backEnd.layouts.master')
@section('title', 'Financial Report')
@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="page-title-box">
                <h4 class="page-title">Financial Analytics & Business Report</h4>
            </div>
        </div>
    </div>

    <!-- Summary Cards -->
    <div class="row">
        <div class="col-md-3">
            <div class="card widget-flat bg-primary text-white">
                <div class="card-body">
                    <h5 class="fw-normal mt-0">Total Sales</h5>
                    <h3 class="mt-3 mb-3">৳{{ number_format($total_sales) }}</h3>
                    <p class="mb-0 text-white-50"><span class="me-2">Revenue from Orders</span></p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card widget-flat bg-success text-white">
                <div class="card-body">
                    <h5 class="fw-normal mt-0">Total Gross Profit</h5>
                    <h3 class="mt-3 mb-3">৳{{ number_format($total_profit) }}</h3>
                    <p class="mb-0 text-white-50"><span class="me-2">Sales - Cost of Goods</span></p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card widget-flat bg-danger text-white">
                <div class="card-body">
                    <h5 class="fw-normal mt-0">Total Expenses</h5>
                    <h3 class="mt-3 mb-3">৳{{ number_format($total_expenses) }}</h3>
                    <p class="mb-0 text-white-50"><span class="me-2">Office & Operational</span></p>
                </div>
            </div>
        </div>
        <div class="col-md-3">
            <div class="card widget-flat bg-info text-white">
                <div class="card-body">
                    <h5 class="fw-normal mt-0">Net Balance</h5>
                    <h3 class="mt-3 mb-3">৳{{ number_format($total_balance) }}</h3>
                    <p class="mb-0 text-white-50"><span class="me-2">Profit - Expenses</span></p>
                </div>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <h4 class="header-title mb-4">Sales & Profit Breakdown</h4>
                    <div class="table-responsive">
                        <table class="table table-hover table-centered mb-0">
                            <thead>
                                <tr>
                                    <th>Date</th>
                                    <th>Invoice</th>
                                    <th>Sales Amount</th>
                                    <th>Purchase Cost</th>
                                    <th>Gross Profit</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($orders as $order)
                                @php
                                    $order_purchase = $order->orderdetails->sum(function($detail) {
                                        return $detail->purchase_price * $detail->qty;
                                    });
                                    $order_profit = $order->amount - $order_purchase;
                                @endphp
                                <tr>
                                    <td>{{ $order->created_at->format('d M, Y') }}</td>
                                    <td>{{ $order->invoice_id }}</td>
                                    <td>৳{{ number_format($order->amount) }}</td>
                                    <td>৳{{ number_format($order_purchase) }}</td>
                                    <td><span class="text-success fw-bold">৳{{ number_format($order_profit) }}</span></td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="row mt-4">
        <div class="col-xl-6">
            <div class="card">
                <div class="card-body">
                    <h4 class="header-title mb-4">Office Expenses</h4>
                    <div class="table-responsive">
                        <table class="table table-hover table-sm">
                            <thead>
                                <tr>
                                    <th>Date</th>
                                    <th>Title</th>
                                    <th>Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($expenses as $expense)
                                <tr>
                                    <td>{{ date('d M, Y', strtotime($expense->expense_date)) }}</td>
                                    <td>{{ $expense->expense_title }}</td>
                                    <td>৳{{ number_format($expense->amount) }}</td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xl-6">
            <div class="card">
                <div class="card-body">
                    <h4 class="header-title mb-4">Supplier Purchases</h4>
                    <div class="table-responsive">
                        <table class="table table-hover table-sm">
                            <thead>
                                <tr>
                                    <th>Date</th>
                                    <th>Supplier</th>
                                    <th>Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($purchases as $purchase)
                                <tr>
                                    <td>{{ date('d M, Y', strtotime($purchase->purchase_date)) }}</td>
                                    <td>{{ $purchase->supplier_name }}</td>
                                    <td>৳{{ number_format($purchase->total_price) }}</td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
