<?php
namespace CarDealerPress\Templates\Inventory;

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

use CarDealerPress\Includes\Database\Requests;
use CarDealerPress\includes\functions\Fn_Inventory;
use CarDealerPress\includes\helpers\helpers as helpers;

$fn_inventory      = new Fn_Inventory();
$this->vms->tracer = 'Obtaining requested inventory print page';

if ( isset( $this->parameters['vin'] ) ) {
    // Currently needed for custom imports on Flamingo.
    $vin                     = ( isset( $this->parameters['vin'] ) ? array( 'vin' => $this->parameters['vin'] ) : array() );
    $vehicle                 = ( $vin ? ( new Requests() )->cdp_db_init( 'inventory', $vin )[0] : array() );
    $contact_info            = array(
            'dealer_id'   => $vehicle['dealer_id'],
            'greeting'    => 'For more information', // TODO need to add an option.
            'dealer_name' => $this->company['name'],
            'phone'       => $this->company['phone'],
    );
    $vehicle['contact_info'] = $contact_info;
}

$type = isset( $vehicle['vin'] ) ? 'detail' : 'list';

if ( 'detail' === $type ) {
    ?>
    <style>
        #print-wrapper {
            display: block;
            overflow: hidden;
            clear: both;
            float: none;
            max-width: 500px;
            margin: 0 auto;
        }

        .print-header {
            display: block;
            overflow: hidden;
            clear: both;
            float: none;
            width: 100%;
            text-align: center;
        }

        .print-header-name, .print-header-phone {
            display: block;
            float: left;
            width: 50%;
        }

        .print-vehicle-information {
            width: 60%;
            margin: 5% auto;
        }

        .print-vehicle-information div {
            border-bottom: 2px solid #000;
        }

        .print-vehicle-information span {
            margin: 0 5% 0 0;
        }

        .print-image {
            text-align: center;
            margin: 5% 0;
            position: relative;
        }

        .print-price {
            text-align: center;
        }

        .print-description {
            font-size: 12px;
            text-align: center;
            margin: 0 0 5%;
        }

        .print-disclaimer {
            font-size: 10px;
            text-align: center;
            display: block;
            width: 100%;
        }

        .marked-sold-overlay {
            position: absolute;
            z-index: 5;
            top: 0;
            left: 120;
            width: 30%;
            object-fit: contain;
            height: auto;
        }
    </style>

    <div id="print-wrapper">
        <div id="detail-print">
            <div class="print-header">
                <div class="print-headline">
                    <?php if ( $vehicle['sold_on'] ) {
                        $content = '<h1>We are sorry but the ' . $vehicle['year'] . ' ' . $vehicle['make'] . ' ' . $vehicle['model'] . ' ' . $vehicle['trim'] . ' is no longer available.</h1>';
                        helpers::output_wp_kses( $content );
                    }
                    ?>
                    <h2><?php helpers::output_wp_kses( $vehicle['year'] . ' ' . $vehicle['make'] . ' ' . $vehicle['model'] . ' ' . $vehicle['trim'] ); ?></h2>
                </div>
                <div class="print-header-name"><?php helpers::output_wp_kses( $fn_inventory->get_custom_contact_setting( $vehicle['saleclass'], $vehicle['dealer_id'], 'name' ) ); ?></div>
                <div class="print-header-phone"><?php helpers::output_wp_kses( $fn_inventory->get_custom_contact_setting( $vehicle['saleclass'], $vehicle['dealer_id'], 'phone' ) ); ?></div>
            </div>
            <div class="print-top">
                <div class="print-image">
                    <?php
                    // phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
                    $sold_overlay = ! empty( $vehicle['sold_on'] ) ? '<img class="marked-sold-overlay" src="' . CDP_IMG . 'templates/inventory/sold_overlay.png" />' : '';
                    $list_image   = $fn_inventory->no_photo_replace( $vehicle['thumbnail'] );
                    // phpcs:ignore PluginCheck.CodeAnalysis.ImageFunctions.NonEnqueuedImage
                    $content = "$sold_overlay<img src='$list_image' width='300' alt='' title='$vehicle[dealer]'/>";
                    helpers::output_wp_kses( $content );
                    ?>
                </div>

                <div class="print-price">
                    <?php
                    $price   = $fn_inventory->get_price_display(
                            array(
                                    'company' => $this->company,
                                    'vehicle' => $vehicle,
                                    'theme'   => 'print',
                            )
                    );
                    $content = ( ! empty( $price['msrp_text'] ) && 'new' === strtolower( $vehicle['saleclass'] ) ? $price['msrp_text'] : '' ) . '
							' . $price['primary_text'] . $price['compare_price_text'] . $price['expire_text'];
                    helpers::output_wp_kses( $content );

                    ?>
                </div>

                <div class="print-vehicle-information">
                    <div><span>Stock Number:</span> <?php echo esc_html( $vehicle['stock'] ); ?></div>
                    <div><span>VIN:</span> <?php echo esc_html( $vehicle['vin'] ); ?></div>
                    <div><span>Odometer:</span> <?php echo esc_html( $vehicle['odometer'] ); ?></div>
                    <div><span>Exterior:</span> <?php echo esc_html( $vehicle['exterior'] ); ?></div>
                    <div><span>Interior:</span> <?php echo esc_html( $vehicle['interior'] ); ?></div>
                    <div><span>Engine:</span> <?php echo esc_html( $vehicle['engine'] ); ?></div>
                    <div><span>Transmission:</span> <?php echo esc_html( $vehicle['transmission'] ); ?></div>
                </div>
            </div>
            <div class="print-bottom">
                <?php if ( $vehicle['description'] ) { ?>
                    <div class="print-description">
                        <?php helpers::output_wp_kses( $vehicle['description'] ); ?>
                    </div>
                <?php } ?>

                <div class="print-disclaimer">
                    <?php helpers::output_wp_kses( $fn_inventory->get_disclaimer() ); ?>
                </div>
            </div>

        </div>
    </div>

    <?php

} else {

    get_header();
    echo '<h2>Not able to display vehicle print page.</h2>';
    get_footer();

}

?>
