<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>

<div class="box">
    <div class="box-header">
        <h2 class="blue"><?= lang('kitchen_display') ?></h2>
    </div>
    <div class="box-content k-container">
        <?php
        echo "<script> console.log(" . json_encode($orders) . ");</script>";

        $html = '';
        foreach ($orders as $order) {
            $booking_table_name = '';
            if($order->booking_table_name) $booking_table_name = '('. $order->booking_table_name . ')';
            
            $html .= '<div class="k-card" id="' . $order->sales_id . '">
            <h3><span>' . ($order->service_name??'No Service Selected') . '</span><span>' . $booking_table_name . '</span></h3>
            <p>' . $order->product_name . '</p>
            <button onclick="window.location.href=\'' . admin_url('sales/kitchen_mark_as_cooked/' . $order->id) . '\'">Mark As Cooked</button>
            <br>
            <button class="view-details-btn" data-sales-id="' . $order->sales_id . '" onclick="window.location.href=\'' . admin_url('pos/view/' . $order->sales_id) . '\'">View Details</button>
          </div>';
        }
        echo $html;
        ?>
    </div>
</div>

<script>
    $(document).ready(function() {
        $('.view-details-btn').on('click', function() {
            var salesId = $(this).data('sales-id');
            var ajaxUrl = "<?= admin_url('sales/kitchen_view_details') ?>";

            $.ajax({
                url: ajaxUrl,
                method: 'GET',
                data: { salesId: salesId },
                success: function(response) {
                    // Handle success response if required
                    window.location.href = response;
                },
                error: function(xhr, status, error) {
                    // Handle error if required
                }
            });
        });
    });
</script>
