<?php
require_once KRISTALL_INTEGRATION_MAIN_DIR . 'includes/class-kristall-integration-settings.php';
require_once KRISTALL_INTEGRATION_MAIN_DIR . 'includes/class-kristall-integration-utils.php';

class Kristall_Integration_Admin_Renderer {
	protected static function get_option_name() {
		return Kristall_Integration_Settings::get_plugin_settings()['option_name'];
	}

	/**
	 * Проводит очистку от HTML.
	 *
	 * @param string  $description  Текст для очистки.
	 *
	 * @return string
	 */
	private function esc_description($description) {
    $allowed_tags = [
			'span'   => [
				'id'    => [],
				'class' => [],
			],
      'a'      => [
        'href'  => [],
        'title' => [],
				'id'    => [],
				'class' => [],
			],
			'b'      => [],
			'strong' => [],
			'i'      => [],
			'em'     => [],
			'mark'   => [],
			'small'  => [],
			'del'    => [],
			'ins'    => [],
			'sub'    => [],
			'sup'    => [],
			'u'      => [],
		];
    return wp_kses($description, $allowed_tags);
  }

	/**
	 * Выводит числовое поле.
	 *
	 * @param string      $name        Имя поля.
	 * @param mixed       $default     Значение по-умолчанию.
	 * @param string|null $description Опциональный текст описания поля.
	 *
	 * @return void
	 */
	public function render_number_field($name, $default, $description = null) {
		$options = get_option(self::get_option_name());
		$value = $options[$name] ?? $default;

		?>
		<fieldset>
			<input
				type="number"
				name="<?php echo esc_attr(self::get_option_name()) ?>[<?php echo esc_attr($name) ?>]"
				min="0"
				value="<?php echo esc_attr($value); ?>"
				style="width: 120px;"
			/>
			<?php if ($description) { ?>
				<p class="description"><?php echo $this->esc_description($description) ?></p>
			<?php } ?>
		</fieldset>
		<?php
	}

	/**
	 * Выводит текстовое поле.
	 *
	 * @param string      $name        Имя поля.
	 * @param mixed       $default     Значение по-умолчанию.
	 * @param string|null $description Опциональный текст описания поля.
	 *
	 * @return void
	 */
	public function render_text_field($name, $default, $description = null, $help_btn_id = null) {
		$options = get_option(self::get_option_name());
		$value = $options[$name] ?? $default;

		?>
		<fieldset style="display: relative">
			<input
				type="text"
				name="<?php echo esc_attr(self::get_option_name()) ?>[<?php echo esc_attr($name) ?>]"
				value="<?php echo esc_attr($value); ?>"
				class="regular-text"
			/>
			<?php if ($help_btn_id) { ?>
				<a href="#" class="krl-input-help" id="<?php echo esc_attr($help_btn_id); ?>">?</a>
			<?php } ?>
			<?php if ($description) { ?>
				<p class="description"><?php echo $this->esc_description($description) ?></p>
			<?php } ?>
		</fieldset>
		<?php
	}

	/**
	 * Выводит поле выбора цвета.
	 *
	 * @param string      $name        Имя поля.
	 * @param mixed       $default     Значение по-умолчанию.
	 * @param string|null $description Опциональный текст описания поля.
	 *
	 * @return void
	 */
	public function render_color_field($name, $default, $description = null) {
		$options = get_option(self::get_option_name());
		$value = $options[$name] ?? $default;

		?>
		<fieldset class="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('color-picker-wrapper')) ?>">
			<input
				type="text"
				name="<?php echo esc_attr(self::get_option_name()) ?>[<?php echo esc_attr($name) ?>]"
				value="<?php echo esc_attr($value); ?>"
				class="regular-text <?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('color-picker')) ?>"
				data-kristall-cp-option-name="<?php echo esc_attr($name); ?>"
			/>
			<?php if ($description) { ?>
				<p class="description"><?php echo $this->esc_description($description) ?></p>
			<?php } ?>
		</fieldset>
		<?php
	}

	/**
	 * Выводит предпросмотр кнопок выбора типа клиента на странице оплаты.
	 *
	 * @return void
	 */
	public function render_buttons_preview_block() {
		?>
		<div id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('btns-preview')) ?>">
			<p class="description"><?php esc_html_e('ПРЕДПРОСМОТР', 'kristall-integration') ?></p>

			<div class="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('billing-info__tabs')) ?>">
					<span class="woocommerce-input-wrapper">
						<input type="radio" class="input-radio" checked="checked" name="preview-billing-info-tabs"
						       id="preview-billing-info-tabs-1">
						<label for="preview-billing-info-tabs-1" class="radio"><?php esc_html_e('Физическое лицо', 'kristall-integration') ?></label>
						<input type="radio" class="input-radio" name="preview-billing-info-tabs"
						       id="preview-billing-info-tabs-2">
						<label for="preview-billing-info-tabs-2" class="radio"><?php esc_html_e('Юридическое лицо / ИП', 'kristall-integration') ?></label>
					</span>
			</div>
		</div>
		<?php
	}

		public function render_additional_content_block() {
			$settings = Kristall_Integration_Settings::get_plugin_settings();
			$options = get_option($settings['option_name']) ?? [];
			$side_name = $settings['field_names']['side_content'];
			$desc_name = $settings['field_names']['description_content'];

			$side_settings = [];
			$desc_settings = [];

			if (isset($options[$side_name])) {
				$side_settings = $options[$side_name];
			}

			if (isset($options[$desc_name])) {
				$desc_settings = $options[$desc_name];
			}
		?>
		<div id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__wrapper')) ?>">
			<input
				type="hidden"
				name="kristall_options_array[<?php echo $side_name; ?>]"
				id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__side_input')) ?>"
				value="<?php echo esc_attr(json_encode($side_settings)) ?>"
			/>
			<input
				type="hidden"
				name="kristall_options_array[<?php echo $desc_name; ?>]"
				id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__desc_input')) ?>"
				value="<?php echo esc_attr(json_encode($desc_settings)) ?>"
			/>

			<div class="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__row')) ?>">
				<div id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__select')) ?>">
					<div id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__placeholder')) ?>">Выберите настройки доп. контента</div>
					<div id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__selected_text')) ?>" style="display:none"></div>
					<div id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__selected_filters')) ?>" style="display:none"></div>
				</div>
				<button
					id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__add')) ?>"
					class="button button-primary"
					type="button"
				>+ Добавить</button>
				
				<div id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__dropdown')) ?>"></div>
				<div id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__dropdown_add')) ?>">
					<div class="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__dd_item')) ?>" data-type="side">Боковой контент</div>
					<div class="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__dd_item')) ?>" data-type="desc">Дополнительный контент описания</div>
				</div>
			</div>

			<div id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__title')) ?>" style="display: none"></div>
			<div id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__filters')) ?>" style="display: none"></div>
			<a href="#" id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__add_filter')) ?>" style="display: none">+ Добавить фильтр</a>
			<div id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__wysiwyg_wrapper')) ?>" style="display: none">
				<?php wp_editor('', Kristall_Integration_Utils::get_prefixed_key('additional_content__wysiwyg')); ?>
			</div>
			<div id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__remove_wrapper')) ?>" style="display: none">
				<a href="#" id="<?php echo esc_attr(Kristall_Integration_Utils::get_prefixed_key('additional_content__remove')) ?>">Удалить контент</a>
			</div>
		</div>
		<?php
	}
}
