<?php
/**
 * Settings Block - Template
 * 
 * @package OrokuBlocks
 * @author OrokuBlocks (https://orokublocks.com/)
 * 
 * @since 1.0.0
 * 
 * @param array $args {
 *   @type string $id           Id of the settings block.
 *   @type string $name         Name of the settings block.
 *   @type string $icon         Name of the icon to put next to the settings block name. Optional.
 *   @type array  $settings {
 *     @type string $id           Id of the setting.
 *     @type string $name         Name of the setting.
 *     @type string $type         Type of the setting. One of: 'text', 'select'.
 *     @type string $default      Setting default value.
 *     @type string $value        Setting current saved value.
 *     @type array  $options {    Optional. Required when type is set to 'select'.
 *       @type string $id     Select option id.
 *       @type string $name   Select option display name.
 *     }
 *   } 
 * }
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
  exit;
}

$defaults = [
  'icon'      => '',
  'settings'  => []
];

$options = array_merge( $defaults, $args );

?>
<div class="oroku-settings-block">
  <div class="oroku-settings-block__header">
  <?php if ( $options['icon'] !== '' ) : ?>
    <div class="oroku-icon-decoration oroku-icon-decoration--theme--<?php echo esc_attr( $options['icon'] ); ?>">
    <?php

      /**
       * SVG
       */
      orokublocks()->admin->requireTemplate(
        'svg',
        [
          'additional_wrapper_classes'  => 'oroku-icon-decoration__icon',
          'icon'                        => $options['icon']
        ]
      );

    ?>
    </div>
  <?php endif; ?>

    <h2 class="oroku-settings-block__heading oroku-h4"><?php echo esc_html( $options['name'] ); ?></h2>
  </div>
<?php if ( count( $options['settings'] ) > 0 ) : ?>
  <div class="oroku-settings-block__options">
    <div class="oroku-input-group oroku-input-group--style--row">
    <?php foreach ( $options['settings'] as $setting ) : ?>
    <?php $setting_id = $options['id'] . '_' . $setting['id']; ?>
      <div class="oroku-input-group__item">
      <?php if ( $setting['type'] !== 'checkbox' ) : ?>
        <label class="oroku-input-label" for="<?php echo esc_attr( $setting_id ); ?>"><?php echo esc_html( $setting['name'] ); ?></label>
      <?php endif; ?>

      <?php if ( $setting['type'] === 'text' ) : ?>
        <input
          class="oroku-input-element"
          type="text"
          id="<?php echo esc_attr( $setting_id ); ?>"
          name="<?php echo esc_attr( $setting_id ); ?>"
          data-setting-section-id="<?php echo esc_attr( $options['id'] ); ?>"
        <?php if ( $setting['value'] !== '' ) : ?>
          value="<?php echo esc_attr( $setting['value'] ); ?>"
        <?php endif; ?>
        >
      <?php endif; ?>

      <?php if ( $setting['type'] === 'select' ) : ?>
        <div class="oroku-input-select-wrapper">
          <select
            class="oroku-input-element oroku-input-select"
            id="<?php echo esc_attr( $setting_id ); ?>"
            name="<?php echo esc_attr( $setting_id ); ?>"
            data-setting-section-id="<?php echo esc_attr( $options['id'] ); ?>"
          >
            <button>
              <selectedcontent></selectedcontent>
            </button>

          <?php foreach ( $setting['options'] as $option ) : ?>
            <option
              value="<?php echo esc_html( $option['id'] ); ?>"
            <?php if ( $option['id'] === $setting['value'] ) : ?>
              selected
            <?php endif; ?>
            >
            <?php echo esc_html( $option['name'] ); ?>
            </option>
          <?php endforeach; ?>
          </select>
        <?php

          /**
           * SVG
           */
          orokublocks()->admin->requireTemplate(
            'svg',
            [
              'additional_wrapper_classes'  => 'oroku-input-select-wrapper__icon',
              'icon'                        => 'dropdown-arrow'
            ]
          );

        ?>
        </div>
      <?php endif; ?>

      <?php if ( $setting['type'] === 'checkbox' ) : ?>
        <div class="oroku-input-selectable-wrapper oroku-input-selectable-wrapper--spacing--top">
          <input
            class="oroku-input-selectable"
            type="checkbox"
            id="<?php echo esc_attr( $setting_id ); ?>"
            name="<?php echo esc_attr( $setting_id ); ?>"
            data-setting-section-id="<?php echo esc_attr( $options['id'] ); ?>"
          <?php if ( $setting['value'] === true ) : ?>
            checked
          <?php endif; ?>
          >
          <div class="oroku-input-selectable-box">
          <?php

            /**
             * SVG
             */
            orokublocks()->admin->requireTemplate(
              'svg',
              [
                'additional_wrapper_classes'  => 'oroku-input-selectable-box__icon',
                'icon'                        => 'cross'
              ]
            );

          ?>
          </div>
          <label class="oroku-input-selectable-label" for="<?php echo esc_attr( $setting_id ); ?>"><?php echo esc_html( $setting['name'] ); ?></label>
        </div>
      <?php endif; ?>
      </div>
    <?php endforeach; ?>
    </div>
  </div>
<?php endif; ?>
</div>