<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}


if ( ! function_exists( 'discountify_checkbox_field' ) ) {
	function discountify_checkbox_field( $args ) {
		$condition_class = ! empty( $args['condition_class'] ) ? $args['condition_class'] : '';
		$disable         = ! empty( $args['disable'] ) ? 'disable' : '';
		$checkbox_label  = ! empty( $args['checkbox_label'] ) ? $args['checkbox_label'] : '';
		$checked         = ( ! empty( $args['checked'] ) && $args['checked'] === 'yes' ) ? 'checked' : '';
		$data_label      = ! empty( $args['data_label'] ) ? $args['data_label'] : '';
		$id              = ! empty( $args['id'] ) ? $args['id'] : '';
		$label           = ! empty( $args['label'] ) ? $args['label'] : '';
		$desc            = ! empty( $args['desc'] ) ? $args['desc'] : '';

		$html = '
			<div class="single-block ' . esc_attr( $condition_class ) . '">
				<div class="form-label">' . esc_html( $label ) . '</div>
				<div class="check-wrap">
					<label class="input-section custom-switcher ' . esc_attr( $disable ) . '">
					<input type="checkbox" class="switcher-ui-toggle" id="' . esc_attr( $id ) . '"
						name="' . esc_attr( $id ) . '" value="yes"  ' . esc_attr( $checked ) . '
						data-label="' . esc_attr( $data_label ) . '"
						/>
						<span class="slider round"></span>
					</label>
					<span class="ml-1">' . esc_html( $desc ) . '</span>
				</div>
			</div>
		';

		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Custom kses function with allowed form tags
	echo \Discountify\Utils\Helper::kses( $html );
	}
}

/**
 * Select option
 */
if ( ! function_exists( 'discountify_select_field' ) ) {

	function discountify_select_field( $args ) {
		$id           = ! empty( $args['id'] ) ? $args['id'] : '';
		$label        = ! empty( $args['label'] ) ? $args['label'] : '';
		$data_label   = ! empty( $args['data_label'] ) ? $args['data_label'] : '';
		$options_html = '';
		$disable      = ! empty( $args['disable'] ) ? $args['disable'] : '';
		$doc          = ! empty( $args['doc'] ) ? $args['doc'] : '';
		$select_type  = ! empty( $args['select_type'] ) ? $args['select_type'] : '';
		$multi_name   = $select_type === 'multiple' ? '[]' : '';

		if ( 'single' === $args['select_type'] ) {
			$selected = ! empty( $args['selected'] ) ? $args['selected'] : '';
			if ( ! empty( $args['options'] ) ) :
				foreach ( $args['options'] as $key => $item ) :
					$selected_val  = $key === $selected ? 'selected' : '';
					$disabled      = $key === $disable ? 'disabled' : '';
					$options_html .= '<option ' . $disabled . ' value="' . $key . '" ' . $selected_val . '>' . $item . '</option>';
				endforeach;
			endif;
		} else {
			$selected = ! empty( $args['selected'] ) ? $args['selected'] : array();
			if ( ! empty( $args['options'] ) ) :
				foreach ( $args['options'] as $key => $item ) :
					$selected_val  = ( is_array( $selected ) && in_array( $key, $selected ) ) ? ' selected="selected" ' : '';
					$options_html .= '<option ' . $selected_val . ' value="' . $key . '">' . $item . '</option>';
				endforeach;
			endif;
		}

		$condition_class = ! empty( $args['condition_class'] ) ? $args['condition_class'] : '';
		$html            = '
			<div class="single-block ' . $condition_class . '">
				<div class="form-label">' . $label . '</div>
				<div class="input-section ' . $disable . '">
					<select ' . $select_type . ' name="' . $id . $multi_name . '" id="' . $id . '" data-option="' . $data_label . '" ' . '>' . $options_html . '</select>
					' . $doc . '
				</div>
			</div>
		';

		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Custom kses function with allowed form tags
	echo \Discountify\Utils\Helper::kses( $html );
	}
}

if ( ! function_exists( 'discountify_text_area' ) ) {
	function discountify_text_area( $args ) {
		$wrapper_class     = ! empty( $args['wrapper_class'] ) ? $args['wrapper_class'] : 'single-block';
		$condition_class   = ! empty( $args['condition_class'] ) ? $args['condition_class'] : '';
		$value             = ! empty( $args['value'] ) ? $args['value'] : '';
		$docs              = ! empty( $args['docs'] ) ? $args['docs'] : '';
		$extra_label_class = ! empty( $args['extra_label_class'] ) ? $args['extra_label_class'] : '';
		$id                = ! empty( $args['id'] ) ? $args['id'] : '';
		$cols              = ! empty( $args['cols'] ) ? $args['cols'] : 29;
		$disable           = ( ! empty( $args['disable'] ) && $args['disable'] !== false ) ? 'disable' : '';

		$html = '
			<div class="' . esc_attr( $wrapper_class ) . ' ' . esc_attr( $condition_class ) . '">
				<div class="form-label">' . $args['label'] . '</div>
				<div class="input-section ' . $disable . '">
					<textarea name=' . $id . ' rows="4" cols="' . $cols . '" id=' . $id . '>' . $value . '</textarea>
					<div class="extra-label ' . $extra_label_class . '">' . $docs . '</div>
				</div>
			</div>
		';

		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Custom kses function with allowed form tags
	echo \Discountify\Utils\Helper::kses( $html );
	}
}

/**
 * Number/Text/Hidden
 */
if ( ! function_exists( 'discountify_number_input_field' ) ) {
	function discountify_number_input_field( $args ) {
		$id                = ! empty( $args['id'] ) ? $args['id'] : '';
		$value             = ! empty( $args['value'] ) ? $args['value'] : '';
		$label             = ! empty( $args['label'] ) ? $args['label'] : '';
		$field_type        = ! empty( $args['field_type'] ) ? $args['field_type'] : 'text';
		$condition_class   = ! empty( $args['condition_class'] ) ? $args['condition_class'] : '';
		$disable           = ! empty( $args['disable'] ) ? $args['disable'] : '';
		$extra_label       = ! empty( $args['extra_label'] ) ? $args['extra_label'] : '';
		$docs              = ! empty( $args['docs'] ) ? $args['docs'] : '';
		$extra_label_class = ! empty( $args['extra_label_class'] ) ? $args['extra_label_class'] : '';

		$html = '
		<div class="single-block ' . esc_attr( $condition_class ) . '">
			<div class="form-label">' . $label . '</div>
			<div class="input-section ' . $disable . '">
				<div class="input-wrap-one">
					<input type="' . esc_attr( $field_type ) . '" name="' . $id . '" id="' . $id . '" value="' . $value . '" />
					<div class="ml-2 extra-label ' . $extra_label_class . '">' . $extra_label . '</div>
				</div>
				<div class="extra-label docs">' . $docs . '</div>
			</div>
		</div>
		';

		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Custom kses function with allowed form tags
	echo \Discountify\Utils\Helper::kses( $html );
	}
}
/**
 * Repeater field
 *
 * @param [type] $args
 * @return void
 */
if ( ! function_exists( 'discountify_repeater_field' ) ) {
	function discountify_repeater_field( $type, $args ) {
		$id              = ! empty( $args['id'] ) ? $args['id'] : '';
		$name            = ! empty( $args['name'] ) ? $args['name'] : '';
		$label           = ! empty( $args['label'] ) ? $args['label'] : '';
		$repeat_field    = ! empty( $args['repeat_field'] ) ? $args['repeat_field'] : array();
		$condition_class = ! empty( $args['condition_class'] ) ? $args['condition_class'] : '';
		$repeat_html     = '';
		$repeat_html    .= '<div class="repeat-section">';
		foreach ( $repeat_field as $key => $value ) {
			$name_part    = $name . '[' . $key . ']';
			$label_class  = ! empty( $value['label_class'] ) ? $value['label_class'] : '';
			$repeat_label = ! empty( $value['label'] ) ? $value['label'] : '';

			$repeat_condition_class = ! empty( $value['condition_class'] ) ? $value['condition_class'] : '';
			$repeat_html           .= '
			<div class="repeat-block ' . $repeat_condition_class . '">
				<div class="repeat-label ' . $label_class . '">' . $repeat_label . '</div>
				<div class="repeat-input">' . discountify_repeat_input_field( $name_part, $value, $key ) . '</div>
			</div>
			';
		}
		$repeat_html .= '</div>';

		$html = '
		<div class="single-block ' . $condition_class . '" id="' . $id . '">
			<div class="form-label">' . $label . '</div>
			<div id="repeater-' . $type . '" class="repeat-wrapper">
				<div class="createElement" id="createElement-' . $type . '"><span class="dashicons dashicons-plus"></span></div>
				<div class="repeat-section mt-4" id="structure-' . $type . '">
					' . $repeat_html . '
				</div>
				<div class="repeating-block" id="containerElement-' . $type . '"></div>
			</div>
		</div>
		';
		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Custom kses function with allowed form tags
	echo \Discountify\Utils\Helper::kses( $html );
	}
}
if ( ! function_exists( 'discountify_repeat_input_field' ) ) {

	function discountify_repeat_input_field( $name_part, $value, $index ) {
		$html = '';
		$name = $name_part . '[' . $value['id'] . ']';

		$condition_class = ! empty( $value['condition_class'] ) ? $value['condition_class'] : '';
		$input_value     = ! empty( $value['value'] ) ? $value['value'] : '';
		switch ( $value['field_type'] ) {
			case 'hidden':
			case 'number':
			case 'text':
				$extra_label_class = ! empty( $value['extra_label_class'] ) ? $value['extra_label_class'] : '';
				$extra_label       = ! empty( $value['extra_label'] ) ? $value['extra_label'] : '';

				$html = '<input data-name="' . $value['id'] . '" 
				data-name="' . $value['id'] . '" 
				class="' . $value['id'] . '-' . $index . '"
				type="' . $value['field_type'] . '"
				name="' . $name . '" 
				value="' . $input_value . '" 
				/>
				<span class="extra-label ' . $extra_label_class . '">' . $extra_label . '</span>';

				break;
			case 'select':
				$options_html   = '';
				$selected_value = '';
				foreach ( $value['options'] as $key => $item ) {
					$selected_value = $key === $input_value ? 'selected' : '';
					$options_html  .= "<option value='" . esc_attr( $key ) . "'>" . esc_html( $item ) . '</option>';
				}
				$html = '<select ' . $selected_value . ' data-name="' . esc_attr( $value['id'] ) . '"
				class=" ' . esc_attr( $value['id'] ) . '" name="' . $name . '" data-id="' . $index . '">'
				. $options_html . '</select>';
				break;

			case 'select2':
				$options_html   = '';
				$selected_value = '';
				foreach ( $value['options'] as $key => $item ) {
					$selected_value = $key === $input_value ? 'selected' : '';
					$options_html  .= "<option value='" . esc_attr( $key ) . "'>" . esc_html( $item ) . '</option>';
				}
				$html = '<select multiple ' . $selected_value . ' data-name="' . esc_attr( $value['id'] ) . '"
				class=" ' . esc_attr( $value['id'] ) . '" name="' . $name . '[]" data-id="' . $index . '">'
				. $options_html . '</select>';
				break;
			case 'span':
				$html = '<span>' . esc_html( $input_value ) . '</span>';

			default:
				break;
		}

		return $html;
	}
}
