<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

use EasyRestaurantManager\Services\Helper;
use EasyRestaurantManager\Services\ReservationHelper;
use EasyRestaurantManager\Resources\TableResource;

$reservation_img = EASY_RESTAURANT_MANAGER_PLUGIN_URL . '/assets/images/hotel-reservation.png';
// $reservation_time = Helper::getReservationTime();

$reservation_person = Helper::getReservationPerson();
$tables = TableResource::getTable();
$selectedDate = gmdate('Y-m-d');
//=========================================================
// dd($settings['reservation_start_end_time']);
$openClose = $settings['reservation_start_end_time'];
$interval = $settings['reservation_time'];


$reservation_time = array(
    array(
        'label' => '12:00 AM',
        'value' => '00:00'
    ),
);
// dd($reservation_time,);
// function generateTimeSlots($openClose, $interval, $tz = 'Asia/Dhaka') {
//     $open = new DateTime($openClose[0], new DateTimeZone('UTC'));
//     $close = new DateTime($openClose[1], new DateTimeZone('UTC'));

//     // Convert to desired time zone
//     $timezone = new DateTimeZone($tz);
//     $open->setTimezone($timezone);
//     $close->setTimezone($timezone);

//     // Convert interval string (e.g. "01:30") to DateInterval
//     list($hours, $minutes) = explode(':', $interval);
//     $intervalObj = new DateInterval("PT{$hours}H{$minutes}M");

//     $slots = [];
//     $current = clone $open;

//     while ($current < $close) {
//         $slots[] = [
//             'label' => $current->format('h:i A'),
//             'value' => $current->format('H:i')
//         ];
//         $current->add($intervalObj); // Properly add interval
//     }

//     return $slots;
// }
?>
<div class="erm_table_reservation_container">

    <div class="easyRestaurantManager-left">
        <h1 class="erm_room_availability_title">Check Room Availability</h1>
        <div class="erm_reservation-container">
            <div class="erm_reservation-grid">
                <div class="erm_reservation-timeline">
                    <input type="date" id="selectedDate" class="erm_reservation-date-picker" value="<?php echo esc_attr( $selectedDate ) ?>">
                    <?php foreach ($reservation_time as $slot) { 
                        ?>
                        <span class="erm_table_time_slot" data-erm_table_time_slot="<?php echo esc_html($slot['value']); ?>"><?php echo esc_html($slot['label']); ?></span>
                        <?php
                     } ?>
                </div>
                <div class="erm_reservation-tables">
                    <?php foreach ($tables as $table) { ?>
                        <div data-erm_reservation_table_id="<?php echo esc_attr( $table->id ) ?>" class="erm_reservation-table <?php echo esc_attr($table->status); ?>">
                            <?php for ($i = 0; $i < $table->capacity; $i++) { ?>
                                <div class="erm_reservation-seat" style="<?php echo esc_attr(ReservationHelper::easyRestaurantManagerSeatPosition($i, $table->capacity)); ?>"></div>
                            <?php } ?>
                            <div class="erm_reservation-table-body"> <?php echo esc_html($table->table_number); ?> </div>
                        </div>
                    <?php } ?>
                </div>
                <div class="erm_reservation-status">
                    <p class="reserved"><?php echo count(array_filter($tables, fn($t) => $t->status === 'reserved')); ?> Reserved</p>
                    <p class="available"><?php echo count(array_filter($tables, fn($t) => $t->status === 'available')); ?> Available</p>
                </div>
            </div>
        </div>
    </div>
    <div class="easyRestaurantManager-right">
        <h1>Reservation</h1>
        <form id="erm_reservation_form" class="erm_reservation_form_group_warper">

            <input name="branch_id" value="1" type="hidden" />
            <input name="customer_id" value="1" type="hidden" />
            <input name="status" value="pending" type="hidden" />
            <input name="reservation_time" id="reservation_time" value="pending" type="hidden" />
            <input name="reservation_week" id="reservation_week" value="pending" type="hidden" />
            <input name="reservation_start_end_date"  id="reservation_start_end_date" value="pending" type="hidden" />
            <input name="reservation_start_end_time"  id="reservation_start_end_time" value="pending" type="hidden" />
            <div class="easyRestaurantManager-form-group">
                <label for="customer_name">FullName </label>
                <input id="customer_name" name="customer_name" placeholder="FullName" type="text" />
            </div>

            <div class="easyRestaurantManager-form-group">
                <label for="email"> E-mail</label>
                <input id="email" name="email" placeholder="E-mail" type="email" />
            </div>
            <div class="easyRestaurantManager-form-group">
                <label for="phone">Phone</label>
                <input id="phone" name="phone" placeholder="Phone" type="tel" />
            </div>


            <div class="erm_form_warper">
            <div class="easyRestaurantManager-form-group">
                    <label for="erm_reservation_date"> Date</label>
                    <input id="erm_reservation_date" name="reservation_date" type="date" />
                </div>
                <div class="easyRestaurantManager-form-group">
                    <label for="erm_reservation_time"> Time</label>
                    <select id="erm_reservation_time" name="reservation_time">
                        <option value="">Select Time</option>
                        <?php foreach ($reservation_time as $time): ?>
                            <option value="<?php echo esc_attr($time['value']); ?>">
                                <?php echo esc_attr($time['label']); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="easyRestaurantManager-form-group">
                    <label for="num_guests"> Persons</label>
                    <select id="num_guests" name="num_guests">
                        <option value="">Select Persons </option>
                        <?php foreach ($reservation_person as $person): ?>
                            <option value="<?php echo esc_attr($person['value']); ?>">
                                <?php echo esc_attr($person['label']); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
               
            </div>

            <div class="easyRestaurantManager-form-group">
                <label for="erm_table_id">Table</label>
                <select id="erm_table_id" name="table_id">
                    <option value="">Select Table </option>
                </select>
            </div>

            <div class="erm_reservation_btn">
                <button type="submit" id="erm_reservation_btn" class="easyRestaurantManager-submit-btn">Submit reservation</button>
            </div>

        </form>
    </div>
</div>