<?php

namespace CarDealerPress\templates\partials\admin;

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

class Table_Rows {
	public static function tr_button( $args ): string {
		$id       = isset( $args['id'] ) ? 'id="' . $args['id'] . '"' : '';
		$content  = "<tr class='cdp-row'>";
		$content .= "<th><label>$args[label]</label></th>";
		$content .= "<td><input $id class='cdp-run-input button' name='$args[name]' type='button' value='$args[value]'/></td>";
		$content .= '</tr>';

		return $content;
	}

	public static function tr_info_div( $args ): string {
		$id    = isset( $args['id'] ) ? ' id="' . $args['id'] . '"' : '';
		$class = isset( $args['class'] ) ? ' class="' . $args['class'] . '"' : 'class="input-short"';
		$name  = isset( $args['name'] ) ? ' name="' . $args['name'] . '"' : '';
		$note  = $args['note'] ?? '';

		$content = "<tr><th><label>$args[label]</label></th>";
		if ( ! empty( $args['tooltip'] ) ) {
			$tooltip_position = $args['tooltip']['position'] ?? '';
			$text             = $args['tooltip']['text'];
			$content         .= "<td>$args[textString]<span class='tooltip'><span class='dashicons dashicons-info-outline'></span><div class='$tooltip_position'>$text</div></span></td>";
		} else {
			$content .= "<td><div $name $id $class>$args[textString]</div><small>$note</small></td>";
		}
		$content .= '</tr>';

		return $content;
	}

	public static function tr_setting_checkbox( $args ): string {
		$id   = $args['id'] ?? '';
		$name = $args['name'] ?? '';
		$value = $args['checked'] ?? '';
		if ( $args['checked'] ?? 'on' ) {
			$checked = 'checked="checked"';
		} else {
			$checked = '';
		}

		$content  = "<tr class='cdp-row'>";
		$content .= '<th>' . $args['label'] . '</th>';
		$content .= "<td class='checkbox'><fieldset class='checkbox test tr setting'><input type='checkbox' id='$id' name='$name'  $checked value='$value' class='cdp-input' />";
		$content .= "<input type='hidden' name='$args[name]' value='$args[checked]'/>";
		$content .= '</fieldset></td>';

		if ( ! empty( $args['notes'] ) ) {
			$content .= "<td class='td-note'><small>$args[notes]</small></td>";
		}
		if ( ! empty( $args['tooltip'] ) ) {
			$tooltip_position = $args['tooltip']['position'];
			$text             = $args['tooltip']['text'];
			$content         .= "<td><span class='tooltip'><span class='dashicons dashicons-editor-help'></span><div class='$tooltip_position'>$text</div></span></td>";
		}
		$content .= '</tr>';

		return $content;
	}

	public static function tr_setting_radio( $args ): string {
		$checked  = true === $args['var'] ? 'checked' : '';
		$content  = "<input type='radio' id='$args[id]' name='$args[name]' value='$args[value]' $checked />";
		$content .= "<label for='$args[for]'>$args[label]</label>";

		return $content;
	}

	public static function tr_setting_text( $args ): string {
		$required    = empty( $args['required'] ) ? '' : ( 'true' === $args['required'] ? 'required' : '' );
		$row_class   = $args['row_class'] ?? '';
		$class       = empty( $args['class'] ) ? 'input-short' : $args['class'];
		$type        = empty( $args['type'] ) ? 'text' : $args['type'];
		$min         = isset( $args['min'] ) ? "min='$args[min]'" : '';
		$placeholder = isset( $args['placeholder'] ) ? " placeholder='$args[placeholder]' " : '';

		$content  = "<tr class='cdp-row $row_class '>";
		$content .= "<th><label>$args[label]</label></th>";

		$content .= "<td class='textbox'><input $min type='$type' id='$args[id]' name='$args[name]' $placeholder";
		if ( ! empty( $args['pattern'] ) ) {
			$content .= " pattern='$args[pattern]' ";
		}
		if ( ! empty( $args['step'] ) ) {
			$content .= " step='$args[step]' ";
		}
		$content .= "value='". rawurldecode( stripslashes( $args['value'] ) ) . "' class='$class' $required/></td>";
		if ( ! empty( $args['notes'] ) ) {
			$content .= "<td class='td-note'><small>$args[notes]</small></td>";
		}
		if ( ! empty( $args['tooltip'] ) ) {
			$tooltip_position = $args['tooltip']['position'];
			$text             = $args['tooltip']['text'];
			$content         .= "<td><span class='tooltip'><span class='dashicons dashicons-editor-help'></span><div class='$tooltip_position'>$text</div></span></td>";		}
		$content .= '</tr>';

		return $content;
	}

	public static function tr_setting_textarea( $args ): string {
		$type      = $args['type'] ?? 'textarea';
		$row_class = $args['row_class'] ?? '';
		$disabled  = $args['disabled'] ?? '';
		$content   = "<tr id='$args[id]' class='cdp-row $row_class'>";
		$content  .= "<th><label>$args[label]</label></th>";
		$content  .= "<td class='textarea'>";
		$content  .= "<textarea type='$type' name='$args[name]' rows='4' cols='50' class='text-inject' $disabled>";
		$content  .= rawurldecode( stripslashes( $args['value'] ) );
		$content  .= '</textarea>';
		$content  .= '</td>';
		if ( ! empty( $args['notes'] ) ) {
			$content .= "<td class='td-note'><small>$args[notes]</small></td>";
		}
		if ( ! empty( $args['tooltip'] ) ) {
			$tooltip_position = $args['tooltip']['position'] ?? 'right';
			$text             = $args['tooltip']['text'];
			$icon             = $args['tooltip']['icon'] ?? 'dashicons-editor-help';
			$content         .= "<td><span class='tooltip'><span class='dashicons $icon'></span><div class='$tooltip_position'>$text</div></span></td>";
		}
		$content .= '</tr>';

		return $content;
	}

	public static function tr_select_dropown( $args ): string {
		$list     = $args['list'];
		$class    = $args['class'] ?? 'cdp_input';
		$multiple = isset( $args['multiple'] ) ? "multiple='multiple'" : '';
		$data     = array_key_exists( 'data', $args ) ? $args['data'] : '';
		$content  = "<tr class='cdp-row'>";
		$content .= "<th><label>$args[label]</label></th>";
		$content .= '<td>';
		$content .= "<select id='$args[id]' name='$args[name]' class='$class' $multiple $data>";
		if ( isset( $args['list_human'] ) ) {
			$list_human = $args['list_human'];
			foreach ( $list_human as $key => $item ) {
				$selected = ( strtolower( $list[ $key ] ) === strtolower( $args['value'] ) ) ? 'selected' : null;
				$content .= "<option $selected value='$list[$key]'>$item</option>";
			}
		} else {
			foreach ( $list as $item ) {
				$selected = ( strtolower( $item ) === strtolower( $args['value'] ) ) ? 'selected' : null;
				if ( str_contains( $item, 'Select' ) ) {
					$value = '';
				} else {
					$value = $item;
				}
				$text     = $item;
				$content .= "<option $selected value='$value'>".ucfirst( $text )."</option>";
			}
		}

		$content .= '</select>';
		$content .= '</td>';
		$content .= '</tr>';

		return $content;
	}

	public static function tr_button_with_confirmation( $args ) {
		$id                = $args['id'] ?? '';
		$class             = $args['class'] ?? 'do-action-button';
		$value             = $args['value'] ?? '';
		$message           = $args['message'] ?? '';
		$ajax_fn           = $args['ajax_fn'] ?? '';
		$tooltip           = $args['tooltip'] ?? '';
		$admin_plugins_url = admin_url( 'plugins.php' );

		$content  = '<tr><td class="td-full center">';
		$content .= "<div id='$id' class='$class'>";
		$content .= "<input type='button' value='$value' name='$admin_plugins_url' data-fn='$ajax_fn' data-message='$message'>";
		$content .= '</div>';
		$content .= '</td></tr>';
		return $content;
	}

	public static function edit_update( $show, $row_id ): string {
		$content = '';
		if ( $show ) {
			$content  = '<tr>';
			$content .= "<td><input id='updateTableRow' class='update-row button' name='$row_id' type='button' value='Update'/></td>";
			$content .= "<td><input class='close-row button' name='$row_id' type='button' value='Close'/></td>";
			$content .= '</tr>';
		}

		return $content;
	}

	public static function td_setting_text( $args ): string {
		$td_class    = empty( $args['td_class'] ) ? 'textbox' : $args['td_class'];
		$id          = empty( $args['id'] ) ? '' : $args['id'];
		$required    = empty( $args['required'] ) ? '' : ( 'true' === $args['required'] ? 'required' : '' );
		$class       = empty( $args['class'] ) ? 'input-short' : $args['class'];
		$type        = empty( $args['type'] ) ? 'text' : $args['type'];
		$min         = isset( $args['min'] ) ? "min='$args[min]'" : '';
		$placeholder = isset( $args['placeholder'] ) ? ' placeholder="' . $args['placeholder'] . '" ' : '';
		$attr        = $args['attr'] ?? '';

		$content = "<td class='$td_class'><input $min type='$type' id='$id' name='$args[name]' $placeholder $attr";
		if ( ! empty( $args['pattern'] ) ) {
			$content .= " pattern='$args[pattern]' ";
		}
		if ( ! empty( $args['step'] ) ) {
			$content .= " step='$args[step]' ";
		}
		$content .= " value='". rawurldecode( stripslashes( $args['value'] ) ) . "' class='$class' $required /></td>";

		return $content;
	}

	public static function td_setting_checkbox( $args ): string {
		$id = $args['id'] ?? '';
		$class = $args['class'] ?? 'checkbox';
		if ( $args['checked'] ?? 'on' ) {
			$checked = "checked='checked'";
		} else {
			$checked = '';
		}

		$content  = "<td class='$class'><fieldset class='checkbox'>";
		$content .= "<input type='checkbox' id='$id' name='$args[name]' $checked value='$args[checked]' class='cdp-input' />";
		$content .= "<input type='hidden' name='$args[name]' value='$args[checked]'/>";
		$content .= '</fieldset></td>';

		return $content;
	}
}
