<?php

namespace CarDealerPress\templates\admin\theme\inventory_display;

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

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

function payment_calculator(): string {
	// @codingStandardsIgnoreStart
	$safe = helpers::sanitize_get( $_GET );
	// @codingStandardsIgnoreEnd
	$section1 = $safe['section1'] ?? 'used';

	$content = '<div id="cdp-loan-wrapper" class="settings-wrapper display">';
	$content .= '<div class="wrap">';
	$content .= '<nav class="nav-tab-wrapper">';
	$content .= '<a href="?page=cardealerpress_theme&tab=inventory_display&section=payment_calculator&section1=used" class="nav-tab ' . ( 'used' === $section1 ? 'nav-tab-active' : '' ) . '">Used</a>';
	$content .= '<a href="?page=cardealerpress_theme&tab=inventory_display&section=payment_calculator&section1=new" class="nav-tab ' . ( 'new' === $section1 ? 'nav-tab-active' : '' ) . '">New</a>';
	$content .= '<a href="?page=cardealerpress_theme&tab=inventory_display&section=payment_calculator&section1=custom" class="nav-tab ' . ( 'custom' === $section1 ? 'nav-tab-active' : '' ) . '">Custom</a>';
	$content .= '</nav>';
	$content .= '<div class="tab-content">';
	switch ( $section1 ) :
		case 'used':
			$content .= default_calculator( 'used' );
			break;
		case 'new':
			$content .= default_calculator( 'new' );
			break;
		case 'custom':
			$content .= custom_calculator();
			break;
	endswitch;
	$content .= '</div>';

	return $content;
}

function custom_calculator(): string {
	$option  = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'payment_calculator',
			'custom',
		)
	);
	$count   = Generic::get_count_of( $option );
	$content = '';

	if ( 0 === $count ) {
		$row_id = 0;
	} else {
		$row_id = $count;
	}
	if ( ! empty( $option ) ) {
		$content .= current_custom_calculator( $option );
		$content .= '<hr>';
	}
	$content .= create_custom_calculator( 'Create Custom Calculator', $row_id );

	return $content;
}

function create_custom_calculator( $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("' . table_custom_calculator( $row_id ) . '"); });</script>';
	$content .= $script;
	$content .= '</section>';

	return $content;
}

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

	return $content;
}

function current_custom_calculator( $option ): string {
	$content = "<section id='current'>";
	$content .= '<div><h3>Current Custom Calculator</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 ) {
		$name    = $item['name'];
		$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='payment_calculator' data-section1='custom'></tr>";
		$content .= "<td><label>$name</label>";
		$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' data-tag='custom-calculator-inner-'$row_id' class='detail inner-row'>";
		$content .= '<td>';
		$content .= "<div class=''>";
		$content .= "<table class='table-wrap current'>";
		$content .= '<tbody>';
		$content .= row_custom_calculator(
			array(
				'row_id' => $row_id,
				'show'   => true,
			)
		);
		$content .= '</tbody>';
		$content .= '</table>';
		$content .= '</div>';
		$content .= '</td>';
		$content .= '</tr>';
	}
	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '</div>';
	$content .= '</section>';

	return $content;
}

function default_calculator( $saleclass ): string {
	$default    = $default ?? 'default';
	$content    = '<section id="' . $saleclass . '-calculator">';
	$content    .= '<div><h3>' . ucfirst( $saleclass ) . ' Calculator</h3></div>';
	$content    .= '<table>';
	$content    .= '<tbody id="loan-' . $saleclass . '" class="loan-calculator">';
	$cal_fields = array(
		'display_calc',
		'display_payment',
		'display_monthly',
		'display_bi_monthly',
		'display_total_cost',
		'down_percent',
		'tax',
	);
	foreach ( $cal_fields as $cal_field ) {
		$option_value              = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'payment_calculator',
				$saleclass,
				$default,
				$cal_field,
			)
		);
		$cal_options[ $cal_field ] = $option_value;
	}

	$content .= table_rows::tr_setting_checkbox(
		array(
			'label'   => 'Display Calculator',
			'id'      => "loan-$saleclass-display-calc",
			'name'    => 'cdp_theme_options[theme][inventory_display][payment_calculator][' . $saleclass . '][' . $default . '][display_calc]',
			'checked' => $cal_options['display_calc'],
			'tooltip' => array(
				'position' => 'right',
				'text'     => 'Will display a Loan Calculator on the detail page, which will give the customer the ability to estimate a payment value.',
			),
		)
	);

	$content .= table_rows::tr_setting_checkbox(
		array(
			'label'   => 'Display Payment',
			'id'      => "loan-$saleclass-display-payment",
			'name'    => 'cdp_theme_options[theme][inventory_display][payment_calculator][' . $saleclass . '][' . $default . '][display_payment]',
			'checked' => $cal_options['display_payment'],
			'tooltip' => array(
				'position' => 'right',
				'text'     => 'Will display an estimated monthly payment based on the default value set. Customer will not be able to alter this value. Will display on both LIST and DETAIL pages.',
			),
		)
	);

	$content .= table_rows::tr_setting_checkbox(
		array(
			'label'   => 'Display Monthly Cost',
			'id'      => "loan-$saleclass-display-monthly",
			'name'    => 'cdp_theme_options[theme][inventory_display][payment_calculator][' . $saleclass . '][' . $default . '][display_monthly]',
			'checked' => $cal_options['display_monthly'],
		)
	);

	$content .= table_rows::tr_setting_checkbox(
		array(
			'label'   => 'Display Bi Monthly Cost',
			'id'      => "loan-$saleclass-display-bi-monthly",
			'name'    => 'cdp_theme_options[theme][inventory_display][payment_calculator][' . $saleclass . '][' . $default . '][display_bi_monthly]',
			'checked' => $cal_options['display_bi_monthly'],
		)
	);

	$content .= table_rows::tr_setting_checkbox(
		array(
			'label'   => 'Display Total Cost',
			'id'      => "loan-$saleclass-display-total-cost",
			'name'    => 'cdp_theme_options[theme][inventory_display][payment_calculator][' . $saleclass . '][' . $default . '][display_total_cost]',
			'checked' => $cal_options['display_total_cost'],
		)
	);

	$content .= table_rows::tr_setting_checkbox(
		array(
			'label'   => 'Down Value as %',
			'id'      => "loan-$saleclass-down-percent",
			'name'    => 'cdp_theme_options[theme][inventory_display][payment_calculator][' . $saleclass . '][' . $default . '][down_percent]',
			'checked' => $cal_options['down_percent'],
		)
	);
	$content .= '<tr><hr></tr>';

	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'Default Sales Tax:',
			'id'    => "loan-$saleclass-tax",
			'name'  => 'cdp_theme_options[theme][inventory_display][payment_calculator][' . $saleclass . '][' . $default . '][tax]',
			'value' => empty( $cal_options['tax'] ) ? 0.0 : $cal_options['tax'],
			'type'  => 'number',
			'step'  => '0.01',
			'class' => 'input-medium',
			'min'   => 0,
		)
	);

	$content .= '<tr><td><hr></td></tr>';
	$content .= common_calculator_info(
		array(
			'saleclass' => $saleclass,
			'type'      => 'default',
		)
	);

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

	return $content;
}

function row_custom_calculator( $args ): string {
	$row_id = $args['row_id'];
	$show   = $args['show'] ?? false;

	$cal_fields = array(
		'name',
		'saleclass',
		'make',
		'model',
		'trim',
		'year',
		'year_qualifier',
		'display',

	);
	foreach ( $cal_fields as $cal_field ) {
		$option_value              = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'payment_calculator',
				'custom',
				$row_id,
				$cal_field,
			)
		);
		$cal_options[ $cal_field ] = $option_value;
	}

	$content = table_rows::tr_setting_text(
		array(
			'label'    => 'Name',
			'id'       => "loan-custom-name-$row_id",
			'name'     => 'cdp_theme_options[theme][inventory_display][payment_calculator][custom][' . $row_id . '][name]',
			'value'    => $cal_options['name'],
			'class'    => 'input-long',
			'required' => true,
		)
	);

	$saleclass = array(
		'',
		'new',
		'used'
	); // stub, detail page doesn't know about certified, not in legacy, #TODO add in future.
	$content   .= table_rows::tr_select_dropown(
		array(
			'label' => 'Sale Class',
			'id'    => "loan-custom-saleclass-$row_id",
			'name'  => 'cdp_theme_options[theme][inventory_display][payment_calculator][custom][' . $row_id . '][saleclass]',
			'value' => $cal_options['saleclass'],
			'list'  => $saleclass,
		)
	);

	$makes_list = Generic::select_attr_list(
		array(
			'key'  => 'make',
			'text' => 'Make',
		)
	);

	$content .= table_rows::tr_select_dropown(
		array(
			'label' => 'Make: (optional)',
			'id'    => "loan-custom-make-$row_id",
			'name'  => 'cdp_theme_options[theme][inventory_display][payment_calculator][custom][' . $row_id . '][make]',
			'value' => $cal_options['make'],
			'list'  => $makes_list,
		)
	);

	$models_list = Generic::select_attr_list(
		array(
			'key'  => 'model',
			'text' => 'Model',
		)
	);

	$content .= table_rows::tr_select_dropown(
		array(
			'label' => 'Model: (optional)',
			'id'    => "loan-custom-model-$row_id",
			'name'  => 'cdp_theme_options[theme][inventory_display][payment_calculator][custom][' . $row_id . '][model]',
			'value' => $cal_options['model'],
			'list'  => $models_list,
		)
	);

	$trims_list = Generic::select_attr_list(
		array(
			'key'  => 'trim',
			'text' => 'Trim',
		)
	);

	$content .= table_rows::tr_select_dropown(
		array(
			'label' => 'Trim: (optional)',
			'id'    => "loan-custom-trim-$row_id",
			'name'  => 'cdp_theme_options[theme][inventory_display][payment_calculator][custom][' . $row_id . '][trim]',
			'value' => $cal_options['trim'],
			'list'  => $trims_list,
		)
	);

	$years_list = Generic::select_attr_list(
		array(
			'key'  => 'year',
			'text' => 'Year',
		)
	);

	$content .= table_rows::tr_select_dropown(
		array(
			'label' => 'Year:(optional)',
			'id'    => "loan-custom-year-symbol-$row_id",
			'name'  => 'cdp_theme_options[theme][inventory_display][payment_calculator][custom][' . $row_id . '][year]',
			'value' => $cal_options['year'],
			'list'  => $years_list,
		)
	);

	$year_qualifier = array( 'equal', 'less than', 'greater than' );
	$content        .= table_rows::tr_select_dropown(
		array(
			'label' => 'Year Qualifier',
			'id'    => "loan-custom-year-qualifier-$row_id",
			'name'  => 'cdp_theme_options[theme][inventory_display][payment_calculator][custom][' . $row_id . '][year_qualifier]',
			'value' => $cal_options['year_qualifier'],
			'list'  => $year_qualifier,
		)
	);

	$content .= '<tr><td><hr></td></tr>';
	$content .= common_calculator_info(
		array(
			'saleclass' => 'custom',
			'type'      => $row_id,
		)
	);

	$content .= table_rows::tr_setting_checkbox(
		array(
			'label'   => 'Display (overrides default)',
			'id'      => "loan-custom-display-$row_id",
			'name'    => 'cdp_theme_options[theme][inventory_display][payment_calculator][custom][' . $row_id . '][display]',
			'checked' => $cal_options['display'],
		)
	);

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

	return $content;
}

function common_calculator_info( $args ) {
	$saleclass = $args['saleclass'] ?? 'custom';
	$type      = $args['type']; // Default or row_id.

	$cal_fields = array(
		'interest',
		'trade',
		'term',
		'down',
		'days_to_payment',
		'disclaimer',
		'display_text',
	);
	foreach ( $cal_fields as $cal_field ) {
		$option_value              = Option::check_for_option(
			'cdp_theme_options',
			array(
				'theme',
				'inventory_display',
				'payment_calculator',
				$saleclass,
				$type,
				$cal_field,
			)
		);
		$cal_options[ $cal_field ] = $option_value;
	}

	$content = table_rows::tr_setting_text(
		array(
			'label' => 'Interest Rate',
			'id'    => "loan-$saleclass-interest",
			'name'  => 'cdp_theme_options[theme][inventory_display][payment_calculator][' . $saleclass . '][' . $type . '][interest]',
			'value' => empty( $cal_options['interest'] ) ? 0 : $cal_options['interest'],
			'type'  => 'number',
			'step'  => '0.01',
			'class' => 'input-medium',
			'min'   => 0,
		)
	);

	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'Trade Value',
			'id'    => "loan-$saleclass-trade",
			'name'  => 'cdp_theme_options[theme][inventory_display][payment_calculator][' . $saleclass . '][' . $type . '][trade]',
			'value' => empty( $cal_options['trade'] ) ? 0 : $cal_options['trade'],
			'type'  => 'number',
			'class' => 'input-medium',
			'min'   => 0,
		)
	);

	$content .= table_rows::tr_setting_text(
		array(
			'label'    => 'Term (months)',
			'id'       => "loan-$saleclass-term",
			'name'     => 'cdp_theme_options[theme][inventory_display][payment_calculator][' . $saleclass . '][' . $type . '][term]',
			'value'    => empty( $cal_options['term'] ) ? 60 : $cal_options['term'],
			'type'     => 'number',
			'class'    => 'input-medium',
			'required' => true,
			'min'      => 0,
		)
	);

	$content .= table_rows::tr_setting_text(
		array(
			'label'   => 'Down Payment',
			'id'      => "loan-$saleclass-down",
			'name'    => 'cdp_theme_options[theme][inventory_display][payment_calculator][' . $saleclass . '][' . $type . '][down]',
			'value'   => empty( $cal_options['down'] ) ? 0 : $cal_options['down'],
			'type'    => 'number',
			'class'   => 'input-medium',
			'min'     => 0,
			'tooltip' => array(
				'position' => 'right',
				'text'     => 'Numeric values only. If \"Down Value as %\" is checked it will automatically calculate site down payment as percent.',
			),
		)
	);

	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'Days to First Payment',
			'id'    => "loan-$saleclass-payment",
			'name'  => 'cdp_theme_options[theme][inventory_display][payment_calculator][' . $saleclass . '][' . $type . '][days_to_payment]',
			'value' => empty( $cal_options['days_to_payment'] ) ? 0 : $cal_options['days_to_payment'],
			'type'  => 'number',
			'class' => 'input-medium',
			'min'   => 0,
		)
	);

	$content .= table_rows::tr_setting_textarea(
		array(
			'label' => 'Disclaimer',
			'id'    => "loan-$saleclass-disclaimer",
			'name'  => 'cdp_theme_options[theme][inventory_display][payment_calculator][' . $saleclass . '][' . $type . '][disclaimer]',
			'value' => $cal_options['disclaimer'],
			'notes' => 'Allowable merge properties [payment], [term], [trade], [down_value], [down], [interest]',

		)
	);

	$content .= table_rows::tr_setting_textarea(
		array(
			'label' => 'Display Text',
			'id'    => "loan-$saleclass-text",
			'name'  => 'cdp_theme_options[theme][inventory_display][payment_calculator][' . $saleclass . '][' . $type . '][display_text]',
			'value' => $cal_options['display_text'],
			'notes' => 'Allowable merge properties [payment], [term], [trade], [down_value], [down], [interest]',
		)
	);

	return $content;
}


