<?php

namespace CarDealerPress\Templates\Admin\Theme;

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

use CarDealerPress\Includes\Database\Requests;
use CarDealerPress\Includes\Helpers\Option;
use CarDealerPress\templates\partials\admin\Table_Rows;

function overrides() {
	$content = '<table>';
	$content .= '<tbody>';
	$content .= '<tr><h4>General Settings</h4></tr>';
	$option  = Option::check_for_option( 'cdp_theme_options', array( 'theme', 'overrides', 'per_page' ) );
	$content .= table_rows::tr_select_dropown(
		array(
			'id'    => 'theme[overrides][per_page]',
			'label' => 'Vehicles Per Page:',
			'name'  => 'cdp_theme_options[theme][overrides][per_page]',
			'value' => $option,
			'list'  => range( 1, 50 ),
		)
	);

	$saleclass = array( 'all', 'new', 'used', 'certified' );
	$option    = Option::check_for_option( 'cdp_theme_options', array( 'theme', 'overrides', 'saleclass' ) );
	$content   .= table_rows::tr_select_dropown(
		array(
			'id'    => 'theme[overrides][saleclass]',
			'label' => 'Sale Class Filter',
			'name'  => 'cdp_theme_options[theme][overrides][saleclass]',
			'value' => $option,
			'list'  => $saleclass,
		)
	);

	$content .= makes_filtered();

	$value   = Option::check_for_option( 'cdp_theme_options', array( 'theme', 'overrides', 'default_no_image' ) );
	$library = '<span id="link-default-no-image" class="td-title"><a id="media-default-no-image" href="#" class="custom_media_upload 
                        custom-media-upload-default-image default-no-image">Media Library</a></span>';
	$content .= table_rows::tr_setting_text(
		array(
			'id'    => 'overrides-default-no-image',
			'class' => 'custom_media_upload_default_image',
			'label' => 'Default No Image',
			'name'  => 'cdp_theme_options[theme][overrides][default_no_image]',
			'value' => $value,
			'notes' => $library,
		)
	);

	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'overrides',
			'custom_contact',
			'phone',
			'new',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'Company Phone New',
			'id'    => 'theme[overrides][custom_contact][phone][new]',
			'class' => 'input-long',
			'name'  => 'cdp_theme_options[theme][overrides][custom_contact][phone][new]',
			'value' => $value,
		)
	);
	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'overrides',
			'custom_contact',
			'phone',
			'used',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'Company Phone Used',
			'class' => 'input-long',
			'id'    => 'theme[overrides][custom_contact][phone][used]',
			'name'  => 'cdp_theme_options[theme][overrides][custom_contact][phone][used]',
			'value' => $value,
		)
	);

	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'overrides',
			'custom_contact',
			'name',
			'new',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'Company Name New',
			'id'    => 'custom-contact-name-new',
			'class' => 'input-long',
			'name'  => 'cdp_theme_options[theme][overrides][custom_contact][name][new]',
			'value' => $value,
		)
	);

	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'overrides',
			'custom_contact',
			'name',
			'used',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'Company Name Used',
			'id'    => 'custom-contact-name-used',
			'class' => 'input-long',
			'name'  => 'cdp_theme_options[theme][overrides][custom_contact][name][used]',
			'value' => $value,
		)
	);

	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'overrides',
			'custom_contact',
			'breadcrumb',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'Company Breadcrumb',
			'id'    => 'theme[custom_contact][breadcrumb]',
			'class' => 'input-long',
			'name'  => 'cdp_theme_options[theme][overrides][custom_contact][breadcrumb]',
			'value' => $value,
		)
	);

	$value   = Option::check_for_option( 'cdp_theme_options', array( 'theme', 'overrides', 'disclaimer' ) );
	$content .= table_rows::tr_setting_textarea(
		array(
			'label' => 'Company Disclaimer',
			'id'    => 'theme[overrides][disclaimer]',
			'name'  => 'cdp_theme_options[theme][overrides][disclaimer]',
			'value' => $value,
		)
	);
	$content .= '</tbody>';
	$content .= '</table>';

	return $content;
}

function makes_filtered(): string {
	$makes_available = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'overrides',
			'makes_new',
			'available',
		),
		array()
	);
	$makes_included  = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'overrides',
			'makes_new',
			'included',
		),
		array()
	);

	$makes_db = ( new Requests() )->cdp_db_init( 'make', array( 'saleclass' => 'new' ) );
	foreach ( $makes_db as $make ) {
		$db_makes[] = $make['Make'];
	}
	if ( ! empty( $db_makes ) ) {
		foreach ( $db_makes as $make ) {
			if ( in_array( $make, $makes_included, true ) ) {
				$makes_included[] = $make;
			} else {
				$makes_available[] = $make;
			}
		}
	}

	$content = '<tr class="cdp-row">';
	$content .= '<th><label>New Makes Available</label></th>';
	$content .= '<td>';
	$content .= '<select id="makes-available" class="basic-multiple keywords exclude" name="cdp_theme_options[theme][overrides][makes_new][available][]" multiple="multiple" data-keyword="exclude">';
	$content .= populate_select_option_data( array_unique( $makes_available ) );
	$content .= '</select>';
	$content .= '</td>';
	$content .= '</tr>';

	$content .= '<tr class="cdp-row">';
	$content .= '<th><label>New Makes to only Included</label></th>';
	$content .= '<td>';
	$content .= '<select id="makes-included" class="basic-multiple keywords pot" name="cdp_theme_options[theme][overrides][makes_new][included][]" multiple="multiple" data-keyword="pot" style="width:90%;">';
	$content .= populate_select_option_data( array_unique( $makes_included ) );
	$content .= '</select>';
	$content .= '</td>';
	$content .= '<td>Remove to make available.</td>';
	$content .= '</tr>';

	return $content;
}

function populate_select_option_data( $data ) {
	$content = '';
	if ( ! empty( $data ) ) {
		foreach ( $data as $item ) {
			$content .= '<label for=' . $item . '>';
			$content .= '<option value="' . $item . '" selected/>' . $item . '</option>';
		}
	}

	return $content;
}

