<?php

namespace CarDealerPress\templates\admin\theme\inventory_display;

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

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

function vehicle_attributes( $args ): string {
	$page     = $args['page'];
	$name     = $args['name'] ?? 'cdp_theme_options[theme][inventory_display]';
	$option   = $args['option'] ?? 'cdp_theme_options';
	$location = array( 'theme', 'inventory_display', $page, 'vehicle_attributes' );
	if ( preg_match( '/inventory_list|inventory_slider/', $page ) ) {
		$location = array( $page, 'vehicle_attributes' );
	}

	$content = '<section id="list-options">';
	$content .= '<div><h3>Vehicle Attributes</h3></div>';

	$content .= '<table id="vehicle-attributes-main-options" class="">';
	$content .= '<tbody>';

	$checked = Option::check_for_option(
		$option,
		array_merge( $location, array(
			'display_attribute',
		) )
	);
	$content .= table_rows::tr_setting_checkbox(
		array(
			'id'      => 'display_attribute',
			'label'   => 'Display if no data exists:',
			'name'    => $name . '[' . $page . '][vehicle_attributes][display_attribute]',
			'checked' => $checked,
		)
	);

	$value   = Option::check_for_option(
		$option,
		array_merge( $location, array(
			'not_found_text',
		) )
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label'       => 'Not found data text:',
			'id'          => 'not_found_text',
			'name'        => $name . '[' . $page . '][vehicle_attributes][not_found_text]',
			'placeholder' => 'Data Not Available',
			'value'       => $value,
			'class'       => 'input-long',
		)
	);
	$checked = Option::check_for_option(
		$option,
		array_merge( $location, array(
			'odometer_show_units',
		) )
	);
	$content .= table_rows::tr_setting_checkbox(
		array(
			'id'      => 'show_units_odometer',
			'label'   => 'Odometer, show units:',
			'name'    => $name . '[' . $page . '][vehicle_attributes][odometer_show_units]',
			'checked' => $checked,
		)
	);
	$content .= '</tbody>';
	$content .= '</table>';

	$attributes = Option::check_for_option(
		$option,
		array_merge( $location, array(
			'attributes',
		) )
	);

	$content .= '<hr>';
	$content .= '<div class="note">Drag and drop to reorder attributes, Press SAVE to finalize order.</div>';
	$content .= '<table class="drag-table draggable-table">';
	$content .= '<thead>';
	$content .= '<th scope="col"></th>';
	$content .= '<th scope="col">VMS Name</th>';
	$content .= '<th scope="col">Label</th>';
	$content .= '<th scope="col">Display Desktop</th>';
	$content .= '<th scope="col">Display Mobile</th>';
	$content .= '</thead>';
	$content .= '<tbody>';

	$db_request = new Requests();
	$company    = $db_request->cdp_company_request()[0];
	foreach ( $attributes as $index => $attribute ) {
		$content  .= '<tr>';
		$content  .= '<td><span class="dashicons dashicons-move"></span></td>';
		$position = $attribute['position'];
		$content  .= table_rows::td_setting_text(
			array(
				'id'       => 'position-' . $attribute['attribute'],
				'name'     => $name . '[' . $page . '][vehicle_attributes][attributes][' . $index . '][position]',
				'value'    => $position ?? $index,
				'type'     => 'number',
				'class'    => 'input-position hidden',
				'min'      => 1,
				'td_class' => 'hidden',
			)
		);
		$content  .= '<td><div>' . $attribute['attribute'] . '</div>';
		$content  .= '<input hidden type="text" name="' . $name . '[' . $page . '][vehicle_attributes][attributes][' . $index . '][attribute]" value="' . $attribute['attribute'] . '" class="input-attribute-label">';
		$content  .= '</td>';
		$label    = $attribute['label'];
		if ( 'mpg' === $attribute['attribute'] && ( 'CA' === $company['country_code'] ) ) {
			$label = 'L/100 km';
		}
		$content .= table_rows::td_setting_text(
			array(
				'id'    => 'label-' . $attribute['label'],
				'name'  => $name . '[' . $page . '][vehicle_attributes][attributes][' . $index . '][label]',
				'type'  => 'text',
				'class' => 'input-attribute-label',
				'value' => $label,
			)
		);

		$checked = $attribute['desktop'];
		$content .= table_rows::td_setting_checkbox(
			array(
				'id'      => 'desktop-display-' . $attribute['attribute'],
				'class'   => 'attribute-checkbox',
				'name'    => $name . '[' . $page . '][vehicle_attributes][attributes][' . $index . '][desktop]',
				'checked' => $checked,
			)
		);

		$checked = $attribute['mobile'];
		$content .= table_rows::td_setting_checkbox(
			array(
				'id'      => 'mobile-display-' . $attribute['attribute'],
				'class'   => 'attribute-checkbox',
				'name'    => $name . '[' . $page . '][vehicle_attributes][attributes][' . $index . '][mobile]',
				'checked' => $checked,
			)
		);
		$content .= '</tr>';
	}
	$content .= '</tbody>';
	$content .= '</table>';

	return $content;

}
