<?php
namespace CarDealerPress\Templates\Inventory;

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

use CarDealerPress\Includes\Database\Requests;
use CarDealerPress\includes\helpers\Allowed_Html;
use CarDealerPress\includes\helpers\helpers;
use CarDealerPress\Includes\Helpers\Option;
use CarDealerPress\Includes\Helpers\Remote_Requests;
use CarDealerPress\templates\partials\inventory\Schema_Island;

if ( $vehicle ) {

    $companies = ( new Requests() )->cdp_db_init( 'company' );
    $company   = array();
    foreach ( $companies as $key => $info ) {
        if ( $info['dealer_id'] === $vehicle['dealer_id'] ) {
            $company = $companies[ $key ];
        }
    }

    $price                    = $fn_inventory->get_price_display(
            array(
                    'company' => $this->company,
                    'vehicle' => $vehicle,
                    'theme'   => 'inventory',
            )
    );
    $vehicle['primary_price'] = $price['primary_price'];

    $parameters['saleclass'] = $vehicle['saleclass'];
    $fn_inventory->apply_gravity_form_hooks( $vehicle );
    // splide.
    wp_enqueue_script( 'cdp_splide_js', CDP_JS . 'splide/splide.min.js', array( 'jquery' ), '4.1.4', true );
    wp_enqueue_style( 'cdp_splide_style', CDP_CSS . 'splide/splide.min.css', array(), '4.1.4' );
    // Map.
    if ( $this->theme_options['theme']['inventory_display']['detail']['maps']['key'] ) {
        $map_output = $ext_functions->extender_map_generator( $vehicle['dealer_id'] );
    } else {
        $map_output = null;
    }
    $ext_functions->load_parameters( $this->parameters );

    helpers::output_wp_kses( $ext_functions->extender_google_tag_script( $vehicle ) );

    ?>
    <div id="inventory-wrapper">
        <?php
        if ( $this->theme_options['theme']['inventory_display']['general']['saved_vehicles']['display'] ) {
            helpers::output_wp_kses( $ext_functions->extender_saved_vehicle_container() );
        }
        ?>
        <div id="inventory-detail" itemscope itemtype="https://schema.org/Car" class="inventory-main-container">
            <div id="<?php helpers::output_wp_kses( $vehicle['vin'] . '" class="inventory-detail-vehicle-container vehicle-container company-' . helpers::company_id() . ' location-' . $vehicle['dealer_id'] ); ?>">
                <div id="page-link" class="vehicle-save" style="display:none;" data-name="link"
                     href="<?php echo esc_url( $page_url ); ?>"></div>
                <div id="inventory-detail-banner">
                    <div class="banner-container previous-link" onclick="window.history.go(-1); return false;">
                        <span class="previous-icon banner-icon"></span>
                        <span class="previous-text banner-text">Previous Search</span>
                    </div>
                    <div class="banner-container friendly-print"
                         onclick="window.open('?print_page','popup','width=800,height=900,scrollbars=yes,resizable=no,toolbar=no,directories=no,location=no,menubar=yes,status=no,left=0,top=0'); return false">
                        <span class="print-icon banner-icon"></span>
                        <span class="print-text banner-text">Friendly Print</span>
                    </div>
                    <div class="banner-container dealer-number">
                        <a href="tel: <?php echo esc_html( $fn_inventory->get_custom_contact_setting( $saleclass, $vehicle['dealer_id'], 'phone' ) ); ?>">
                            <span class="phone-icon banner-icon"></span>
                            <span class="phone-text banner-text">Sales: <?php echo esc_html( $fn_inventory->get_custom_contact_setting( $saleclass, $vehicle['dealer_id'], 'phone' ) ); ?> </span>
                        </a>
                    </div>
                    <?php
                    if ( 'on' === $this->theme_options['theme']['inventory_display']['detail']['general']['social']['facebook']['enable'] ) {
                        echo '<div class="banner-container facebook-share">';
                        echo '<div class="fb-share-button" data-href="' . esc_url( $page_url ) . '" data-layout="button" data-size="small" data-mobile-iframe="true">
								<a class="fb-xfbml-parse-ignore" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2Fplugins%2F&amp;src=sdkpreparse">Share</a>
								</div>';
                        echo '</div>';
                    }
                    ?>
                </div>
                <?php
                $banners = Option::check_for_option( 'cdp_theme_options', array(
                        'theme',
                        'inventory_display',
                        'banners'
                ) );
                if ( ! empty( $banners ) ) {
                    $content        = '<div id="cdp-banner-top-container">';
                    $banner_results = $fn_inventory->cdp_get_banners( 'detail', 'top', $banners, $this->parameters );
                    foreach ( $banner_results as $banner ) {
                        $content .= $banner;
                    }
                    $content .= '</div>';
                    echo wp_kses( $content, 'post' );
                }
                helpers::output_wp_kses( $ext_functions->detail_page_sold_vehicle( $vehicle ) );
                ?>
                <div id="inventory-detail-top-container">
                    <?php
                    echo wp_kses( $ext_functions->extender_detail_top_view( $vehicle, $price, $company ), 'post' );
                    ?>
                </div>
                <div id="inventory-detail-body-container">
                    <?php echo $ext_functions->extender_detail_body_view( $vehicle ); ?>
                </div>
                <div id="inventory-detail-bottom-container">
                    <?php
                    if ( $this->theme_options['theme']['inventory_display']['detail']['similar_vehicles']['display_similar'] ) {
                        helpers::output_wp_kses( '<div id="similar-container" class="inventory-detail-bottom"><div id="similar-title">Similar Vehicles</div>' . $ext_functions->extender_similar_vehicle_pull( $vehicle ) . '</div>' );
                    }
                    ?>
                    <?php
                    echo empty( $map_output ) ? '' : '<div id="map-container" class="inventory-detail-bottom"></div>';
                    wp_register_script( 'cdp_injector_js', CDP_JS . 'templates/inventory/injector.js', 'jquery', CDP_VERSION, true ); //injector.js is a placeholder file.
                    wp_enqueue_script( 'cdp_injector_js' );
                    wp_add_inline_script( 'cdp_injector_js', $map_output ); ?>
                    <?php helpers::output_wp_kses( '<div class="inventory-detail-bottom inventory-disclaimer"><p>' . $fn_inventory->get_disclaimer() . '</p></div>' ); ?>
                </div>
            </div>
            <?php
            if ( is_active_sidebar( 'vehicle-detail-page' ) ) :
                echo '<div id="inventory-widget-area" >';
                dynamic_sidebar( 'vehicle-detail-page' );
                echo '</div>';
            endif;
            ?>
        </div>
        <?php
        if ( $this->theme_options['theme']['inventory_display']['general']['saved_vehicles']['display'] ) {
            helpers::output_wp_kses( $ext_functions->extender_compare_vehicle_container() );
        }
        ?>
        <?php helpers::output_wp_kses( Schema_Island::island( $vehicle, $company, $price ) ); ?>
    </div>
    <?php
}
?>
