<?php

namespace CarDealerPress\templates\admin\theme\inventory_display;

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

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

function prices(): string {

	$content = '<section id="price-options">';
	$content .= '<div><h3>Price Options</h3></div>';
	$content .= '<div><h3>Display Options</h3></div>';
	$content .= display_options();
	$content .= '<div><h3>Overrides</h3></div>';
	$content .= text_overrides();
	$content .= '</section>';

	return $content;
}

function text_overrides() {
	$content = price_fields( 'new' );
	$content .= '<div><hr/></div>';
	$content .= price_fields( 'used' );

	return $content;
}

function display_options() {
	$options = Option::get_cdp_theme_options()['theme']['inventory_display']['prices']['display'];
	$content = '<table id="" class="">';
	$content .= '<tbody>';
	$keys    = array_keys( $options );
	$remove  = array( 'new', 'used' );
	$keys    = array_diff( $keys, $remove );
	foreach ( $keys as $key ) {
		// Option strike_through not part of Flamingo, legacy implementation.
		if ( ! ( 'strike_through' === $key && Display::theme_check() ) ) {
			$checked = Option::check_for_option(
				'cdp_theme_options',
				array(
					'theme',
					'inventory_display',
					'prices',
					'display',
					$key,
				)
			);
			$label   = '';
			foreach ( explode( '_', $key ) as $word ) {
				$label .= ucfirst( $word );
				$label .= ' ';
			}
			$content .= table_rows::tr_setting_checkbox(
				array(
					'id'      => 'price_display_' . $key,
					'label'   => $label . ':',
					'name'    => 'cdp_theme_options[theme][inventory_display][prices][display][' . $key . ']',
					'checked' => $checked,
				)
			);
		}
	}
	$content .= '</tbody>';
	$content .= '</table>';

	return $content;

}

function price_fields( $saleclass ) {
	$content = '<table id="price-options-table" style="margin-bottom: 10px;border: 2px solid #000000; border-collapse: collapse; text-align: left;" class="table table-striped table-bordered table-hover table-condensed">';
	$content .= "<caption style='font-weight: bold; text-align: left;'>" . ucfirst( $saleclass ) . "</caption>";
	$content .= '<thead>';
	$content .= '<tr style="border-bottom: 1pt solid black;">';
	$content .= '<th scope="col">Name</th>';
	$content .= '<th scope="col">Label</th>';
	$content .= '<th scope="col">Display</th>';
	$content .= '</tr>';
	$content .= '</thead>';
	$content .= '<tbody>';

	$options = Option::get_cdp_theme_options()['theme']['inventory_display']['prices']['text'][ $saleclass ];
	$keys    = array_keys( $options );
	$class   = '';
	foreach ( $keys as $key ) {
		if ( 'used' === $saleclass && $key == 'msrp' ) {
			$content .= "<tr style='border-bottom: 1pt solid black;' class='hidden'>";
		} else {
			$content .= "<tr style='border-bottom: 1pt solid black;'>";
		}
		if ( ! ( 'discount' === $key && ! Display::theme_check() ) ) {
			$value = Option::check_for_option(
				'cdp_theme_options',
				array(
					'theme',
					'inventory_display',
					'prices',
					'text',
					$saleclass,
					$key,
				)
			);
			$label = '';
			foreach ( explode( '_', $key ) as $word ) {
				$label .= ucfirst( $word );
				$label .= ' ';
			}

			$tooltip_text = '';
			switch ( $key ) {
				case 'compare_price' === $key:
					$tooltip_text = 'If no sale price will display Price, unless otherwise changed';
					break;
				case 'discount' === $key:
					$tooltip_text = 'Calculated between Asking and Sale';
					break;
			}
			if ( ! empty( $tooltip_text ) ) {
				$tooltip_text = "<span class='tooltip'><span class='dashicons dashicons-editor-help'></span><div class='right'>$tooltip_text</div></span>";
			}
			$content .= "<td><span class=''>$label:</span>$tooltip_text</td>";

			$content .= table_rows::td_setting_text(
				array(
					'id'    => "price-text-" . $saleclass . "-" . $key,
					'label' => $label . ':',
					'name'  => 'cdp_theme_options[theme][inventory_display][prices][text][' . $saleclass . '][' . $key . ']',
					'value' => $value,
					'class' => 'input-long',
				)
			);
			$checked = Option::check_for_option(
				'cdp_theme_options',
				array(
					'theme',
					'inventory_display',
					'prices',
					'display',
					$saleclass,
					$key,
				)
			);
			$content .= table_rows::td_setting_checkbox(
				array(
					'id'      => "display-" . $saleclass . "-" . $label,
					'class'   => 'price-checkbox',
					'name'    => 'cdp_theme_options[theme][inventory_display][prices][display][' . $saleclass . '][' . $key . ']',
					'checked' => $checked,
				)
			);
		}
		$content .= '</tr>';
	}
	$content .= '</tbody>';
	$content .= '</table>';

	return $content;
}
