<?php if (isset($component)) { $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54 = $attributes; } ?>
<?php $component = App\View\Components\AppLayout::resolve([] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('app-layout'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\App\View\Components\AppLayout::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes([]); ?>
     <?php $__env->slot('header', null, []); ?> 
        <div>
            <h1 class="text-lg font-semibold text-gray-800">Customer Ledger Report</h1>
            <p class="text-xs text-gray-400 mt-0.5">Track sales, tax invoices, and ledger details per customer</p>
        </div>
     <?php $__env->endSlot(); ?>

    <div x-data="customerReport()" class="max-w-7xl mx-auto space-y-6">
        
        <!-- Print Trigger Bar -->
        <div class="flex flex-col sm:flex-row justify-between sm:items-center bg-white rounded-2xl border border-gray-100 p-5 gap-4 shadow-sm">
            <div>
                <h2 class="text-sm font-semibold text-gray-800">Export Ledger Statement</h2>
                <p class="text-xs text-gray-400 mt-0.5">Click to generate a print-friendly statement matching the filters below.</p>
            </div>
            <div>
                <button @click="printLedger()" :disabled="loading || invoices.length === 0"
                        class="px-4 py-2 bg-indigo-600 hover:bg-indigo-700 disabled:opacity-40 text-white text-xs font-semibold rounded-xl transition shadow-sm inline-flex items-center gap-1.5">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 17h2a2 2 0 002-2v-4a2 2 0 00-2-2H5a2 2 0 00-2 2v4a2 2 0 002 2h2m2 4h6a2 2 0 002-2v-4a2 2 0 00-2-2H9a2 2 0 00-2 2v4a2 2 0 002 2zm8-12V5a2 2 0 00-2-2H9a2 2 0 00-2 2v4h10z" />
                    </svg>
                    Print Statement
                </button>
            </div>
        </div>

        <!-- Filters panel -->
        <div class="bg-white rounded-2xl border border-gray-100 p-6 shadow-sm">
            <h3 class="text-xs font-bold text-gray-400 uppercase tracking-wider mb-4">Advance Filters</h3>
            <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-5 gap-4">
                
                <!-- Customer Selection -->
                <div>
                    <label class="block text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1.5">Select Customer</label>
                    <select x-model="filters.customer_id" @change="fetchData()"
                            class="w-full rounded-xl border-gray-200 text-xs focus:border-indigo-400 focus:ring-indigo-400">
                        <option value="">All Customers</option>
                        <?php $__currentLoopData = $customers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $c): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($c->id); ?>"><?php echo e($c->name); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>

                <!-- Date From -->
                <div>
                    <label class="block text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1.5">From Date</label>
                    <input type="text" x-flatpickr x-model="filters.from_date" @change="fetchData()" placeholder="Select Date"
                           class="w-full rounded-xl border-gray-200 text-xs focus:border-indigo-400 focus:ring-indigo-400">
                </div>

                <!-- Date To -->
                <div>
                    <label class="block text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1.5">To Date</label>
                    <input type="text" x-flatpickr x-model="filters.to_date" @change="fetchData()" placeholder="Select Date"
                           class="w-full rounded-xl border-gray-200 text-xs focus:border-indigo-400 focus:ring-indigo-400">
                </div>

                <!-- Invoice Type -->
                <div>
                    <label class="block text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1.5">Type</label>
                    <select x-model="filters.invoice_type" @change="fetchData()"
                            class="w-full rounded-xl border-gray-200 text-xs focus:border-indigo-400 focus:ring-indigo-400">
                        <option value="">All Types</option>
                        <option value="Sale Invoice">Sale Invoice</option>
                        <option value="Debit Note">Debit Note</option>
                        <option value="Credit Note">Credit Note</option>
                    </select>
                </div>

                <!-- Sync status -->
                <div>
                    <label class="block text-xs font-semibold text-gray-500 uppercase tracking-wider mb-1.5">FBR Sync Status</label>
                    <select x-model="filters.sync_status" @change="fetchData()"
                            class="w-full rounded-xl border-gray-200 text-xs focus:border-indigo-400 focus:ring-indigo-400">
                        <option value="">All Statuses</option>
                        <option value="draft">Draft</option>
                        <option value="queued">Queued</option>
                        <option value="syncing">Syncing</option>
                        <option value="synced">Synced</option>
                        <option value="failed">Failed</option>
                    </select>
                </div>
            </div>
            
            <div class="mt-4 flex justify-end">
                <button @click="resetFilters()"
                        class="px-3.5 py-1.5 text-xs text-gray-500 hover:text-gray-700 font-semibold border border-gray-200 rounded-xl hover:bg-gray-50 transition">
                    Reset Filters
                </button>
            </div>
        </div>

        <!-- Metrics cards -->
        <div class="grid grid-cols-1 sm:grid-cols-3 gap-4">
            <!-- Total Invoiced Sales -->
            <div class="bg-white rounded-2xl border border-gray-100 p-5 shadow-sm">
                <p class="text-xs font-semibold text-gray-400 uppercase tracking-wider mb-2">Total Ledger Sales</p>
                <p class="text-2xl font-bold text-gray-900">
                    Rs. <span x-text="Number(totals.total_sales).toLocaleString(undefined, {minimumFractionDigits: 2})"></span>
                </p>
                <p class="text-[10px] text-gray-400 mt-1" x-text="`${totals.count} transactions in ledger`"></p>
            </div>

            <!-- Tax Collected -->
            <div class="bg-white rounded-2xl border border-gray-100 p-5 shadow-sm">
                <p class="text-xs font-semibold text-gray-400 uppercase tracking-wider mb-2">Sales Tax Collected</p>
                <p class="text-2xl font-bold text-gray-900">
                    Rs. <span x-text="Number(totals.total_tax).toLocaleString(undefined, {minimumFractionDigits: 2})"></span>
                </p>
                <p class="text-[10px] text-gray-400 mt-1">Sum of Sales Tax values in filtered invoices</p>
            </div>

            <!-- Taxable Value (Excl. Tax) -->
            <div class="bg-white rounded-2xl border border-gray-100 p-5 shadow-sm">
                <p class="text-xs font-semibold text-gray-400 uppercase tracking-wider mb-2">Taxable Subtotal</p>
                <p class="text-2xl font-bold text-gray-900">
                    Rs. <span x-text="Number(totals.total_subtotal).toLocaleString(undefined, {minimumFractionDigits: 2})"></span>
                </p>
                <p class="text-[10px] text-gray-400 mt-1">Excludes sales tax amount</p>
            </div>
        </div>

        <!-- Ledger Grid Table -->
        <div class="bg-white rounded-2xl border border-gray-100 shadow-sm overflow-hidden">
            <div class="overflow-x-auto">
                <table class="w-full text-left border-collapse">
                    <thead>
                        <tr class="bg-gray-50 border-b border-gray-100 text-xs font-semibold text-gray-500 uppercase tracking-wider">
                            <th class="px-6 py-4">Date</th>
                            <th class="px-6 py-4">Transaction / Local Ref</th>
                            <th class="px-6 py-4">Customer</th>
                            <th class="px-6 py-4">Type</th>
                            <th class="px-6 py-4 text-right font-semibold">Subtotal</th>
                            <th class="px-6 py-4 text-right font-semibold">Tax</th>
                            <th class="px-6 py-4 text-right font-semibold">Total Amount</th>
                            <th class="px-6 py-4 text-center">FBR Sync Status</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100 text-sm text-gray-600">
                        <template x-for="inv in invoices" :key="inv.id">
                            <tr class="hover:bg-gray-50 transition">
                                <td class="px-6 py-4 whitespace-nowrap" x-text="inv.invoice_date"></td>
                                <td class="px-6 py-4">
                                    <a :href="`/invoices/${inv.id}`" class="font-medium text-indigo-600 hover:text-indigo-800" x-text="inv.local_invoice_no"></a>
                                    <div class="text-[10px] text-gray-400" x-show="inv.fbr_invoice_number" x-text="`FBR Ref: ${inv.fbr_invoice_number}`"></div>
                                </td>
                                <td class="px-6 py-4">
                                    <div class="font-medium text-gray-800" x-text="inv.customer?.name || 'Walk-in Customer'"></div>
                                    <div class="text-xs text-gray-400" x-text="inv.customer?.ntn_cnic || '—'"></div>
                                </td>
                                <td class="px-6 py-4">
                                    <span class="text-xs font-semibold px-2.5 py-0.5 rounded-full"
                                          :class="{
                                              'bg-blue-50 text-blue-700': inv.invoice_type === 'Sale Invoice',
                                              'bg-rose-50 text-rose-700': inv.invoice_type === 'Credit Note',
                                              'bg-amber-50 text-amber-700': inv.invoice_type === 'Debit Note'
                                          }"
                                          x-text="inv.invoice_type"></span>
                                </td>
                                <td class="px-6 py-4 text-right font-mono" x-text="Number(inv.subtotal).toFixed(2)"></td>
                                <td class="px-6 py-4 text-right font-mono text-gray-500" x-text="Number(inv.total_tax).toFixed(2)"></td>
                                <td class="px-6 py-4 text-right font-mono font-semibold text-gray-900" x-text="Number(inv.total_amount).toFixed(2)"></td>
                                <td class="px-6 py-4 text-center">
                                    <span class="inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs font-medium"
                                          :class="{
                                              'bg-gray-100 text-gray-700': inv.sync_status === 'draft',
                                              'bg-blue-100 text-blue-700': inv.sync_status === 'queued' || inv.sync_status === 'syncing',
                                              'bg-emerald-100 text-emerald-700': inv.sync_status === 'synced',
                                              'bg-rose-100 text-rose-700': inv.sync_status === 'failed'
                                          }">
                                        <span x-text="inv.sync_status.toUpperCase()"></span>
                                    </span>
                                </td>
                            </tr>
                        </template>
                        <tr x-show="invoices.length === 0">
                            <td colspan="8" class="px-6 py-8 text-center text-gray-400">
                                No matching customer ledger items found.
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script>
        function customerReport() {
            return {
                invoices: [],
                loading: false,
                filters: {
                    customer_id: '',
                    from_date: '',
                    to_date: '',
                    invoice_type: '',
                    sync_status: ''
                },
                totals: {
                    total_sales: 0,
                    total_tax: 0,
                    total_subtotal: 0,
                    count: 0
                },
                init() {
                    this.fetchData();
                },
                fetchData() {
                    this.loading = true;
                    let url = `/reports/customer/data?1=1`;
                    if (this.filters.customer_id) url += `&customer_id=${this.filters.customer_id}`;
                    if (this.filters.from_date) url += `&from_date=${this.filters.from_date}`;
                    if (this.filters.to_date) url += `&to_date=${this.filters.to_date}`;
                    if (this.filters.invoice_type) url += `&invoice_type=${this.filters.invoice_type}`;
                    if (this.filters.sync_status) url += `&sync_status=${this.filters.sync_status}`;

                    fetch(url, { headers: { 'Accept': 'application/json' } })
                        .then(r => r.json())
                        .then(res => {
                            this.loading = false;
                            if (res.success) {
                                this.invoices = res.data;
                                this.totals = res.totals;
                            }
                        })
                        .catch(() => { this.loading = false; });
                },
                resetFilters() {
                    this.filters = { customer_id: '', from_date: '', to_date: '', invoice_type: '', sync_status: '' };
                    this.fetchData();
                },
                printLedger() {
                    let url = `/reports/customer/print?1=1`;
                    if (this.filters.customer_id) url += `&customer_id=${this.filters.customer_id}`;
                    if (this.filters.from_date) url += `&from_date=${this.filters.from_date}`;
                    if (this.filters.to_date) url += `&to_date=${this.filters.to_date}`;
                    if (this.filters.invoice_type) url += `&invoice_type=${this.filters.invoice_type}`;
                    if (this.filters.sync_status) url += `&sync_status=${this.filters.sync_status}`;

                    window.open(url, '_blank');
                }
            }
        }
    </script>
 <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $attributes = $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $component = $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?>
<?php /**PATH /www/wwwroot/an.shiningerp.com/resources/views/reports/customer.blade.php ENDPATH**/ ?>