<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>

<?php

require( AMAZINGAFFILIATES_PLUGIN_URI . 'public/blocks/product/assets/back_end_icons.php' );

$i=0;

foreach($entries as $entry): ?>

<?php 	

if($skip_first_entry AND $i++==0) continue;


$currency					= @get_post_meta( $entry, 'currency', true);
$current_price				= @get_post_meta( $entry, 'current_price', true);
$list_price					= @get_post_meta( $entry, 'list_price', true);
$discount					= @get_post_meta( $entry, 'discount', true);
$offer						= @get_post_meta( $entry, 'offer', true);
$offer_msg					= array( 1 => 'On sale!' );
$stock						= @get_post_meta( $entry, 'stock', true);
$condition_value			= @get_post_meta( $entry, 'condition-value', true);
$merchant_info_name			= @get_post_meta( $entry, 'merchant_info-name', true);

$product_taxonomies			= array();
$tax_index = AmazingAffiliates_Admin::$amazingaffiliates_tax_index;
foreach($tax_index as $taxonomy) {
	$product_taxonomies[$taxonomy['taxSlug']]['list'] = get_the_terms( $entry, $taxonomy['taxSlug'] );
	$product_taxonomies[$taxonomy['taxSlug']]['taxNamePlural'] = $taxonomy['taxNamePlural'];
	$product_taxonomies[$taxonomy['taxSlug']]['taxColor'] = $taxonomy['taxColor'];
}

$creation_date	= sanitize_text_field( get_post_meta( $entry, 'creation_date', true) );
$last_update	= sanitize_text_field( get_post_meta( $entry, 'last_update', true) );
$now			= time();
$delta			= $now - intval($last_update);			
$delta_create	= $now - intval($creation_date);
$asin			= sanitize_text_field( get_post_meta( $entry, 'asin', true) );
$dead_product	= (sanitize_text_field( get_post_meta( $entry, 'not_updateable', true) ) == 1);
$has_issues		= str_contains( sanitize_text_field( get_post_meta( $entry, 'has_issues', true) ) , "ligible");

$availability = sanitize_text_field( get_post_meta( $entry, 'availability', true) );
$availability_msg = sanitize_text_field( get_post_meta( $entry, 'availability_msg', true) );
if( empty($availability_msg) ) $availability_msg = "Stock issues to be checked";
$stock = sanitize_text_field( get_post_meta( $entry, 'stock', true) );
if($delta > 7*86400 ) {
	$availability_color = 'lightgray';
}
else if($has_issues) {
	$availability_color = 'lightgray';
}
else {
	switch ($stock) 	{
		case '1' : 	$availability_color = 'limegreen';		break;	
		case '0' : 	$availability_color = 'tomato';			break;
		default :	$availability_color = 'gold';			break;
	}
}

if($dead_product) {
	$availability_color = 'black;color:white;';
	$availability_msg = 'Probably discontinued. Check on Amazon.';
}
?>

	<div class="product_container">
		<div class="product_toolbar">
			<button
					class="product_update"
					onclick="product_update('<?php echo esc_html( $asin ); ?>' , '<?php echo esc_html( $entry ); ?>' , this);" 
					data-asin="<?php echo esc_attr( $asin ); ?>" data-prod_id="<?php echo esc_attr( $entry ); ?>"
					title="Force refresh this product information with the Amazon APIs"
					>
				<img alt="" width="28" height="28" src="<?php echo esc_attr( AMAZINGAFFILIATES_PLUGIN_URL . '/admin/img/update.jpg' ); ?>" >
			</button>
			<a 
			   class="product_edit"
			   target="_blank"
			   rel="noopener"
			   href="<?php echo esc_url( site_url() . '/wp-admin/post.php?post=' . $entry . '&action=edit' ); ?>"
			   title="Edit this product"
			   >
				<img alt="" width="28" height="28" src="<?php echo esc_attr( AMAZINGAFFILIATES_PLUGIN_URL . '/admin/img/edit.jpg' ); ?>" >
			</a>
			<button
					class="product_delete"
					onclick="product_delete(this);"
					data-asin="<?php echo esc_attr( $asin ); ?>"
					title="Delete this product from your database"
					>
				<img alt="" width="28" height="28" src="<?php echo esc_attr( AMAZINGAFFILIATES_PLUGIN_URL . '/admin/img/delete.jpg' ); ?>" >
			</button>
		</div>

		<div class="product_tile" style="background-color:<?php echo esc_attr( $availability_color ); ?>;">
			<div class="tile_title" style="background-color:<?php echo esc_attr( $availability_color ); ?>;display:flex;flex-wrap:wrap;align-items:center;gap:5px;position:relative;" >
				<?php if($dead_product) echo '<div style="border: 3px solid red;background-color:red;border-radius: 5px;padding:5px 10px;line-height: 1em;position: relative;right: 3px;font-weight:600;font-size:16px;">!</div>'; ?>
				<div>
					<?php 
					if(empty($creation_date)) $creation_date = 60; 
					if(empty($last_update)) $last_update = 60;
					?>
					<small>Added: <?php echo esc_html( human_time_diff( $creation_date ) ); ?> ago</small><br>
					<small>Updated:  <?php echo esc_html( human_time_diff( $last_update ) ); ?> ago</small><br>
				</div>						
			</div>

			<div class="inside_tile">

				<div class="product_tile_editorbar">
					<button
							class="input tocopy"
							value="<?php echo esc_attr( $asin ); ?>"
							data-tagline="ASIN copied!" 
							data-prefix="ASIN"
							>
						<span>ASIN: </span><?php echo esc_attr( $asin ); ?>
					</button>
					<button
							class="input tocopy"
							value="<?php echo esc_attr( $entry ); ?>"
							data-tagline="ID copied!" 
							data-prefix="ID"
							>
						<span>ID: </span><?php echo esc_attr( $entry ); ?>
					</button>
				</div>

				<?php
				$title	= get_post_meta($entry, 'custom_title', true );
				if(empty($title)){ $title 	= get_the_title ($entry); }
				$title = substr( $title, 0, 72 );
				if(strlen($title)>69) $title .= "...";
				$link	= explode( '?' , get_post_meta($entry, 'affiliate_link', true) );
				$edit_link = site_url() . '/wp-admin/post.php?post=' . $entry . '&action=edit';
				?>
				<a class="product_card" href="<?php echo esc_url( $edit_link ); ?>" target="_blank" rel="noopener" title="Edit this product" style="text-decoration: none;">

					<div style="display:flex;gap:10px;">
						<div style="border-radius:10px;overflow:hidden;flex-basis:100px;height:100px;">
							<?php
							if(empty(get_post_thumbnail_id($entry))){
								$amazon_api_img = get_post_meta($entry, 'primary_img_url', true );
								if(!empty($amazon_api_img)) {
							?>
							<img width="100" height="100" alt="<?php echo esc_attr( wp_trim_words($title,7,'') ); ?>" src="<?php echo esc_url( $amazon_api_img ); ?>" >
							<?php
								}
								else {
							?>
							<div style="background-color:lightgray;display:flex;justify-content:center;align-items:center;width:100px;height:100px;" >No Image</div>
							<?php
								}

							}
							else {
								echo wp_get_attachment_image(get_post_thumbnail_id($entry),array('100', '100'),"",array( "class" => "product_img2" ));
							}
							?>
						</div>


						<div style="flex-basis:100px;flex-grow:1;">
							<p style="margin:0 0 10px 0;min-height:2em;">
								<?php 
								echo esc_html( substr( $title, 0 , 40 ) );
								if(strlen($title)>40) echo '...';
								?>
								<img class="edittextbutton" alt="" width="22" height="22" src="<?php echo esc_attr( AMAZINGAFFILIATES_PLUGIN_URL . '/admin/img/edit.jpg' ); ?>" >
							</p>
							<div style="font-size:12px;line-height: 1.2em;">
								<?php if( ! empty($current_price) ): ?>
								<span style="text-decoration:none;margin-bottom:0;">Last Price:</span><br>
								<?php if( intval($discount) != 0 OR $list_price != $current_price ): ?>
								<span style="color:lightgray;text-decoration:line-through;" >
									<?php echo esc_html( $list_price . $currency ); ?>
								</span>
								<span> -<?php echo esc_html( $discount ); ?>% </span>						    
								<?php endif; ?>
								<?php if( ! empty($current_price) ): ?>
								<b style="color:green;" ><?php echo esc_html( $current_price . $currency ); ?></b>
								<span><?php echo esc_html( $offer_msg[$offer] ); ?></span>
								<?php endif; ?>
								<?php endif; ?>
							</div>

						</div>

					</div>

				</a>

				<p class="info">

					<?php foreach($product_taxonomies as $product_taxonomy): ?>

					<?php foreach($product_taxonomy['list'] as $product_taxonomy_entry) : ?>

					<span class="taxonomy_label"
						  style="background-color:<?php echo esc_attr( $product_taxonomy['taxColor'] ); ?>"
						  title="This product is tagged with [<?php echo esc_attr( $product_taxonomy_entry->name ); ?>]"
						  >
						<?php echo esc_html( $product_taxonomy_entry->name ); ?>
					</span>

					<?php endforeach; ?>

					<?php endforeach; ?>

				</p>



				<div class="product_tile_editorbar">
					<a class="availabilitybutton" style="background-color:<?php echo esc_attr( $availability_color ); ?>;word-break:break-word;display:flex;padding:0 5px;"
					   href="<?php echo esc_url( $link[0] . '?' . $link[1] ); ?>" target="_blank" rel="noopener" title="See the product on Amazon"
					   >
						<img alt="" width="20" height="20" title="See on Amazon" src="<?php echo esc_attr( $amazon_icon ); ?>" >
						<span style="padding:5px 0;"><?php echo "  " . esc_html( $availability_msg ); ?></span>
						<span style="min-width:20px;"> [➚]</span>
					</a>
				</div>

			</div>

		</div>
	</div>

<?php endforeach; ?>

<div style="height:0;flex-basis:300px;flex-grow:1;margin:0.5em;"></div>
<div style="height:0;flex-basis:300px;flex-grow:1;margin:0.5em;"></div>
<div style="height:0;flex-basis:300px;flex-grow:1;margin:0.5em;"></div>
<div style="height:0;flex-basis:300px;flex-grow:1;margin:0.5em;"></div>
<div style="height:0;flex-basis:300px;flex-grow:1;margin:0.5em;"></div>
