@extends('layouts.app')
@section('title', 'Orders')
@section('content')
<div class="d-flex justify-content-between align-items-center mb-3">
    <h4>Orders</h4>
    <a href="{{ route('orders.create') }}" class="btn btn-primary btn-sm"><i class="bi bi-plus"></i> New Order</a>
</div>

<div class="card mb-3">
    <div class="card-body py-2">
        <div class="d-flex flex-wrap align-items-center gap-2">
            <span class="text-muted small me-1">Filter:</span>
            <a href="{{ route('orders.index', ['filter' => 'today']) }}" class="btn btn-sm {{ $filter == 'today' ? 'btn-primary' : 'btn-outline-secondary' }}">Today</a>
            <a href="{{ route('orders.index', ['filter' => 'yesterday']) }}" class="btn btn-sm {{ $filter == 'yesterday' ? 'btn-primary' : 'btn-outline-secondary' }}">Yesterday</a>
            <a href="{{ route('orders.index', ['filter' => 'week']) }}" class="btn btn-sm {{ $filter == 'week' ? 'btn-primary' : 'btn-outline-secondary' }}">This Week</a>
            <a href="{{ route('orders.index', ['filter' => 'month']) }}" class="btn btn-sm {{ $filter == 'month' ? 'btn-primary' : 'btn-outline-secondary' }}">This Month</a>
            <form action="{{ route('orders.index') }}" method="GET" class="d-flex align-items-center gap-1 ms-2">
                <input type="hidden" name="filter" value="custom">
                <input type="date" name="from" value="{{ $from }}" class="form-control form-control-sm" style="width:140px">
                <span class="text-muted">to</span>
                <input type="date" name="to" value="{{ $to }}" class="form-control form-control-sm" style="width:140px">
                <button class="btn btn-sm btn-outline-primary">Go</button>
            </form>
        </div>
    </div>
</div>

<div class="row g-3 mb-3">
    <div class="col-md-4">
        <div class="card border-start border-primary border-3">
            <div class="card-body py-2">
                <div class="text-muted small">{{ $label }}</div>
                <div class="fs-5 fw-bold">{{ $orders->total() }} orders</div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card border-start border-success border-3">
            <div class="card-body py-2">
                <div class="text-muted small">Total Sales</div>
                <div class="fs-5 fw-bold">Rs. {{ number_format($totalSales, 2) }}</div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card border-start border-info border-3">
            <div class="card-body py-2">
                <div class="text-muted small">Total Received</div>
                <div class="fs-5 fw-bold">Rs. {{ number_format($totalPaid, 2) }}</div>
            </div>
        </div>
    </div>
</div>

<div class="card">
    <div class="card-body p-0">
        <table class="table table-hover mb-0">
            <thead><tr><th>#</th><th>Customer</th><th>Date</th><th>Items</th><th>Total</th><th>Received</th><th>Actions</th></tr></thead>
            <tbody>
            @forelse($orders as $order)
                <tr>
                    <td>{{ $order->id }}</td>
                    <td>
                        @if($order->customer)
                            <a href="{{ route('customers.show', $order->customer) }}">{{ $order->customer->name }}</a>
                            @if($order->customer->balance > 0)
                                <br><small class="text-danger">Balance: Rs. {{ number_format($order->customer->balance, 2) }}</small>
                            @endif
                        @else
                            {{ $order->customer_name ?: 'Walk-in' }}
                        @endif
                    </td>
                    <td>{{ $order->date->format('d M Y') }}</td>
                    <td>
                        @foreach($order->items as $item)
                            <small>{{ $item->product->category->name ?? '' }} {{ $item->product->weight->label ?? '' }} x{{ (int)$item->quantity }}</small>{{ !$loop->last ? ', ' : '' }}
                        @endforeach
                    </td>
                    <td>Rs. {{ number_format($order->total, 2) }}</td>
                    <td>Rs. {{ number_format($order->paid, 2) }}</td>
                    <td>
                        <a href="{{ route('orders.show', $order) }}" class="btn btn-sm btn-outline-info">View</a>
                        <a href="{{ route('orders.edit', $order) }}" class="btn btn-sm btn-outline-primary">Edit</a>
                        <form action="{{ route('orders.destroy', $order) }}" method="POST" class="d-inline" onsubmit="return confirm('Delete this order?')">
                            @csrf @method('DELETE')
                            <button class="btn btn-sm btn-outline-danger">Del</button>
                        </form>
                    </td>
                </tr>
            @empty
                <tr><td colspan="7" class="text-center text-muted">No orders for this period</td></tr>
            @endforelse
            </tbody>
        </table>
    </div>
</div>
<div class="mt-3">{{ $orders->links() }}</div>
@endsection
