<?php
/**
 * User: Media-Store.net
 * Date: 19.09.2016
 * Time: 22:34
 */

namespace wpi_classes;

//use \wpi_classes\SingleViewClass;

class ListViewClass {

	public function __construct() {

		// Laden der Optinen aus DB
		$optionsClass         = new WpOptionsClass();
		$this -> admin        = new AdminClass();
		$this -> options      = $optionsClass -> wpi_get_options();
		$this -> upload_url   = $this -> options[ 'wpi_upload_url' ];
		$this -> list_options = $this -> options[ 'wpi_list_options' ];

		// Info über Versionsstaus
		$admin       = new AdminClass;
		$this -> pro = $admin -> versionStatus;

	}


	public function wrapper_template() {
		ob_start();
		$single_view = new SingleViewClass;

		?>
		<!-- Template von WP Immo Manager created by Media-Store.net - http://media-store.net -->

		<div id="wpi-primary" class=" wpi container-fluid">
			<div class="row">
				<?php
				if ( $this -> admin -> versionStatus != false ) :
					switch ( $this -> list_options[ 'wpi_list_search' ] ) {
						case 'searchfilter' :
							echo do_shortcode( '[search_filter_form]' );
							break;
						case 'searchbar' :
							echo $single_view -> view_searchfield_wpmi();
							break;
						default :
							'';
					}
				endif;
				?>

				<div class="clearfix"></div>
				<div class="content-div">
					<?php echo $this -> content_template( $meta = '' ); ?>
				</div>
			</div><!-- row -->
		</div><!-- wpi-primary -->
		<!-- Ende Template von WP Immo Manager created by Media-Store.net - http://media-store.net -->
		<?php
		return ob_get_clean();
	}

	public function content_template() {
		ob_start();

		/**
		 * Anzeige einer Sidebar wenn in den Einstellungen aktiviert
		 */
		if ( $this -> list_options[ 'wpi_list_sidebar' ] == 'true' ) :
			$content_row = 'col-xs-12 col-md-9';
			$sidebar     = $this -> list_options[ 'wpi_list_sidebar_name' ];
			$sidebar     = substr( strstr( $sidebar, '-' ), 1 );
			// zeige sidebar
			echo '<div class="aside col-md-3">';
			get_sidebar( $sidebar );
			echo '</div>';
		else :
			$content_row = 'col-xs-12 col-md-12';
		endif;
		?>
		<!-- Ende Aside -->
		<div id="wpi-main" class="content site-main <?php echo $content_row; ?>" role="main">
			<article id="post-<?php the_ID(); ?>" <?php post_class( 'archiv-immobilien' ); ?>>
				<div class="panel panel-default">
					<div class="panel-body">
						<div class="row">

							<?php

							global $wp_query;

							$big = 999999999; // need an unlikely integer

							$pagination = paginate_links( array(
								'base'    => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
								'format'  => '?paged=%#%',
								'current' => max( 1, get_query_var( 'paged' ) ),
								'total'   => $wp_query -> max_num_pages,
								'type'    => 'list',
							) );

							// LOOP
							if ( $wp_query -> have_posts() ) :

								while ( $wp_query -> have_posts() ) :

									$wp_query -> the_post();
									$id = get_the_ID();
									$meta = get_post_meta( get_the_ID() );
									switch ( $this -> list_options[ 'wpi_list_view_column' ] ):
										case 'column':
											echo $this -> list_view_columns( $meta );
											break;

										case 'div':
											?>
											<div class="content">
												<div class="col-md-3">
													<?php echo $this -> list_view_image( $meta ); ?>
												</div>
												<div class="col-md-9">
													<header class="wpi-header">
														<h2>
															<?php echo $this->new_post_label($id); ?>
															<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
														</h2>
													</header>
													<div class="details-panel">
														<?php echo $this -> list_view_meta( $meta, 'div' ); ?>
													</div>

													<?php
													// Link Button
													echo $this -> list_link_button();
													?>
												</div>
											</div>
											<?php
											break;

										default:
											?>
											<div class="content">
												<div class="col-xs-12">
													<header class="wpi-header">
														<h2>
															<?php echo $this->new_post_label($id); ?>
															<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
														</h2>
													</header>
												</div>
												<div class="col-md-3">
													<?php echo $this -> list_view_image( $meta ); ?>
												</div>
												<div class="col-md-9">
													<?php

													if ( $this -> list_options[ 'wpi_list_excerpt' ] === 'true' ) :
														echo $this -> list_view_excerpt( $meta );
													else :
														echo $this -> list_view_meta( $meta );
													endif;
													// Link Button
													echo $this -> list_link_button();
													?>
												</div>
											</div>
											<?php
											break;
									endswitch;
									?>

									<?php

								endwhile; // end of the loop.

							endif; // End of IF-Loop

							wp_reset_postdata();
							?>

						</div><!-- row -->
					</div><!-- panel-body -->
				</div><!-- panel -->
			</article>

		</div>
		<!-- #main -->

		<div class="clearfix"></div>

		<nav class="navbar navbar-default" role="navigation">
			<div class="container-fluid">
				<div class="text-center">
					<?php echo $pagination; ?>
				</div>
			</div>
		</nav>

		<?php
		return ob_get_clean();
	}

	/**
	 * @param $id
	 *
	 * @return string
	 */
	public function list_view_post( $id ) {
		ob_start();
		$meta = get_post_meta( $id );
		if ( $this -> list_options[ 'wpi_list_view_column' ] === 'column' ) {
			echo $this -> list_view_columns( $meta );
		} else {
			?>
			<div class="col-md-3">
				<?php echo $this -> list_view_image( $meta ); ?>
			</div>
			<div class="col-md-9">
				<header class="wpi-header">
					<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
				</header>
				<?php

				if ( $this -> list_options[ 'wpi_list_excerpt' ] === 'true' ) :
					echo $this -> list_view_excerpt( $meta );
				else :
					echo $this -> list_view_meta( $meta );

				endif;

				// Link Button
				echo $this -> list_link_button();
				?>
			</div>
			<div class="clearfix"></div>
			<?php
		}

		return ob_get_clean();
	}

	/**
	 * @param $meta -> MetdDaten des Posts
	 *
	 * @return string
	 */
	public function list_view_image( $meta ) {
		$bilder = unserialize( $meta[ 'anhaenge' ][ 0 ] );
		// Auslesen der Kategorien
		$taxonomies = get_the_taxonomies();
		// Objekttypen
		$objekttyp = strstr( $taxonomies[ 'objekttyp' ], ' ' );
		$objekttyp = trim( $objekttyp, '.' );
		// Vermarktungsarten
		$vermarktung = strstr( $taxonomies[ 'vermarktungsart' ], ' ' );
		$vermarktung = trim( $vermarktung, '.' );
		ob_start();
		$single_view = new SingleViewClass;

		?>

		<div>
			<a href="<?php the_permalink(); ?>" class="thumbnail">
				<?php
				//Bilder aus Meta laden
				@$bilder = $single_view -> help_handle_array( $bilder, 'anhang' );
				@$bild = array_values( $bilder[ 'bilder' ][ 0 ] )[ 0 ];
				if ( ! empty( $bild ) ) :
					$first_image = $bild;
					?>
					<img src="<?php echo $this -> upload_url . $first_image; ?> "/>
					<?php
				else :
					?>
					<img src="<?php echo $this -> options[ 'wpi_img_platzhalter' ]; ?>"/>
					<?php
				endif;
				?>
			</a>
			<div class="text-center">
				<span class="info-text text-capitalize"><?php echo $objekttyp ?></span>
				<span class="info-text text-capitalize"><?php echo $vermarktung ?></span>
			</div>
			<div class="visible-xs">
				<hr/>
			</div>
		</div>
		<?php if ( isset( $meta[ 'topimmo' ] ) && $this -> options[ 'wpi_show_top_immo' ] == 'true' ): ?>
			<div class="topimmo">
				<img src="<?php echo $this -> options[ 'wpi_top_immo_source' ]; ?>"/>
			</div>
		<?php endif; ?>
		<?php if ( isset( $meta[ 'sold' ] ) && $this -> options[ 'wpi_show_sold' ] == 'true' ): ?>
			<div class="sold-text">
				<p><?php echo $this -> options[ 'wpi_sold_text' ]; ?></p>
			</div>
		<?php endif; ?>
		<?php if ( isset( $meta[ 'reserved' ] ) && $this -> options[ 'wpi_show_reserved' ] == 'true' ): ?>
			<div class="reserved-text">
				<p><?php echo $this -> options[ 'wpi_reserved_text' ]; ?></p>
			</div>
		<?php endif; ?>

		<?php
		return ob_get_clean();
	}

	/**
	 * @param $meta -> MetdDaten des Posts
	 *
	 * @return string
	 */
	public function list_view_excerpt( $meta ) {
		ob_start();

		$exc_length = $this -> list_options[ 'wpi_list_excerpt_length' ]; ?>
		<div class="">
			<?php echo wp_trim_words( get_the_excerpt(), $exc_length ); ?>
		</div>
		<?php
		return ob_get_clean();
	}

	/**
	 * @param $meta -> MetdDaten des Posts
	 *
	 * @return string
	 */
	public function list_view_columns( $meta ) {

		// Objektdaten als Arrays
		$objektkategorie_array = unserialize( $meta[ 'objektkategorie' ][ 0 ] );
		$geodaten              = unserialize( $meta[ 'geodaten' ][ 0 ] );
		$kontaktperson         = unserialize( $meta[ 'kontaktperson' ][ 0 ] );
		$preise                = unserialize( $meta[ 'preise' ][ 0 ] );
		$flaechen              = unserialize( $meta[ 'flaechen' ][ 0 ] );
		$ausstattung           = unserialize( $meta[ 'ausstattung' ][ 0 ] );
		$zustand_angaben       = unserialize( $meta[ 'zustand_angaben' ][ 0 ] );
		$anhaenge              = unserialize( $meta[ 'anhaenge' ][ 0 ] );
		$freitexte             = unserialize( $meta[ 'freitexte' ][ 0 ] );
		$verwaltung_objekt     = unserialize( $meta[ 'verwaltung_objekt' ][ 0 ] );
		$verwaltung_techn      = unserialize( $meta[ 'verwaltung_techn' ][ 0 ] );

		// Listen Array aus den Options holen
		$list_details = $this -> list_options[ 'wpi_list_detail' ];
		if ( ! empty( $list_details ) ):
			foreach ( $list_details as $listkey => $listvalue ) {
				if ( array_key_exists( $listkey, $preise ) ):
					if ( is_numeric( $preise[ $listkey ] ) ):
						$liste[ $listvalue ] = number_format( $preise[ $listkey ], 2, ',', '.' );
					else:
						$liste[ $listvalue ] = $preise[ $listkey ];
					endif;
				endif;
				if ( array_key_exists( $listkey, $flaechen ) ):
					if ( is_numeric( $flaechen[ $listkey ] ) ):
						$liste[ $listvalue ] = number_format( $flaechen[ $listkey ], 1, ',', '' );
					else:
						$liste[ $listvalue ] = $flaechen[ $listkey ];
					endif;
				endif;
			}
		endif;

		// Setzen der Variable "Preis"
		if ( array_key_exists( 'kaufpreis', $preise ) ):
			$preis = preg_replace( "#[.].*#", "", $preise[ 'kaufpreis' ] );
			$preis = 'Kaufpreis: ' . $preis . ' ' . $preise[ "waehrung" ][ "@attributes" ][ "iso_waehrung" ];
		elseif ( array_key_exists( 'kaltmiete', $preise ) ):
			$preis = preg_replace( "#[.].*#", "", $preise[ 'kaltmiete' ] );
			$preis = 'Kaltmiete: ' . $preis . ' ' . $preise[ "waehrung" ][ "@attributes" ][ "iso_waehrung" ];
		endif;


		// Link zur Single-Seite
		$link = get_permalink();

		ob_start();
		//zeigen($list_details);
		?>
		<div class="col-sm-6 col-md-4 immo-columns">
			<div class="thumbnail"><?php

				echo '<span class="preis">' . $preis . '</span>';

				echo $this -> list_view_image( $meta );
				?>

				<div class="caption">
					<h4><?php the_title(); ?></h4>
					<table>
						<tr>
							<td><?= __( 'PLZ / Ort' ); ?></td>
							<td><span class="eckdaten_ort">
                                <?php echo $geodaten[ 'plz' ] . ' ' . $geodaten[ 'ort' ] ?>
                            </span>
							</td>
						</tr>
						<?php
						// TODO Ort-PLZ wie bei list-view hinzufügen
						// Tabellen-Array ohne leere Werte in die Tabelle schreiben
						if ( ! empty( $liste ) && array_filter( $liste ) ):
							array_walk( $liste, 'trim_value' );
							//zeigen(array_filter($liste));
							foreach ( array_filter( $liste ) as $key => $wert ) {
								echo '<tr>';
								echo '<td>' . $key . '</td>';
								echo '<td>' . $wert . '</td>';
								echo '</tr>';
							}
						endif;

						?>
					</table>
					<p class="text-center"><a href="<?php echo $link; ?>" class="btn btn-default"
					                          role="button">Ansehen</a>
					</p>
				</div>
			</div>
		</div>

		<?php
		return ob_get_clean();
	}

	/**
	 * @param $meta -> MetdDaten des Posts
	 *
	 * @return string
	 */
	public function list_view_meta( $meta, $element = 'list' ) {
		$anbieterkennung = $meta[ 'anbieterkennung' ][ 0 ];
		$firma           = $meta[ 'anbieterfirma' ][ 0 ];
		if ( strpos( $firma, ";" ) ) {
			$firma = explode( ';', $firma );
		}
// Objektdaten als Arrays
		$objektkategorie_array = unserialize( $meta[ 'objektkategorie' ][ 0 ] );
		$geodaten              = unserialize( $meta[ 'geodaten' ][ 0 ] );
		$kontaktperson         = unserialize( $meta[ 'kontaktperson' ][ 0 ] );
		$preise                = unserialize( $meta[ 'preise' ][ 0 ] );
		$flaechen              = unserialize( $meta[ 'flaechen' ][ 0 ] );
		$ausstattung           = unserialize( $meta[ 'ausstattung' ][ 0 ] );
		$zustand_angaben       = unserialize( $meta[ 'zustand_angaben' ][ 0 ] );
		$anhaenge              = unserialize( $meta[ 'anhaenge' ][ 0 ] );
		$freitexte             = unserialize( $meta[ 'freitexte' ][ 0 ] );
		$verwaltung_objekt     = unserialize( $meta[ 'verwaltung_objekt' ][ 0 ] );
		$verwaltung_techn      = unserialize( $meta[ 'verwaltung_techn' ][ 0 ] );

		$list_details = $this -> list_options[ 'wpi_list_detail' ];
		if ( ! empty( $list_details ) ):
			foreach ( $list_details as $listkey => $listvalue ) {
				if ( array_key_exists( $listkey, $preise ) ):
					if ( is_numeric( $preise[ $listkey ] ) ):
						$liste[ $listvalue ] = number_format( $preise[ $listkey ], 2, ',', '.' );
					else:
						$liste[ $listvalue ] = $preise[ $listkey ];
					endif;
				endif;
				if ( array_key_exists( $listkey, $flaechen ) ):
					if ( is_numeric( $flaechen[ $listkey ] ) ):
						$liste[ $listvalue ] = number_format( $flaechen[ $listkey ], 1, ',', '' );
					else:
						$liste[ $listvalue ] = $flaechen[ $listkey ];
					endif;
				endif;
			}
		endif;

		ob_start();
		switch ( $element ):
			case 'div':
				?>
				<div class="div">
					<div class="ortsangaben row">
						<div class="col-xs-12">
							<span class="glyphicon glyphicon-map-marker"></span>
							<span class="eckdaten_ort">
                                            <?php echo $geodaten[ 'plz' ] . ' ' . $geodaten[ 'ort' ] ?>
                                        </span>
						</div>
					</div>
					<?php
					// Tabellen-Array ohne leere Werte in die Tabelle schreiben
					if ( ! empty( $liste ) && array_filter( $liste ) ):
						array_walk( $liste, 'trim_value' );
						//zeigen(array_filter($liste));
						foreach ( array_filter( $liste ) as $key => $wert ) {
							echo '<div class="hardfacts">';
							echo '<div class="hardfacts-value">' . $wert . '</div>';
							echo '<div class="hardfacts-label">' . $key . '</div>';
							echo '</div>';
						}
					endif;

					?>
				</div>
				<?php
				break;
			default;
				?>
				<table class="table table-hover">
					<tr>
						<td class="col-xs-4"><?= __( 'PLZ / Ort', WPI_PLUGIN_NAME ); ?></td>
						<td>
							<span class="glyphicon glyphicon-map-marker"></span>
							<span class="eckdaten_ort">
                                            <?php echo $geodaten[ 'plz' ] . ' ' . $geodaten[ 'ort' ] ?>
                                        </span>
						</td>
					</tr>
					<?php
					// Tabellen-Array ohne leere Werte in die Tabelle schreiben
					if ( ! empty( $liste ) && array_filter( $liste ) ):
						array_walk( $liste, 'trim_value' );
						//zeigen(array_filter($liste));
						foreach ( array_filter( $liste ) as $key => $wert ) {
							echo '<tr>';
							echo '<td class="col-xs-4">' . $key . '</td>';
							echo '<td>' . $wert . '</td>';
							echo '</tr>';
						}
					endif;

					?>
				</table>
				<?php
				break;
		endswitch;

		return ob_get_clean();
	}

	public function list_link_button() {
		ob_start();
		?>
		<div class="more pull-right">
			<a href="<?php the_permalink(); ?>">
				<button type="button" class="btn btn-default">
					Mehr Details <span class="glyphicon glyphicon-chevron-right"></span>
				</button>
			</a>
		</div>
		<!-- more -->

		<?php
		return ob_get_clean();
	}

	public function new_post_label($id) {
		$post = get_post($id);
		$current_date = strtotime(date('Y-m-d H:m:s'));
		$post_time = strtotime($post->post_date);
		$setting = (int)$this->options['wpi_list_options']['wpi_list_view_new_label'];
		$time_dif = ($setting * 24 * 60 * 60);

		if($this->options['wpi_pro'] === 'true' && $current_date - $time_dif <= $post_time):
			return '<span class="new-post">' . __('Neu', WPI_PLUGIN_NAME) . '</span>';
		else:
			return;
		endif;
		}

}
