<?php

/**

 * Booking schedule exceptions (Settings tab).

 *

 * @package Maca_MenuList

 */



if (!defined('ABSPATH')) {

    exit;

}



/**

 * Render exceptions panel on the settings page.

 */

function maca_menulist_render_booking_exceptions_settings_panel() {

    $edit_id = isset($_GET['edit_exception']) ? absint(wp_unslash($_GET['edit_exception'])) : 0;

    $edit_recurring_id = isset($_GET['edit_recurring_exception']) ? absint(wp_unslash($_GET['edit_recurring_exception'])) : 0;

    $exceptions = maca_menulist_db_get_booking_exceptions(wp_date('Y-m-d'));

    $recurring_exceptions = maca_menulist_db_get_booking_recurring_exceptions();

    $edit_exception = null;

    $edit_recurring = null;



    if ($edit_id > 0) {

        foreach ($exceptions as $exception) {

            if ((int) $exception->id === $edit_id) {

                $edit_exception = $exception;

                break;

            }

        }

    }



    if ($edit_recurring_id > 0) {

        $edit_recurring = maca_menulist_db_get_booking_recurring_exception($edit_recurring_id);

    }



    $base_url = maca_menulist_get_booking_exceptions_base_url();

    $weekday_labels = maca_menulist_weekday_labels_ordered();

    $edit_recurring_days = $edit_recurring

        ? maca_menulist_parse_days_of_week($edit_recurring->days_of_week ?? '')

        : array();

    ?>

    <section class="maca-settings-section maca-settings-tab-panel" id="maca-settings-booking-exceptions" data-settings-tab="exceptions">

        <h2 class="maca-settings-section-title"><?php esc_html_e('Exceptions', 'maca-restu-pro'); ?></h2>

        <p class="maca-settings-section-desc description"><?php esc_html_e('Override the regular schedule for specific dates. Use Closed for holidays, Fully booked when all tables are taken, or Custom hours for special opening times.', 'maca-restu-pro'); ?></p>



        <h3><?php echo $edit_exception ? esc_html__('Edit exception', 'maca-restu-pro') : esc_html__('Add exception', 'maca-restu-pro'); ?></h3>



        <form method="post" action="<?php echo esc_url($base_url); ?>">

            <?php wp_nonce_field('maca_bookings', 'maca_bookings_nonce'); ?>

            <input type="hidden" name="maca_bookings_action" value="save_exception">

            <input type="hidden" name="exception_id" value="<?php echo esc_attr($edit_exception ? (string) $edit_exception->id : '0'); ?>">



            <table class="form-table">

                <tr>

                    <th><label for="exception_date"><?php esc_html_e('Date', 'maca-restu-pro'); ?></label></th>

                    <td>

                        <input type="date" id="exception_date" name="exception_date" required

                            value="<?php echo esc_attr($edit_exception ? $edit_exception->exception_date : ''); ?>">

                    </td>

                </tr>

                <tr>

                    <th><label for="exception_type"><?php esc_html_e('Type', 'maca-restu-pro'); ?></label></th>

                    <td>

                        <select id="exception_type" name="exception_type">

                            <?php foreach (maca_menulist_get_booking_exception_types() as $type) : ?>

                                <option value="<?php echo esc_attr($type); ?>" <?php selected($edit_exception ? $edit_exception->exception_type : 'closed', $type); ?>>

                                    <?php echo esc_html(maca_menulist_get_booking_exception_type_label($type)); ?>

                                </option>

                            <?php endforeach; ?>

                        </select>

                        <p class="description" id="maca-exception-type-help">

                            <?php

                            $default_type = $edit_exception ? $edit_exception->exception_type : 'closed';

                            echo esc_html(maca_menulist_get_booking_exception_type_description($default_type));

                            ?>

                        </p>

                        <script>

                            (function () {

                                var select = document.getElementById('exception_type');

                                var help = document.getElementById('maca-exception-type-help');

                                if (!select || !help) {

                                    return;

                                }

                                var descriptions = <?php

                                $desc_map = array();

                                foreach (maca_menulist_get_booking_exception_types() as $type) {

                                    $desc_map[ $type ] = maca_menulist_get_booking_exception_type_description($type);

                                }

                                echo wp_json_encode($desc_map);

                                ?>;

                                select.addEventListener('change', function () {

                                    help.textContent = descriptions[select.value] || '';

                                });

                            })();

                        </script>

                    </td>

                </tr>

                <tr>

                    <th><label for="time_start"><?php esc_html_e('Open (custom hours)', 'maca-restu-pro'); ?></label></th>

                    <td>

                        <input type="time" id="time_start" name="time_start"

                            value="<?php echo esc_attr($edit_exception && $edit_exception->time_start ? substr($edit_exception->time_start, 0, 5) : ''); ?>">

                    </td>

                </tr>

                <tr>

                    <th><label for="time_end"><?php esc_html_e('Close (custom hours)', 'maca-restu-pro'); ?></label></th>

                    <td>

                        <input type="time" id="time_end" name="time_end"

                            value="<?php echo esc_attr($edit_exception && $edit_exception->time_end ? substr($edit_exception->time_end, 0, 5) : ''); ?>">

                    </td>

                </tr>

                <tr>

                    <th><label for="note"><?php esc_html_e('Note (optional)', 'maca-restu-pro'); ?></label></th>

                    <td><input type="text" id="note" name="note" class="regular-text" value="<?php echo esc_attr($edit_exception ? (string) $edit_exception->note : ''); ?>"></td>

                </tr>

            </table>



            <?php submit_button($edit_exception ? __('Update exception', 'maca-restu-pro') : __('Add exception', 'maca-restu-pro')); ?>

            <?php if ($edit_exception) : ?>

                <a href="<?php echo esc_url($base_url); ?>" class="button"><?php esc_html_e('Cancel', 'maca-restu-pro'); ?></a>

            <?php endif; ?>

        </form>



        <h3><?php esc_html_e('Upcoming exceptions', 'maca-restu-pro'); ?></h3>

        <table class="wp-list-table widefat fixed striped">

            <thead>

                <tr>

                    <th><?php esc_html_e('Date', 'maca-restu-pro'); ?></th>

                    <th><?php esc_html_e('Type', 'maca-restu-pro'); ?></th>

                    <th><?php esc_html_e('Hours', 'maca-restu-pro'); ?></th>

                    <th><?php esc_html_e('Note', 'maca-restu-pro'); ?></th>

                    <th><?php esc_html_e('Actions', 'maca-restu-pro'); ?></th>

                </tr>

            </thead>

            <tbody>

                <?php if (empty($exceptions)) : ?>

                    <tr><td colspan="5"><?php esc_html_e('No upcoming exceptions.', 'maca-restu-pro'); ?></td></tr>

                <?php else : ?>

                    <?php foreach ($exceptions as $exception) : ?>

                        <tr>

                            <td><?php echo esc_html(maca_menulist_format_wp_date((string) $exception->exception_date)); ?></td>

                            <td><?php echo esc_html(maca_menulist_get_booking_exception_type_label($exception->exception_type)); ?></td>

                            <td>

                                <?php

                                if ($exception->exception_type === 'custom_hours' && $exception->time_start && $exception->time_end) {

                                    echo esc_html(substr($exception->time_start, 0, 5) . '–' . substr($exception->time_end, 0, 5));

                                } else {

                                    echo '—';

                                }

                                ?>

                            </td>

                            <td><?php echo esc_html($exception->note ?: '—'); ?></td>

                            <td>

                                <a href="<?php echo esc_url($base_url . '&edit_exception=' . (int) $exception->id); ?>" class="button button-small"><?php esc_html_e('Edit', 'maca-restu-pro'); ?></a>

                                <form method="post" action="<?php echo esc_url($base_url); ?>" style="display:inline;" onsubmit="return confirm('<?php echo esc_js(__('Delete this exception?', 'maca-restu-pro')); ?>');">

                                    <?php wp_nonce_field('maca_bookings', 'maca_bookings_nonce'); ?>

                                    <input type="hidden" name="maca_bookings_action" value="delete_exception">

                                    <input type="hidden" name="exception_id" value="<?php echo esc_attr((string) $exception->id); ?>">

                                    <button type="submit" class="button button-small button-link-delete"><?php esc_html_e('Delete', 'maca-restu-pro'); ?></button>

                                </form>

                            </td>

                        </tr>

                    <?php endforeach; ?>

                <?php endif; ?>

            </tbody>

        </table>



        <hr>



        <h3><?php echo $edit_recurring ? esc_html__('Edit recurring weekday exception', 'maca-restu-pro') : esc_html__('Add recurring weekday exception', 'maca-restu-pro'); ?></h3>

        <p class="description"><?php esc_html_e('Apply an exception on selected weekdays between two dates — for example every Wednesday with custom hours during summer. Shown under that weekday in the opening hours block.', 'maca-restu-pro'); ?></p>



        <form method="post" action="<?php echo esc_url($base_url); ?>">

            <?php wp_nonce_field('maca_bookings', 'maca_bookings_nonce'); ?>

            <input type="hidden" name="maca_bookings_action" value="save_recurring_exception">

            <input type="hidden" name="recurring_exception_id" value="<?php echo esc_attr($edit_recurring ? (string) $edit_recurring->id : '0'); ?>">



            <table class="form-table">

                <tr>

                    <th scope="row"><?php esc_html_e('Period', 'maca-restu-pro'); ?></th>

                    <td>

                        <label for="recurring_date_from"><?php esc_html_e('From', 'maca-restu-pro'); ?></label>

                        <input type="date" id="recurring_date_from" name="recurring_date_from" required

                            value="<?php echo esc_attr($edit_recurring ? (string) $edit_recurring->date_from : ''); ?>">

                        <label for="recurring_date_to" style="margin-left:12px;"><?php esc_html_e('To', 'maca-restu-pro'); ?></label>

                        <input type="date" id="recurring_date_to" name="recurring_date_to" required

                            value="<?php echo esc_attr($edit_recurring ? (string) $edit_recurring->date_to : ''); ?>">

                    </td>

                </tr>

                <tr>

                    <th scope="row"><?php esc_html_e('Weekdays', 'maca-restu-pro'); ?></th>

                    <td>

                        <fieldset>

                            <legend class="screen-reader-text"><?php esc_html_e('Weekdays', 'maca-restu-pro'); ?></legend>

                            <?php foreach ($weekday_labels as $day => $label) : ?>

                                <label style="display:inline-block;margin-right:12px;margin-bottom:6px;">

                                    <input type="checkbox" name="recurring_days_of_week[]" value="<?php echo esc_attr((string) $day); ?>"

                                        <?php checked(in_array($day, $edit_recurring_days, true)); ?>>

                                    <?php echo esc_html($label); ?>

                                </label>

                            <?php endforeach; ?>

                        </fieldset>

                        <p class="description"><?php esc_html_e('Select at least one weekday. The rule applies on each matching day within the period.', 'maca-restu-pro'); ?></p>

                    </td>

                </tr>

                <tr>

                    <th><label for="recurring_exception_type"><?php esc_html_e('Type', 'maca-restu-pro'); ?></label></th>

                    <td>

                        <select id="recurring_exception_type" name="recurring_exception_type">

                            <?php foreach (maca_menulist_get_booking_exception_types() as $type) : ?>

                                <?php if ($type === 'fully_booked') : ?>

                                    <?php continue; ?>

                                <?php endif; ?>

                                <option value="<?php echo esc_attr($type); ?>" <?php selected($edit_recurring ? $edit_recurring->exception_type : 'custom_hours', $type); ?>>

                                    <?php echo esc_html(maca_menulist_get_booking_exception_type_label($type)); ?>

                                </option>

                            <?php endforeach; ?>

                        </select>

                        <p class="description" id="maca-recurring-exception-type-help">

                            <?php

                            $default_recurring_type = $edit_recurring ? $edit_recurring->exception_type : 'custom_hours';

                            echo esc_html(maca_menulist_get_booking_exception_type_description($default_recurring_type));

                            ?>

                        </p>

                        <script>

                            (function () {

                                var select = document.getElementById('recurring_exception_type');

                                var help = document.getElementById('maca-recurring-exception-type-help');

                                if (!select || !help) {

                                    return;

                                }

                                var descriptions = <?php

                                $recurring_desc_map = array();

                                foreach (maca_menulist_get_booking_exception_types() as $type) {

                                    if ($type === 'fully_booked') {

                                        continue;

                                    }

                                    $recurring_desc_map[ $type ] = maca_menulist_get_booking_exception_type_description($type);

                                }

                                echo wp_json_encode($recurring_desc_map);

                                ?>;

                                select.addEventListener('change', function () {

                                    help.textContent = descriptions[select.value] || '';

                                });

                            })();

                        </script>

                    </td>

                </tr>

                <tr>

                    <th><label for="recurring_time_start"><?php esc_html_e('Open (custom hours)', 'maca-restu-pro'); ?></label></th>

                    <td>

                        <input type="time" id="recurring_time_start" name="recurring_time_start"

                            value="<?php echo esc_attr($edit_recurring && $edit_recurring->time_start ? substr($edit_recurring->time_start, 0, 5) : ''); ?>">

                    </td>

                </tr>

                <tr>

                    <th><label for="recurring_time_end"><?php esc_html_e('Close (custom hours)', 'maca-restu-pro'); ?></label></th>

                    <td>

                        <input type="time" id="recurring_time_end" name="recurring_time_end"

                            value="<?php echo esc_attr($edit_recurring && $edit_recurring->time_end ? substr($edit_recurring->time_end, 0, 5) : ''); ?>">

                    </td>

                </tr>

                <tr>

                    <th><label for="recurring_note"><?php esc_html_e('Note (optional)', 'maca-restu-pro'); ?></label></th>

                    <td><input type="text" id="recurring_note" name="recurring_note" class="regular-text" value="<?php echo esc_attr($edit_recurring ? (string) $edit_recurring->note : ''); ?>"></td>

                </tr>

            </table>



            <?php submit_button($edit_recurring ? __('Update recurring exception', 'maca-restu-pro') : __('Add recurring exception', 'maca-restu-pro')); ?>

            <?php if ($edit_recurring) : ?>

                <a href="<?php echo esc_url($base_url); ?>" class="button"><?php esc_html_e('Cancel', 'maca-restu-pro'); ?></a>

            <?php endif; ?>

        </form>



        <h3><?php esc_html_e('Active recurring weekday exceptions', 'maca-restu-pro'); ?></h3>

        <table class="wp-list-table widefat fixed striped">

            <thead>

                <tr>

                    <th><?php esc_html_e('Period', 'maca-restu-pro'); ?></th>

                    <th><?php esc_html_e('Weekdays', 'maca-restu-pro'); ?></th>

                    <th><?php esc_html_e('Type', 'maca-restu-pro'); ?></th>

                    <th><?php esc_html_e('Hours', 'maca-restu-pro'); ?></th>

                    <th><?php esc_html_e('Note', 'maca-restu-pro'); ?></th>

                    <th><?php esc_html_e('Actions', 'maca-restu-pro'); ?></th>

                </tr>

            </thead>

            <tbody>

                <?php if (empty($recurring_exceptions)) : ?>

                    <tr><td colspan="6"><?php esc_html_e('No active recurring exceptions.', 'maca-restu-pro'); ?></td></tr>

                <?php else : ?>

                    <?php foreach ($recurring_exceptions as $recurring_exception) : ?>

                        <tr>

                            <td><?php echo esc_html(maca_menulist_format_recurring_exception_date_range($recurring_exception)); ?></td>

                            <td><?php echo esc_html(maca_menulist_format_recurring_exception_weekdays($recurring_exception)); ?></td>

                            <td><?php echo esc_html(maca_menulist_get_booking_exception_type_label($recurring_exception->exception_type)); ?></td>

                            <td>

                                <?php

                                if ($recurring_exception->exception_type === 'custom_hours' && $recurring_exception->time_start && $recurring_exception->time_end) {

                                    echo esc_html(substr($recurring_exception->time_start, 0, 5) . '–' . substr($recurring_exception->time_end, 0, 5));

                                } else {

                                    echo '—';

                                }

                                ?>

                            </td>

                            <td><?php echo esc_html($recurring_exception->note ?: '—'); ?></td>

                            <td>

                                <a href="<?php echo esc_url($base_url . '&edit_recurring_exception=' . (int) $recurring_exception->id); ?>" class="button button-small"><?php esc_html_e('Edit', 'maca-restu-pro'); ?></a>

                                <form method="post" action="<?php echo esc_url($base_url); ?>" style="display:inline;" onsubmit="return confirm('<?php echo esc_js(__('Delete this recurring exception?', 'maca-restu-pro')); ?>');">

                                    <?php wp_nonce_field('maca_bookings', 'maca_bookings_nonce'); ?>

                                    <input type="hidden" name="maca_bookings_action" value="delete_recurring_exception">

                                    <input type="hidden" name="recurring_exception_id" value="<?php echo esc_attr((string) $recurring_exception->id); ?>">

                                    <button type="submit" class="button button-small button-link-delete"><?php esc_html_e('Delete', 'maca-restu-pro'); ?></button>

                                </form>

                            </td>

                        </tr>

                    <?php endforeach; ?>

                <?php endif; ?>

            </tbody>

        </table>

    </section>

    <?php

}



/**

 * Redirect URL after exception save/delete on the settings page.

 *

 * @return string

 */

function maca_menulist_get_booking_exceptions_base_url() {

    if (function_exists('maca_menulist_user_is_booking_host_only') && maca_menulist_user_is_booking_host_only()) {

        return admin_url('admin.php?page=maca-restu-booking-schedule&tab=exceptions');

    }



    return admin_url('admin.php?page=maca-restu-settings&tab=exceptions');

}



/**

 * Redirect URL after exception save/delete on the settings page.

 *

 * @return string

 */

function maca_menulist_booking_exceptions_settings_redirect_url() {

    return maca_menulist_get_booking_exceptions_base_url();

}

