<?php

namespace CarDealerPress\templates\partials\inventory;

use CarDealerPress\Includes\Functions\Extender_Functions;
use CarDealerPress\Includes\Functions\Fn_Inventory;
use CarDealerPress\Includes\Helpers\Option;

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

// for non ajax themes.
class Forms_Display {

	public $fn_inventory;
	public $ext_functions;

	public function __construct() {
		$this->fn_inventory  = new Fn_Inventory();
		$this->ext_functions = new Extender_Functions();
	}

	public function tabs( $forms, $name ): string {
		$tab_labels = $tab_content = '';
		$class   = 'active';
		$content = '';
		if ( ! empty( $forms ) ) {
			foreach ( $forms as $form_data ) {
				if ( ! empty( $form_data ) ) {
					foreach ( $form_data as $idx => $item ) {
						$title = esc_html( $item['name'] );
						$form_code = $item['content'];
						$tab_labels  .= "<div id='$name-body-tab-$idx' class='tab-name $class '>$title</div>";
						$tab_content .= "<div id='$name-body-tab-$idx-content' class='tab-content $class'>";
						$tab_content .= "<div class='tab-content-inner'>$form_code</div>";
						$tab_content .= "</div>";
						$class       = '';
					}
				}
			}
			$content .= '<div class="' . $name . '-container">';
			$content .= ( empty( $tab_labels ) ? '' : '<div class="tab-name-container">' . $tab_labels . '</div>' );
			$content .= ( empty( $tab_content ) ? '' : '<div class="tab-content-container">' . $tab_content . '</div>' );
			$content .= '</div>';
		}
		return $content;
	}

	public function buttons( $forms, $name ): string {
		$content = '';
		if ( ! empty( $forms ) ) {
			foreach ( $forms as $tab => $data ) {
				if ( ! empty( $data ) ) {
					foreach ( $data as $index => $item ) {
						$item_name = $item['name'];
						$form_name = strtolower(str_replace( ' ', '_', $item_name ));
						$content .= "<div class='form-container form-$form_name'>";
						if ($index === 0) {
							$content .= '<details open>';
						} else {
							$content .= '<details>';
						}
						$content .= "<summary class='form-wrapper button' >".esc_html($item_name)."</summary>";
                        $content .= '<div class="dropdown-content '.$form_name.'">';
						$content .= $item['content'];
						$content .= '</div>';
						$content .= "<button class='from-close' onclick='this.parentElement.removeAttribute(\"open\")'>Close</button>";
						$content .= '</details>';
						$content .= '</div>';
					}
				}
			}
		}
		return $content;
	}
}

