<?php

namespace CarDealerPress\templates\admin\theme\inventory_display\detail;

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

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

function forms(): string {
	$option = Option::check_for_option( 'cdp_theme_options', array( 'theme', 'inventory_display', 'detail', 'forms' ) );
	$count  = \CarDealerPress\Includes\Helpers\Generic::get_count_of( $option );

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

	$content = hooks();
	$content .= '<hr>';
	if ( ! empty( $option ) ) {
		$content .= current( $option );
		$content .= '<hr>';
	}
	$content .= create_new( 'Form Settings Create/Apply Form', $row_id );

	return $content;
}

function hooks(): string {
	$content = "<section id='hooks'>";
	$content .= '<h3>Hooks (Also apply to list forms)</h3>'; // TODO expand explanation.
	$content .= '<table>';
	$content .= "<tbody class='header'>";
	$content .= '<tr>';
	$content .= "<td><ul class='form-hook-columns'>";
	$hooks   = array(
		'dt_stock_number',
		'dt_vin',
		'dt_year',
		'dt_make',
		'dt_model',
		'dt_trim',
		'dt_saleclass',
		'dt_exterior',
		'dt_interior',
		'dt_mileage',
		'dt_price',
		'dt_dealer',
		'dt_dealer_id',
		'dt_location',
		'dt_phone',
		'dt_automall_email',
	);
	foreach ( $hooks as $hook ) {
		$content .= "<li>$hook</li>";
	}
	$content .= '</ul></td>';
	$content .= '</tr>';
	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '</section>';

	return $content;
}

function current( $option ): string {
	$content = "<section id='current'>";
	$content .= '<div><h3>Current Forms</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 ) {
		$id_value = 'form-' . $row_id;
		$name     = ! empty( $item['title'] ) ? $item['title'] : 'form id-' . $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='inventory_display' data-section='detail' data-section1='forms'></tr>";
		$content  .= "<td><label>$name</label>";
		$content  .= "<input id='edit-$row_id' class='edit-row' name='$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='inner-$row_id' class='detail inner-row'>";
		$content  .= '<td>';
		$content  .= "<div class=''>";
		$content  .= "<table class='table-wrap current'>";
		$content  .= '<tbody>';
		$content  .= form_rows( $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_new( $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("' . forms_add_table( $row_id ) . '"); });</script>';
	$content .= $script;
	$content .= '</section>';

	return $content;
}

function forms_add_table( $row_id ): string {
	$content = "<table id='addNewTable' class='table-wrap add'>";
	$content .= '<tbody>';
	$content .= form_rows( $row_id );
	$content .= '</tbody>';
	$content .= '</table>';

	return $content;
}

function form_rows( $row_id, $show = false ): string {
	$form_id = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'detail',
			'forms',
			$row_id,
			'id',
		)
	);
	$content = table_rows::tr_setting_text(
		array(
			'label'    => 'ID',
			'id'       => "form-$row_id-id",
			'name'     => 'cdp_theme_options[theme][inventory_display][detail][forms][' . $row_id . '][id]',
			'value'    => $form_id,
			'type'     => 'number',
			'class'    => 'input-medium',
			'min'      => 1,
			'required' => 'true',
		)
	);

	$form_title = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'detail',
			'forms',
			$row_id,
			'title',
		), 'Request Information'
	);
	$content    .= table_rows::tr_setting_text(
		array(
			'label' => 'Form Title',
			'id'    => "form-$row_id-title",
			'name'  => 'cdp_theme_options[theme][inventory_display][detail][forms][' . $row_id . '][title]',
			'value' => $form_title,
			'class' => 'input-long',
			'notes' => 'defaults to form title, depending on theme.',
		)
	);

	$saleclass      = array( 'all', 'new', 'used', 'certified' );
	$form_saleclass = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'detail',
			'forms',
			$row_id,
			'saleclass',
		)
	);
	$content        .= table_rows::tr_select_dropown(
		array(
			'label' => 'Sale Class',
			'id'    => "form-$row_id-saleclass",
			'name'  => 'cdp_theme_options[theme][inventory_display][detail][forms][' . $row_id . '][saleclass]',
			'value' => $form_saleclass,
			'list'  => $saleclass,
		)
	);

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

	return $content;
}
