@extends('layouts.app')

@section('content')
<div class="container">
    <h3>Commission Reports</h3>

    <form method="GET" action="{{ route('commission.reports') }}" class="row mb-4 align-items-end">
    <!-- Status Filter -->
    <div class="col-md-2">
        <label>Status</label>
        <select name="status" class="form-control" onchange="this.form.submit()">
            <option value="">All</option>
            <option value="unpaid" {{ request('status') == 'unpaid' ? 'selected' : '' }}>Unpaid</option>
            <option value="paid" {{ request('status') == 'paid' ? 'selected' : '' }}>Paid</option>
        </select>
    </div>

    <!-- Receiver Filter -->
    <div class="col-md-2">
        <label>Receiver</label>
        <select name="receiver_name" class="form-control" onchange="this.form.submit()">
            <option value="">All</option>
            @foreach($receivers as $receiver_name)
                <option value="{{ $receiver_name }}" {{ request('receiver_name') == $receiver_name ? 'selected' : '' }}>
                    {{ $receiver_name }}
                </option>
            @endforeach 
        </select>
    </div>

    <!-- Buyer Filter -->
    <div class="col-md-2">
        <label>Buyer</label>
        <select name="buyer_name" class="form-control" onchange="this.form.submit()">
            <option value="">All</option>
            @foreach($buyers as $buyer)
                <option value="{{ $buyer }}" {{ request('buyer_name') == $buyer ? 'selected' : '' }}>
                    {{ $buyer }}
                </option>
            @endforeach 
        </select>
    </div>

    <!-- Seller Filter -->
    <div class="col-md-2">
        <label>Seller</label>
        <select name="seller_name" class="form-control" onchange="this.form.submit()">
            <option value="">All</option>
            @foreach($sellers as $seller)
                <option value="{{ $seller }}" {{ request('seller_name') == $seller ? 'selected' : '' }}>
                    {{ $seller }}
                </option>
            @endforeach 
        </select>
    </div>

    <!-- Month-Year Filter -->
    <div class="col-md-2">
        <label>Month & Year</label>
        <input type="month" name="month_year" value="{{ request('month_year') }}" class="form-control" onchange="this.form.submit()">
    </div>

    <div class="col-md-1 d-flex">
        <a href="{{ route('commission.reports') }}" class="btn btn-success btn-sm">Reset</a>
    </div>
</form>



        <table class="table table-bordered table-striped" id="commissionTable">
            <thead>
                <tr>
                    <th>Car Sale</th>
                    <th >Buyer Commission</th>
                    <th>Paid</th>
                    <th >Seller Commission</th>
                    <th>Paid</th>
                    <th>Receiver</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                @foreach($commissions as $commission)
                    <tr>
                    <td>{{ $commission->carSale->registration_no }} | {{ $commission->carSale->make }} | {{ $commission->carSale->varient }} | {{ $commission->carSale->model }} <br> ({{ $commission->carSale->buyer_name }} → {{ $commission->carSale->seller_name }})</td>
                        <td>{{ $commission->carSale->buyer_name ?? '-' }}</td>
                        <td>{{ number_format($commission->buyer_commission) }}</td>
                        <td>{{ $commission->carSale->seller_name ?? '-' }}</td>
                        <td>{{ number_format($commission->seller_commission) }}</td>
                        <td>{{ $commission->receiver_name }}</td>
                        <td>
                            @if(($commission->buyer_commission > 0) && ($commission->seller_commission > 0))
                                Paid
                            @else
                                Unpaid    
                            @endif
                        </td>
                    </tr>
                @endforeach



            </tbody>
                <!-- Totals Row -->
                <tr class="table-secondary fw-bold total-row">
                    <th style="text-align:center;"> → </th>
                    <td>Total Buyer Commission</td>
                    <td>{{ number_format($totals['buyer_commission']) }}</td>
                    <td>Total Seller Commission</td>
                    <td>{{ number_format($totals['seller_commission']) }}</td>
                    <th></th>
                    <td>Grand Total: {{ number_format($totals['grand_total']) }}</td>
                </tr>
        </table>
        <script>
            $(document).ready(function() {
                $('#commissionTable').DataTable({
                    dom: 'Bfrtip',
                    paging: false,   // no pagination
                    info: false,     // hide "Showing X of Y entries"
                    searching: true, // keep search box
                    buttons: [
                        { extend: 'copy', text: 'Copy' },
                        { extend: 'excel', text: 'Export to Excel' },
                        { extend: 'pdf', text: 'Export to PDF' },
                        { extend: 'print', text: 'Print' }
                    ]
                });
            });
</script>



    <!-- {{ $commissions->links() }} -->
</div>
@endsection


<style>
    .total-row th,
    .total-row td {
        border: 1px solid gray !important;
    }
</style>

