<?php
/**
 * Title: XML Feed Processor Images Page
 * Author: Merv Barrett
 * Author URI: http://realestateconnected.com.au/
 * Version: 2.0.0
 * @since 3.5.93 Recent logs at the top. Fix missing image previews.
 * @since 3.7.6 View source button. Open log file in new tab.
 */
require_once('../../config.php');
require_once('../functions.php');
do_action('init');
global $pagination;
$page_now = 'listing-details';
get_header('Imported');
get_listings_sub_header( $page_now );

if( isset( $_GET['id'] ) && $_GET['id'] >= 0 ) {
	$results = get_listing_data();
}

$map_img = get_option('site_url').'core/assets/images/feedsync-map-not-set.svg';

if( !in_array($results['data']->geocode, array('','NULL','-1,-1') ) ){
        $map_img = get_option('site_url').'core/assets/images/feedsync-map.svg';
        $atts = ' id="map-'.$results['data']->unique_id.'" class="item-has-map" data-toggle="tooltip" data-html="true"  title="'.$results['data']->geocode.'" data-placement="top" ';
} else {
        $atts = ' id="map-'.$results['data']->unique_id.'" class="item-has-map" data-toggle="tooltip" data-html="true"  title="No coordinates set, click to process coordinates." data-placement="top" ';
}

$rated_class = strpos($results['data']->xml, '<feedsyncFeaturedListing>') !== false ? 'rated' : '';

?>
<div class="panel panel-default">
	<!-- Default panel contents -->
	<div class="panel-heading">
		<?php
			if( isset( $_GET['id'] ) && $_GET['id'] >= 0 ) {

                                $access_key_enabled 	= get_option('feedsync_enable_access_key');
                                $access_key_raw		= get_option('feedsync_access_key');
                                $access_key 		= '';


                                if ( $access_key_enabled == 'on' ) {
                                        $access_key = '&#38;access_key=' . $access_key_raw;
                                }

                                $source_url = SITE_URL.'?action=do_output'.$access_key.'&unique_id='.$results['data']->unique_id;

                                if( in_array( $results['data']->status, [ 'withdrawn', 'offmarket', 'deleted' ] ) ) {
                                        $source_url .= '&status=all';
                                }

				echo '<span class="fs-details-title">Details</span>'; ?>
                                <a href="<?php echo $source_url; ?>" target="_blank" class="btn btn-info pull-right" >View Source</a> <?php
			}
		?>
	</div>
        <div class="panel-body">
                <div class="row">
                        <div class="col-md-12">
                                <div class="fs-details-title-block">
                                        <div class="fs-details-address">
                                                <span class="fs-image-address"><?php echo $results['data']->address; ?></span>
                                                <span class="fs-details-fav-icon" >
                                                        <a href="javascript:void(0);" data-id="<?php echo $results['data']->id; ?>" class="rating mark-fav" title="">
                                                                <span class="<?php echo $rated_class; ?>">&#9734;</span>
                                                        </a>
                                                </span>
                                                <span>
                                                        <a href="#"  data-id="<?php echo $results['data']->id; ?>" <?php echo $atts; ?> >
                                                                <img src="<?php echo $map_img; ?>"/>
                                                        </a>
                                                </span>
                                        </div>
                                        <div class="fs-details-status">
						
						<?php if ( is_editing_allowed() ) : ?>
							<span class="badge fs-edit-listing-status">
								Edit
							</span>
						<?php endif; ?>
                                                
                                                <span class="fs-details-status-text btn <?php echo 'current' == $results['data']->status ? ' btn-success ': ' btn-danger '; ?>"><?php echo ucfirst( $results['data']->status ); ?></span>
                                                
                                                <?php if ( is_editing_allowed() ) : ?>

                                                        <select data-id="<?php echo $results['data']->id; ?>" style="display:none;" class="fs-details-status-dropdown">
                                                                <option <?php echo ('current' == $results['data']->status) ? 'selected' : ''; ?> value="current">Current</option>
                                                                <?php if ('rental' !== $results['data']->type): ?>
                                                                        <option <?php echo ('sold' == $results['data']->status) ? 'selected' : ''; ?> value="sold">Sold</option>
                                                                <?php endif; ?>
                                                                <?php if (in_array($results['data']->type, ['rental', 'commercial', 'commercialLand'])): ?>
                                                                        <option <?php echo ('leased' == $results['data']->status) ? 'selected' : ''; ?> value="leased">Leased</option>
                                                                <?php endif; ?>
                                                                <option <?php echo ('withdrawn' == $results['data']->status) ? 'selected' : ''; ?> value="withdrawn">Withdrawn</option>
                                                                <option <?php echo ('offmarket' == $results['data']->status) ? 'selected' : ''; ?> value="offmarket">Off Market</option>
                                                                <option <?php echo ('deleted' == $results['data']->status) ? 'selected' : ''; ?> value="deleted">Deleted</option>
                                                        </select>

                                                <?php endif; ?>
                                        </div>
                                </div>
                        </div>
                </div>
                <div class="row">
                        <div class="col-md-7">
                                <ul class="list-group">
                                        <li class="list-group-item">
                                                <label>Unique ID</label><br>
                                                #<?php echo $results['data']->unique_id; ?>
                                        </li>
                                        <li class="list-group-item">
                                                <label>First Date</label><br>
                                                <span class="fs-first-date-value">
                                                        <?php echo $results['data']->firstdate; ?>
                                                </span>

                                                <?php if( is_editing_allowed() ) : ?>

                                                <span class="fs-first-date-edit-box" style="display:none">
                                                        <input type="text" value="<?php echo $results['data']->firstdate; ?>" id="fs-first-date-edited">
                                                </span>
                                                <span data-id="<?php echo $results['data']->id; ?>" class="badge fs-edit-first-date">
                                                        Edit
                                                </span>

                                                <?php endif; ?>
                                        </li>
                                        <li class="list-group-item">
                                                <label>Modified Date</label><br>
                                                <span class="fs-details-mod-date">
                                                        <?php echo $results['data']->mod_date; ?>
                                                </span>
                                        </li>
                                        <li class="list-group-item">
                                                <label>Agent ID</label><br>
                                                <?php echo $results['data']->agent_id; ?>
                                        </li>
                                        <li class="list-group-item">
                                                <label>Type</label><br>
                                                <?php echo $results['data']->type; ?>
                                        </li>
                                </ul>
                        </div>
                        <div class="col-md-5">
                                <div class="fs-listing-map-wrap">
                                        <?php
                                                $api_key      = get_option( 'feedsync_google_api_key' );
                                                $show_warning = apply_filters( 'feedsync_show_map_key_warning', true );
                                        
                                                if ( empty( $api_key ) ) {
                                                        if ( $show_warning && is_user_logged_in() ) {
                                                                feedsync_map_api_key_warning();
                                                        }
                                                        
                                                } elseif( !in_array($results['data']->geocode, array('','NULL','-1,-1') ) ) { ?>
                                                        <div id="fs-listing-map" class="fs-listing-map" data-address="<?php echo $results['data']->address; ?>">
                                                        </div> <?php
                                                }
                                        ?>
                                        
                                </div>
                        </div>
                </div>
        </div>
	
</div>

<div class="panel panel-default">
	<!-- Default panel contents -->
	<div class="panel-heading">
		<strong>
			Images
		</strong>
	</div>
	<div class="table fs-images-table"> 
	
			<?php
				
				if( isset( $_GET['id'] ) && $_GET['id'] >= 0 ) {
					
					foreach($results['images'] as $result) { 
						
						$line = '<span class="image-holder"><a rel="prettyPhoto" href="'.$result . '">' 
										. '<img  class="feedsync-image" src="' . $result .  '" width="149" height="150" />' .
									"</a><span class='fs-image-info'>
											<span class='fs-image-size'>".get_remote_file_size($result)."</span>
											<span class='fs-image-dimension'></span>
										</span>
								</span>"; 
						echo $line; 
					}

				}
				 
			?>
	</div>

</div>
<div class="panel panel-default">
	<!-- Default panel contents -->
	<div class="panel-heading">
               <strong>Update Log</strong>
	</div>
	<div class="fs-listing-map-wrap">
        <table class="table">
                <tr>
                        <th width=150>
                                <a href="#">  File name  </a>
                        </th>
                        <th width=100> 
                                <a href="#"> Last Modified </a>
                        </th>
                </tr>
                <?php
			$update_logs      = (array) get_post_meta( intval( $_GET['id'] ), 'file_processed', true );
                        $update_logs      = array_filter( $update_logs );
                        $update_logs = array_reverse( $update_logs );
                        if( !empty( $update_logs ) ) {

                                foreach( $update_logs as $log_date => $update_log ) { 
                                        $log_date = explode( ' ', $log_date )[1]; ?>
                                        <tr>
                                                <td class="filename">
                                                        <a href="<?php echo PROCESSED_URL.date( 'Y', $log_date ).'/'.date( 'm', $log_date ).'/'.$update_log; ?>" target="_blank"><?php echo $update_log; ?></a>
                                                </td>
                                                <td class="date"><?php echo date('F j, Y, g:i:s a', $log_date ) ; ?></td>
                                        </tr> <?php
                                }
                        }
		?>
        </table>
        <ul class="list-group">
                
        </ul>	
		
		
	</div>
</div>
	
<?php echo get_footer(); ?>
