<?php

namespace CarDealerPress\Templates\Admin\Theme;

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

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

function injector_scripts_detail(): string {
	$content = '<section id="list-ajax">';
	$content .= '<div><h3>Detail Script Injector</h3></div>';
	$content .= '<div><h4>Opening and closing script tags need to be included.</h4></div>';
	$content .= '<table><tbody>';
	$content .= '<tr><th>Allowable merge tags</th>';
	$content .= '<td><ul class="allowable-merge-tags">';
	$hooks   = array( '[VIN]', '[STOCK]' );
	foreach ( $hooks as $hook ) {
		$content .= "<li>$hook</li>";
	}
	$content .= '</ul></td>';
	$content .= '</tr>';
	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'detail',
			'scripts',
			'inject',
		)
	);
	$content .= table_rows::tr_setting_textarea(
		array(
			'label' => 'Detail Script Inject:',
			'id'    => 'detail-script',
			'name'  => 'cdp_theme_options[theme][inventory_display][detail][scripts][inject]',
			'value' => $value,
		)
	);
	$content .= '</tbody></table>';
	$content .= '</section>';

	return $content;
}

function injector_scripts_list(): string {
	$content = '<section id="list-ajax">';
	$content .= '<div><h3>List Scripts Injector</h3></div>';
	$content .= '<div><h4>Opening and closing script tags need to be included.</h4></div>';
	$content .= '<table><tbody>';
//	$content .= '<tr><th>Allowable merge tags</th>';
//	$content .= '<td><ul class="allowable-merge-tags">';
//	$hooks    = array( '[VIN]', '[STOCK]' );
//	foreach ( $hooks as $hook ) {
//		$content .= "<li>$hook</li>";
//	}
//	$content .= '</ul></td>';
//	$content .= '</tr>';
	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'scripts',
			'inject',
		)
	);
	$content .= table_rows::tr_setting_textarea(
		array(
			'label' => 'List Script Inject:',
			'id'    => 'list-script',
			'name'  => 'cdp_theme_options[theme][inventory_display][list][scripts][inject]',
			'value' => $value,
		)
	);

	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'list',
			'scripts',
			'ajax',
		)
	);
	$content .= table_rows::tr_setting_textarea(
		array(
			'label' => 'List Script Ajax Inject:',
			'id'    => 'list-ajax-script',
			'name'  => 'cdp_theme_options[theme][inventory_display][list][scripts][ajax]',
			'value' => $value,
		)
	);
	$content .= '</tbody></table>';
	$content .= '</section>';

	return $content;
}

function injector_style( $args ): string {
	$page = $args['location'];
	if ( 'both' === $args['location'] ) {
		$location      = array( 'theme', 'inventory_display', 'general', "$page", 'styles', 'inject' );
		$location_name = "general][$page";
	} else {
		$location      = array( 'theme', 'inventory_display', "$page", 'styles', 'inject' );
		$location_name = "$page";
	}
	$content = '<section id="style-injector' . $page . '">';
	$content .= '<div><h3>Styles</h3></div>';
	if ( 'both' === $page ) {
		$content .= '<div><h5></h5></div>';
		$tip     = array( 'text'     => 'Will be overwritten if filled out in List/Detail section.',
		                  'position' => 'right'
		);
	}
	$content .= '<table><tbody>';
	$value   = Option::check_for_option( 'cdp_theme_options', $location );
	$content .= table_rows::tr_setting_textarea(
		array(
			'label'   => 'Style Inject:',
			'id'      => "style-inject-$page",
			'name'    => "cdp_theme_options[theme][inventory_display][$location_name][styles][inject]",
			'value'   => $value,
			'tooltip' => $tip ?? '',
		)
	);
	$content .= '</tbody></table>';
	$content .= '</section>';

	return $content;
}
