<?php

namespace CarDealerPress\templates\admin\theme\inventory_display;

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

use CarDealerPress\Includes\Helpers\{Option};
use CarDealerPress\templates\partials\admin\Display;
use CarDealerPress\templates\partials\admin\Table_Rows;

function vehicle_filtering() {
	$content = '<table>';
	$content .= '<tbody>';
	$content .= '<tr><th><h2>Search Filter Settings</h2></th></tr>';
	if ( Display::theme_check() ) {
		$value            = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'list',
				'search_filters',
				'general',
				'search_view',
			)
		);
		$search_view_list = array( 'top', 'side' );
		$content          .= table_rows::tr_select_dropown(
			array(
				'label' => 'Search View',
				'id'    => 'search_view',
				'name'  => 'cdp_theme_options[theme][inventory_display][list][search_filters][general][search_view]',
				'value' => $value,
				'list'  => $search_view_list,
			)
		);

		$value         = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'list',
				'search_filters',
				'general',
				'srp_view',
			)
		);
		$srp_view_list = array( 'list', 'grid' );
		$content       .= table_rows::tr_select_dropown(
			array(
				'label' => 'SRP View',
				'id'    => 'srp_view',
				'name'  => 'cdp_theme_options[theme][inventory_display][list][search_filters][general][srp_view]',
				'value' => $value,
				'list'  => $srp_view_list,
			)
		);
	}

	$steps = [ 'price', 'odometer' ];
	foreach ( $steps as $step ) {
		$value   = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'list',
				'search_filters',
				'general',
				"{$step}_step",
			)
		);
		$content .= table_rows::tr_setting_text(
			array(
				'id'    => $step,
				'label' => ucfirst( $step ) . " Step",
				'name'  => "cdp_theme_options[theme][inventory_display][list][search_filters][general][{$step}_step]",
				'value' => $value,
				'type'  => 'number',
				'class' => 'input-medium'
			)
		);
	}

	$content .= '</tbody>';
	$content .= '</table>';

	$content .= '<hr>';
	$content .= '<div class="note">Drag and drop to reorder attributes, Press SAVE to finalize order.</div>';

	$content         .= '<table class="drag-table draggable-table">';
	$content         .= '<thead>';
	$content         .= '<th scope="col"></th>'; // position
	$content         .= '<th scope="col">Name</th>';
	$content         .= '<th scope="col">Display Type</th>';
	$content         .= '<th scope="col">Display</th>';
	$content         .= '<th scope="col">Display Label</th>';
	$content         .= '</thead>';
	$content         .= '<tbody>';
	$filters         = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'search_filters',
			'general',
			'filters',
		)
	);
	$vehicle_filters = [ 'saleclass', 'vehicleclass', 'body', 'make', 'model', 'trim' ];
	foreach ( $filters as $index => $filter ) {
		$name     = $filter['name'];
		$content  .= '<tr>';
		$content  .= '<td><span class="dashicons dashicons-move"></span></td>';
		$position = $filter['position'];
		$content  .= table_rows::td_setting_text(
			array(
				'id'       => "filter_position_{$name}",
				'name'     => "cdp_theme_options[theme][inventory_display][list][search_filters][general][filters][$index][position]",
				'value'    => $position ?? $index,
				'type'     => 'number',
				'class'    => 'input-position hidden',
				'min'      => 1,
				'td_class' => 'hidden',
			)
		);
		$tooltip  = '';
		if ( 'color' === $filter['name'] ) {
			$tooltip = '<span class="tooltip"><span class="dashicons dashicons-info-outline"></span><div class="right">This will update on the next import run, unless you Manually Import Purge</div></span>';
		}
		$content .= '<td><div>' . ucwords( str_replace( "_", " ", $filter['name'] ) ) . $tooltip . '</div>';
		$content .= "<input hidden type='text' name='cdp_theme_options[theme][inventory_display][list][search_filters][general][filters][$index][name]' value=" . $filter['name'] . " class='input-attribute-label'>";
		$content .= '</td>';

		if ( preg_match( '/year|price|odometer/', $name ) ) {
			$display_type_dropdown = false;
			$display_type_slider   = false;
			if ( 'dropdown' === $filter['display_type'] ) {
				$display_type_dropdown = true;
			} else {
				$display_type_slider = true;
			}

			$content .= '<td id="search-filter-display-type">';
			$content .= table_rows::tr_setting_radio(
				array(
					'label' => 'Dropdown',
					'id'    => "filter_type_{$name}_dropdown",
					'for'   => "filter_type_{$name}_dropdown",
					'name'  => "cdp_theme_options[theme][inventory_display][list][search_filters][general][filters][$index][display_type]",
					'value' => 'dropdown',
					'var'   => $display_type_dropdown,
				)
			);
			$content .= table_rows::tr_setting_radio(
				array(
					'label' => 'Slider',
					'id'    => "filter_type_{$name}_slider",
					'for'   => "filter_type_{$name}_slider",
					'name'  => "cdp_theme_options[theme][inventory_display][list][search_filters][general][filters][$index][display_type]",
					'value' => 'slider',
					'var'   => $display_type_slider,
				)
			);
			$content .= '</td>'; //display-type
		} else {
			$content .= '<td></td>';
		}

		$checked = $filter['display'];
		$content .= table_rows::td_setting_checkbox(
			array(
				'id'      => "display_{$name}_{$index}",
				'class'   => 'checkbox',
				'name'    => "cdp_theme_options[theme][inventory_display][list][search_filters][general][filters][$index][display]",
				'checked' => $checked,
			)
		);
		//if ( !preg_match( '//', $name ) ) {
		$checked = $filter['display_label'];
		$content .= table_rows::td_setting_checkbox(
			array(
				'id'      => "display_{$name}_{$index}",
				'class'   => 'checkbox',
				'name'    => "cdp_theme_options[theme][inventory_display][list][search_filters][general][filters][$index][display_label]",
				'checked' => $checked,
			)
		);
		//}


		$content .= '</tr>';
	}
	$content .= '</tbody>';
	$content .= '</table>';

	return $content;
}

function geo_settings() {
	$content = '<table>';
	$content .= '<tbody>';
	$checked = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'search_filters',
			'geo_search',
			'display_geo',
		)
	);
	$content .= table_rows::tr_setting_checkbox(
		array(
			'id'      => 'display_geo',
			'label'   => 'Display Geo (State/City) search',
			'name'    => 'cdp_theme_options[theme][inventory_display][list][search_filters][geo_search][display_geo]',
			'checked' => $checked,
		)
	);

	if ( Display::theme_check() ) {
		$value_city = $value_add_dealer = $value_only_dealer = $value_default = false;
		if ( Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'list',
				'search_filters',
				'geo_search',
				'geo_options',
			)
		)
		) {
			$value = Option::check_for_option(
				'cdp_theme_options',
				array(
					'theme',
					'inventory_display',
					'list',
					'search_filters',
					'geo_search',
					'geo_options',
				)
			);
			switch ( $value ) {
				case 'geo_hide_cities':
					$value_city = true;
					break;
				case 'geo_add_dealer_names':
					$value_add_dealer = true;
					break;
				case 'geo_dealer_names_only':
					$value_only_dealer = true;
					break;
				default:
					$value_default = true;
					break;
			}
		}
		$content .= '<tr>';
		$content .= '<th><label>Geo Options</label></th>';
		$content .= '<td>';
		$content .= table_rows::tr_setting_radio(
			array(
				'id'    => 'geo_hide_cities',
				'label' => 'Hide Cities',
				'for'   => 'geo-hide-cities',
				'name'  => 'cdp_theme_options[theme][inventory_display][list][search_filters][geo_search][geo_options]',
				'value' => 'geo_hide_cities',
				'var'   => $value_city,
			)
		);
		$content .= '</td>';
		$content .= '<td>';
		$content .= table_rows::tr_setting_radio(
			array(
				'id'    => 'geo_add_dealer_names',
				'label' => 'Add Dealer Name',
				'for'   => 'geo-dealer-names',
				'name'  => 'cdp_theme_options[theme][inventory_display][list][search_filters][geo_search][geo_options]',
				'value' => 'geo_add_dealer_names',
				'var'   => $value_add_dealer,
			)
		);
		$content .= '</td>';
		$content .= '<td>';
		$content .= table_rows::tr_setting_radio(
			array(
				'id'    => 'geo_dealer_names_only',
				'label' => 'Only Display Dealer Name',
				'for'   => 'geo-default',
				'name'  => 'cdp_theme_options[theme][inventory_display][list][search_filters][geo_search][geo_options]',
				'value' => 'geo_dealer_names_only',
				'var'   => $value_only_dealer,
			)
		);
		$content .= '</td>';
		$content .= '<td>';
		$content .= table_rows::tr_setting_radio(
			array(
				'id'    => 'geo_default',
				'label' => 'Default',
				'for'   => 'geo-default',
				'name'  => 'cdp_theme_options[theme][inventory_display][list][search_filters][geo_search][geo_options]',
				'value' => 'geo_default',
				'var'   => $value_default,
			)
		);
		$content .= '</td></tr>';
	} else {
		$checked = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'list',
				'search_filters',
				'geo_search',
				'add_geo_zip',
			)
		);
		$content .= table_rows::tr_setting_checkbox(
			array(
				'id'      => 'add_geo_zip',
				'label'   => 'Add Geo Zip to Search',
				'name'    => 'cdp_theme_options[theme][inventory_display][list][search_filters][geo_search][add_geo_zip]',
				'checked' => $checked,
			)
		);
	}
	$content .= '</tbody></table>';

	return $content;
}
