@extends('layouts.app')
@section('title', 'Order #' . $order->id)
@section('content')
<div class="d-flex justify-content-between align-items-center mb-3">
    <h4>Order #{{ $order->id }}</h4>
    <div>
        <a href="{{ route('orders.edit', $order) }}" class="btn btn-primary btn-sm"><i class="bi bi-pencil"></i> Edit</a>
        <a href="{{ route('orders.index') }}" class="btn btn-secondary btn-sm">Back</a>
    </div>
</div>
<div class="card mb-3">
    <div class="card-body">
        <div class="row">
            <div class="col-md-4">
                <strong>Customer:</strong>
                @if($order->customer)
                    <a href="{{ route('customers.show', $order->customer) }}">{{ $order->customer->name }}{{ $order->customer->shop_name ? ' (' . $order->customer->shop_name . ')' : '' }}</a>
                @else
                    {{ $order->customer_name ?: 'Walk-in' }}
                @endif
            </div>
            <div class="col-md-4"><strong>Date:</strong> {{ $order->date->format('d M Y') }}</div>
            <div class="col-md-4"><strong>Type:</strong> {{ $order->customer_id ? 'B2B' : 'Walk-in' }}</div>
        </div>
        @if($order->notes)
            <div class="mt-2"><strong>Notes:</strong> {{ $order->notes }}</div>
        @endif
    </div>
</div>
<div class="card">
    <div class="card-body p-0">
        <table class="table mb-0">
            <thead><tr><th>Category</th><th>Weight</th><th>Bags</th><th>Price/Bag</th><th>Subtotal</th></tr></thead>
            <tbody>
            @foreach($order->items as $item)
                <tr>
                    <td>{{ $item->product->category->name ?? '-' }}</td>
                    <td>{{ $item->product->weight->label ?? '-' }}</td>
                    <td>{{ (int)$item->quantity }}</td>
                    <td>Rs. {{ number_format($item->price, 2) }}</td>
                    <td>Rs. {{ number_format($item->subtotal, 2) }}</td>
                </tr>
            @endforeach
            </tbody>
            <tfoot>
                <tr><td colspan="4" class="text-end fw-bold">Order Total:</td><td class="fw-bold">Rs. {{ number_format($order->total, 2) }}</td></tr>
                <tr><td colspan="4" class="text-end">Received at Order:</td><td>Rs. {{ number_format($order->paid, 2) }}</td></tr>
            </tfoot>
        </table>
    </div>
</div>

@if($order->customer)
    <div class="card mt-3">
        <div class="card-body d-flex justify-content-between align-items-center">
            <div>
                <strong>{{ $order->customer->name }}'s Current Balance:</strong>
                @if($order->customer->balance > 0)
                    <span class="text-danger fs-5 fw-bold ms-2">Rs. {{ number_format($order->customer->balance, 2) }}</span>
                @else
                    <span class="text-success fs-5 fw-bold ms-2">Clear (No dues)</span>
                @endif
            </div>
            <div>
                <a href="{{ route('customers.pay', $order->customer) }}" class="btn btn-success btn-sm">Receive Payment</a>
                <a href="{{ route('customers.show', $order->customer) }}" class="btn btn-outline-info btn-sm">View Customer</a>
            </div>
        </div>
    </div>
@endif
@endsection
