<?php $ids = ! empty( $_GET['ids'] ) ? array_filter( explode( ',', $_GET['ids'] ) ) : array(); ?>

<?php $query = new WP_Query( array(
	'post_type'         => 'property',
	'posts_per_page'    => count( $ids ),
	'post__in'          => $ids
) ); ?>

<?php if ( count( $ids ) > 0 ) : ?>
	<table class="compare-table">
		<thead>
		<tr>
			<th></th>

			<?php foreach ( $query->posts as $post ) : ?>
				<th>
					<div class="compare-table-image">
						<a href="<?php echo get_permalink( $post->ID ); ?>">
							<?php echo get_the_post_thumbnail( $post->ID, 'large'); ?>

							<?php $is_featured = get_post_meta( $post->ID, REALIA_PROPERTY_PREFIX . 'featured', true ); ?>
							<?php $is_reduced = get_post_meta( $post->ID, REALIA_PROPERTY_PREFIX . 'reduced', true ); ?>
							<?php $is_sticky = get_post_meta( $post->ID, REALIA_PROPERTY_PREFIX . 'sticky', true ); ?>

							<?php if ( $is_featured && $is_reduced ) : ?>
								<span class="property-badge"><?php echo __( 'Featured', 'realia' ); ?> / <?php echo __( 'Reduced', 'realia' ); ?></span>
							<?php elseif ( $is_featured ) : ?>
								<span class="property-badge"><?php echo __( 'Featured', 'realia' ); ?></span>
							<?php elseif ( $is_reduced ) : ?>
								<span class="property-badge"><?php echo __( 'Reduced', 'realia' ); ?></span>
							<?php endif; ?>

							<?php if ( $is_sticky ) : ?>
								<span class="property-badge property-badge-sticky"><?php echo __( 'TOP', 'realia' ); ?></span>
							<?php endif; ?>
						</a>
					</div><!-- /.compare-table-image -->

					<div class="compare-table-title">
						<h2>
							<a href="<?php echo get_permalink( $post->ID ); ?>">
								<?php echo esc_html( $post->post_title ); ?>
							</a>
						</h2>

						<h3><?php echo Realia_Query::get_property_location_name( $post->ID ); ?></h3>
					</div><!-- /.compare-table-title -->
				</th>
			<?php endforeach; ?>
		</tr>
		</thead>

		<tbody>
		<!-- PRICE -->
		<tr>
			<td class="compare-table-label"><?php echo __( 'Price', 'realia' ); ?></td>
			<?php foreach ( $query->posts as $post ) : ?>
				<td class="compare-table-col"><?php echo Realia_Price::get_property_price( $post->ID ); ?></td>
			<?php endforeach; ?>
		</tr>
		<!-- TYPE -->
		<tr>
			<td class="compare-table-label"><?php echo __( 'Type', 'realia' ); ?></td>
			<?php foreach ( $query->posts as $post ) : ?>
				<td class="compare-table-col"><?php echo Realia_Query::get_property_type_name( $post->ID ); ?></td>
			<?php endforeach; ?>
		</tr>
		<!-- CONTRACT  -->
		<tr>
			<td class="compare-table-label"><?php echo __( 'Contract', 'realia' ); ?></td>
			<?php foreach ( $query->posts as $post ) : ?>
				<?php $contract = Realia_Query::get_property_contract_name( $post->ID ); ?>
				<td class="compare-table-col"><?php echo ! empty( $contract ) ? esc_attr( $contract ) : '-'; ?></td>
			<?php endforeach; ?>
		</tr>
		<!-- BEDS  -->
		<tr>
			<td class="compare-table-label"><?php echo __( 'Beds', 'realia' ); ?></td>
			<?php foreach ( $query->posts as $post ) : ?>
				<?php $value = get_post_meta( $post->ID, REALIA_PROPERTY_PREFIX . 'attributes_beds', true ); ?>
				<td class="compare-table-col"><?php echo ! empty( $value ) ? $value : '-'; ?></td>
			<?php endforeach; ?>
		</tr>
		<!-- BATHS -->
		<tr>
			<td class="compare-table-label"><?php echo __( 'Baths', 'realia' ); ?></td>
			<?php foreach ( $query->posts as $post ) : ?>
				<?php $value = get_post_meta( $post->ID, REALIA_PROPERTY_PREFIX . 'attributes_baths', true ); ?>
				<td class="compare-table-col"><?php echo ! empty( $value ) ? $value : '-'; ?></td>
			<?php endforeach; ?>
		</tr>
		<!-- GARAGES -->
		<tr>
			<td class="compare-table-label"><?php echo __( 'Garages', 'realia' ); ?></td>
			<?php foreach ( $query->posts as $post ) : ?>
				<?php $value = get_post_meta( $post->ID, REALIA_PROPERTY_PREFIX . 'attributes_garages', true ); ?>
				<td class="compare-table-col"><?php echo ! empty( $value ) ? $value : '-'; ?></td>
			<?php endforeach; ?>
		</tr>
		<!-- AREA -->
		<tr>
			<td class="compare-table-label"><?php echo __( 'Area', 'realia' ); ?></td>
			<?php foreach ( $query->posts as $post ) : ?>
				<?php $value = get_post_meta( $post->ID, REALIA_PROPERTY_PREFIX . 'attributes_area', true ); ?>
				<?php $measure = get_theme_mod( 'realia_measurement_area_unit', 'sqft' ); ?>
				<td class="compare-table-col"><?php echo ! empty( $value ) ? $value . ' ' . $measure : '-'; ?></td>
			<?php endforeach; ?>
		</tr>

		<?php $amenities = get_terms( 'amenities', array(
			'hide_empty' => false,
		) ); ?>

		<?php if ( is_array( $amenities ) ) : ?>
			<?php foreach ( $amenities as $amenity ) : ?>
				<tr>
					<td class="compare-table-label"><?php echo esc_html( $amenity->name ); ?></td>

					<?php foreach ( $query->posts as $post ) : ?>
						<td class="compare-table-col">
							<?php if ( has_term( $amenity->term_id, 'amenities', $post->ID ) ) : ?>
								<span class="yes"><i class="fa fa-check"></i><span>
                            <?php else : ?>
                                <span class="no"><i class="fa fa-times"></i></span>
                            <?php endif; ?>
						</td>
					<?php endforeach; ?>
				</tr>
			<?php endforeach ?>
		<?php endif; ?>
		</tbody>
	</table>
<?php else : ?>
	<?php get_template_part( 'templates/content-not-found' ); ?>
<?php endif; ?>
