<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php /* @var $block \Magento\Catalog\Block\Product\View\Options\Type\Date */ ?>
<?php $_option = $block->getOption(); ?>
<?php $_optionId = $_option->getId(); ?>
<div class="admin__field field<?php if ($_option->getIsRequire()) echo ' required _required' ?>">
  <label class="label admin__field-label">
      <?php echo  $block->escapeHtml($_option->getTitle()) ?>
      <?php /* @escapeNotVerified */ echo $block->getFormatedPrice() ?>
  </label>
  <div class="admin__field-control control">

  <?php if ($_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_DATE_TIME
      || $_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_DATE): ?>

      <?php echo $block->getDateHtml() ?>

      <?php if (!$block->useCalendar()): ?>
      <script>
require([
  "prototype",
  "Magento_Catalog/catalog/product/composite/configure"
], function(){

      window.dateOption = productConfigure.opConfig.dateOption;
      Event.observe('options_<?php /* @escapeNotVerified */ echo $_optionId ?>_month', 'change', dateOption.reloadMonth.bind(dateOption));
      Event.observe('options_<?php /* @escapeNotVerified */ echo $_optionId ?>_year', 'change', dateOption.reloadMonth.bind(dateOption));      
});
</script>
      <?php endif; ?>

  <?php endif; ?>

  <?php if ($_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_DATE_TIME
      || $_option->getType() == \Magento\Catalog\Model\Product\Option::OPTION_TYPE_TIME): ?>
      <span class="time-picker"><?php echo $block->getTimeHtml() ?></span>
  <?php endif; ?>

    <input type="hidden" name="validate_datetime_<?php /* @escapeNotVerified */ echo $_optionId ?>" class="validate-datetime-<?php /* @escapeNotVerified */ echo $_optionId ?>" value="" />
    <script>
require([
  "jquery",
  "mage/backend/validation"
], function(jQuery){

    //<![CDATA[
<?php if ($_option->getIsRequire()): ?>
        jQuery.validator.addMethod('validate-datetime-<?php /* @escapeNotVerified */ echo $_optionId ?>', function(v) {
            var dateTimeParts = jQuery('.datetime-picker[id^="options_<?php /* @escapeNotVerified */ echo $_optionId ?>"]');
            for (var i=0; i < dateTimeParts.length; i++) {
                if (dateTimeParts[i].value == "") return false;
            }
            return true;
        }, '<?php /* @escapeNotVerified */ echo $block->escapeJsQuote( __('This is a required option.') )?>');
<?php else: ?>
        jQuery.validator.addMethod('validate-datetime-<?php /* @escapeNotVerified */ echo $_optionId ?>', function(v) {
            var dateTimeParts = jQuery('.datetime-picker[id^="options_<?php /* @escapeNotVerified */ echo $_optionId ?>"]');
            var hasWithValue = false, hasWithNoValue = false;
            var pattern = /day_part$/i;
            for (var i=0; i < dateTimeParts.length; i++) {
                if (! pattern.test(dateTimeParts[i].id)) {
                    if (dateTimeParts[i].value === "") {
                        hasWithValue = true;
                    } else {
                        hasWithNoValue = true;
                    }
                }
            }
            return hasWithValue ^ hasWithNoValue;
        }, '<?php /* @escapeNotVerified */ echo $block->escapeJsQuote( __('The field isn\'t complete.') )?>');
<?php endif; ?>
    //]]>
    
});
</script>
  </div>
</div>
