<?php
use Dreamfox\DeliveryDate\Helpers\DateHelper;

$checkout = WC()->checkout();
if (! array_key_exists(DREAMFOX_WDD_FIELD_NAME, $checkout->checkout_fields) ) {
  return false;
}

/**
 * Prepare data for JS and enqueue the script.
 * We call wp_enqueue_script() here so the script is only enqueued when this template is rendered.
 * This keeps the template free of inline JS while still passing dynamic PHP values to the script.
 */
$script_handle = 'dreamfox-wdd-premium';
$script_src    = plugin_dir_url( __FILE__ ) . 'delivery.js';

wp_register_script(
  $script_handle,
  $script_src,
  array( 'jquery', 'jquery-ui-datepicker' ),
  DREAMFOX_WDD_VERSION,
  true
);

wp_enqueue_style(
  'dreamfox-wdd-jquery-ui',
  plugins_url( 'assets/jquery-ui/themes/smoothness/jquery-ui.min.css', DREAMFOX_WDD_PLUGIN_FILE ),
  array(),
  DREAMFOX_WDD_VERSION
);

wp_enqueue_script( $script_handle );

$timeslot_data = $this->getTimeslotData();

// Ensure disableDays are integers so $.inArray() strict-equality works in JS.
$disable_days_int = array_map( 'intval', (array) $this->getDisableDays() );

wp_localize_script( $script_handle, 'dreamfoxWdd', array(
  'holidays'         => array_values( (array) $this->getHolidays() ),
  'disableDays'      => $disable_days_int,
  'minDate'          => $this->getMinDate(),
  'dateFormat'       => DateHelper::getJsDateFormat( get_option( 'date_format' ) ),
  'selector'         => '#' . esc_js( DREAMFOX_WDD_FIELD_NAME ),
  'timeslotsEnabled' => $timeslot_data['enabled'],
  'ajaxUrl'          => $timeslot_data['ajaxUrl'],
  'nonce'            => $timeslot_data['nonce'],
  'i18n'             => array_merge(
    array(
      'holidayLabel' => __( 'Holiday', 'woocommerce-delivery-date' ),
    ),
    $timeslot_data['i18n']
  ),
) );
?>

<div class="dreamfox-delivery-date" id="dd__checkout_field">
  <h3><?php esc_html_e( 'Delivery Info', 'woocommerce-delivery-date' ); ?></h3>

  <?php foreach ( $checkout->checkout_fields[ DREAMFOX_WDD_FIELD_NAME ] as $key => $field ) : ?>
    <?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
  <?php endforeach; ?>

  <?php if ( $timeslot_data['enabled'] ) : ?>
  <div id="dreamfox-timeslot-wrapper" class="form-row form-row-wide validate-required">
    <label for="dreamfox_wdd_timeslot">
      <?php esc_html_e( 'Delivery timeslot', 'woocommerce-delivery-date' ); ?>
      <abbr class="required" title="<?php esc_attr_e( 'required', 'woocommerce-delivery-date' ); ?>">*</abbr>
    </label>
    <select
      name="<?php echo esc_attr( DREAMFOX_WDD_TIMESLOT_FIELD_NAME ); ?>"
      id="dreamfox_wdd_timeslot"
      class="select input-text dreamfox-timeslot-select"
    >
      <option value=""><?php esc_html_e( '— Please select a delivery date first —', 'woocommerce-delivery-date' ); ?></option>
    </select>
    <span id="dreamfox-timeslot-loading" style="display:none;font-style:italic;">
      <?php esc_html_e( 'Loading timeslots…', 'woocommerce-delivery-date' ); ?>
    </span>
    <span id="dreamfox-timeslot-no-slots" style="display:none;color:#c00;">
      <?php esc_html_e( 'No timeslots available for this date.', 'woocommerce-delivery-date' ); ?>
    </span>
  </div>
  <?php endif; ?>

  <small><?php echo esc_html( $this->getDescription() ); ?></small>
</div>
