<?php
/**
 * File for the Help section of the plugin.
 *
 * @package CarDealerPress
 */

namespace CarDealerPress\Templates\Admin\Admin;

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

use CarDealerPress\Admin\Functions\Main;
use CarDealerPress\Includes\Database\Requests;
use CarDealerPress\includes\helpers\helpers as helpers;
use CarDealerPress\templates\partials\admin\Table_Rows;

/**
 * View for the Help section of the plugin.
 *
 * @package CarDealerPress
 */
function help(): string {
	$home_url           = home_url();
	$inventory_url_new  = "$home_url" . helpers::rewrite_rules()['link_new'];
	$inventory_url_used = "$home_url" . helpers::rewrite_rules()['link_used'];
	$video_check        = ( new Requests() )->cdp_inventory_video_check();

	if ( 0 === intval( $video_check[0]['Total'] ) ) {
		$video_site_map_url = '';
	} else {
		$video_site_map_url = '<div><a target="_blank" href="' . $home_url . '/video-vehicle-sitemap.xml">Sitemap Videos</a></div>';
	}
	if ( ! empty( Main::$company['seo'] ) ) {
		$saleclass_check     = ( new Requests() )->cdp_db_init( 'saleclass' );
		$sitemap_information = '<tr><td class="td-full center">';
		if ( 'new' === $saleclass_check[0]['saleclass'] ) {
			$sitemap_information .= '<div><a target="_blank" href="' . $home_url . '/new-vehicle-sitemap.xml">Sitemap New</a></div>';
		}
		$sitemap_information .= '<div><a target="_blank" href="' . $home_url . '/used-vehicle-sitemap.xml">Sitemap Used</a></div>';
		$sitemap_information .= $video_site_map_url;
		$sitemap_information .= '</td></tr>';
	} else {
		$sitemap_information = '<tr><td class="td-full center">SEO is not set up.</td></tr>';
	}

	$api_url           = urldecode( BASE_INVENTORY_API_URL );
	$admin_plugins_url = admin_url( 'plugins.php' );
	$cdp_admin_page    = esc_url( add_query_arg( array( 'page' => 'cardealerpress_admin&tab=general' ), $admin_plugins_url ) );

	$reset_rewrite                       = table_rows::tr_button_with_confirmation(
		array(
			'id'      => 'run-flush',
			'class'   => 'do-action-button',
			'value'   => 'Manually Flush Re-Write Rules',
			'ajax_fn' => 'runFlush',
			'message' => 'This will flush the the rewrite rules.',
		)
	);
	$clear_transient                     = table_rows::tr_button_with_confirmation(
		array(
			'id'      => 'admin-clear-transient',
			'class'   => 'do-action-button',
			'value'   => 'Manually Clear Update Transient',
			'ajax_fn' => 'clearTransient',
			'message' => 'This will clear the transient data.',
		)
	);
	$delete_cron                         = table_rows::tr_button_with_confirmation(
		array(
			'id'      => 'admin-delete-cron',
			'class'   => 'do-action-button',
			'value'   => 'Manually Delete WP Cron',
			'ajax_fn' => 'deleteCDPCronJob',
			'message' => 'This will delete the cron job that pulls inventory.',
		)
	);
	$truncate_inventory_table            = table_rows::tr_button_with_confirmation(
		array(
			'id'      => 'admin-truncate-inventory',
			'class'   => 'do-action-button',
			'value'   => 'Reset Inventory Table',
			'ajax_fn' => 'resetInventory',
			'message' => 'This will delete the inventory table.',
		)
	);
	$reset                               = table_rows::tr_button_with_confirmation(
		array(
			'id'      => 'cdp-reset-button',
			'class'   => 'do-action-button',
			'value'   => 'Perform Clean Reset',
			'ajax_fn' => 'runCleanReset',
			'message' => 'This will RESET the plugin back to its original default settings, and deactivate the plugin.',
		)
	);
	$alter_tables                        = table_rows::tr_button_with_confirmation(
		array(
			'id'      => 'cdp-alter-table-button',
			'class'   => 'do-action-button',
			'value'   => 'Update Company/Inventory Tables',
			'ajax_fn' => 'alterTables',
			'message' => 'This will check and update the database tables for any updates needed.',
		)
	);
	$show_companies_table_columns        = table_rows::tr_button_with_confirmation(
		array(
			'id'      => 'cdp-companies-table-column-button',
			'class'   => 'do-action-button',
			'value'   => 'Show Companies Table Columns',
			'ajax_fn' => 'showCompaniesTableColumns',
			'message' => 'This will output the companies table columns data, to the log file.',
		)
	);
	$show_companies_table                = table_rows::tr_button_with_confirmation(
		array(
			'id'      => 'cdp-companies-table-button',
			'class'   => 'do-action-button',
			'value'   => 'Show Companies Table',
			'ajax_fn' => 'showCompaniesTable',
			'message' => 'This will outpute the companies table data, to the log file.',
		)
	);
	$show_grants                         = table_rows::tr_button_with_confirmation(
		array(
			'id'      => 'cdp-grants-button',
			'class'   => 'do-action-button',
			'value'   => 'Show Grants',
			'ajax_fn' => 'showGrants',
			'message' => 'This will output the grants for the current user, to the log file.',
		)
	);
	$create_company_and_inventory_tables = table_rows::tr_button_with_confirmation(
		array(
			'id'      => 'cdp-create-inventory-tables-button',
			'class'   => 'do-action-button',
			'value'   => 'Create Company/Inventory Tables',
			'ajax_fn' => 'createCompanyAndInventoryTables',
			'message' => "This create the inventory tables for the inventory data, if they do not exist.",
		)
	);

	$content = '<div id="Help" class="tab-content">';
	$content .= '<div id="cdp-content-wrapper" class="settings-wrapper">';
	$content .= '<table class="help-info-box">';
	$content .= '<thead>';
	$content .= '<tr>';
	$content .= '<th class="th-label"><label><h3>Initial Setup</h3></label></th>';
	$content .= '</tr>';
	$content .= '</thead>';
	$content .= '<tbody>';
	$content .= '<tr>';
	$content .= '<td class="td-full center">';
	$content .= "<p>To get the plugin started you will need a valid Company ID.</p>";
	$content .= '<p>A company ID will be provided to you upon purchasing a subscription with ';
	$content .= '<a href="https://cardealerpress.com" target="_blank">CarDealerPress</a>';
	$content .= '</p>';
	$content .= '<p>After you have received a Company ID, you will need to go to the ';
	$content .= "<a id='settings-link' href='$cdp_admin_page' title='CarDealerPress API Settings'>Admin Settings</a>";
	$content .= ' and fill in the respective company ID field.';
	$content .= '</p>';
	$content .= '<p>For a full list of features and tutorials: ';
	$content .= '<a href="https://cardealerpress.com/cardealerpresss-overview/" title="CarDealerPress Help Link" target="_blank">CarDealerPress Features</a>';
	$content .= '</p>';
	$content .= '<p>For any further assistance or inventory questions: ';
	$content .= '<a href="https://support.dealertrend.com" target="_blank" title="CarDealerPress Inventory Help Link">CarDealerPress Help</a>';
	$content .= '</p>';
	$content .= '</td>';
	$content .= '</tr>';
	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '<table class="help-info-box">';
	$content .= '<thead>';
	$content .= '<tr>';
	$content .= '<th class="th-label"><label><h3>Viewing Inventory</h3></label></th>';
	$content .= '</tr>';
	$content .= '</thead>';
	$content .= '<tbody>';
	$content .= '<tr>';
	$content .= '<td class="td-full center">';
	$content .= '<p>If the VMS and Company Feed are both loaded, you may view your inventory here:<br>';
	$content .= "<div><a href='$inventory_url_new' target='_blank'>New Inventory</a></div>";
	$content .= "<div><a href='$inventory_url_used' target='_blank'>Used Inventory</a></div>";
	$content .= '</p>';
	$content .= '<p>Please note that any pages or sub-pages that reside at this permalink will no longer be shown.</p>';
	$content .= '</td>';
	$content .= '</tr>';
	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '<table class="help-info-box">';
	$content .= '<thead>';
	$content .= '<tr>';
	$content .= '<th class="th-label"><label><h3>Sitemap Links<small>(Requires SEO Helpers)</small></h3></label></th>';
	$content .= '</tr>';
	$content .= '</thead>';
	$content .= '<tbody>';
	$content .= $sitemap_information;
	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '<table class="help-info-box">';
	$content .= '<thead>';
	$content .= '<tr>';
	$content .= '<th class="th-label"><label><h3>API URL</h3></label></th>';
	$content .= '</tr>';
	$content .= '</thead>';
	$content .= '<tbody>';
	$content .= '<tr>';
	$content .= '<td class="td-full center">';
	$content .= '<input disabled name="cdp_admin_options[admin][help][host]" type="text" class="cdp-input text-inject" style="width: 200px; margin: 10px 0 0 0;" value="' . $api_url . '">';
	$content .= '</td>';
	$content .= '</tr>';
	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '<table class="help-info-box">';
	$content .= '<thead>';
	$content .= '<tr>';
	$content .= '<th class="th-label"><label><h3>Clean Up and Resets</h3></label></th>';
	$content .= '</tr>';
	$content .= '</thead>';
	$content .= '<tbody>';
	$content .= $reset_rewrite;
	$content .= $clear_transient;
	$content .= $delete_cron;
	$content .= $truncate_inventory_table;
	$content .= $reset;
	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '<table class="help-info-box">';
	$content .= '<thead>';
	$content .= '<tr>';
	$content .= '<th class="th-label"><label><h3>Debug and Database Information</h3></label>';
	$content .= '<span>Useful for support issues.</span></th>';
	$content .= '</tr>';
	$content .= '</thead>';
	$content .= '<tbody>';
	$content .= $create_company_and_inventory_tables;
	$content .= $alter_tables;
	$content .= $show_companies_table_columns;
	$content .= $show_companies_table;
	$content .= $show_grants;
	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '</div>';
	$content .= '</div>';

	return $content;
}
