<?php

namespace CarDealerPress\templates\admin\theme\inventory_display;

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 banners(): string {
	$option  = Option::check_for_option( 'cdp_theme_options', array( 'theme', 'inventory_display', 'banners' ) );
	$count   = Generic::get_count_of( $option );
	$content = '';

	if ( 0 === $count ) {
		$row_id = 0;
	} else {
		$row_id = $count;
	}
	if ( ! empty( $option ) ) {
		$content .= current_banners( $option );
		$content .= '<hr>';
	}
	$content .= create_banner( 'Banner Settings Create/Apply Banner', $row_id );

	return $content;
}

function current_banners( $option ): string {
	$content = "<section id='current'>";
	$content .= '<div><h3>Current Banners</h3></div>';
	$content .= "<div id='current-rows' class='inner-table-content'>";
	$content .= "<table id='current-table' class=''>";
	$content .= '<tbody>';
	foreach ( $option as $row_id => $item ) {
		$name    = $item['name'];
		$content .= "<tr id='outer-$row_id' data-tag='outer-$row_id' class='outer-row' data-label-name='$name' data-option='theme' data-tab='inventory_display' data-section='banners'></tr>";
		$content .= "<td><label>$name</label>";
		$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' data-tag='banner-inner-$row_id' class='detail inner-row'>";
		$content .= '<td>';
		$content .= "<div class=''>";
		$content .= "<table class='table-wrap current'>";
		$content .= '<tbody>';
		$content .= row_banner( $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_banner( $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("' . table_banner( $row_id ) . '"); });</script>';
	$content .= $script;
	$content .= '</section>';

	return $content;
}

function table_banner( $row_id ) {
	$content = "<table id='addNewTable' class='table-wrap add'>";
	$content .= '<tbody>';
	$content .= row_banner( $row_id );
	$content .= '</tbody>';
	$content .= '</table>';

	return $content;
}

function row_banner( $row_id, $show = false ): string {
	$content      = '';
	$makes        = array();
	$models       = array();
	$years        = array();
	$vehicleclass = array();
	$tags         = array();
	$dealers      = array();
	// Page.
	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'banners',
			$row_id,
			'page',
		)
	);
	$content .= table_rows::tr_select_dropown(
		array(
			'label'      => 'Page',
			'id'         => "$row_id-page",
			'name'       => 'cdp_theme_options[theme][inventory_display][banners][' . $row_id . '][page]',
			'value'      => $value,
			'list'       => array( '', 'list', 'detail' ),
			'list_human' => array( 'Both', 'List', 'Detail' ),
		)
	);
	// Position.
	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'banners',
			$row_id,
			'position',
		)
	);
	$content .= table_rows::tr_select_dropown(
		array(
			'label' => 'Position',
			'id'    => "$row_id-position",
			'name'  => 'cdp_theme_options[theme][inventory_display][banners][' . $row_id . '][position]',
			'value' => $value,
			'list'  => array( 'top', 'bottom' ),
		)
	);
	// Dealers.
	$dealers      = ( new Requests() )->cdp_db_init( 'dealers', array( 'pass' => '1' ) );
	$dealers_list = Generic::build_list(
		array(
			'data' => $dealers,
			'key'  => 'Dealer',
		)
	);
	if ( $dealers && count( $dealers ) > 1 ) {
		$value   = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'banners',
				$row_id,
				'dealer',
			)
		);
		$content .= table_rows::tr_select_dropown(
			array(
				'label' => 'Dealers',
				'id'    => "$row_id-dealer",
				'name'  => 'cdp_theme_options[theme][inventory_display][banners][' . $row_id . '][dealer]',
				'value' => $value,
				'list'  => $dealers_list,
			)
		);
	}
	// Sale Class.
	// $saleclass = Main::$db_request->cdp_db_init('saleclass', array('pass'=>'1')); ## TODO: Future no existing function for saleclass, should there be?
	$saleclass       = array( '', 'new', 'used', 'certified' );
	$saleclass_human = array( 'All', 'New', 'Used', 'Certified' );
	$value           = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'banners',
			$row_id,
			'saleclass',
		)
	);
	$content         .= table_rows::tr_select_dropown(
		array(
			'label'      => 'Sale Class',
			'id'         => "$row_id-saleclass",
			'name'       => 'cdp_theme_options[theme][inventory_display][banners][' . $row_id . '][saleclass]',
			'value'      => $value,
			'list'       => $saleclass,
			'list_human' => $saleclass_human,
		)
	);
	// Vehicle Class.
	$vehicle_class_list = Generic::select_attr_list(
		array(
			'key'  => 'vehicleclass',
			'text' => 'Vehicle Class',
		)
	);
	$value              = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'banners',
			$row_id,
			'vehicleclass',
		)
	);
	$content            .= table_rows::tr_select_dropown(
		array(
			'label' => 'Vehicle Class',
			'id'    => "$row_id-vehicleclass",
			'name'  => 'cdp_theme_options[theme][inventory_display][banners][' . $row_id . '][vehicleclass]',
			'value' => $value,
			'list'  => $vehicle_class_list,
		)
	);
	// Makes.
	$makes_list = Generic::select_attr_list(
		array(
			'key'  => 'make',
			'text' => 'Make',
		)
	);
	$value      = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'banners',
			$row_id,
			'make',
		)
	);
	$content    .= table_rows::tr_select_dropown(
		array(
			'label' => 'Select a Make',
			'id'    => "$row_id-make",
			'name'  => 'cdp_theme_options[theme][inventory_display][banners][' . $row_id . '][make]',
			'value' => $value,
			'list'  => $makes_list,
		)
	);
	// Models.
	$models_list = Generic::select_attr_list(
		array(
			'key'  => 'model',
			'text' => 'Model',
		)
	);
	$value       = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'banners',
			$row_id,
			'model',
		)
	);
	$content     .= table_rows::tr_select_dropown(
		array(
			'label' => 'Select a Model',
			'id'    => "$row_id-model",
			'name'  => 'cdp_theme_options[theme][inventory_display][banners][' . $row_id . '][model]',
			'value' => $value,
			'list'  => $models_list,
		)
	);

	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'banners',
			$row_id,
			'startdate',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'Start Date',
			'id'    => "$row_id-startdate",
			'name'  => 'cdp_theme_options[theme][inventory_display][banners][' . $row_id . '][startdate]',
			'value' => $value,
			'type'  => 'date',
			'class' => 'input-long',
		)
	);
	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'banners',
			$row_id,
			'enddate',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'End Date',
			'id'    => "$row_id-enddate",
			'name'  => 'cdp_theme_options[theme][inventory_display][banners][' . $row_id . '][enddate]',
			'value' => $value,
			'type'  => 'date',
			'class' => 'input-long',
		)
	);

	$banner_data = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'banners',
			$row_id,
			'banner_data',
		)
	);
	if ( empty( $banner_data ) ) {
		$required = 'true';
	} else {
		$required = '';
	}
	$content .= table_rows::tr_setting_textarea(
		array(
			'label'    => 'Banner Data:',
			'id'       => "$row_id-data",
			'name'     => 'cdp_theme_options[theme][inventory_display][banners][' . $row_id . '][banner_data]',
			'value'    => $banner_data,
			'required' => $required,
		)
	);

	$value = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'banners',
			$row_id,
			'name',
		)
	);
	if ( empty( $value ) ) {
		$required = 'true';
	} else {
		$required = '';
	}
	$content .= table_rows::tr_setting_text(
		array(
			'label'    => 'Name',
			'id'       => "$row_id-name",
			'name'     => 'cdp_theme_options[theme][inventory_display][banners][' . $row_id . '][name]',
			'value'    => $value,
			'class'    => 'input-long',
			'required' => $required,
		)
	);

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

	return $content;
}

