<?php

namespace CarDealerPress\Templates\Admin\Theme;

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

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

function automall_emails(): string {
	$option = Option::check_for_option( 'cdp_theme_options', array( 'theme', 'automall_emails', 'dealers' ) );
	$count  = Generic::get_count_of( $option );

	if ( 0 === $count ) {
		$row_id = 0;
	} else {
		$row_id = $count;
	}

	$content = default_automall_emails();
	$content .= '<hr>';
	$content .= '<h3>Email routing based on Dealer ID</h3>';
	if ( ! empty( $option ) ) {
		$content .= current_automall_email( $option );
		$content .= '<hr>';
	}
	$content .= create_automall_email( 'Create', $row_id );

	return $content;
}

function default_automall_emails(): string {
	$content = '<section id="default">';
	$content .= '<div><span>Gravity Form field parameter name: dt_automall_email</span></div>';
	$content .= '<div><span>see <a href="?page=cardealerpress_theme&tab=inventory_display&section=detail&section1=forms" target="_blank">for a full list of parameters</a></span></div>';
	$content .= '<hr>';
	$content .= '<div><h4>Default AutoMall Emails</h4></div>';
	$content .= '<table>';
	$content .= '<tbody id="default-automall-email" class="automall-emails">';

	$value   = Option::check_for_option( 'cdp_theme_options', array( 'theme', 'automall_emails', 'defaults', 'new' ) );
	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'Default Email New:',
			'id'    => 'default_email_new',
			'name'  => 'cdp_theme_options[theme][automall_emails][defaults][new]',
			'value' => $value,
			'class' => 'input-long',
		)
	);
	$value   = Option::check_for_option( 'cdp_theme_options', array( 'theme', 'automall_emails', 'defaults', 'used' ) );
	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'Default Email Used:',
			'id'    => 'default_email_used',
			'name'  => 'cdp_theme_options[theme][automall_emails][defaults][used]',
			'value' => $value,
			'class' => 'input-long',
		)
	);

	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '</section>';

	return $content;
}

function current_automall_email( $option ): string {
	$content = '<section id="current">';
	$content .= '<div><h4>Current Emails</h4></div>';
	$content .= '<div id="current-rows" class="inner-table-content">';
	$content .= '<table id="current-table" class="">';
	$content .= '<tbody>';
	foreach ( $option as $row_id => $item ) {
		$id_value = 'form-' . $row_id;
		$name     = $item['id'];
		$content  .= "<tr id='outer-$row_id' data-tag='outer-$row_id' class='outer-row' data-label-name='$name' data-option='theme' data-tab='automall_emails' data-section='dealers'></tr>";
		$content  .= '<td>Dealer Id: ' . $name . ' ';
		$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 . '" tag="inner-' . $row_id . '" class="detail inner-row">';
		$content  .= '<td>';
		$content  .= '<div class="">';
		$content  .= '<table class="table-wrap current">';
		$content  .= '<tbody>';
		$content  .= row_automall_email( $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_automall_email( $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("' . new_row_automall_email( $row_id ) . '"); });</script>';
	wp_register_script( 'cdp_injector_js', CDP_JS . 'templates/inventory/injector.js', 'jquery', CDP_VERSION, true ); //injector.js is a placeholder file.
	wp_enqueue_script( 'cdp_injector_js' );
	wp_add_inline_script( 'cdp_injector_js', $script );
	$content .= '</section>';

	return $content;
}

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

	return $content;
}

function row_automall_email( $row_id, $show = false ): string {
	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'automall_emails',
			'dealers',
			$row_id,
			'id',
		)
	);
	$content = table_rows::tr_setting_text(
		array(
			'label'    => 'VMS ID',
			'id'       => "automall-emails-$row_id-id",
			'name'     => "cdp_theme_options[theme][automall_emails][dealers][$row_id][id]",
			'value'    => $value,
			'type'     => 'number',
			'class'    => 'input-medium',
			'required' => 'true',
			'min'      => '0',
		)
	); // TODO: future change to dropdown with DealerName

	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'automall_emails',
			'dealers',
			$row_id,
			'email',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label'    => 'Email',
			'id'       => "automall-emails-$row_id-email",
			'name'     => "cdp_theme_options[theme][automall_emails][dealers][$row_id][email]",
			'value'    => $value,
			'class'    => 'input-long',
			'required' => 'true',
		)
	);

	$saleclass = array( 'all', 'new', 'used', 'certified' ); // stub
	$value     = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'automall_emails',
			'dealers',
			$row_id,
			'saleclass',
		)
	);
	$content   .= table_rows::tr_select_dropown(
		array(
			'label' => 'Sale Class',
			'id'    => "automall-emails-$row_id-saleclass",
			'name'  => "cdp_theme_options[theme][automall_emails][dealers][$row_id][saleclass]",
			'value' => $value,
			'list'  => $saleclass,
		)
	);

	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'automall_emails',
			'dealers',
			$row_id,
			'is_adf',
		)
	);
	$content .= table_rows::tr_select_dropown(
		array(
			'label' => 'Is ADF',
			'id'    => "automall-emails-$row_id-is_adf",
			'name'  => "cdp_theme_options[theme][automall_emails][dealers][$row_id][is_adf]",
			'value' => $value,
			'list'  => array( 'no', 'yes' ),
		)
	);

	$content .= "<tr class='json_options-$row_id' style='display:none;' name='dealers'>";
	$content .= "<td>[{'id':'automall-emails-$row_id-id', 'type':'input', 'key': 'id'},";
	$content .= "{'id':'automall-emails-$row_id-email', 'type': 'input', 'key': 'email'},";
	$content .= "{'id':'automall-emails-$row_id-saleclass', 'type': 'dropdown', 'key': 'saleclass'},";
	$content .= "{'id':'automall-emails-$row_id-is_adf', 'type': 'dropdown', 'key': 'is_adf'}]</td></tr>";

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

	return $content;
}

