<?php

namespace CarDealerPress\Templates\Admin\Theme\Inventory_Display;

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

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

function extra_boxes( $args ): string {
	$page = $args['location'];
	if ( 'both' === $args['location'] ) {
		$location      = array( 'theme', 'inventory_display', 'general', "$page", 'extra_boxes' );
		$location_name = "general][$page";
	} else {
		$location      = array( 'theme', 'inventory_display', "$page", 'extra_boxes' );
		$location_name = "$page";
	}
	$content = '<section id="extra-boxes-' . $page . '">';
	$content .= '<div><h3>Extra Boxes</h3></div>';
	if ( 'both' === $page ) {
		$content .= '<div><h5>Will be overwritten if filled out in List/Detail section. Can contain any valid HTML or SCRIPT code. 
						For scripts the opening and closing tags must be included.</h5></div>';
	}
	$content .= '<table id="extra-boxes-' . $page . '-table" class="">';
	$content .= '<tbody>';
	$content .= '<tr><th>Allowable merge tags</th>';
	$content .= '<td><ul class="allowable-merge-tags">';
	$hooks   = array( '[VIN]', '[STOCK]', '[DEALER]' );
	foreach ( $hooks as $hook ) {
		$content .= "<li>$hook</li>";
	}
	$content    .= '</ul></td>';
	$content    .= '</tr>';
	$location[] = 'box_one';
	$value      = Option::check_for_option( 'cdp_theme_options', $location );
	$disabled   = '';
	if ( Option::check_for_option( 'cdp_theme_options', array(
		'theme',
		'inventory_display',
		'tags',
		'display_tags'
	) ) ) {
		$disabled = 'disabled';
	}
	$content .= table_rows::tr_setting_textarea(
		array(
			'label'    => 'Extra Box One Data',
			'id'       => 'extra-box-1-' . $page,
			'name'     => "cdp_theme_options[theme][inventory_display][$location_name][extra_boxes][box_one]",
			'tooltip'  => array(
				'text'     => 'Box will be disabled if Tags is enabled. No Javascipt allowed.',
				'position' => 'right'
			),
			'value'    => $value,
			'disabled' => $disabled,
		)
	);
	array_pop( $location );
	$location[] = 'box_two';
	$value      = Option::check_for_option( 'cdp_theme_options', $location );
	$content    .= table_rows::tr_setting_textarea(
		array(
			'label'   => 'Extra Box Two Data',
			'id'      => 'extra-box-2-' . $page,
			'name'    => "cdp_theme_options[theme][inventory_display][$location_name][extra_boxes][box_two]",
			'tooltip' => array(
				'text'     => 'Box will be overwritten if either carfax or autocheck is enabled. No Javascipt allowed.',
				'position' => 'right'
			),
			'value'   => $value,
		)
	);
	array_pop( $location );
	$boxes = array( 'box_three', 'box_four' );
	foreach ( $boxes as $box ) {
		$label      = ucwords( str_replace( '-', ' ', $box ) );
		$location[] = $box;
		$value      = Option::check_for_option( 'cdp_theme_options', $location );
		$content    .= table_rows::tr_setting_textarea(
			array(
				'label'   => "Extra $label Data",
				'id'      => "extra-$box-$page",
				'name'    => "cdp_theme_options[theme][inventory_display][$location_name][extra_boxes][$box]",
				'value'   => $value,
				'tooltip' => array(
					'text'     => 'No Javascipt allowed.',
					'position' => 'right'
				),
			)
		);
		array_pop( $location );
	}
	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '</section>';

	return $content;
}
