<?php

namespace CarDealerPress\Templates\ShortCode;

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

// SaleClass DropDown Array.
use CarDealerPress\Includes\Database\Requests;

switch ( $this->sc_atts['saleclass_display'] ) {
	case 0:
		$saleclass_a = array( 'New', 'Used', 'Certified' );
		break;
	case 1:
		$saleclass_a = array( 'New', 'Used' );
		break;
	case 2:
		$saleclass_a = array( 'Used', 'Certified' );
		break;
	case 3:
		$saleclass_a = array( 'New' );
		break;
	case 4:
		$saleclass_a = array( 'Used' );
		break;
	case 5:
		$saleclass_a = array( 'Certified' );
		break;
}

// SaleClass Default Value.
switch ( $this->sc_atts['saleclass_default'] ) {
	case 0:
		$saleclass_default = $saleclass_a[0];
		break;
	case 1:
		$saleclass_default = 'New';
		break;
	case 2:
		$saleclass_default = 'Used';
		break;
	case 3:
		$saleclass_default = 'Certified';
		break;
	case 4:
		$saleclass_default = '';
		break;
}

if ( 'inline' === $this->sc_atts['style'] ) {
	$flex_class = $this->sc_atts['show_trims'] && $this->sc_atts['show_search'] ? 'flexSix' : ( $this->sc_atts['show_trims'] || $this->sc_atts['show_search'] ? 'flexFive' : 'flexFour' );
} else {
	$flex_class = $this->sc_atts['show_trims'] && $this->sc_atts['show_search'] ? 'flexFive' : ( $this->sc_atts['show_trims'] || $this->sc_atts['show_search'] ? 'flexFour' : 'flexThree' );
}
$makes       = array();
$models      = array();
$model_codes = array();
$trims       = array();
if ( ! empty( $saleclass_default ) ) {
	$saleclass_default = esc_sql( $saleclass_default );

	$makes = ( new Requests() )->cdp_db_init( 'make', array( 'saleclass' => $saleclass_default ) );
	foreach ( $makes as $key => $value ) {
		// common partial query for model,model_code,trim
		$make  = esc_sql( $value['Make'] );
		$param = "saleclass = '{$saleclass_default}' AND make = '{$make}'";

		// Model
		$columns  = 'Model, count(*) as Total';
		$group_by = 'Model';
		$order_by = 'Model';
		if ( count( $makes ) > 1 ) {
			$models[] = ( new Requests() )->cdp_inventory_special_request( $columns, $param, $group_by, $order_by )[0];
		} else {
			$models = ( new Requests() )->cdp_inventory_special_request( $columns, $param, $group_by, $order_by );
		}

		// Model Code
		$columns  = 'Model_Code, count(*) as Total';
		$group_by = 'Model_Code';
		$order_by = 'Model_Code';
		if ( count( $makes ) > 1 ) {
			$model_codes[] = ( new Requests() )->cdp_inventory_special_request( $columns, $param, $group_by, $order_by )[0];
		} else {
			$model_codes = ( new Requests() )->cdp_inventory_special_request( $columns, $param, $group_by, $order_by );
		}
		// Trim
		$columns  = 'Trim, count(*) as Total';
		$group_by = 'Trim';
		$order_by = 'Trim';
		if ( count( $makes ) > 1 ) {
			$trims[] = ( new Requests() )->cdp_inventory_special_request( $columns, $param, $group_by, $order_by )[0];
		} else {
			$trims = ( new Requests() )->cdp_inventory_special_request( $columns, $param, $group_by, $order_by );
		}
	}
}

$content = '<div class="cdp-inventory-shortcode search-shortcode-container wildcard ' . $this->sc_atts['style'] . ' ">';
if ( ! $this->sc_atts['show_counts'] ) {
	$content .= '<span class="hide-counts" style="display:none;"></span>';
}
// Saleclass.
$content .= '<div class="shortcode-search-list shortcode-search-saleclass ' . $flex_class . '" >';
$content .= ( ! $this->sc_atts['hide_labels'] ? '<div class="shortcode-search-label">Saleclass</div>' : '' );
$content .= '<div class="shortcode-search-content">';
$content .= '<select class="shortcode-input-action shortcode-select-saleclass" name="saleclass">';
if ( empty( $saleclass_default ) ) {
	$content .= '<option class="select-saleclass" value="" selected>Select a Saleclass</option>';
}
foreach ( $saleclass_a as $value ) {
	$selected = ( $saleclass_default === $value ? 'selected' : '' );
	$content  .= '<option class="' . strtolower( $value ) . '" value="' . $value . '" ' . $selected . '>' . $value . '</option>';
}
$content .= '</select>';
$content .= '</div>';
$content .= '</div>';

// Makes.
$content .= '<div class="shortcode-search-list shortcode-search-makes ' . $flex_class . '" >';
$content .= ( ! $this->sc_atts['hide_labels'] ? '<div class="shortcode-search-label">Makes</div>' : '' );
$content .= '<div class="shortcode-search-content">';
if ( 0 === count( $makes ) ) {
	$content .= '<select class="shortcode-input-action shortcode-select-make lockable" name="make" disabled><option value="0">Makes</option></select>';
} else {
	$content .= '<select class="shortcode-input-action shortcode-select-make lockable" name="make" ' . ( 1 === count( $makes ) ? 'disabled' : '' ) . '>';
	$content .= ( count( $makes ) > 1 ? '<option value="0">All</option>' : '' );
	if ( 0 !== count( $makes ) ) {
		foreach ( $makes as $key => $value ) {
			$selected = ( 1 === count( $makes ) ? 'selected' : '' );
			$content  .= '<option value="' . $value['Make'] . '" ' . $selected . '>' . $value['Make'];
			$content  .= ( $this->sc_atts['show_counts'] ? ' <span class="shortcode-search-count">(' . $value['Total'] . ')</span>' : '' );
			$content  .= '</option>';
		}
	}
}
$content .= '</select>';
$content .= '</div>';
$content .= '</div>';

// Models.
$content .= '<div class="shortcode-search-list shortcode-search-models ' . $flex_class . '" >';
$content .= ( ! $this->sc_atts['hide_labels'] ? '<div class="shortcode-search-label">Models</div>' : '' );
$content .= '<div class="shortcode-search-content">';
if ( count( $models ) > 1 ) {
	$content .= '<select class="shortcode-input-action shortcode-select-model lockable" name="model" ' . ( 1 === count( $models ) ? 'disabled' : '' ) . '>';
	$content .= ( count( $models ) > 1 ? '<option value="0">All</option>' : '' );
	foreach ( $models as $key => $value ) {
		$selected = ( 1 === count( $models ) ? 'selected' : '' );
		$content  .= '<option value="' . $value['Model'] . '" ' . $selected . '>' . $value['Model'];
		$content  .= ( $this->sc_atts['show_counts'] ? ' <span class="shortcode-search-count">(' . $value['Total'] . ')</span>' : '' );
		$content  .= '</option>';
	}
	$content .= '</select>';
} else {
	$content .= '<select class="shortcode-input-action shortcode-select-model lockable" name="model" disabled><option value="0">Models</option></select>';
}
$content .= '</div>';
$content .= '</div>';

// Model Codes.
if ( $this->sc_atts['show_model_codes'] ) {
	$content .= '<div class="shortcode-search-list shortcode-search-model-codes ' . $flex_class . '" >';
	$content .= ( ! $this->sc_atts['hide_labels'] ? '<div class="shortcode-search-label">Model Codes</div>' : '' );
	$content .= '<div class="shortcode-search-content">';
	if ( count( $models ) > 1 ) {
		$content .= '<select class="shortcode-input-action shortcode-select-model_code lockable" name="model_code" ' . ( 1 === count( $model_codes ) ? 'disabled' : '' ) . '>';
		$content .= ( count( $models ) > 1 ? '<option value="0">All</option>' : '' );
		foreach ( $model_codes as $key => $value ) {
			$selected = ( 1 === count( $model_codes ) ? 'selected' : '' );
			$content  .= '<option value="' . $value['Model_Code'] . '" ' . $selected . '>' . $value['Model_Code'];
			$content  .= ( $this->sc_atts['show_counts'] ? ' <span class="shortcode-search-count">(' . $value['Total'] . ')</span>' : '' );
			$content  .= '</option>';
		}
		$content .= '</select>';
	} else {
		$content .= '<select class="shortcode-input-action shortcode-select-model_code lockable" name="model" disabled><option value="0">Model Codes</option></select>';
	}
	$content .= '</div>';
	$content .= '</div>';
}
// Trims.
if ( $this->sc_atts['show_trims'] ) {
	$content .= '<div class="shortcode-search-list shortcode-search-trims ' . $flex_class . '" >';
	$content .= ( ! $this->sc_atts['hide_labels'] ? '<div class="shortcode-search-label">Trims</div>' : '' );
	$content .= '<div class="shortcode-search-content">';
	if ( 1 < count( $models ) ) {
		$content .= '<select class="shortcode-input-action shortcode-select-trim lockable foo" name="trim" ' . ( 1 === count( $trims ) ? 'disabled' : '' ) . '>';
		$content .= ( count( $trims ) > 1 ? '<option value="0">All</option>' : '' );
		foreach ( $trims as $key => $value ) {
			$selected = ( 1 === count( $trims ) ? 'selected' : '' );
			$content  .= '<option value="' . $value['Trim'] . '" ' . $selected . '>' . $value['Trim'];
			$content  .= ( $this->sc_atts['show_counts'] ? ' <span class="shortcode-search-count">(' . $value['Total'] . ')</span>' : '' );
			$content  .= '</option>';
		}
		$content .= '</select>';
	} else {
		$content .= '<select class="shortcode-input-action shortcode-select-trim lockable" name="trim" disabled><option value="0">Trims</option></select>';
	}
	$content .= '</div>';
	$content .= '</div>';
}

// Search.
if ( $this->sc_atts['show_search'] ) {
	$content .= '<div class="shortcode-search-text ' . $flex_class . '">';
	$content .= ( ! $this->sc_atts['hide_labels'] ? '<div class="shortcode-search-label">Text Search</div>' : '' );
	$content .= '<div class="shortcode-search-content">';
	$content .= '<input class="shortcode-text-input shortcode-input-search" type="text" value="" placeholder="Search" list="text-data-list" />';
	$content .= '</div>';
	$content .= '</div>';
}

// Action Button.
$content .= '<div class="shortcode-search-button" data-name="search">Search</div>';

$content .= '</div>';

$this->sc_content = $content;