<?php $__env->startSection('content'); ?>
    <?php
        $role = auth()->user()?->role?->nama_roles;
    ?>
    <div class="space-y-6">
        <?php if (isset($component)) { $__componentOriginal53747ceb358d30c0105769f8471417f6 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal53747ceb358d30c0105769f8471417f6 = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.card','data' => []] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('card'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes([]); ?>
            <div class="flex flex-wrap items-center justify-between gap-4">
                <div>
                    <h2 class="text-2xl font-semibold text-slate-900">Close Bill</h2>
                    <p class="text-sm text-slate-500">Order <?php echo e($order->order_no); ?> - <?php echo e($order->tanggal->format('d-m-Y H:i')); ?></p>
                </div>
                <div class="text-right">
                    <?php if (isset($component)) { $__componentOriginal2ddbc40e602c342e508ac696e52f8719 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal2ddbc40e602c342e508ac696e52f8719 = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.badge','data' => ['variant' => $order->status === 'open' ? 'success' : ($order->status === 'closed' ? 'info' : 'danger')]] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('badge'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['variant' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute($order->status === 'open' ? 'success' : ($order->status === 'closed' ? 'info' : 'danger'))]); ?>
                        <?php echo e(ucfirst($order->status)); ?>

                     <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal2ddbc40e602c342e508ac696e52f8719)): ?>
<?php $attributes = $__attributesOriginal2ddbc40e602c342e508ac696e52f8719; ?>
<?php unset($__attributesOriginal2ddbc40e602c342e508ac696e52f8719); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal2ddbc40e602c342e508ac696e52f8719)): ?>
<?php $component = $__componentOriginal2ddbc40e602c342e508ac696e52f8719; ?>
<?php unset($__componentOriginal2ddbc40e602c342e508ac696e52f8719); ?>
<?php endif; ?>
                    <?php if($order->status === 'closed' && $role === 'cashier'): ?>
                        <div class="mt-2 flex justify-end">
                            <?php if (isset($component)) { $__componentOriginald0f1fd2689e4bb7060122a5b91fe8561 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginald0f1fd2689e4bb7060122a5b91fe8561 = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.button','data' => ['type' => 'button','id' => 'print-close-bill','variant' => 'secondary']] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('button'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['type' => 'button','id' => 'print-close-bill','variant' => 'secondary']); ?>Print <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginald0f1fd2689e4bb7060122a5b91fe8561)): ?>
<?php $attributes = $__attributesOriginald0f1fd2689e4bb7060122a5b91fe8561; ?>
<?php unset($__attributesOriginald0f1fd2689e4bb7060122a5b91fe8561); ?>
<?php endif; ?>
<?php if (isset($__componentOriginald0f1fd2689e4bb7060122a5b91fe8561)): ?>
<?php $component = $__componentOriginald0f1fd2689e4bb7060122a5b91fe8561; ?>
<?php unset($__componentOriginald0f1fd2689e4bb7060122a5b91fe8561); ?>
<?php endif; ?>
                        </div>
                    <?php endif; ?>
                    <?php
                        $orderTotal = $order->grand_total_after_discount ?? $order->grand_total;
                    ?>
                    <p class="mt-2 text-lg font-semibold text-slate-900"><?php echo e(format_rupiah($orderTotal)); ?></p>
                    <?php if(($order->discount_amount ?? 0) > 0): ?>
                        <p class="mt-1 text-xs text-slate-500">
                            Total sebelum diskon: <?php echo e(format_rupiah($order->grand_total)); ?>

                        </p>
                        <p class="mt-1 text-xs text-amber-700">
                            Diskon <?php echo e($order->discount_name ? '(' . $order->discount_name . ')' : ''); ?>:
                            -<?php echo e(format_rupiah($order->discount_amount)); ?>

                        </p>
                    <?php endif; ?>
                </div>
            </div>

            <div class="mt-4 flex flex-wrap gap-6 text-sm text-slate-600">
                <div>Waiter: <span class="font-semibold text-slate-800"><?php echo e($order->waiter?->nama ?? '-'); ?></span></div>
                <div>
                    <?php if($order->type === 'dine_in'): ?>
                        Meja: <span class="font-semibold text-slate-800"><?php echo e($order->meja?->no_meja ?? '-'); ?></span>
                    <?php else: ?>
                        Pelanggan: <span class="font-semibold text-slate-800"><?php echo e($order->nama_pelanggan ?? '-'); ?></span>
                    <?php endif; ?>
                </div>
                <?php if($order->status === 'closed'): ?>
                    <?php
                        $paidAtDisplay = '-';
                        if ($order->updated_at) {
                            $paidAtDisplay = \Illuminate\Support\Carbon::parse($order->updated_at)->format('d-m-Y H:i');
                        }
                    ?>
                    <div>
                        Waktu Bayar: <span class="font-semibold text-slate-800"><?php echo e($paidAtDisplay); ?></span>
                    </div>
                    <div>
                        Pembayaran: <span class="font-semibold text-slate-800"><?php echo e(strtoupper($order->tipe_pembayaran ?? '-')); ?></span>
                        <?php if(($order->tipe_pembayaran ?? '') !== 'cash' && $order->no_transaksi_pembayaran): ?>
                            <span class="text-xs text-slate-500">(<?php echo e($order->no_transaksi_pembayaran); ?>)</span>
                        <?php endif; ?>
                    </div>
                    <?php if(($order->discount_amount ?? 0) > 0): ?>
                        <div>
                            Discount: <span class="font-semibold text-slate-800">
                                <?php echo e($order->discount_name ?? 'Diskon'); ?>

                            </span>
                            <span class="text-xs text-slate-500">(-<?php echo e(format_rupiah($order->discount_amount)); ?>)</span>
                        </div>
                        <div>
                            Total Setelah Diskon:
                            <span class="font-semibold text-slate-800"><?php echo e(format_rupiah($order->grand_total_after_discount ?? $order->grand_total)); ?></span>
                        </div>
                    <?php endif; ?>
                <?php endif; ?>
            </div>
         <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal53747ceb358d30c0105769f8471417f6)): ?>
<?php $attributes = $__attributesOriginal53747ceb358d30c0105769f8471417f6; ?>
<?php unset($__attributesOriginal53747ceb358d30c0105769f8471417f6); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal53747ceb358d30c0105769f8471417f6)): ?>
<?php $component = $__componentOriginal53747ceb358d30c0105769f8471417f6; ?>
<?php unset($__componentOriginal53747ceb358d30c0105769f8471417f6); ?>
<?php endif; ?>

        <?php if (isset($component)) { $__componentOriginal53747ceb358d30c0105769f8471417f6 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal53747ceb358d30c0105769f8471417f6 = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.card','data' => []] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('card'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes([]); ?>
            <h3 class="text-lg font-semibold text-slate-900">Items</h3>
            <div class="mt-4 grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
                <?php ($activeDetails = $order->details->where('status', 'active')); ?>
                <?php $__empty_1 = true; $__currentLoopData = $activeDetails; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $detail): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); $__empty_1 = false; ?>
                    <div class="w-full rounded-xl border border-slate-200 bg-white p-4 shadow-sm">
                        <div class="flex items-center justify-between">
                            <div class="text-sm font-semibold text-slate-900"><?php echo e($detail->menu?->nama); ?></div>
                            <?php if (isset($component)) { $__componentOriginal2ddbc40e602c342e508ac696e52f8719 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal2ddbc40e602c342e508ac696e52f8719 = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.badge','data' => ['variant' => 'info']] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('badge'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['variant' => 'info']); ?>Qty <?php echo e($detail->qty); ?> <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal2ddbc40e602c342e508ac696e52f8719)): ?>
<?php $attributes = $__attributesOriginal2ddbc40e602c342e508ac696e52f8719; ?>
<?php unset($__attributesOriginal2ddbc40e602c342e508ac696e52f8719); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal2ddbc40e602c342e508ac696e52f8719)): ?>
<?php $component = $__componentOriginal2ddbc40e602c342e508ac696e52f8719; ?>
<?php unset($__componentOriginal2ddbc40e602c342e508ac696e52f8719); ?>
<?php endif; ?>
                        </div>
                        <div class="mt-3 grid gap-2 text-sm text-slate-600">
                            <div class="flex items-center justify-between">
                                <span>Harga</span>
                                <span class="font-semibold text-slate-800"><?php echo e(format_rupiah($detail->harga)); ?></span>
                            </div>
                            <div class="flex items-center justify-between">
                                <span>Line Total</span>
                                <span class="font-semibold text-slate-800"><?php echo e(format_rupiah($detail->line_total)); ?></span>
                            </div>
                            <div class="text-xs text-slate-500">Note: <?php echo e($detail->note ?? '-'); ?></div>
                        </div>
                    </div>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); if ($__empty_1): ?>
                    <div class="rounded-lg border border-slate-200 bg-slate-50 px-4 py-3 text-sm text-slate-500">
                        Tidak ada item aktif.
                    </div>
                <?php endif; ?>
            </div>
         <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal53747ceb358d30c0105769f8471417f6)): ?>
<?php $attributes = $__attributesOriginal53747ceb358d30c0105769f8471417f6; ?>
<?php unset($__attributesOriginal53747ceb358d30c0105769f8471417f6); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal53747ceb358d30c0105769f8471417f6)): ?>
<?php $component = $__componentOriginal53747ceb358d30c0105769f8471417f6; ?>
<?php unset($__componentOriginal53747ceb358d30c0105769f8471417f6); ?>
<?php endif; ?>

        <?php if($order->status === 'open'): ?>
            <?php if (isset($component)) { $__componentOriginal53747ceb358d30c0105769f8471417f6 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal53747ceb358d30c0105769f8471417f6 = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.card','data' => []] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('card'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes([]); ?>
                <h3 class="text-lg font-semibold text-slate-900">Pembayaran</h3>
                <?php if($needsOpenSaldo): ?>
                    <div class="mt-4 rounded-lg border border-amber-200 bg-amber-50 px-4 py-3 text-sm text-amber-700">
                        Mohon lakukan Open Saldo terlebih dahulu sebelum Close Bill.
                    </div>
                <?php else: ?>
                    <div id="close-error" class="mt-4 hidden rounded-lg border border-rose-200 bg-rose-50 px-4 py-3 text-sm text-rose-700"></div>
                    <form method="post" action="<?php echo e(route('orders.close', $order)); ?>" class="mt-4 grid gap-4 md:grid-cols-2" id="close-order-form" data-print-url="<?php echo e(route('orders.print', $order)); ?>" data-grand-total="<?php echo e($order->grand_total); ?>">
                        <?php echo csrf_field(); ?>
                        <input type="hidden" name="discount_id" id="discount-id" value="">

                        <?php if(($discounts ?? collect())->count()): ?>
                            <div class="md:col-span-2">
                                <label class="mb-2 block text-sm font-semibold text-slate-700">Discount</label>
                                <div class="flex flex-wrap items-center gap-2 text-xs">
                                    <button type="button" data-discount data-id="" data-type="" data-value="0"
                                            class="rounded-full border px-3 py-1 font-semibold border-slate-900 bg-slate-900 text-white">
                                        Tanpa Discount
                                    </button>
                                    <?php $__currentLoopData = $discounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $discount): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <button type="button" data-discount data-id="<?php echo e($discount->id); ?>" data-type="<?php echo e($discount->tipe); ?>" data-value="<?php echo e($discount->nilai); ?>"
                                                class="rounded-full border px-3 py-1 font-semibold border-slate-200 text-slate-600 hover:bg-slate-50">
                                            <?php echo e($discount->nama); ?>

                                            <?php if($discount->tipe === 'percent'): ?>
                                                (<?php echo e(rtrim(rtrim(number_format((float) $discount->nilai, 2, '.', ''), '0'), '.')); ?>%)
                                            <?php else: ?>
                                                (<?php echo e(format_rupiah($discount->nilai)); ?>)
                                            <?php endif; ?>
                                        </button>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </div>
                            </div>
                        <?php endif; ?>

                        <?php if (isset($component)) { $__componentOriginaled2cde6083938c436304f332ba96bb7c = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginaled2cde6083938c436304f332ba96bb7c = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.select','data' => ['name' => 'tipe_pembayaran','label' => 'Tipe Pembayaran','required' => true,'id' => 'payment-type']] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('select'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['name' => 'tipe_pembayaran','label' => 'Tipe Pembayaran','required' => true,'id' => 'payment-type']); ?>
                            <option value="">Pilih</option>
                            <?php $__currentLoopData = ['cash' => 'Cash', 'transfer' => 'Transfer', 'qris' => 'QRIS']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                <option value="<?php echo e($value); ?>" <?php echo e(old('tipe_pembayaran') === $value ? 'selected' : ''); ?>><?php echo e($label); ?></option>
                            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                         <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginaled2cde6083938c436304f332ba96bb7c)): ?>
<?php $attributes = $__attributesOriginaled2cde6083938c436304f332ba96bb7c; ?>
<?php unset($__attributesOriginaled2cde6083938c436304f332ba96bb7c); ?>
<?php endif; ?>
<?php if (isset($__componentOriginaled2cde6083938c436304f332ba96bb7c)): ?>
<?php $component = $__componentOriginaled2cde6083938c436304f332ba96bb7c; ?>
<?php unset($__componentOriginaled2cde6083938c436304f332ba96bb7c); ?>
<?php endif; ?>
                        <div>
                            <label for="payment-amount-display" class="mb-2 block text-sm font-semibold text-slate-700">Bayar</label>
                            <input type="text" id="payment-amount-display" inputmode="numeric" autocomplete="off"
                                   value="<?php echo e(old('bayar') ? format_rupiah(old('bayar')) : ''); ?>"
                                   class="w-full rounded-lg border border-slate-200 px-3 py-2 text-sm text-slate-700 shadow-sm focus:border-amber-400 focus:ring-amber-200">
                            <input type="hidden" name="bayar" id="payment-amount" value="<?php echo e(old('bayar')); ?>">
                        </div>
                        <div id="bank-account-field" class="hidden md:col-span-2">
                            <?php if (isset($component)) { $__componentOriginaled2cde6083938c436304f332ba96bb7c = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginaled2cde6083938c436304f332ba96bb7c = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.select','data' => ['name' => 'bank_account_id','label' => 'Akun Bank']] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('select'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['name' => 'bank_account_id','label' => 'Akun Bank']); ?>
                                <option value="">Pilih Akun Bank</option>
                                <?php $__currentLoopData = $bankAccounts; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $bank): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <option value="<?php echo e($bank->id); ?>" <?php echo e(old('bank_account_id') == $bank->id ? 'selected' : ''); ?>>
                                        <?php echo e($bank->bank_name); ?> - <?php echo e($bank->account_name); ?> (<?php echo e($bank->account_number); ?>)
                                    </option>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                             <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginaled2cde6083938c436304f332ba96bb7c)): ?>
<?php $attributes = $__attributesOriginaled2cde6083938c436304f332ba96bb7c; ?>
<?php unset($__attributesOriginaled2cde6083938c436304f332ba96bb7c); ?>
<?php endif; ?>
<?php if (isset($__componentOriginaled2cde6083938c436304f332ba96bb7c)): ?>
<?php $component = $__componentOriginaled2cde6083938c436304f332ba96bb7c; ?>
<?php unset($__componentOriginaled2cde6083938c436304f332ba96bb7c); ?>
<?php endif; ?>
                        </div>
                        <?php if (isset($component)) { $__componentOriginalc2fcfa88dc54fee60e0757a7e0572df1 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginalc2fcfa88dc54fee60e0757a7e0572df1 = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.input','data' => ['name' => 'no_transaksi_pembayaran','label' => 'No Transaksi Pembayaran','value' => old('no_transaksi_pembayaran')]] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('input'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['name' => 'no_transaksi_pembayaran','label' => 'No Transaksi Pembayaran','value' => \Illuminate\View\Compilers\BladeCompiler::sanitizeComponentAttribute(old('no_transaksi_pembayaran'))]); ?>
<?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginalc2fcfa88dc54fee60e0757a7e0572df1)): ?>
<?php $attributes = $__attributesOriginalc2fcfa88dc54fee60e0757a7e0572df1; ?>
<?php unset($__attributesOriginalc2fcfa88dc54fee60e0757a7e0572df1); ?>
<?php endif; ?>
<?php if (isset($__componentOriginalc2fcfa88dc54fee60e0757a7e0572df1)): ?>
<?php $component = $__componentOriginalc2fcfa88dc54fee60e0757a7e0572df1; ?>
<?php unset($__componentOriginalc2fcfa88dc54fee60e0757a7e0572df1); ?>
<?php endif; ?>
                        <div class="md:col-span-2 rounded-lg border border-amber-100 bg-amber-50/50 p-4 text-sm text-slate-700">
                            <div class="flex items-center justify-between">
                                <span>Total</span>
                                <span class="font-semibold text-slate-900"><?php echo e(format_rupiah($order->grand_total)); ?></span>
                            </div>
                            <div class="mt-2 flex items-center justify-between">
                                <span>Diskon</span>
                                <span id="discount-display" class="font-semibold text-amber-700">Rp 0</span>
                            </div>
                            <div class="mt-2 flex items-center justify-between">
                                <span>Total Setelah Diskon</span>
                                <span id="total-after-display" class="font-semibold text-slate-900"><?php echo e(format_rupiah($order->grand_total)); ?></span>
                            </div>
                            <div class="mt-2 flex items-center justify-between">
                                <span>Kembalian</span>
                                <span id="change-display" class="font-semibold text-emerald-600">Rp 0</span>
                            </div>
                        </div>
                        <div class="md:col-span-2 flex justify-end">
                            <?php if (isset($component)) { $__componentOriginald0f1fd2689e4bb7060122a5b91fe8561 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginald0f1fd2689e4bb7060122a5b91fe8561 = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.button','data' => ['type' => 'submit']] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('button'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['type' => 'submit']); ?>Close Bill <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginald0f1fd2689e4bb7060122a5b91fe8561)): ?>
<?php $attributes = $__attributesOriginald0f1fd2689e4bb7060122a5b91fe8561; ?>
<?php unset($__attributesOriginald0f1fd2689e4bb7060122a5b91fe8561); ?>
<?php endif; ?>
<?php if (isset($__componentOriginald0f1fd2689e4bb7060122a5b91fe8561)): ?>
<?php $component = $__componentOriginald0f1fd2689e4bb7060122a5b91fe8561; ?>
<?php unset($__componentOriginald0f1fd2689e4bb7060122a5b91fe8561); ?>
<?php endif; ?>
                        </div>
                    </form>
                <?php endif; ?>
             <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal53747ceb358d30c0105769f8471417f6)): ?>
<?php $attributes = $__attributesOriginal53747ceb358d30c0105769f8471417f6; ?>
<?php unset($__attributesOriginal53747ceb358d30c0105769f8471417f6); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal53747ceb358d30c0105769f8471417f6)): ?>
<?php $component = $__componentOriginal53747ceb358d30c0105769f8471417f6; ?>
<?php unset($__componentOriginal53747ceb358d30c0105769f8471417f6); ?>
<?php endif; ?>
        <?php endif; ?>

        <div class="flex flex-wrap items-center gap-3">
            <?php if (isset($component)) { $__componentOriginald0f1fd2689e4bb7060122a5b91fe8561 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginald0f1fd2689e4bb7060122a5b91fe8561 = $attributes; } ?>
<?php $component = Illuminate\View\AnonymousComponent::resolve(['view' => 'components.button','data' => ['href' => ''.e(route('orders.all')).'','variant' => 'secondary']] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('button'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\Illuminate\View\AnonymousComponent::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes(['href' => ''.e(route('orders.all')).'','variant' => 'secondary']); ?>Kembali <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginald0f1fd2689e4bb7060122a5b91fe8561)): ?>
<?php $attributes = $__attributesOriginald0f1fd2689e4bb7060122a5b91fe8561; ?>
<?php unset($__attributesOriginald0f1fd2689e4bb7060122a5b91fe8561); ?>
<?php endif; ?>
<?php if (isset($__componentOriginald0f1fd2689e4bb7060122a5b91fe8561)): ?>
<?php $component = $__componentOriginald0f1fd2689e4bb7060122a5b91fe8561; ?>
<?php unset($__componentOriginald0f1fd2689e4bb7060122a5b91fe8561); ?>
<?php endif; ?>
        </div>
    </div>

    <script>
        window.addEventListener('load', () => {
            const printCloseBtn = document.getElementById('print-close-bill');
            const closeOrderForm = document.getElementById('close-order-form');
            const paymentType = document.getElementById('payment-type');
            const bankField = document.getElementById('bank-account-field');
            const bankSelect = bankField?.querySelector('select');
            const paymentAmount = document.getElementById('payment-amount');
            const paymentAmountDisplay = document.getElementById('payment-amount-display');
            const changeDisplay = document.getElementById('change-display');
            const discountButtons = document.querySelectorAll('[data-discount]');
            const discountInput = document.getElementById('discount-id');
            const discountDisplay = document.getElementById('discount-display');
            const totalAfterDisplay = document.getElementById('total-after-display');
            const grandTotal = parseFloat(closeOrderForm?.dataset?.grandTotal ?? '0') || 0;
            let selectedDiscount = { type: '', value: 0 };

            function toggleBankField() {
                if (!paymentType || !bankField) return;
                const isTransfer = paymentType.value === 'transfer';
                bankField.classList.toggle('hidden', !isTransfer);
                bankField.style.display = isTransfer ? 'block' : 'none';
                if (bankSelect) {
                    bankSelect.required = isTransfer;
                }
            }

            function getDiscountAmount() {
                if (!selectedDiscount.type || !selectedDiscount.value) {
                    return 0;
                }
                if (selectedDiscount.type === 'percent') {
                    return Math.min(grandTotal, grandTotal * (selectedDiscount.value / 100));
                }
                return Math.min(grandTotal, selectedDiscount.value);
            }

            function getTotalAfterDiscount() {
                return Math.max(0, grandTotal - getDiscountAmount());
            }

            function formatRupiahNumber(value) {
                return new Intl.NumberFormat('id-ID', {
                    style: 'currency',
                    currency: 'IDR',
                    minimumFractionDigits: 0,
                }).format(value);
            }

            function parseRupiah(value) {
                if (!value) return 0;
                const numeric = value.replace(/[^\d]/g, '');
                return parseInt(numeric, 10) || 0;
            }

            function syncPaymentInput() {
                if (!paymentType || !paymentAmount || !paymentAmountDisplay) return;
                const isCash = paymentType.value === 'cash';
                paymentAmountDisplay.readOnly = !isCash;
                if (!isCash) {
                    const totalAfter = Math.round(getTotalAfterDiscount());
                    paymentAmount.value = totalAfter;
                    paymentAmountDisplay.value = formatRupiahNumber(totalAfter);
                } else {
                    const parsed = parseRupiah(paymentAmountDisplay.value || '');
                    paymentAmount.value = parsed ? parsed : '';
                }
                updateChange();
            }

            function updateChange() {
                if (!paymentAmount || !changeDisplay) return;
                const bayar = parseFloat(paymentAmount.value || '0') || 0;
                const totalAfter = getTotalAfterDiscount();
                const change = Math.max(0, bayar - totalAfter);
                changeDisplay.textContent = formatRupiahNumber(change);

                if (discountDisplay) {
                    discountDisplay.textContent = formatRupiahNumber(getDiscountAmount());
                }
                if (totalAfterDisplay) {
                    totalAfterDisplay.textContent = formatRupiahNumber(totalAfter);
                }
            }

            discountButtons?.forEach((btn) => {
                btn.addEventListener('click', () => {
                    discountButtons.forEach((el) => {
                        el.classList.remove('border-slate-900', 'bg-slate-900', 'text-white');
                        el.classList.add('border-slate-200', 'text-slate-600');
                    });
                    btn.classList.remove('border-slate-200', 'text-slate-600');
                    btn.classList.add('border-slate-900', 'bg-slate-900', 'text-white');
                    const id = btn.getAttribute('data-id') || '';
                    const type = btn.getAttribute('data-type') || '';
                    const value = parseFloat(btn.getAttribute('data-value') || '0') || 0;
                    selectedDiscount = { type, value };
                    if (discountInput) {
                        discountInput.value = id;
                    }
                    syncPaymentInput();
                    updateChange();
                });
            });

            document.addEventListener('change', (event) => {
                if (event.target && event.target.id === 'payment-type') {
                    toggleBankField();
                    syncPaymentInput();
                }
            });

            if (window.$ && $.fn.select2 && paymentType) {
                $(paymentType).on('select2:select', () => {
                    toggleBankField();
                    syncPaymentInput();
                });
            }

            toggleBankField();
            syncPaymentInput();
            updateChange();
            paymentAmountDisplay?.addEventListener('input', () => {
                if (!paymentAmount || !paymentAmountDisplay) return;
                const raw = parseRupiah(paymentAmountDisplay.value);
                paymentAmount.value = raw;
                paymentAmountDisplay.value = raw ? formatRupiahNumber(raw) : '';
                updateChange();
            });

            closeOrderForm?.addEventListener('submit', async (event) => {
                event.preventDefault();
                const printUrl = closeOrderForm.dataset.printUrl;
                const submitButton = closeOrderForm.querySelector('button[type="submit"]');
                const errorBox = document.getElementById('close-error');
                submitButton?.setAttribute('disabled', 'disabled');

                try {
                    const response = await fetch(closeOrderForm.action, {
                        method: 'POST',
                        headers: { 'X-Requested-With': 'XMLHttpRequest' },
                        body: new FormData(closeOrderForm),
                    });

                    if (!response.ok) {
                        let message = 'Gagal menutup bill.';
                        try {
                            const data = await response.json();
                            if (data?.message) {
                                message = data.message;
                            }
                        } catch (error) {
                            // ignore parse errors
                        }
                        if (errorBox) {
                            errorBox.textContent = message;
                            errorBox.classList.remove('hidden');
                        } else {
                            alert(message);
                        }
                        submitButton?.removeAttribute('disabled');
                        return;
                    }

                    if (errorBox) {
                        errorBox.classList.add('hidden');
                        errorBox.textContent = '';
                    }

                    if (printUrl) {
                        const printWindow = window.open(`${printUrl}?t=${Date.now()}`, '_blank');
                        if (printWindow) {
                            printWindow.focus();
                        }
                    }

                    if (response.redirected) {
                        window.location.href = response.url;
                    } else {
                        window.location.reload();
                    }
                } catch (error) {
                    submitButton?.removeAttribute('disabled');
                }
            });

            printCloseBtn?.addEventListener('click', () => {
                const url = <?php echo json_encode(route('orders.print', $order), 512) ?>;
                const win = window.open(`${url}?t=${Date.now()}`, '_blank');
                if (win) {
                    win.focus();
                }
            });
        });
    </script>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH C:\laragon\www\foodcourt\resources\views\orders\close.blade.php ENDPATH**/ ?>