<?php
// Defaults
if (empty($layout)) {
    $layout = 'normal';
}
if (empty($columns)) {
    $columns = '3';
}
if (!isset($img_ratio)) {
    $img_ratio = 80;
}
if (empty($show_title)) {
    $show_title = '';
}
if (empty($show_category)) {
    $show_category = '';
}
if (empty($show_buttons)) {
    $show_buttons = '';
}
if (empty($image_size)) {
    $image_size = 'large';
}
if (empty($content_position)) {
    $content_position = 'middle';
}


$post_class = array('projects-i', 'projects-i-cp-'.$content_position);

$post_class[] = 'pl-15';
$post_class[] = 'pr-15';

$category = get_the_terms(get_the_ID(), 'motor_project_category');
if (!empty($category)) {
    foreach ($category as $cat) {
        $post_class[] = $cat->slug;
    }
}

if (!empty($align)) {
    $post_class[] = 'projects-i-'.$align;
}

/*if ($layout === 'packery') {
    $item_index = $index + (($page_num - 1) * $posts_per_page) + 1;
    $columns_default = $columns;
    $img_ratio_default = $img_ratio;
    $packery_column = motor_packery_column($item_index, $layout_packery_items, $columns);
    $columns = $packery_column['width'];
    $img_ratio = $img_ratio * $packery_column['height'];
}*/

if ($layout === 'packery') {
    $item_index = $index + (($page_num - 1) * $posts_per_page) + 1;
    //$columns_default = $columns;
    $img_ratio_default = $img_ratio;
    $packery_column = motor_packery_column($item_index, $layout_packery_items, $columns);
    $img_ratio = $img_ratio * $packery_column['height'];
    switch ($packery_column['width']) {
        case '1':
            $post_class[] = 'col-sm-6 col-xl-1'; break;
        case '2':
            $post_class[] = 'col-sm-6 col-xl-2'; break;
        case '3':
            $post_class[] = 'col-sm-6 col-xl-3'; break;
        case '4':
            $post_class[] = 'col-sm-6 col-xl-4'; break;
        case '5':
            $post_class[] = 'col-sm-6 col-xl-5'; break;
        case '6':
            $post_class[] = 'col-sm-6 col-xl-6'; break;
        case '7':
            $post_class[] = 'col-sm-6 col-xl-7'; break;
        case '8':
            $post_class[] = 'col-sm-6 col-xl-8'; break;
        case '9':
            $post_class[] = 'col-sm-6 col-xl-9'; break;
        case '10':
            $post_class[] = 'col-sm-6 col-xl-10'; break;
        case '11':
            $post_class[] = 'col-sm-6 col-xl-11'; break;
        case '12':
            $post_class[] = 'col-sm-12 full-width'; break;
        default:
            $post_class[] = 'col-sm-6 col-xl-3'; break;
    }
} else {
switch ($columns) {
    case '1':
        $post_class[] = 'col-sm-12 full-width';
        break;
    case '2':
        $post_class[] = 'col-sm-6';
        break;
    case '3':
        $post_class[] = 'col-sm-6 col-xl-4';
        break;
    case '4':
        $post_class[] = 'col-sm-6 col-lg-4 col-xl-3';
        break;
    case '6':
        $post_class[] = 'col-sm-6 col-md-4 col-lg-3 col-xl-2';
        break;
    case 'metro':
        $item_index = $index + (($page_num - 1) * $posts_per_page);
        $metro_column = motor_metro_column($item_index, $layout_metro_row);
        switch ($metro_column['column']) {
            case '1':
                $post_class[] = 'col-sm-6 col-xl-1'; break;
            case '2':
                $post_class[] = 'col-sm-6 col-xl-2'; break;
            case '3':
                $post_class[] = 'col-sm-6 col-xl-3'; break;
            case '4':
                $post_class[] = 'col-sm-6 col-xl-4'; break;
            case '5':
                $post_class[] = 'col-sm-6 col-xl-5'; break;
            case '6':
                $post_class[] = 'col-sm-6 col-xl-6'; break;
            case '7':
                $post_class[] = 'col-sm-6 col-xl-7'; break;
            case '8':
                $post_class[] = 'col-sm-6 col-xl-8'; break;
            case '9':
                $post_class[] = 'col-sm-6 col-xl-9'; break;
            case '10':
                $post_class[] = 'col-sm-6 col-xl-10'; break;
            case '11':
                $post_class[] = 'col-sm-6 col-xl-11'; break;
            case '12':
                $post_class[] = 'col-sm-12 full-width'; break;
            default:
                $post_class[] = 'col-sm-6 col-xl-3'; break;
        }
        break;
}
}

$img_src = array();
if (has_post_thumbnail()) {
    $img_src = wp_get_attachment_image_src(get_post_thumbnail_id(), $image_size);
}
?>
<article id="post-<?php the_ID(); ?>" <?php post_class($post_class); ?>>
<div class="projects-i-inner">

    <?php if (!empty($img_src[0])) : ?>
        <a href="<?php the_permalink(); ?>" class="projects-i-img" style="background-image: url(<?php echo esc_url($img_src[0]); ?>);<?php if (!empty($metro_column['height'])) { echo 'height:'.intval($metro_column['height']).'px;'; } ?>">
            <div class="img-ratio img-ratio-<?php echo intval($img_ratio); ?>"></div>
            <img src="<?php echo esc_url($img_src[0]); ?>" alt="<?php the_title(); ?>">
        </a>
    <?php else:
        if (empty($img_ratio)) {
            $img_ratio = 60;
        }
        ?>
        <a href="<?php the_permalink(); ?>" class="projects-i-img"<?php if (!empty($metro_column['height'])) { echo 'style="height:'.intval($metro_column['height']).'px;"'; } ?>>
            <div class="img-ratio img-ratio-<?php echo intval($img_ratio); ?>"></div>
        </a>
    <?php endif; ?>

    <div class="projects-i-cont">
        <a class="projects-i-cont-link" href="<?php the_permalink(); ?>"></a>

        <?php if ($show_category == 'show') : ?>
            <?php if (get_post_type() == 'motor_project') : ?>
                <?php if (!empty($category)) : ?>
                    <div class="projects-i-cat">
                        <?php foreach ($category as $key=>$cat) : ?>
                            <a href="<?php echo esc_url(get_category_link($cat->term_id)); ?>"><?php echo esc_attr($cat->name); ?></a><?php echo ($key+1<count($category)) ? ' / ' : ''; ?>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            <?php else: ?>
                <div class="projects-i-cat"><span><?php echo get_post_type(); ?></span></div>
            <?php endif; ?>
        <?php endif; ?>

        <?php if ($show_title == 'show') : ?>
        <h3 class="projects-i-ttl">
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </h3>
        <?php endif; ?>

        <?php if ($show_buttons == 'show') : ?>
        <div class="projects-i-btns">
            <a href="#" data-url="<?php echo esc_url(admin_url('admin-ajax.php')); ?>" data-file="template-parts/project-quickview" data-project-qview="<?php the_ID(); ?>" title="<?php esc_attr_e('Quick View', 'motor'); ?>"><?php if (!empty($settings['content_style_buttons_qview']['url'])) { echo motor_get_contents($settings['content_style_buttons_qview']['url']); } else { echo motor_get_contents(get_template_directory_uri().'/img/ico-loupe.svg'); } ?></a>
            <a href="<?php the_permalink(); ?>" title="<?php esc_attr_e('Show Details', 'motor'); ?>"><?php if (!empty($settings['content_style_buttons_link']['url'])) { echo motor_get_contents($settings['content_style_buttons_link']['url']); } else { echo motor_get_contents(get_template_directory_uri().'/img/ico-link.svg'); } ?></a>
        </div>
        <?php endif; ?>
    </div>

</div>
</article>
<?php
if ($layout === 'packery') {
    //$columns = $columns_default;
    $img_ratio = $img_ratio_default;
}