<?php
get_header();
$layout=lt_theme_option('insight_archive_layout');
$show_filters=(bool)lt_theme_option('archive_show_filters');
$current=array('q'=>lt_theme_filter_value('lt_q'),'type'=>lt_theme_filter_value('lt_type','int'),'topic'=>lt_theme_filter_value('lt_topic','int'),'practice'=>lt_theme_filter_value('lt_practice','int'),'sector'=>lt_theme_filter_value('lt_sector','int'),'author'=>lt_theme_filter_value('lt_author','int'),'year'=>lt_theme_filter_value('lt_year','year'),'sort'=>lt_theme_filter_value('lt_sort')?:'date-desc');
$types=$show_filters&&taxonomy_exists('insight_type')?get_terms(array('taxonomy'=>'insight_type','hide_empty'=>true,'number'=>200)):array();
$topics=$show_filters&&taxonomy_exists('insight_topic')?get_terms(array('taxonomy'=>'insight_topic','hide_empty'=>true,'number'=>200)):array();
$sectors=$show_filters&&taxonomy_exists('sector')?get_terms(array('taxonomy'=>'sector','hide_empty'=>true,'number'=>200)):array();
$practices=$show_filters&&post_type_exists('practice')?get_posts(array('post_type'=>'practice','posts_per_page'=>200,'post_status'=>'publish','orderby'=>'title','order'=>'ASC','no_found_rows'=>true)):array();
$authors=$show_filters&&post_type_exists('lawyer')?get_posts(array('post_type'=>'lawyer','posts_per_page'=>200,'post_status'=>'publish','orderby'=>'title','order'=>'ASC','no_found_rows'=>true)):array();
$years=array();if($show_filters){global $wpdb;$years=$wpdb->get_col($wpdb->prepare("SELECT DISTINCT YEAR(post_date) FROM {$wpdb->posts} WHERE post_type=%s AND post_status=%s ORDER BY YEAR(post_date) DESC LIMIT 100",'insight','publish'));}
?>
<main id="main-content"><section class="lt-page-hero <?php echo esc_attr(lt_theme_archive_hero_class()); ?>"><div class="lt-container"><?php lt_theme_breadcrumbs(); ?><div class="lt-kicker"><?php echo esc_html(function_exists('lt_core_label')?lt_core_label('insights',__('Knowledge','law-temple-theme')):__('Knowledge','law-temple-theme')); ?></div><h1><?php echo esc_html(lt_theme_option('insight_archive_heading') ?: (function_exists('lt_core_label')?lt_core_label('insights',__('Insights','law-temple-theme')):post_type_archive_title('',false))); ?></h1><?php if(lt_theme_option('insight_archive_intro')): ?><p class="lt-lead"><?php echo esc_html(lt_theme_option('insight_archive_intro')); ?></p><?php endif; ?></div></section>
<section class="lt-section"><div class="lt-container">
<?php if($show_filters): ?><form class="lt-filterbar lt-filterbar--server" method="get" action="<?php echo esc_url(get_post_type_archive_link('insight')); ?>" role="search" aria-label="<?php esc_attr_e('Filter insights','law-temple-theme'); ?>">
<label class="screen-reader-text" for="lt-insight-q"><?php esc_html_e('Search insights','law-temple-theme'); ?></label><input id="lt-insight-q" type="search" name="lt_q" value="<?php echo esc_attr($current['q']); ?>" placeholder="<?php esc_attr_e('Search insights','law-temple-theme'); ?>">
<?php if(!is_wp_error($types)&&$types): ?><select name="lt_type"><option value=""><?php esc_html_e('All content types','law-temple-theme'); ?></option><?php foreach($types as $t): ?><option value="<?php echo esc_attr($t->term_id); ?>" <?php selected($current['type'],$t->term_id); ?>><?php echo esc_html($t->name); ?></option><?php endforeach; ?></select><?php endif; ?>
<?php if(!is_wp_error($topics)&&$topics): ?><select name="lt_topic"><option value=""><?php esc_html_e('All topics','law-temple-theme'); ?></option><?php foreach($topics as $t): ?><option value="<?php echo esc_attr($t->term_id); ?>" <?php selected($current['topic'],$t->term_id); ?>><?php echo esc_html($t->name); ?></option><?php endforeach; ?></select><?php endif; ?>
<?php if($practices): ?><select name="lt_practice"><option value=""><?php esc_html_e('All Practice Areas','law-temple-theme'); ?></option><?php foreach($practices as $p): ?><option value="<?php echo esc_attr($p->ID); ?>" <?php selected($current['practice'],$p->ID); ?>><?php echo esc_html($p->post_title); ?></option><?php endforeach; ?></select><?php endif; ?>
<?php if(!is_wp_error($sectors)&&$sectors): ?><select name="lt_sector"><option value=""><?php esc_html_e('All Sectors','law-temple-theme'); ?></option><?php foreach($sectors as $t): ?><option value="<?php echo esc_attr($t->term_id); ?>" <?php selected($current['sector'],$t->term_id); ?>><?php echo esc_html($t->name); ?></option><?php endforeach; ?></select><?php endif; ?>
<?php if($authors): ?><select name="lt_author"><option value=""><?php esc_html_e('All authors','law-temple-theme'); ?></option><?php foreach($authors as $a): ?><option value="<?php echo esc_attr($a->ID); ?>" <?php selected($current['author'],$a->ID); ?>><?php echo esc_html($a->post_title); ?></option><?php endforeach; ?></select><?php endif; ?>
<?php if($years): ?><select name="lt_year"><option value=""><?php esc_html_e('All years','law-temple-theme'); ?></option><?php foreach($years as $y): ?><option value="<?php echo esc_attr($y); ?>" <?php selected($current['year'],absint($y)); ?>><?php echo esc_html($y); ?></option><?php endforeach; ?></select><?php endif; ?>
<select name="lt_sort"><option value="date-desc" <?php selected($current['sort'],'date-desc'); ?>><?php esc_html_e('Newest first','law-temple-theme'); ?></option><option value="date-asc" <?php selected($current['sort'],'date-asc'); ?>><?php esc_html_e('Oldest first','law-temple-theme'); ?></option><option value="name-asc" <?php selected($current['sort'],'name-asc'); ?>><?php esc_html_e('Title A–Z','law-temple-theme'); ?></option></select>
<button class="lt-button lt-button--primary" type="submit"><?php esc_html_e('Apply filters','law-temple-theme'); ?></button><a class="lt-button lt-button--text" href="<?php echo esc_url(lt_theme_filter_reset_url()); ?>"><?php esc_html_e('Reset','law-temple-theme'); ?></a></form><?php endif; ?>
<?php if(have_posts()): ?><div class="lt-grid lt-archive-responsive lt-insights--<?php echo esc_attr($layout); ?>" style="<?php echo esc_attr(lt_theme_archive_columns_style('insight')); ?>"><?php while(have_posts()):the_post(); ?><article class="lt-card"><?php if(lt_theme_option('archive_show_images')&&has_post_thumbnail()): ?><a class="lt-card-media" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('lt-card'); ?></a><?php endif; ?><div class="lt-card-meta"><?php echo esc_html(get_the_date()); ?></div><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3><?php if(lt_theme_option('archive_show_excerpts'))the_excerpt(); ?></article><?php endwhile; ?></div><?php lt_theme_server_pagination(); ?><?php else: ?><div class="lt-empty-state"><h2><?php esc_html_e('No insights match these filters','law-temple-theme'); ?></h2><p><?php esc_html_e('Try changing the search, Practice Area, Sector, author or year.','law-temple-theme'); ?></p><a class="lt-button lt-button--outline" href="<?php echo esc_url(lt_theme_filter_reset_url()); ?>"><?php esc_html_e('Clear filters','law-temple-theme'); ?></a></div><?php endif; ?>
</div></section></main><?php get_footer(); ?>
