<?php

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

use CarDealerPress\Includes\Database\Requests;
use CarDealerPress\includes\functions\Fn_Inventory;
use CarDealerPress\Includes\Helpers\Option;

$this->fn_inventory = new Fn_Inventory();
if ( ( new Requests() )->cdp_company_request() ) {
	$this->company = ( new Requests() )->cdp_company_request()[0];
} else {
	$this->company = array();
}

$rules    = get_option( 'rewrite_rules' );
$url_rule = isset( $rules['^(inventory)'] );

$state = $this->company['state'];
$city  = $this->company['city'];

$site_url = site_url();

$raw_makes = ( new Requests() )->cdp_db_init( 'make', array( 'saleclass' => $this->sc_atts['saleclass'] ) );

if ( empty( $this->sc_atts['make'] ) ) {
	$makes = $raw_makes;
} else {
	foreach ( $raw_makes as $key => $make_data ) {
		if ( $make_data['Make'] === $this->sc_atts['make'] ) {
			$makes[] = $make_data;
		}
	}
}

$sc_content = '<div id="sc-inventory-counter-' . strtolower( esc_attr( $this->sc_atts['saleclass'] ) ) . '" class="counter-container" >';

if ( empty( $makes ) ) {
	$sc_content .= '<div class="sc-count-not-found"><h2><strong>No count inventory found. Double check shortcode settings.</strong></h2></div>';
} else {
	foreach ( $makes as $key => $make_data ) {
		$link       = $this->fn_inventory->generate_inventory_link( array(
				'params' => array(
					'saleclass' => ucfirst( $this->sc_atts['saleclass'] ),
					'make'      => $make_data['Make'],
				)
			)
		);
		$sc_content .= '<div class="counter-wrapper ' . $make_data['Make'] . ' ' . strtolower( esc_attr( $this->sc_atts['saleclass'] ) ) . '">';
		$sc_content .= '<div class="make-label"><a href="' . $link . '"><span class="make-name">' . $make_data['Make'] . '</span>' . ( ! empty( $this->sc_atts['show_make_count'] ) ? ' <span class="make-count">(' . $make_data['Total'] . ')</span>' : '' ) . '</a></div>';
		if ( $this->sc_atts['show_models'] || $this->sc_atts['model'] ) {
			$raw_models = ( new Requests() )->cdp_db_init(
				'model',
				array(
					'saleclass' => $this->sc_atts['saleclass'],
					'make'      => $make_data['Make'],
				)
			);
			if ( empty( $this->sc_atts['model'] ) ) {
				$models = $raw_models;
			} else {
				foreach ( $raw_models as $key => $model_data ) {
					if ( $model_data['Model'] === $this->sc_atts['model'] ) {
						$models[] = $model_data;
					}
				}
			}
			foreach ( $models as $key => $model_data ) {
				$link       = $this->fn_inventory->generate_inventory_link( array(
						'params' => array(
							'saleclass' => ucfirst( $this->sc_atts['saleclass'] ),
							'make'      => $make_data['Make'],
							'model'     => $model_data['Model'],
						)
					)
				);
				$sc_content .= '<div class="model-label ' . $model_data['Model'] . '"><a href="' . $link . '"><span class="model-name">' . $model_data['Model'] . '</span>' . ( $this->sc_atts['show_model_count'] ? ' <span class="model-count">(' . $model_data['Total'] . ')</span>' : '' ) . '</a></div>';
				if ( $this->sc_atts['show_trims'] ) {
					$trims = ( new Requests() )->cdp_db_init(
						'trim',
						array(
							'saleclass' => $this->sc_atts['saleclass'],
							'make'      => $make_data['Make'],
							'model'     => $model_data['Model'],
						)
					);
					foreach ( $trims as $key => $trim_data ) {
						$link       = $this->fn_inventory->generate_inventory_link( array(
								'params' => array(
									'saleclass' => ucfirst( $this->sc_atts['saleclass'] ),
									'make'      => $make_data['Make'],
									'model'     => $model_data['Model'],
									'trim'      => rawurlencode( $trim_data['Trim'] ),
								)
							)
						);
						$sc_content .= '<div class="trim-label ' . $trim_data['Trim'] . '"><a href="' . $link . '"><span class="trim-name">' . $trim_data['Trim'] . '</span>' . ( $this->sc_atts['show_trim_count'] ? ' <span class="trim-count">(' . $trim_data['Total'] . ')</span>' : '' ) . '</a></div>';
					}
				}
			}
			if ( $this->sc_atts['model'] ) {
				$trims = ( new Requests() )->cdp_db_init(
					'trim',
					array(
						'saleclass' => $this->sc_atts['saleclass'],
						'make'      => $make_data['Make'],
						'model'     => $this->sc_atts['model'],
					)
				);
				foreach ( $trims as $key => $trim_data ) {
					$link       = $this->fn_inventory->generate_inventory_link( array(
							'params' => array(
								'saleclass' => ucfirst( $this->sc_atts['saleclass'] ),
								'make'      => $make_data['Make'],
								'model'     => $model_data['Model'],
								'trim'      => rawurlencode( $trim_data['Trim'] ),
							)
						)
					);
					$sc_content .= '<div class="trim-label ' . $trim_data['Trim'] . '"><a href="' . $link . '"><span class="trim-name">' . $trim_data['Trim'] . '</span>' . ( $this->sc_atts['show_trim_count'] ? ' <span class="trim-count">(' . $trim_data['Total'] . ')</span>' : '' ) . '</a></div>';
				}
			}
		}
		$sc_content .= '</div>';
	}
}

$sc_content       .= '</div>';
$this->sc_content = $sc_content;
