<?php

namespace CarDealerPress\Templates\Admin\Admin;

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

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

function dealers(): string {
	$option = Option::check_for_option( 'cdp_admin_options', array( 'admin', 'advanced', 'dealers' ) );
	// $id  = array_column($option, 'company_id');
	// array_multisort($id, SORT_ASC, $option);
	$count   = Generic::get_count_of( $option );
	$content = '';

	if ( 0 === $count ) {
		$row_id = 0;
	} else {
		$row_id = $count;
	}
	if ( ! empty( $option ) ) {
		$content .= current_dealers( $option );
		$content .= '<hr>';
	}
	$content .= create_dealer( 'Add New Dealer', $row_id );

	return $content;
}

function current_dealers( $option ): string {
	$content    = '<section id="current">';
	$content    .= '<div><h3>Current Dealers</h3></div>';
	$content    .= '<div id="current-rows" class="inner-table-content">';
	$content    .= '<table id="current-table" class="">';
	$content    .= '<tbody>';
	$db_request = new Requests();
	foreach ( $option as $row_id => $item ) {
		$dealer    = $db_request->cdp_company_dealer_request( $item['company_id'] )[0];
		$name      = $dealer['name'];
		$dealer_id = $dealer['dealer_id'];
		$content   .= "<tr id='outer-$row_id' data-tag='outer-$row_id' class='outer-row' data-label-name='Company id: $name' data-option='admin' data-tab='advanced' data-section='dealers'></tr>";
		$content   .= "<td>Company Name: $name - Dealer Id:  $dealer_id";
		// $content .= '<input id="edit-' . $row_id . '" class="edit-row" name="' . $row_id . '" type="button" value="Edit"/>';
		$content .= "<input id='removeTableRow' class='delete-row' data-dealer-id='$dealer_id' data-row-id='$row_id' type='button' value='Delete'/><td>";
		$content .= '</tr>';
		$content .= "<tr id='inner-$row_id' data-tag='dealer-inner-$row_id' class='detail inner-row'>";
		$content .= '<td>';
		$content .= '<div class="">';
		$content .= '<table class="table-wrap current">';
		$content .= '<tbody>';
		// content .= dealer_row(array('row_id'=> $row_id, 'show' => true, 'dealer' => $dealer));
		$content .= dealer_row(
			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 create_dealer( $title, $row_id ): string { # not working
	$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("' . dealers_add_table( $row_id ) . '"); });</script>';
	$content .= $script;
	$content .= '</section>';

	return $content;
}

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

	return $content;
}

function dealer_row( $args ): string {
	$row_id  = $args['row_id'];
	$show    = $args['show'] ?? false;
	$dealer  = $args['dealer'] ?? '';
	$value   = Option::check_for_option(
		'cdp_admin_options',
		array(
			'admin',
			'advanced',
			'dealers',
			$row_id,
			'company_id',
		)
	);
	$content = table_rows::tr_setting_text(
		array(
			'label' => 'Dealer Id',
			'id'    => "advanced-dealer-$row_id",
			'name'  => 'cdp_admin_options[admin][advanced][dealers][' . $row_id . '][company_id]',
			'value' => $value,
			'type'  => 'number',
			'class' => 'input-medium add-dealer',
		)
	);

	if ( empty( $value ) ) {
		$content .= '<tr><td>';
		$content .= "<input id='add-dealer-button' class='add-dealer-row' data-row-id='$row_id' type='button' value='Add Dealer'/><td>";
		$content .= '</td><tr>';
		$content .= '<tr><td>';
		$content .= "<div class='td-full-small center'><p id='dealer-ajax-message-advanced' class='data-ajax-message'></p></div>";
		$content .= '</td><tr>';
	}
	if ( $dealer ) {
		$content .= "<div class='import-dealer-content'>";
		$content .= "<div class='import-dealer-custom-imports'>";

		$filters = Option::check_for_option( 'cdp_admin_options', array( 'admin', 'advanced', 'import_filters' ) );
		$result  = array_multi_search( $filters, array( 'dealer_id' => $dealer['dealer_id'] ) );
		// $filters = Fn_Views::check_for_custom_import_filters(array('id' => $dealer['dealer_id']));
		$content .= empty( $result ) ? '' : '<div>Custom Import Filters</div>';
		$content .= '</div>';
		$content .= '</div>';
	}

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

	return $content;
}
