<?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
/**
 *
 * Field: icon
 *
 * @since 1.0.0
 * @version 1.0.0
 *
 */
if ( ! class_exists( 'DSPILOT_Field_icon' ) ) {
  class DSPILOT_Field_icon extends DSPILOT_Fields {

    public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) {
      parent::__construct( $field, $value, $unique, $where, $parent );
    }

    public function render() {

      $args = wp_parse_args( $this->field, array(
        'button_title' => esc_html__( 'Add Icon', 'discountpilot-dynamic-pricing' ),
        'remove_title' => esc_html__( 'Remove Icon', 'discountpilot-dynamic-pricing' ),
      ) );

      echo wp_kses_post($this->field_before());

      $nonce  = wp_create_nonce( 'dspilot_icon_nonce' );
      $hidden = ( empty( $this->value ) ) ? ' hidden' : '';

      echo '<div class="dspilot-icon-select">';
      echo '<span class="dspilot-icon-preview'. esc_attr( $hidden ) .'"><i class="'. esc_attr( $this->value ) .'"></i></span>';
      echo '<a href="#" class="button button-primary dspilot-icon-add" data-nonce="'. esc_attr( $nonce ) .'">'. esc_html($args['button_title']) .'</a>';
      echo '<a href="#" class="button dspilot-warning-primary dspilot-icon-remove'. esc_attr( $hidden ) .'">'. esc_html($args['remove_title']) .'</a>';
      echo '<input type="hidden" name="'. esc_attr( $this->field_name() ) .'" value="'. esc_attr( $this->value ) .'" class="dspilot-icon-value"'. esc_attr($this->field_attributes()) .' />';
      echo '</div>';

      echo wp_kses_post($this->field_after());

    }

    public function enqueue() {
      add_action( 'admin_footer', array( 'DSPILOT_Field_icon', 'add_footer_modal_icon' ) );
      add_action( 'customize_controls_print_footer_scripts', array( 'DSPILOT_Field_icon', 'add_footer_modal_icon' ) );
    }

    public static function add_footer_modal_icon() {
    ?>
      <div id="dspilot-modal-icon" class="dspilot-modal dspilot-modal-icon hidden">
        <div class="dspilot-modal-table">
          <div class="dspilot-modal-table-cell">
            <div class="dspilot-modal-overlay"></div>
            <div class="dspilot-modal-inner">
              <div class="dspilot-modal-title">
                <?php esc_html_e( 'Add Icon', 'discountpilot-dynamic-pricing' ); ?>
                <div class="dspilot-modal-close dspilot-icon-close"></div>
              </div>
              <div class="dspilot-modal-header">
                <input type="text" placeholder="<?php esc_html_e( 'Search...', 'discountpilot-dynamic-pricing' ); ?>" class="dspilot-icon-search" />
              </div>
              <div class="dspilot-modal-content">
                <div class="dspilot-modal-loading"><div class="dspilot-loading"></div></div>
                <div class="dspilot-modal-load"></div>
              </div>
            </div>
          </div>
        </div>
      </div>
    <?php
    }

  }
}
