<?php

namespace CarDealerPress\Templates\Admin\Admin;

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

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

function import_filters(): string {
	$option  = Option::check_for_option( 'cdp_admin_options', array( 'admin', 'advanced', 'import_filters' ) );
	$count   = \CarDealerPress\Includes\Helpers\Generic::get_count_of( $option );
	$content = '';

	if ( 0 === $count ) {
		$row_id = 0;
	} else {
		$row_id = $count;
	}
	if ( ! empty( $option ) ) {
		$content .= current_filters( $option );
		$content .= '<hr>';
	}
	$content .= create_filter( 'Add New Filter', $row_id );

	return $content;
}

function current_filters( $option ): string {
	$content = "<section id='current'>";
	$content .= "<div><h3>Current Filters</div>";
	$content .= "<div id='current-rows' class='inner-table-content'>";
	$content .= "<table id='current-table' class=''>";
	$content .= "<tbody>";
	foreach ( $option as $row_id => $item ) {
		$id_value  = 'filter-' . $row_id;
		$dealer_id = $item['dealer_id'];
		$content   .= "<tr id='outer-$row_id' data-tag='outer-$row_id' class='outer-row' data-label-name='$dealer_id' data-option='admin' data-tab='advanced' data-section='import_filters'></tr>";
		$content   .= "<td>Dealer Id: $dealer_id ";
		$content   .= "<input id='edit-$row_id' class='edit-row' name='$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' data-tag='inner-$row_id' class='detail inner-row'>";
		$content   .= "<td>";
		$content   .= "<div class=''>";
		$content   .= "<table class='table-wrap current'>";
		$content   .= "<tbody>";
		$content   .= filter_row( $row_id, true );
		$content   .= "</tbody>";
		$content   .= "</table>";
		$content   .= "</div>";
		$content   .= "</td>";
		$content   .= "</tr>";
	}
	$content .= "</tbody>";
	$content .= "</table>";
	$content .= "</div>";
	$content .= "</section>";

	return $content;
}

function create_filter( $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("' . filters_add_table( $row_id ) . '"); });</script>';
	$content .= $script;
	$content .= '</section>';

	return $content;
}

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

	return $content;
}

function filter_row( $row_id, $show = false ): string {
	// $value = Option::check_for_option('cdp_admin_options', array('admin', 'advanced', 'import_filters', $row_id, 'name'));
	// $content = table_rows::tr_setting_text(array('label' => 'Name', 'id' => "name-$row_id",
	// 'name' => 'cdp_admin_options[admin][advanced][import_filters][' . $row_id . '][name]', 'value' => $value));

	$dealers     = ( new Requests() )->cdp_db_init( 'company', array( 'pass' => '1' ) );
	$dealer_list = Generic::build_list(
		array(
			'data'         => $dealers,
			'key'          => 'dealer_id',
			'first_option' => 'Select Dealer',
		)
	);
	$value       = Option::check_for_option(
		'cdp_admin_options',
		array(
			'admin',
			'advanced',
			'import_filters',
			$row_id,
			'dealer_id',
		)
	);
	$content     = table_rows::tr_select_dropown(
		array(
			'label' => 'Dealer ID',
			'id'    => "dealer_id-$row_id",
			'name'  => 'cdp_admin_options[admin][advanced][import_filters][' . $row_id . '][dealer_id]',
			'value' => $value,
			'list'  => $dealer_list,
		)
	);

	// Sale Class TODO: FUTURE find/define saleclass
	// no existing function for saleclass, should there be?
	// $saleclass = Requests::$db_request->cdp_db_init('saleclass', array('pass'=>'1'));
	$saleclass = array( 'all', 'new', 'used', 'certified' );
	$value     = Option::check_for_option(
		'cdp_admin_options',
		array(
			'admin',
			'advanced',
			'import_filters',
			$row_id,
			'saleclass',
		)
	);
	$content   .= table_rows::tr_select_dropown(
		array(
			'label' => 'Sale Class',
			'id'    => "$row_id-saleclass",
			'name'  => 'cdp_admin_options[admin][advanced][import_filters][' . $row_id . '][saleclass]',
			'value' => $value,
			'list'  => $saleclass,
		)
	);

	$value   = Option::check_for_option(
		'cdp_admin_options',
		array(
			'admin',
			'advanced',
			'import_filters',
			$row_id,
			'makes',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label'       => 'Make(s)',
			'id'          => "$row_id-makes",
			'name'        => 'cdp_admin_options[admin][advanced][import_filters][' . $row_id . '][makes]',
			'value'       => $value,
			'class'       => 'input-long',
			'placeholder' => 'Ford,Chevrolet',
			'notes'       => 'Multiple makes separated by comma',
		)
	);

	// $selected_saleclass = $value;
	// $selected_saleclass = 'new';  //jQuery('#0-saleclass').val()
	// $makes_db = (new Requests())->cdp_db_init('make', array('saleclass'=> $selected_saleclass ));
	//
	// $makes_list = Generic::build_list(array('data'=>$makes_db,'key'=>'Make'));
	// $current_makes = Option::check_for_option('cdp_admin_options', array('admin', 'advanced', 'import_filters', $row_id, 'makes'));
	// $content .= '<tr>';
	// $content .= '<th><label>Make(s)</label></th>';
	// $content .= '<td>';
	// $content .= '<select class="basic-multiple" name="cdp_admin_options[admin][advanced][import_filters][' . $row_id . '][makes][]" multiple="multiple">';
	// if ($makes_list) {
	// foreach ($makes_list as $make) {
	// if ($make) {
	// $selected = !empty($current_makes) ? (in_array($make, $current_makes) ? 'selected' : '') : '';
	// $content .= '<label for=' . $make . '>';
	// $content .= '<option  value="' . $make . '" ' . $selected . '/>' . $make . '</option>';
	// }
	// }
	// }
	// $content .= '</select>';
	// $content .= '</td>';
	// $content .= '</tr>';

	$list    = array( 'Equal', 'Not Equal' );
	$value   = Option::check_for_option(
		'cdp_admin_options',
		array(
			'admin',
			'advanced',
			'import_filters',
			$row_id,
			'make_qualifier',
		)
	);
	$content .= table_rows::tr_select_dropown(
		array(
			'label' => 'Make Qualifier',
			'id'    => "make-$row_id",
			'name'  => 'cdp_admin_options[admin][advanced][import_filters][' . $row_id . '][make_qualifier]',
			'value' => $value,
			'list'  => $list,
		)
	);

	$value   = Option::check_for_option(
		'cdp_admin_options',
		array(
			'admin',
			'advanced',
			'import_filters',
			$row_id,
			'years',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label'       => 'Year',
			'id'          => "advanced-filter-$row_id",
			'name'        => 'cdp_admin_options[admin][advanced][import_filters][' . $row_id . '][years]',
			'value'       => $value,
			'type'        => 'number',
			'class'       => 'input-long',
			'placeholder' => '1999,2000,2001',
			'notes'       => 'Only one year can be entered when using Great Than or Less Than.',
		)
	);

	$list    = array( 'Equal', 'Greater Than', 'Less Than' );
	$value   = Option::check_for_option(
		'cdp_admin_options',
		array(
			'admin',
			'advanced',
			'import_filters',
			$row_id,
			'year_qualifier',
		)
	);
	$content .= table_rows::tr_select_dropown(
		array(
			'label' => 'Year Qualifier',
			'id'    => "year-$row_id",
			'name'  => 'cdp_admin_options[admin][advanced][import_filters][' . $row_id . '][year_qualifier]',
			'value' => $value,
			'list'  => $list,
		)
	);

	$content .= table_rows::edit_update( $show, $row_id );

	return $content;
}
