<?php

namespace CarDealerPress\Templates\Admin\Admin;

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

use CarDealerPress\Includes\Database\Requests;
use CarDealerPress\includes\functions\Fn_Views;
use CarDealerPress\Includes\Helpers\Option;
use CarDealerPress\templates\partials\admin\Display;
use CarDealerPress\templates\partials\admin\Table_Rows as table_rows;

function general(): string {
	$content    = '<table>';
	$content    .= '<tbody>';
	$company_id = Option::check_for_option( 'cdp_admin_options', array( 'admin', 'general', 'company_id' ), 0 );
	$content    .= table_rows::tr_setting_text(
		array(
			'id'      => 'company_id',
			'class'   => 'input-medium',
			'label'   => 'Company ID',
			'name'    => 'cdp_admin_options[admin][general][company_id]',
			'value'   => $company_id,
			'type'    => 'number',
			'tooltip' => array(
				'position' => 'right',
				'text'     => 'Company Id provided by DealerTrend',
			),
		)
	);

	$vdp_url_option = Option::check_for_option(
		'cdp_admin_options',
		array(
			'admin',
			'general',
			'vdp_url_identifier',
		),
		'vin'
	);
	$vdp_url_vin    = ( $vdp_url_option === 'vin' );
	$vdp_url_id     = ! $vdp_url_vin;
	$content        .= '<tr class="cdp-row">';
	$content        .= '<th><label>VDP URL Identifier</label></th>';
	$content        .= '<td>';
	$content        .= table_rows::tr_setting_radio(
		array(
			'id'    => '',
			'label' => 'vin',
			'for'   => 'vin-identifier',
			'name'  => 'cdp_admin_options[admin][general][vdp_url_identifier]',
			'value' => 'vin',
			'var'   => $vdp_url_vin,
		)
	);
	$content        .= '</td>';
	$content        .= '<td>';
	$content        .= table_rows::tr_setting_radio(
		array(
			'id'    => '',
			'label' => 'vehicle id',
			'for'   => 'vehicle-id-identifier',
			'name'  => 'cdp_admin_options[admin][general][vdp_url_identifier]',
			'value' => 'id',
			'var'   => $vdp_url_id,
		)
	);
	$content        .= '</td>';
	$tool_tip_text  = 'For replacing the default VIN in the vdp url with the vehicle id. If you need to hide the vin from the url.';
	$content        .= '<td><span class="tooltip"><span class="dashicons dashicons-editor-help"></span><div class="right">' . $tool_tip_text . '</div></span></td>';
	$content        .= '</tr>';

//	$content .= '<tr class="cdp-row">';
//	$vdp_url_separator = Option::check_for_option(
//		'cdp_admin_options',
//		array(
//			'admin',
//			'general',
//			'vdp_url_separator',
//		),
//		'/'
//	);
//	$vdp_url_slash = ($vdp_url_separator === '/');
//	$vdp_url_dash = !$vdp_url_slash;
//	$content .= '<th><label>VDP URL Separator</label></th>';
//	$content .= '<td>';
//	$content .= table_rows::tr_setting_radio(
//		array(
//			'id'    => '',
//			'label' => 'slash',
//			'for'   => 'vdp-separator',
//			'name'  => 'cdp_admin_options[admin][general][vdp_url_separator]',
//			'value' => '/',
//			'var'   => $vdp_url_slash,
//		)
//	);
//	$content .= '</td>';
//	$content .= '<td>';
//	$content .= table_rows::tr_setting_radio(
//		array(
//			'id'    => '',
//			'label' => 'dash',
//			'for'   => 'vdp-separator',
//			'name'  => 'cdp_admin_options[admin][general][vdp_url_separator]',
//			'value' => '-',
//			'var'   => $vdp_url_dash,
//		)
//	);
//	$content .= '</td>';
//	$tool_tip_text = 'For replacing the default separator ( / ) in the vdp url.';
//	$content .= '<td><span class="tooltip"><span class="dashicons dashicons-editor-help"></span><div class="right">' . $tool_tip_text . '</div></span></td>';
//	$content .= '</tr>';

	$checked = Option::check_for_option(
		'cdp_admin_options',
		array(
			'admin',
			'general',
			'discourage_seo_visibility',
		)
	);
	$content .= table_rows::tr_setting_checkbox(
		array(
			'id'      => 'discourage_seo_visibility',
			'label'   => 'Disable Search Engine Visibility:',
			'name'    => 'cdp_admin_options[admin][general][discourage_seo_visibility]',
			'notes'   => 'Discourage search engines from indexing inventory pages',
			'checked' => $checked,
		)
	);
	$checked = Option::check_for_option( 'cdp_admin_options', array( 'admin', 'general', 'debug_plugin_info' ) );
	$content .= table_rows::tr_setting_checkbox(
		array(
			'id'      => 'debug_plugin_info',
			'label'   => 'Show Plugin Debug Info:',
			'name'    => 'cdp_admin_options[admin][general][debug_plugin_info]',
			'notes'   => 'Shows current plugin settings "commented out" on front end to help debug issues.',
			'checked' => $checked,
		)
	);
	$content .= table_rows::tr_info_div(
		array(
			'id'         => 'blog-id',
			'label'      => 'Blog ID',
			'name'       => 'blog_id',
			'textString' => get_current_blog_id(),
		)
	);

	$content .= table_rows::tr_info_div(
		array(
			'id'         => 'blog-id-inventory-table',
			'label'      => 'Blog Inventory Table',
			'name'       => 'blog_id',
			'textString' => CDP_INVENTORY_TABLE,
		)
	);

	if ( Display::theme_check() ) {
		$google_tags = Option::check_for_option(
			'cdp_admin_options',
			array(
				'admin',
				'general',
				'google_tags',
				'key',
			)
		);
		$content     .= table_rows::tr_setting_text(
			array(
				'id'    => 'google_tags',
				'class' => 'input-long',
				'label' => 'Google Tag',
				'name'  => 'cdp_admin_options[admin][general][google_tags][key]',
				'value' => $google_tags,
				'notes' => 'Google Tags ID (GTM-XXXX)',
			)
		);
	}

	$content .= refresh_options();
	$content .= '</tbody>';
	$content .= '</table>';

	if ( 0 !== $company_id ) {
		$content .= import_status();
	}

	return $content;

}


function refresh_options(): string {
	$content = table_rows::tr_button(
		array(
			'id'    => 'manually-run-company-job',
			'label' => 'Refresh Company Info',
			'name'  => 'companyRefresh',
			'value' => 'Manually Refresh Company',
		)
	);
	$content .= table_rows::tr_button(
		array(
			'id'    => 'manually-run-import-job-all',
			'label' => 'Manually Import All Inventory',
			'name'  => 'dealerRefresh',
			'value' => 'Manually Import All Inventory',
		)
	);
	$content .= '<tr class="manually-import-all"><td><div id="manually-run-import-job-confirm" class="td-full center">';
	$content .= '<p>This will delete all inventory from this <b>Blog</b>. If multiple dealers are assigned to this blog, it’s
            recommended to only manually update the dealer(s) which need to be updated. If you wish to continue, please 
            click confirm to initiate the full re-import.</p>';
	$content .= '<div id="manually-run-import-confirm-buttons">';
	$content .= '<input id="confirm-full-manual-run" class="cdp-run-input button"  type="button" value="Confirm"/>';
	$content .= '<input id="decline-full-manual-run" class="cdp-run-input button"  type="button" value="Close"/>';
	$content .= '</div>';
	$content .= '</div></td></tr>';

	return $content;

}

function import_status(): string {
	$company_db       = 0;
	$company_db_check = ( new Requests() )->cdp_db_init( 'company' );
	if ( $company_db_check ) {
		$company_db = $company_db_check;
	}
	$content = '<h3>Import Status</h3>';
	$content .= '<div id="container-company-import-counts-and-status">';
	$dealers = empty( $company_db['automalls'] ) ? array() : $company_db['automalls'];
	if ( $company_db ) {
		foreach ( $company_db as $dealer ) {
			$content .= dealer_display( $dealer, $company_db );
		}
	} else {
		$content .= '<div><span style="color: red;font-weight: bold;">Invalid Company id</span></div>';
	}

	$content .= '</div>';

	return $content;

}

function dealer_display( $dealer, $company_db ) {
	$db_request = new Requests();
	$content    = '<div class="company-item">';
	$content    .= company_header_info( array( 'dealer' => $dealer ) );
	if ( ! empty( $dealer['automalls'] ) ) {
		if ( ! empty( $company_db[0]['automalls'] ) && in_array( intval( $dealer['dealer_id'] ), $company_db[0]['automalls'], true ) ) {
			foreach ( $dealer['automalls'] as $dealer_id ) {
				$content .= '<div class="automall-items blue">';
				$dealer  = $db_request->cdp_company_dealer_request( $dealer_id )[0];
				$content .= company_header_info( array( 'dealer' => $dealer ) );
				$content .= counts_and_status( array( 'dealer' => $dealer ) );
				$content .= '</div>';
			}
		}
	} else {
		$content .= counts_and_status( array( 'dealer' => $dealer ) );
	}

	$content .= '</div>';

	return $content;
}


function company_header_info( $args ) {
	$dealer           = $args['dealer'];
	$automall         = ! empty( $dealer['automalls'] );
	$content          = '<div class="company-id-name">';
	$content          .= '<table style="">';
	$content          .= '<tbody>';
	$content          .= '<tr><td><div>VMS ID: ' . $dealer['dealer_id'] . '</div></td></tr>';
	$content          .= '<tr><td><div>Dealer Name: ' . $dealer['name'] . '</div></td></tr>';
	$text_string      = wp_date( 'Y-m-d H:i', strtotime( $dealer['last_updated'] ) );
	$tooltip          = human_time_diff( strtotime( $text_string ), time() ) . ' ago';
	$tooltip_position = 'right';
	$content          .= '<tr><td><div>Last Updated: ';
	$content          .= '<span class="tooltip">' . $text_string . '<span class="dashicons dashicons-info-outline"></span><div class="' . $tooltip_position . '">' . $tooltip . '</div></span></div></td></tr>';
	$content          .= '</tbody>';
	$content          .= '</table>';
	$content          .= '<hr>';
	if ( $automall ) {
		$content .= '<span>Company is an automall, does not contain its own inventory.</span>';
	}

	$content .= '</div>';

	return $content;

}


function counts_and_status( $args ) {
	$dealer  = $args['dealer'];
	$filters = Fn_Views::check_for_custom_import_filters( array( 'id' => $dealer['dealer_id'] ) );
	$content = '';
	if ( $filters ) {
		$content .= $filters['string'];
	}

	$content    .= '<div class="company-counts">';
	$content    .= '<table style="">';
	$content    .= '<tbody>';
	$content    .= '<tr><label>Counts:</label></tr>';
	$new_count  = ( new Requests() )->cdp_db_init(
		'searchtotal',
		array(
			'saleclass' => 'new',
			'dealer_id' => $dealer['dealer_id'],
		)
	);
	$content    .= table_rows::tr_info_div(
		array(
			'label'      => 'New Vehicles: ',
			'class'      => 'cdp-row import-dealer-new-count',
			'textString' => $new_count,
		)
	);
	$used_count = ( new Requests() )->cdp_db_init(
		'searchtotal',
		array(
			'saleclass' => 'used',
			'dealer_id' => $dealer['dealer_id'],
		)
	);
	$content    .= table_rows::tr_info_div(
		array(
			'label'      => 'Used Vehicles: ',
			'class'      => 'cdp-row import-dealer-used-count',
			'textString' => $used_count,
		)
	);
	$total      = ( new Requests() )->cdp_db_init( 'searchtotal', array( 'dealer_id' => $dealer['dealer_id'] ) );
	$content    .= table_rows::tr_info_div(
		array(
			'label'      => 'Total Vehicles: ',
			'class'      => 'cdp-row import-dealer-total-count',
			'textString' => $total,
		)
	);
	$content    .= '</tbody>';
	$content    .= '</table>';
	$content    .= '</div>';

	$content .= '<div class="inventory-status">';
	$content .= '<table style="">';
	$content .= '<tbody>';
	$content .= '<tr><label>Status:</label></tr>';
	$content .= table_rows::tr_info_div(
		array(
			'label'      => 'API Last Modified:',
			'class'      => '',
			'textString' => wp_date( 'Y-m-d H:i', strtotime( $dealer['inventory_api_last_modified'] ) ),
			'tooltip'    => array(
				'text'     => human_time_diff( strtotime( $dealer['inventory_api_last_modified'] ), time() ) . ' ago',
				'position' => 'right',
			),
		)
	);
	$content .= table_rows::tr_info_div(
		array(
			'label'      => 'Site Last Updated:',
			'class'      => '',
			'textString' => wp_date( 'Y-m-d H:i', strtotime( $dealer['inventory_last_updated'] ) ),
			'tooltip'    => array(
				'text'     => human_time_diff( strtotime( $dealer['inventory_last_updated'] ), time() ) . ' ago',
				'position' => 'right',
			),
		)
	);
	$content .= table_rows::tr_info_div(
		array(
			'label'      => 'Last Checked API:',
			'class'      => '',
			'textString' => wp_date( 'Y-m-d H:i', strtotime( $dealer['inventory_last_checked'] ) ),
			'tooltip'    => array(
				'text'     => human_time_diff( strtotime( $dealer['inventory_last_checked'] ), time() ) . ' ago',
				'position' => 'right',
			),
		)
	);
	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '</div>';

	$manual_import_tooltip = '<span class="tooltip"><span class="dashicons dashicons-info-outline"></span><div class="right">This will call the DealerTrend API for a database refresh the vehicle data.</div></span>';
	$content               .= '<div class="manually-run-button">';
	$content               .= '<input id="manually-run-dealer-inventory" class="cdp-run-input button disable" type="button" name="dealerImport" data-dealer="' . $dealer['dealer_id'] . '" value="Manually Import"/>';
	$content               .= $manual_import_tooltip . '</div>';

	$manual_import_purge_tooltip = '<span class="tooltip"><span class="dashicons dashicons-info-outline"></span><div class="right">Use this for an immediate change to the database data. This will purge the API cache.</div></span>';
	$content                     .= '<div class="manually-run-button">';
	$content                     .= '<input id="manually-run-dealer-inventory-purge" class="cdp-run-input button" type="button" name="dealerImportPurge" data-dealer="' . $dealer['dealer_id'] . '" value="Manually Import Purge"/>';
	$content                     .= $manual_import_purge_tooltip . '</div>';

	return $content;

}
