<?php
/**
 * The template for displaying product content within loops
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/content-product.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see     https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates
 * @version 3.6.0
 */

defined( 'ABSPATH' ) || exit;

global $product, $post, $PIXAD_Autos;;
// Ensure visibility.
$catalog_item_id = get_post_meta($product->id, 'pixba_sim_product_id', true);


$Settings = new PIXAD_Settings();
$settings = $Settings->getSettings('WP_OPTIONS', '_pixad_autos_settings', true);
$is_scale_image = isset($settings['scale_image']) ? $settings['scale_image'] : 0;
$validate = $Settings->getSettings('WP_OPTIONS', '_pixad_autos_validation', true); // Get validation settings

$showInSidebar = pixad::getsideviewfields($validate);
$validate = pixad::validation($validate); // Fix undefined index notice

$PIXAD_Autos->Query_Args(["auto_id" => $catalog_item_id]);

if(class_exists('Pix_Autos')){
    if(isset($catalog_item_id) && $catalog_item_id === ''){
        if ( empty( $product ) || ! $product->is_visible() ) {
            return;
        }
    }
    $status = get_post_status($catalog_item_id);
    if(isset($status) && $status == 'pending'){
        return;
    }
} else {
    if ( empty( $product ) || ! $product->is_visible() ) {
        return;
    }
}
?>
<?php if(class_exists('Pix_Autos')){ ?>
    <div class="col-md-4 col-sm-6 col-xs-12">
        <div class="slider-grid__inner slider-grid__inner_mod-b">
            <div class="card__img">
                <?php if ('' != get_the_post_thumbnail($catalog_item_id)) : ?>
                    <a href="<?php echo get_permalink($catalog_item_id); ?>">
                        <?php  if($is_scale_image) : ?>
                            <div class="scale-image-box"  style='background-image: url(<?php echo get_the_post_thumbnail_url($catalog_item_id, 'autozone_latest_item'); ?>);'></div>
                        <?php  else : ?>
                            <?php echo get_the_post_thumbnail($catalog_item_id, 'autozone_latest_item', array('class' => 'img-responsive')); ?>
                        <?php endif; ?>
                    </a>
                <?php else : ?>
                    <img class="no-image" src="<?php echo PIXAD_AUTO_URI . 'assets/img/no_image.jpg'; ?>" alt="no-image">
                <?php endif; ?>


                <?php if (get_post_meta($catalog_item_id, 'pixad_auto_featured_text', true)) : ?>
                    <span class="card__wrap-label"><span class="card__label"><?php echo get_post_meta($catalog_item_id, 'pixad_auto_featured_text', true); ?></span></span>
                <?php endif; ?>


                <?php if ($validate['auto-price_show'] && $PIXAD_Autos->get_meta('_auto_price')) : ?>
                    <?php
                    $custom_price_catalog = get_post_meta($post->ID, 'custom_price_catalog', 1);
                    $price_catalog = $custom_price_catalog ? $custom_price_catalog : $PIXAD_Autos->get_price(null, $daysCount, $discount_data);
                    //$price_catalog = is_numeric($PIXAD_Autos->get_meta('_auto_price')) || $PIXAD_Autos->get_meta('_auto_price') == '' ? $PIXAD_Autos->get_price() : $auto_translate[$PIXAD_Autos->get_price()];
                    // var_dump($price_catalog);
                    ?>

                    <?php
                    $Settings = new PIXAD_Settings();
                    $options = $Settings->getSettings('WP_OPTIONS', '_pixad_autos_settings', true);

                    $text_after = "";
                    if ($daysCount === 1) {
                        $text_after     = isset($options['autos_price_text']) ? $options['autos_price_text'] : '';
                    } else {
                        $text_after = "/ per " . $daysCount . " days";
                    }
                    // var_dump($text_after);
                    $currentTime = new DateTime();
                    $currency = pixad_get_currencies($options['autos_site_currency']);
                    $pixad_auto_price_season = get_post_meta($catalog_item_id, 'pixad_auto_price_season', []);

                    foreach ($pixad_auto_price_season as $key => $price) {
                        if(is_array($price) && !empty($price)){
                            foreach ($price as $p) {

                                $date_start = DateTime::createFromFormat('Y-m-d', $p['pixad_auto_price_season_start']);
                                $date_end = DateTime::createFromFormat('Y-m-d', $p['pixad_auto_price_season_end']);
                                $date_start->getTimestamp();
                                $date_end->getTimestamp();
                                $currentTime->getTimestamp();
                                if (($date_start <= $currentTime) && ($currentTime <= $date_end)) {
                                    $price_catalog = $currency['symbol'] . $p['pixad_auto_price_season_price'] * $daysCount . '<span class="after-price-text">' . $text_after . '</span>';
                                } else {
                                    $price_catalog_bottom = '<div class="season_prices">';
                                    $price_catalog_bottom .= '<span class="card__price-number">' . $currency['symbol'] . $p['pixad_auto_price_season_price'] * $daysCount . '<span class="after-price-text">' . $text_after . '</span></span>';
                                    $price_catalog_bottom .= '<span class="price_description">' . $date_start->format('d F') . __(' to ', 'autozone') . $date_end->format('d F') . '</span>';
                                    $price_catalog_bottom .= '</div>';
                                }
                            }
                        }
                    }
                    ?>

                    <span class="slider-grid__price_wrap"><span class="slider-grid__price"><span>
                            <?php echo wp_kses_post($price_catalog); ?></span></span></span>
                <?php endif; ?>
                <?php do_action('autozone_autos_single_auto_img', $post); ?>

                <?php if (is_user_logged_in()) : ?>
                    <?php
                    $author_ID = get_post_field('post_author', $catalog_item_id);
                    $user = get_user_by('ID', $author_ID);
                    if(class_exists('WeDevs_Dokan')){
                        $vendor = dokan()->vendor->get( $author_ID );
                        if ($vendor->data->user_nicename != ''){
                            $permalink_levelss =  get_site_url() . '/members/' . $vendor->data->user_nicename . '/add_item/?auto_id=' . $catalog_item_id;
                            $permalink_show =  get_site_url() . '/members/' . $vendor->data->user_nicename . '/autos?hide=false&auto_id=' . $catalog_item_id;
                            $permalink_hide =  get_site_url() . '/members/' . $vendor->data->user_nicename . '/autos?hide=true&auto_id=' . $catalog_item_id;
                        }
                    } else {
                        $permalink_levelss =  get_site_url() . '/members/' . $user->user_login . '/add_item/?auto_id=' . $catalog_item_id;
                        $permalink_show =  get_site_url() . '/members/' . $user->user_login . '/autos?hide=false&auto_id=' . $catalog_item_id;
                        $permalink_hide =  get_site_url() . '/members/' . $user->user_login . '/autos?hide=true&auto_id=' . $catalog_item_id;
                    }

                    $dokan_dash = get_option('dokan_pages')['dashboard'];
                    $permalink_levelss = get_permalink($dokan_dash). '/add/' . '?auto_id='.$catalog_item_id;

                    if ($author_ID == get_current_user_id()) { ?>
                        <a class="tm-autos-top-edit-button" href=" <?php echo esc_url($permalink_levelss);?>"><span><?php echo __('Edit', 'autozone') ?></span></a>
                    <?php } ?>
                <?php endif; ?>

            </div>
            <div class="tmpl-gray-footer">
                <a class="tmpl-slider-grid__name" href="<?php echo get_permalink($catalog_item_id); ?>"><?php echo wp_kses_post(get_the_title()) ?></a>
                <?php if (!empty($post_rating)) : ?>
                    <div class="star-rating"><span style="width:<?php echo  esc_html(array_sum($post_rating) / count($post_rating) * 20); ?>%"></span></div>
                <?php endif; ?>

                <ul class="tmpl-slider-grid__info list-unstyled">

                    <?php
                    foreach ($showInSidebar as $id => $sideAttribute) : ?>
                        <?php $id = '_' . $id;
                        $id = str_replace('-', '_', $id);

                        $val_attr = get_post_meta($catalog_item_id, $id);
                        ?>
                        <?php if ($val_attr[0]) : ?>
                            <li><i class="<?php echo esc_html($sideAttribute['icon']) ?>"></i>
                                <?php
                                if (!empty($auto_translate[$val_attr[0]])) {
                                    echo esc_html($auto_translate[$val_attr[0]]);


                                } else {
                                    echo esc_html($val_attr[0]);


                                }
                                ?>
                            </li>
                        <?php endif; ?>

                    <?php endforeach; ?>
                </ul>
                <?php
                if (isset($price_catalog_bottom)) {
                    echo wp_kses_post($price_catalog_bottom);
                    $price_catalog_bottom = '';
                }
                ?>


                <div class="grid__book_link">
                    <a href="<?php echo get_permalink($catalog_item_id); ?><?php esc_attr_e($datesRequest); ?>"><?php esc_html_e('Read More', 'autozone') ?><i class="fa fa-angle-double-right" aria-hidden="true"></i></a>
                </div>

            </div>



        </div>
    </div>
<?php } ?>
