<?php

use Dreamfox\DeliveryDate\Helpers\DateHelper;

  $checkout = WC()->checkout();
  if (!array_key_exists(DREAMFOX_WDD_FIELD_NAME, $checkout->checkout_fields)) {
    return false;
  }
?>
<div class="dreamfox-delivery-date" id="dd__checkout_field">
  <h3><?php echo __('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; ?>
  <small><?php echo $this->getDescription(); ?></small>
</div>


<script type="text/javascript">
  (function($){
    $(document).ready(function() {
      var holidays = <?php echo json_encode($this->getHolidays()); ?>;
      var disableDays = <?php echo json_encode($this->getDisableDays()); ?>;
      $('#<?php echo DREAMFOX_WDD_FIELD_NAME; ?>').datepicker({
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 2,
        dateFormat : '<?php echo DateHelper::getJsDateFormat(get_option( 'date_format' )) ?>',
        minDate: <?php echo $this->getMinDate(); ?>,
        beforeShowDay	: function(date) {
          var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
          if( jQuery.inArray( string, holidays ) != -1 ){
            return true;
          }
          var day = date.getDay() + 1;
          return [jQuery.inArray( day, disableDays ) == -1]
        },
      });
    })
  })(jQuery);
</script>