<?php

namespace CarDealerPress\Templates\Admin\Theme;

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

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

function style_assets(): string {
	$option  = Option::check_for_option( 'cdp_theme_options', array( 'theme', 'inventory_display', 'style_assets' ) );
	$count   = \CarDealerPress\Includes\Helpers\Generic::get_count_of( $option );
	$content = '';

	if ( 0 === $count ) {
		$row_id = 0;
	} else {
		$row_id = $count;
	}

	if ( ! empty( $option ) ) {
		$content .= current_styles( $option );
		$content .= '<hr>';
	}
	$content .= create_style( 'Create', $row_id );

	return $content;
}


function current_styles( $option ): string {
	$content = "<section id='current'>";
	$content .= '<div><h3>Current Styles</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='style_assets'></tr>";
		$content .= '<td>Name: ' . $name . ' ';
		$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='inner-$row_id' class='detail inner-row'>";
		$content .= '<td>';
		$content .= "<div class=''>";
		$content .= "<table class='table-wrap current'>";
		$content .= '<tbody>';
		$content .= row_style( $row_id, true );
		$content .= '</tbody>';
		$content .= '</table>';
		$content .= '</div>';
		$content .= '</td>';
		$content .= '</tr>';
	}
	$content .= '</tbody>';
	$content .= '</table>';
	$content .= '</div>';
	$content .= '</section>';

	return $content;

}

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

	return $content;
}

function new_style( $row_id ) {
	$content = "<table id='addNewTable' class='table-wrap add'>";
	$content .= '<tbody>';
	$content .= row_style( $row_id );
	$content .= '</tbody>';
	$content .= '</table>';

	return $content;
}

function row_style( $row_id, $show = false ): string {
	$content = '';
	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'style_assets',
			$row_id,
			'name',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'Name',
			'id'    => "name-$row_id",
			'class' => 'input-long',
			'name'  => 'cdp_theme_options[theme][inventory_display][style_assets][' . $row_id . '][name]',
			'value' => $value,
		)
	);

	$saleclass = array( 'all', 'new', 'used', 'certified' );
	$value     = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'style_assets',
			$row_id,
			'saleclass',
		)
	);
	$content   .= table_rows::tr_select_dropown(
		array(
			'label' => 'Sale Class',
			'id'    => "saleclass-$row_id",
			'name'  => 'cdp_theme_options[theme][inventory_display][style_assets][' . $row_id . '][saleclass]',
			'value' => $value,
			'list'  => $saleclass,
		)
	);

	$page    = array( 'all', 'list', 'detail' );
	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'style_assets',
			$row_id,
			'page',
		)
	);
	$content .= table_rows::tr_select_dropown(
		array(
			'label' => 'Page',
			'id'    => "page-$row_id",
			'name'  => 'cdp_theme_options[theme][inventory_display][style_assets][' . $row_id . '][page]',
			'value' => $value,
			'list'  => $page,
		)
	);

	$value   = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'style_assets',
			$row_id,
			'url',
		)
	);
	$content .= table_rows::tr_setting_text(
		array(
			'label' => 'Url',
			'id'    => "url-$row_id",
			'name'  => 'cdp_theme_options[theme][inventory_display][style_assets][' . $row_id . '][url]',
			'value' => $value,
			'class' => 'input-long',
		)
	);

	$checked = Option::check_for_option(
		'cdp_theme_options',
		array(
			'theme',
			'inventory_display',
			'style_assets',
			$row_id,
			'override',
		)
	);
	$content .= table_rows::tr_setting_checkbox(
		array(
			'label'   => 'Override',
			'id'      => "override-$row_id",
			'name'    => 'cdp_theme_options[theme][inventory_display][style_assets][' . $row_id . '][override]',
			'checked' => $checked,
			'notes'   => 'Will remove theme style',
		)
	);

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

	return $content;
}
