@extends('layouts.master')

@push('css')
<style>
    .spr-wrap { overflow-x: auto; }
    .spr-header { text-align: center; margin-bottom: 12px; }
    .spr-header .co-name { font-size: 22px; font-weight: 700; letter-spacing: 1px; }
    .spr-header .co-sub  { font-size: 14px; font-weight: 600; }
    .spr-header .co-date { font-size: 12px; color: #555; margin-top: 2px; }
    .spr-table { width: 100%; border-collapse: collapse; font-size: 11.5px; white-space: nowrap; }
    .spr-table thead th { border: 1px solid #888; padding: 7px 8px; text-align: center; background: #f5f5f5; font-weight: 700; }
    .spr-table thead th.th-l { text-align: left; }
    .spr-table tbody td { border: 1px solid #ddd; padding: 5px 8px; vertical-align: middle; font-size: 11px; }
    .spr-table td.td-l   { text-align: left; }
    .spr-table td.td-c   { text-align: center; }
    .spr-table td.td-num { text-align: right; font-variant-numeric: tabular-nums; }
    .spr-table tr.row-party td { background: #fff3cd; font-weight: 800; font-size: 13px; color: #856404; border-top: 2px solid #aaa !important; border-bottom: 1px solid #aaa !important; text-align: left; }
    .spr-table tr.row-vtotal td { background: #e8f4e8; font-weight: 700; border-top: 1px solid #aaa !important; border-bottom: 2px solid #888 !important; }
    .spr-table tr.row-grand td { background: #cce5ff; font-weight: 800; font-size: 12px; color: #004085; border-top: 2px solid #004085 !important; border-bottom: 2px solid #004085 !important; }
    .spr-table tr.row-item td { background: #fff; }
    .spr-table tr.row-item:nth-child(even) td { background: #fafafa; }
    .neg { color: #c00; }
</style>
@endpush

@section('content')
<div class="card">
    <div class="card-header"><h3>Service Purchase Report</h3></div>
    <div class="card-body">

        <form class="row g-2 align-items-end" method="GET" action="{{ route('service-purchase-report.index') }}">
            <div class="col-md-3">
                <label class="form-label">Date From</label>
                <input type="date" name="date_from" class="form-control form-control-sm" value="{{ $dateFrom }}">
            </div>
            <div class="col-md-3">
                <label class="form-label">Date To</label>
                <input type="date" name="date_to" class="form-control form-control-sm" value="{{ $dateTo }}">
            </div>
            <div class="col-md-4">
                <label class="form-label">Supplier</label>
                <select name="supplier_id" class="form-select form-select-sm">
                    <option value="">-- All Suppliers --</option>
                    @foreach($suppliers as $sup)
                        <option value="{{ $sup->id_supplier }}" {{ $supplierId == $sup->id_supplier ? 'selected' : '' }}>{{ $sup->supplier_name }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-2 d-flex gap-2">
                <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-search"></i> Search</button>
                @if($reportData)
                    <a href="{{ route('service-purchase-report.print', request()->query()) }}" target="_blank" class="btn btn-warning btn-sm"><i class="fa fa-print"></i> Print</a>
                @endif
                <a href="{{ route('service-purchase-report.index') }}" class="btn btn-secondary btn-sm"><i class="fa fa-refresh"></i> Reset</a>
            </div>
        </form>

        <hr />

        @if($reportData)
            @php
                $get = function($item, $key, $default = '') {
                    if (is_array($item)) return $item[$key] ?? $default;
                    if (is_object($item)) return $item->$key ?? $default;
                    return $default;
                };
                $fmt = fn($v) => $v == 0 ? '-' : ($v < 0 ? '(' . number_format(abs($v), 3) . ')' : number_format($v, 3));
                $partiesList = $reportData['parties'];
                $grandTotal  = $reportData['grandTotal'];
            @endphp

            @if(empty($partiesList))
                <div class="alert alert-info">No records found for selected filters.</div>
            @else
                <div class="spr-wrap">
                    <table class="spr-table">
                        <thead>
                            <tr>
                                <th>Date</th><th>SP #</th><th>Bill #</th><th>Vehicle #</th>
                                <th class="th-l">Party</th><th class="th-l">Expense A/C</th>
                                <th class="th-l">Narration</th><th>Qty</th><th>Unit</th><th>Price</th><th>Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($partiesList as $party)
                                <tr class="row-party">
                                    <td colspan="11">Party: {{ strtoupper($get($party, 'party_name')) }}</td>
                                </tr>
                                @foreach($get($party, 'vehicles', []) as $vehicle)
                                    @foreach($get($vehicle, 'bills', []) as $bill)
                                        @php $items = $get($bill, 'items', []); @endphp
                                        @foreach($items as $idx => $item)
                                            <tr class="row-item">
                                                @if($idx === 0)
                                                    <td class="td-c" rowspan="{{ count($items) }}">{{ \Carbon\Carbon::parse($get($bill, 'date'))->format('d/m/y') }}</td>
                                                    <td class="td-c" rowspan="{{ count($items) }}">{{ $get($bill, 'voucher_no') }}</td>
                                                    <td class="td-c" rowspan="{{ count($items) }}">{{ $get($bill, 'bill_no') }}</td>
                                                    <td class="td-c" rowspan="{{ count($items) }}">{{ $get($vehicle, 'truck_no') }}</td>
                                                    <td class="td-l" rowspan="{{ count($items) }}">{{ strtoupper($get($bill, 'party_name')) }}</td>
                                                @endif
                                                <td class="td-l">{{ $get($item, 'expense_account_name', 'LABOUR') }}</td>
                                                <td class="td-l">{{ $get($item, 'narration') }}</td>
                                                <td class="td-num">{{ $fmt($get($item, 'qty', 0)) }}</td>
                                                <td class="td-c">{{ $get($item, 'unit', 'Kgs') }}</td>
                                                <td class="td-num">{{ number_format($get($item, 'price', 0), 1) }}</td>
                                                <td class="td-num">{{ $fmt($get($item, 'amount', 0)) }}</td>
                                            </tr>
                                        @endforeach
                                        @if(empty($items))
                                            <tr class="row-item">
                                                <td class="td-c">{{ \Carbon\Carbon::parse($get($bill, 'date'))->format('d/m/y') }}</td>
                                                <td class="td-c">{{ $get($bill, 'voucher_no') }}</td>
                                                <td class="td-c">{{ $get($bill, 'bill_no') }}</td>
                                                <td class="td-c">{{ $get($vehicle, 'truck_no') }}</td>
                                                <td class="td-l">{{ strtoupper($get($bill, 'party_name')) }}</td>
                                                <td colspan="6">—</td>
                                            </tr>
                                        @endif
                                    @endforeach
                                    <tr class="row-vtotal">
                                        <td colspan="7" class="td-l fw-bold">Vehicle {{ $get($vehicle, 'truck_no') }} — Total</td>
                                        <td class="td-num">{{ number_format($get($vehicle, 'vehicleTotals')['total_qty'] ?? 0, 3) }}</td>
                                        <td></td><td></td>
                                        <td class="td-num">{{ $fmt($get($vehicle, 'vehicleTotals')['total_amount'] ?? 0) }}</td>
                                    </tr>
                                @endforeach
                            @endforeach
                            <tr class="row-grand">
                                <td colspan="7" class="td-l">GRAND TOTAL</td>
                                <td class="td-num">{{ number_format($grandTotal['total_qty'] ?? 0, 3) }}</td>
                                <td></td><td></td>
                                <td class="td-num">{{ $fmt($grandTotal['total_amount'] ?? 0) }}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            @endif
        @endif
    </div>
</div>
@endsection
