<table>
    <thead>
        <tr></tr>
    </thead>
    <tbody>
        <td></td>
    </tbody>
</table>
<table class="table table-striped mt-2" style="width:100%">
    <thead>
        <tr>
            <th display="none"></th>
            <th display="none"></th>
            <th style="text-align: center">Invoice</th>
            <th style="text-align: center">Tanggal</th>
            <th style="text-align: center">Jam</th>
            <th style="text-align: center">User</th>
            <th style="text-align: center">Staff</th>
            <th style="text-align: center">Member</th>
            <th style="text-align: center">Item Code</th>
            <th style="text-align: center">Item Barcode</th>
            <th style="text-align: center">Item Name</th>
            @if ($udf)
                @foreach ($field as $fld)
                    <th style="text-align: center">
                        Item {{ Str::after($fld, 'U_IDU_') }}
                    </th>
                @endforeach
            @endif
            <th style="text-align: center">Qty</th>
            <th style="text-align: center">Price</th>
            <th style="text-align: center">Disc</th>
            <th style="text-align: center">Sub Total</th>
            <th style="text-align: center">Grand Total</th>

        </tr>
    </thead>
    <tbody>
        @php
            $totalDiscount = 0;
            $totalSumsubtotal = 0;
        @endphp
        @foreach ($invoicesdetail as $invoice)
            @php
                $discount = $invoice->nprice - $invoice->price;
                $totalDiscount += $discount;
                $subtotal = $invoice->nprice * $invoice->qty;
                $totalSumsubtotal += $subtotal;
            @endphp
            <tr>
                <td display="none"></td>
                <td display="none"></td>
                <td>{{ $invoice->invoice }}</td>
                <td>{{ $invoice->created_at->format('d/m/y') }}</td>
                <td>{{ $invoice->created_at->format('H:i') }}</td>
                <td>{{ $invoice->kasir->name }}</td>
                <td>{{ $invoice->sales->name }}</td>
                @if ($invoice->id_member)
                    <td>{{ $invoice->member->name }}</td>
                @else
                    <td> - </td>
                @endif
                <td>{{ $invoice->item->code }}</td>
                <td>{{ $invoice->item->barcode }}</td>
                <td>{{ $invoice->item->name }}</td>
                @if ($udf)
                    @foreach ($field as $fld)
                        <td>{{ $invoice->item->$fld ?? '-' }}</td>
                    @endforeach
                @endif
                <td>{{ $invoice->qty }}</td>
                <td>{{ $invoice->nprice }}</td>
                <td>{{ $discount }}</td>
                <td>{{ $subtotal }}</td>
                <td>{{ $invoice->total_price }}</td>
            </tr>
        @endforeach
        <tr>
            <td display="none"></td>
            <td display="none"></td>
            <td colspan="9">Grand Total</td>
            @if ($udf)
                @foreach ($field as $fld)
                    <td display="none"></td>
                @endforeach
            @endif
            @php
                $totalQty = $invoicesdetail
                    ->filter(function ($detail) {
                        return $detail->qty > 0;
                    })
                    ->sum('qty');
            @endphp

            <td>{{ $totalQty }}</td>
            <td></td>
            <td>{{ $totalDiscount }}</td>
            <td>{{ $totalSumsubtotal }}</td>
            <td>{{ $invoicesdetail->sum('total_price') }}</td>
        </tr>
    </tbody>
</table>
