    @extends('layouts.app')
    @section('title', 'Credit Sales Details')

    @section('content')

    <section class="content-header">
        <h1>
            Credit Sales Details
            <small>Register #{{ $register->id }} &mdash;
                {{ \Carbon\Carbon::parse($open_time)->format('d M Y, h:i A') }}
                &rarr;
                {{ \Carbon\Carbon::parse($close_time)->format('d M Y, h:i A') }}
            </small>
        </h1>
    </section>

    <section class="content">

        {{-- ── Register-matching summary (same formula as the modal) ─────────── --}}
        <div class="row">
            <div class="col-md-4">
                <div class="info-box">
                    <span class="info-box-icon bg-aqua"><i class="fa fa-shopping-cart"></i></span>
                    <div class="info-box-content">
                        <span class="info-box-text">Total Sales (invoices this session)</span>
                        <span class="info-box-number">@format_currency($total_sales)</span>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="info-box">
                    <span class="info-box-icon bg-green"><i class="fa fa-money"></i></span>
                    <div class="info-box-content">
                        <span class="info-box-text">Total Collected at Register</span>
                        <span class="info-box-number">@format_currency($register_collected)</span>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="info-box">
                    <span class="info-box-icon {{ $credit_sales_register < 0 ? 'bg-yellow' : 'bg-red' }}">
                        <i class="fa fa-exclamation-triangle"></i>
                    </span>
                    <div class="info-box-content">
                        <span class="info-box-text">Credit Sales (matches register modal)</span>
                        <span class="info-box-number {{ $credit_sales_register < 0 ? 'text-warning' : '' }}">
                            @format_currency($credit_sales_register)
                        </span>
                    </div>
                </div>
            </div>
        </div>

        {{-- ── Why the two numbers exist ───────────────────────────────────────── --}}
        <div class="row">
            <div class="col-md-12">
                <div class="box box-info">
                    <div class="box-header with-border">
                        <h3 class="box-title">
                            <i class="fa fa-info-circle"></i>
                            How Credit Sales is calculated (and why it can be negative)
                        </h3>
                    </div>
                    <div class="box-body">

                        <h4>Register Modal formula (exact match):</h4>
                        <pre class="bg-light" style="padding:10px; border-radius:4px; font-size:13px;">Credit Sales  =  Total Sales (invoices)  −  Total Collected at Register
                =  @format_currency($total_sales)  −  @format_currency($register_collected)
                =  @format_currency($credit_sales_register)</pre>

                        <h4>Why "Total Collected at Register" ({{ '@' }}format_currency($register_collected)}) can be higher than invoices:</h4>
                        <p>
                            The register's <strong>Total Collected</strong> is read from
                            <code>cash_register_transactions</code> — it records <em>every</em>
                            payment accepted at this physical register, regardless of which session
                            the invoice came from. This includes:
                        </p>
                        <ul>
                            <li>Payments for invoices <strong>created in this session</strong> → @format_currency($total_paid_on_session_invoices)</li>
                            <li>Payments for invoices <strong>from earlier sessions</strong> (old due amounts collected today) → <strong class="text-warning">@format_currency($other_session_payments)</strong></li>
                        </ul>
                        <p>
                            When old-invoice payments exceed the unpaid balance of this session's
                            invoices, credit sales becomes <strong>negative</strong> — which is
                            what is shown in the register modal
                            (<strong>@format_currency($credit_sales_register)</strong>).
                        </p>

                        <hr>

                        <h4>Per-invoice breakdown (this session's invoices only):</h4>
                        <p>
                            The table below uses <code>transaction_payments</code> to show how much
                            was actually paid against each invoice created in this session.
                            The total unpaid balance on this session's invoices is
                            <strong class="text-danger">@format_currency($unpaid_on_session_invoices)</strong>
                            — that is the {{ $credit_transactions->count() }} invoice(s) highlighted
                            in <span class="label label-danger">red</span>.
                        </p>

                    </div>
                </div>
            </div>
        </div>

        {{-- ── Full transaction table ───────────────────────────────────────────── --}}
        <div class="row">
            <div class="col-md-12">
                <div class="box">
                    <div class="box-header with-border">
                        <h3 class="box-title">All Transactions in this Register Session</h3>
                        <div class="box-tools pull-right">
                            <a href="/additional_reports/register_payments" class="btn btn-default btn-sm">
                                <i class="fa fa-arrow-left"></i> Back
                            </a>
                        </div>
                    </div>
                    <div class="box-body">
                        <div class="table-responsive">
                            <table id="credit_sales_table" class="table table-bordered table-striped">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Invoice No</th>
                                        <th>Customer</th>
                                        <th>Date</th>
                                        <th>Payment Status</th>
                                        <th class="text-right">Total Amount</th>
                                        <th class="text-right">Paid Amount</th>
                                        <th class="text-right">Unpaid (Credit)</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($transactions as $index => $txn)
                                    <tr class="{{ $txn->credit_amount > 0 ? 'danger' : '' }}">
                                        <td>{{ $index + 1 }}</td>
                                        <td>
                                            <a href="{{ url('sells/' . $txn->id) }}" target="_blank">
                                                {{ $txn->invoice_no ?? '—' }}
                                            </a>
                                        </td>
                                        <td>{{ $txn->customer_name ?? '—' }}</td>
                                        <td>{{ \Carbon\Carbon::parse($txn->transaction_date)->format('d M Y, h:i A') }}</td>
                                        <td>
                                            @if($txn->payment_status === 'paid')
                                                <span class="label label-success">Paid</span>
                                            @elseif($txn->payment_status === 'partial')
                                                <span class="label label-warning">Partial</span>
                                            @else
                                                <span class="label label-danger">Due</span>
                                            @endif
                                        </td>
                                        <td class="text-right">@format_currency($txn->final_total)</td>
                                        <td class="text-right">@format_currency($txn->paid_amount)</td>
                                        <td class="text-right">
                                            @if($txn->credit_amount > 0)
                                                <strong class="text-danger">@format_currency($txn->credit_amount)</strong>
                                            @else
                                                <span class="text-success">—</span>
                                            @endif
                                        </td>
                                    </tr>
                                    @endforeach
                                </tbody>
                                <tfoot>
                                    <tr class="active">
                                        <th colspan="5" class="text-right">Totals (this session's invoices)</th>
                                        <th class="text-right">@format_currency($total_sales)</th>
                                        <th class="text-right">@format_currency($total_paid_on_session_invoices)</th>
                                        <th class="text-right">
                                            <strong class="text-danger">@format_currency($unpaid_on_session_invoices)</strong>
                                        </th>
                                    </tr>
                                    <tr class="warning">
                                        <th colspan="6" class="text-right">+ Payments collected at this register for earlier sessions' invoices</th>
                                        <th class="text-right text-warning">@format_currency($other_session_payments)</th>
                                        <th></th>
                                    </tr>
                                    <tr class="info">
                                        <th colspan="5" class="text-right">Register Credit Sales (= what the modal shows)</th>
                                        <th class="text-right">@format_currency($total_sales)</th>
                                        <th class="text-right">@format_currency($register_collected)</th>
                                        <th class="text-right">
                                            <strong>@format_currency($credit_sales_register)</strong>
                                        </th>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </section>

    @endsection

    @section('javascript')
    <script>
    $(document).ready(function () {
        $('#credit_sales_table').DataTable({
            aLengthMenu: [[25, 50, 100, -1], [25, 50, 100, 'All']],
            iDisplayLength: 25,
            order: [[3, 'asc']],
        });
    });
    </script>
    @endsection
