<?php

namespace CarDealerPress\Templates\ShortCode;

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

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

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

$this->vms->tracer = 'Obtaining requested sc inventory.';
$inventory         = ( new Requests() )->cdp_db_init( 'inventory', $this->inventory_params );

$sc_content = '<div class="sc-slider-wrapper">';
if ( isset( $this->inventory_params['title'] ) ) {
	$sc_content .= '<div class="sc-slider-title">' . $this->inventory_params['title'] . '</div>';
	unset( $this->inventory_params['title'] );
}
if ( empty( $inventory ) ) {
	$sc_content .= '<div class="sc-not-found"><h2><strong>Search criteria did not return any results.</strong></h2></div>';
} else {
	if ( $this->splide_params['autoplay'] ) {
		//unset( $this->splide_params['autoplay'] ); //conflicts with js
		$splide .= "data-splide='{";
		$class  = 'sc-slider-init';
		foreach ( $this->splide_params as $key => $value ) {
			if ( is_numeric( $value ) ) {
				$splide .= '"' . $key . '": ' . $value . ' ,';
			} else {
				$splide .= '"' . $key . '": ' . str_contains( $value, '1' ) . ' ,';
			}
		}
		$splide = rtrim( $splide, ',' );
		$splide .= "}'";
	} else {
		$class  = 'sc-slider-flex';
		$splide .= "data-splide='{\"autoplay\": false}'";
	}
	$sc_content .= '<div class="sc-slider-container splide" ' . $splide . ' aria-labelledby="vehicle-inventory-slider">';
	$sc_content .= '<div class="splide__arrows">';
	$sc_content .= '<div class="splide__arrow splide__arrow--prev"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40" width="40" height="40"><path d="m15.5 0.932-4.3 4.38 14.5 14.6-14.5 14.5 4.3 4.4 14.6-14.6 4.4-4.3-4.4-4.4-14.6-14.6z"></path></svg></div>';
	$sc_content .= '<div class="splide__arrow splide__arrow--next"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40" width="40" height="40"><path d="m15.5 0.932-4.3 4.38 14.5 14.6-14.5 14.5 4.3 4.4 14.6-14.6 4.4-4.3-4.4-4.4-14.6-14.6z"></path></svg></div>';
	$sc_content .= '</div>';
	$sc_content .= '<div class="splide__track">';
	$sc_content .= '<ul class="splide__list sc-slide-wrapper">';

	foreach ( $inventory as $vehicle ) {
		$sc_content .= $this->sc_views->get_sc_slider_view( $vehicle, 'sc-slider', $this->company, $this->theme_options, $url_rule, $this->inventory_params );
	}
	$sc_content .= '</ul>';

	$sc_content .= '</div></div>';
}
$sc_content .= '</div>';

$this->sc_content .= $sc_content;
