<?php

namespace CarDealerPress\templates\admin\theme\inventory_display\list_page;

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

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

function sort() {
	$option  = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'sort',
			'custom',
		)
	);
	$count   = Generic::get_count_of( $option );
	$content = '';

	if ( 0 === $count ) {
		$row_id = 0;
	} else {
		$row_id = $count;
	}

	$content .= default_sort();
	$content .= '<hr>';
	if ( ! empty( $option ) ) {
		$content .= current_sort( $option );
		$content .= '<hr>';
	}
	$content .= create_sort( 'Create', $row_id );

	return $content;
}

function default_sort(): string {
	$content    = '<section id="default">';
	$content    .= '<table>';
	$content    .= '<tbody>';
	$content    .= '<tr><th><h2>Default Sort Settings</h2></th></tr>';
	$value      = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'sort',
			'default',
		)
	);
	$list       = array( 'makeModel', 'makeModelD', 'createdA', 'createdD', 'yearA', 'yearD', 'priceA', 'priceD' );
	$list_human = array(
		'Make/Model A -> Z',
		'Make/Model Z -> A',
		'Created Date Low -> High',
		'Created Date High -> Low',
		'Year Low -> High',
		'Year High -> Low',
		'Price Low -> High',
		'Price High -> Low',
	);
	$content    .= table_rows::tr_select_dropown(
		array(
			'label'      => 'Default Sort Filter',
			'id'         => 'sortCreateDefault',
			'name'       => 'cdp_theme_options[theme][inventory_display][list][sort][default]',
			'value'      => $value,
			'list'       => $list,
			'list_human' => $list_human,
		)
	);
	$content    .= '</tbody>';
	$content    .= '</table>';
	$content    .= '</section>';

	return $content;
}

function current_sort( $option ): string {
	$content = '<section id="current">';
	$content .= '<div><h3>Current Sorts</h3></div>';
	$content .= '<div id="SortRows" class="inner-table-content">';
	$content .= '<table id="current-sort-table">';
	foreach ( $option as $row_id => $sort ) {
		$id_value   = 'sort-' . $row_id;
		$name       = $sort['name'];
		$sort_value = $sort['sort_value'];
		$content    .= "<tr id='outer-$row_id' data-outer='outer-$row_id' class='outer-row' data-label-name='$name' data-option='theme' data-tab='inventory_display' data-section='list' data-section1='sort' data-section2='custom'></tr>";
		$content    .= "<td><span>Name: $name, Sort Order: $sort_value</span> ";
		$content    .= "<input id='edit-$row_id' class='edit-row' data-row-id='$row_id' type='button' value='Edit'/>";
		$content    .= "<input id='removeTableRow' class='delete-row' data-row-id='$row_id' type='button' value='Delete'/><td>";
		$content    .= '</tr>';
		$content    .= "<tr id='inner-$row_id' class='detail inner-row'>";
		$content    .= '<td>';
		$content    .= '<div class="">';
		$content    .= "<table class='table-wrap current table-$row_id'>";
		$content    .= '<tbody>';
		$content    .= sort_rows( $row_id, true );
		$content    .= '</table>';
		$content    .= '</div>';
		$content    .= '</td>';
		$content    .= '</tr>';
	}
	$content .= '</table>';
	$content .= '</div>';
	$content .= '</section>';

	return $content;
}

function create_sort( $title, $row_id ): string {
	$content = '<section id="new">';
	$content .= '<h3>' . $title . '</h3>';
	$content .= "<input id='addNewTableButton' class='add-row toggle-row' type='button' value='Add +'/>";
	$content .= "<div class='table-content' style='display:none;'></div>";
	$script  = '<script>jQuery("#addNewTableButton").click(function (e) { jQuery(".table-content").append("' . new_sort( $row_id ) . '"); });</script>';
	$content .= $script;
	$content .= '</section>';

	return $content;
}

function new_sort( $row_id ): string {
	$content = "<table id='addNewTable' class='table-wrap add table-$row_id'>";
	$content .= '<tbody>';
	$content .= sort_rows( $row_id );
	$content .= '</tbody>';
	$content .= '</table>';

	return $content;
}

function sort_rows( $row_id, $show = false ): string {

	// Sale Class.
	$saleclass  = array( 'all', 'new', 'used', 'certified' );
	$sale_class = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'sort',
			'custom',
			$row_id,
			'saleclass',
		)
	);
	$content    = table_rows::tr_select_dropown(
		array(
			'label' => 'Sale Class',
			'id'    => "sort-saleclass-$row_id",
			'class' => 'cdp_input sort-filter',
			'data'  => "data-name = 'saleclass'",
			'name'  => 'cdp_theme_options[theme][inventory_display][list][sort][custom][' . $row_id . '][saleclass]',
			'value' => $sale_class,
			'list'  => $saleclass,
		)
	);

	$make    = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'sort',
			'custom',
			$row_id,
			'make',
		)
	);
	$content .= table_rows::tr_select_dropown(
		array(
			'label' => 'Select a Make',
			'id'    => "sort-make-$row_id",
			'class' => 'cdp_input sort-filter',
			'data'  => "data-name = 'make'",
			'name'  => 'cdp_theme_options[theme][inventory_display][list][sort][custom][' . $row_id . '][make]',
			'value' => $make,
			'list'  => Generic::select_attr_list(
				array(
					'key'  => 'make',
					'text' => 'Make',
				)
			),
		)
	);

	$model   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'sort',
			'custom',
			$row_id,
			'model',
		)
	);
	$content .= table_rows::tr_select_dropown(
		array(
			'label' => 'Select a Model',
			'id'    => "sort-model-$row_id",
			'class' => 'cdp_input sort-filter',
			'data'  => "data-name = 'model'",
			'name'  => 'cdp_theme_options[theme][inventory_display][list][sort][custom][' . $row_id . '][model]',
			'value' => $model,
			'list'  => Generic::select_attr_list(
				array(
					'key'  => 'model',
					'text' => 'Model',
				)
			),
		)
	);

	$year    = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'sort',
			'custom',
			$row_id,
			'year',
		)
	);
	$content .= table_rows::tr_select_dropown(
		array(
			'label' => 'Select a Year',
			'id'    => "sort-year-$row_id",
			'class' => 'cdp_input sort-filter',
			'data'  => "data-name = 'year'",
			'name'  => 'cdp_theme_options[theme][inventory_display][list][sort][custom][' . $row_id . '][year]',
			'value' => $year,
			'list'  => Generic::select_attr_list(
				array(
					'key'  => 'year',
					'text' => 'Year',
				)
			),
		)
	);

	$vehicle_class = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'sort',
			'custom',
			$row_id,
			'vehicleclass',
		)
	);
	$content       .= table_rows::tr_select_dropown(
		array(
			'label' => 'Select a Vehicle Class',
			'id'    => "sort-vehicle-class-$row_id",
			'class' => 'cdp_input sort-filter',
			'data'  => "data-name = 'vehicleclass'",
			'name'  => 'cdp_theme_options[theme][inventory_display][list][sort][custom][' . $row_id . '][vehicleclass]',
			'value' => $vehicle_class,
			'list'  => Generic::select_attr_list(
				array(
					'key'  => 'vehicleclass',
					'text' => 'Vehicle Class',
				)
			),
		)
	);

	$tags      = Main::$db_request->cdp_db_init( 'tags', array() );
	$tags_list = Generic::build_list(
		array(
			'data'         => $tags,
			'first_option' => 'Select a Tag',
		)
	);
	$tag       = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'sort',
			'custom',
			$row_id,
			'tag',
		)
	);
	$content   .= table_rows::tr_select_dropown(
		array(
			'label' => 'Select a Tag',
			'id'    => "sort-tag-$row_id",
			'class' => 'cdp_input sort-tag sort-filter',
			'data'  => "data-name = 'tags' data-row = '$row_id'",
			'name'  => 'cdp_theme_options[theme][inventory_display][list][sort][custom][' . $row_id . '][tags]',
			'value' => $tag,
			'list'  => $tags_list,
		)
	);

	$dealers = Main::$db_request->cdp_db_init( 'dealers', array( 'pass' => '1' ) );
	// Dealers.
	if ( $dealers && count( $dealers ) > 1 ) {
		foreach ( $dealers as $dealer ) {
			$dealers_list[] = $dealer['Dealer'];
		}
		$dealers_name_list = Generic::build_list(
			array(
				'data'         => $dealers_list,
				'first_option' => 'Select a Dealer',
			)
		);
		$dealer            = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'list',
				'sort',
				'custom',
				$row_id,
				'dealer',
			)
		);
		$content           .= table_rows::tr_select_dropown(
			array(
				'label' => 'Select a Dealer',
				'id'    => "sort-dealer-$row_id",
				'class' => 'cdp_input sort-dealer sort-filter',
				'data'  => "data-name = 'dealer' data-row = '$row_id'",
				'name'  => 'cdp_theme_options[theme][inventory_display][list][sort][custom][' . $row_id . '][dealer]',
				'value' => $dealer,
				'list'  => $dealers_name_list,
			)
		);
	}

	$filters = array(
		'saleclass'    => $sale_class,
		'make'         => $make,
		'model'        => $model,
		'year'         => $year,
		'vehicleclass' => $vehicle_class,
		'tags'         => $tag,
	);
	$count   = ( new Requests() )->cdp_admin_sort_count( array_filter( $filters ) );
	$content .= "<tr id='found-$row_id'><td>Found <span class='current-sort-count-value'>($count)</span></td></tr>";

	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'sort',
			'custom',
			$row_id,
			'sort_value',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label'    => 'Sort Value',
			'id'       => "sort-value-$row_id",
			'name'     => 'cdp_theme_options[theme][inventory_display][list][sort][custom][' . $row_id . '][sort_value]',
			'value'    => $value,
			'type'     => 'number',
			'class'    => 'input-medium',
			'min'      => '0',
			'required' => 'true',
		)
	);

	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'sort',
			'custom',
			$row_id,
			'name',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label'    => 'Name',
			'id'       => "name-$row_id",
			'name'     => 'cdp_theme_options[theme][inventory_display][list][sort][custom][' . $row_id . '][name]',
			'value'    => $value,
			'class'    => 'input-long',
			'required' => 'true',
		)
	);
	$content .= table_rows::edit_update( $show, $row_id );

	return $content;

}

