@extends('layouts.app')
@section('title', 'Register Payments Report Details')

@section('content')

<section class="content-header no-print">
    <h1>Cashier Payments Details</h1>
</section>

<section class="content">
    <div class="row">
        @component('components.filters', ['title' => __('report.filters')])
            <div class="col-md-10">
                <label class="form-control bg-dark header-print">
                    Register Payments Report — Date Range: {{ $daterange_new_formate }}
                </label>
            </div>
            <div class="col-md-2">
                <a href="/additional_reports/register_payments" style="text-decoration:none;">
                    <button class="form-control button"
                        style="background:#e10f28!important;color:white;border-radius:50px;border:none;font-weight:700;">
                        New Report
                    </button>
                </a>
            </div>
        @endcomponent
    </div>

    {{-- ── Register Sessions Table ─────────────────────────────────────── --}}
    <div class="row">
        <div class="col-md-12">
            <div class="box">
                <div class="box-header with-border">
                    <h3 class="box-title">Register Sessions</h3>
                </div>
                <div class="box-body">
                    <div class="table-responsive">
                        <table id="dtRegisterPayments" class="table table-bordered table-striped text-center">
                            <caption style="display:none;">
                                Register Payments — {{ $daterange_new_formate }} — Printed by: {{ $printed_user }}
                            </caption>
                            <thead>
                                <tr>
                                    <th rowspan="2" style="vertical-align:middle">Register #</th>
                                    <th rowspan="2" style="vertical-align:middle">Location</th>
                                    <th rowspan="2" style="vertical-align:middle">Cashier</th>
                                    <th rowspan="2" style="vertical-align:middle">Opened At</th>
                                    <th rowspan="2" style="vertical-align:middle">Closed At</th>
                                    <th colspan="3" class="bg-success text-white">Sales (IN)</th>
                                    <th colspan="3" class="bg-warning">POS Returns (OUT)</th>
                                    <th colspan="3" class="bg-danger text-white">Purchases (OUT)</th>
                                    <th colspan="3" style="background:#8e44ad;color:white">Expenses (OUT)</th>
                                    <th colspan="3" class="bg-info text-white">Net</th>
                                </tr>
                                <tr>
                                    {{-- Sales --}}
                                    <th class="bg-success text-white">Cash</th>
                                    <th class="bg-success text-white">Card</th>
                                    <th class="bg-success text-white">Other</th>
                                    {{-- POS Returns --}}
                                    <th class="bg-warning">Cash</th>
                                    <th class="bg-warning">Card</th>
                                    <th class="bg-warning">Other</th>
                                    {{-- Purchases --}}
                                    <th class="bg-danger text-white">Cash</th>
                                    <th class="bg-danger text-white">Card</th>
                                    <th class="bg-danger text-white">Other</th>
                                    {{-- Expenses --}}
                                    <th style="background:#8e44ad;color:white">Cash</th>
                                    <th style="background:#8e44ad;color:white">Card</th>
                                    <th style="background:#8e44ad;color:white">Other</th>
                                    {{-- Net --}}
                                    <th class="bg-info text-white">Net Cash</th>
                                    <th class="bg-info text-white">Net Card</th>
                                    <th class="bg-info text-white">Net Other</th>
                                </tr>
                            </thead>
                            <tbody>
                                @php
                                    $g = [
                                        'SellCash'      => 0, 'SellCard'      => 0, 'SellOther'      => 0,
                                        'CashReturn'    => 0, 'CardReturn'    => 0, 'OtherReturn'    => 0,
                                        'PurchaseCash'  => 0, 'PurchaseCard'  => 0, 'PurchaseOther'  => 0,
                                        'ExpenseCash'   => 0, 'ExpenseCard'   => 0, 'ExpenseOther'   => 0,
                                        'NetCash'       => 0, 'NetCard'       => 0, 'NetOther'       => 0,
                                    ];
                                @endphp

                                @foreach($results as $r)
                                @php
                                    $g['SellCash']     += $r->totalSellCash;
                                    $g['SellCard']     += $r->totalSellCard;
                                    $g['SellOther']    += $r->totalSellOther;
                                    $g['CashReturn']   += $r->totalCashReturn;
                                    $g['CardReturn']   += $r->totalCardReturn;
                                    $g['OtherReturn']  += $r->totalOtherReturn;
                                    $g['PurchaseCash'] += $r->totalPurchaseCash;
                                    $g['PurchaseCard'] += $r->totalPurchaseCard;
                                    $g['PurchaseOther']+= $r->totalPurchaseOther;
                                    $g['ExpenseCash']  += $r->totalExpenseCash;
                                    $g['ExpenseCard']  += $r->totalExpenseCard;
                                    $g['ExpenseOther'] += $r->totalExpenseOther;
                                    $g['NetCash']      += $r->netCash;
                                    $g['NetCard']      += $r->netCard;
                                    $g['NetOther']     += $r->netOther;
                                @endphp
                                <tr>
                                    <td>
                                        <a href="{{ url('additional_reports/credit_sales_details/' . $r->register_id) }}"
                                           target="_blank" title="View credit sales details">
                                            #{{ $r->register_id }}
                                        </a>
                                    </td>
                                    <td>{{ $r->business_location_name }}</td>
                                    <td>{{ $r->cashier_name }}</td>
                                    <td>{{ $r->created_at }}</td>
                                    <td>{{ $r->closed_at }}</td>

                                    {{-- Sales --}}
                                    <td>{{ number_format($r->totalSellCash,  2) }}</td>
                                    <td>{{ number_format($r->totalSellCard,  2) }}</td>
                                    <td>{{ number_format($r->totalSellOther, 2) }}</td>

                                    {{-- POS Returns --}}
                                    <td class="text-warning">{{ number_format($r->totalCashReturn,  2) }}</td>
                                    <td class="text-warning">{{ number_format($r->totalCardReturn,  2) }}</td>
                                    <td class="text-warning">{{ number_format($r->totalOtherReturn, 2) }}</td>

                                    {{-- Purchases --}}
                                    <td class="text-danger">{{ number_format($r->totalPurchaseCash,  2) }}</td>
                                    <td class="text-danger">{{ number_format($r->totalPurchaseCard,  2) }}</td>
                                    <td class="text-danger">{{ number_format($r->totalPurchaseOther, 2) }}</td>

                                    {{-- Expenses --}}
                                    <td style="color:#8e44ad">{{ number_format($r->totalExpenseCash,  2) }}</td>
                                    <td style="color:#8e44ad">{{ number_format($r->totalExpenseCard,  2) }}</td>
                                    <td style="color:#8e44ad">{{ number_format($r->totalExpenseOther, 2) }}</td>

                                    {{-- Net --}}
                                    <td class="{{ $r->netCash  < 0 ? 'text-danger' : 'text-success' }}" style="font-weight:700">
                                        {{ number_format($r->netCash,  2) }}
                                    </td>
                                    <td class="{{ $r->netCard  < 0 ? 'text-danger' : 'text-success' }}" style="font-weight:700">
                                        {{ number_format($r->netCard,  2) }}
                                    </td>
                                    <td class="{{ $r->netOther < 0 ? 'text-danger' : 'text-success' }}" style="font-weight:700">
                                        {{ number_format($r->netOther, 2) }}
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                            <tfoot>
                                <tr class="active" style="font-weight:700">
                                    <td colspan="5" class="text-right">Grand Total</td>
                                    <td>{{ number_format($g['SellCash'],      2) }}</td>
                                    <td>{{ number_format($g['SellCard'],      2) }}</td>
                                    <td>{{ number_format($g['SellOther'],     2) }}</td>
                                    <td class="text-warning">{{ number_format($g['CashReturn'],   2) }}</td>
                                    <td class="text-warning">{{ number_format($g['CardReturn'],   2) }}</td>
                                    <td class="text-warning">{{ number_format($g['OtherReturn'],  2) }}</td>
                                    <td class="text-danger">{{ number_format($g['PurchaseCash'],  2) }}</td>
                                    <td class="text-danger">{{ number_format($g['PurchaseCard'],  2) }}</td>
                                    <td class="text-danger">{{ number_format($g['PurchaseOther'], 2) }}</td>
                                    <td style="color:#8e44ad">{{ number_format($g['ExpenseCash'],  2) }}</td>
                                    <td style="color:#8e44ad">{{ number_format($g['ExpenseCard'],  2) }}</td>
                                    <td style="color:#8e44ad">{{ number_format($g['ExpenseOther'], 2) }}</td>
                                    <td class="{{ $g['NetCash']  < 0 ? 'text-danger' : 'text-success' }}">{{ number_format($g['NetCash'],  2) }}</td>
                                    <td class="{{ $g['NetCard']  < 0 ? 'text-danger' : 'text-success' }}">{{ number_format($g['NetCard'],  2) }}</td>
                                    <td class="{{ $g['NetOther'] < 0 ? 'text-danger' : 'text-success' }}">{{ number_format($g['NetOther'], 2) }}</td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{-- ── Sell Returns Table ────────────────────────────────────────────── --}}
    <div class="row">
        <div class="col-md-12">
            <div class="box box-warning">
                <div class="box-header with-border">
                    <h3 class="box-title">
                        <i class="fa fa-undo"></i> Sell Returns — {{ $daterange_new_formate }}
                    </h3>
                </div>
                <div class="box-body">
                    @if($sell_returns->isEmpty())
                        <p class="text-muted text-center">No sell returns found for this period.</p>
                    @else
                    <div class="table-responsive">
                        <table id="dtSellReturns" class="table table-bordered table-striped text-center">
                            <thead>
                                <tr>
                                    <th>Date</th>
                                    <th>Ref #</th>
                                    <th>Location</th>
                                    <th>Processed By</th>
                                    <th class="bg-warning">Cash Refunded</th>
                                    <th class="bg-warning">Card Refunded</th>
                                    <th class="bg-warning">Other Refunded</th>
                                    <th class="bg-warning" style="font-weight:700">Total Refunded</th>
                                </tr>
                            </thead>
                            <tbody>
                                @php
                                    $srTotals = ['cash' => 0, 'card' => 0, 'other' => 0, 'total' => 0];
                                @endphp
                                @foreach($sell_returns as $sr)
                                @php
                                    $srTotals['cash']  += $sr->returnCash;
                                    $srTotals['card']  += $sr->returnCard;
                                    $srTotals['other'] += $sr->returnOther;
                                    $srTotals['total'] += $sr->returnTotal;
                                @endphp
                                <tr>
                                    <td>{{ $sr->return_date }}</td>
                                    <td>{{ $sr->ref_no }}</td>
                                    <td>{{ $sr->business_location_name }}</td>
                                    <td>{{ $sr->cashier_name }}</td>
                                    <td class="text-warning">{{ number_format($sr->returnCash,  2) }}</td>
                                    <td class="text-warning">{{ number_format($sr->returnCard,  2) }}</td>
                                    <td class="text-warning">{{ number_format($sr->returnOther, 2) }}</td>
                                    <td class="text-warning" style="font-weight:700">{{ number_format($sr->returnTotal, 2) }}</td>
                                </tr>
                                @endforeach
                            </tbody>
                            <tfoot>
                                <tr class="active" style="font-weight:700">
                                    <td colspan="4" class="text-right">Grand Total</td>
                                    <td class="text-warning">{{ number_format($srTotals['cash'],  2) }}</td>
                                    <td class="text-warning">{{ number_format($srTotals['card'],  2) }}</td>
                                    <td class="text-warning">{{ number_format($srTotals['other'], 2) }}</td>
                                    <td class="text-warning">{{ number_format($srTotals['total'], 2) }}</td>
                                </tr>
                            </tfoot>
                        </table>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</section>

<style>
    th[colspan] { text-align: center; }
    #dtRegisterPayments th, #dtRegisterPayments td { white-space: nowrap; }
</style>

@endsection

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