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

<details style="border: 1px solid lightgray;border-radius: 5px;margin-top: 15px;">
	<summary style="display:flex;"><h2 style="font-size:150%;font-weight:500;">How to Edit product data <span>ⓘ</span></h2></summary>
	<div style="margin-left:25px;margin-bottom:25px;">
		<b>Here you can manually edit almost all product data.</b>
		<p>This is the default data of this product, every change done here is applied to the entire website (except when the default data is overridden).</p>
		<p>Usually most of these fields are frequently updated through the APIs <i>(need APIs access):</i></p>
		<ul>
			<li>- <b style="color:gray">Gray colored fields</b> are fields dedicated to the APIs and should not be modified.</li>
			<li>- <b style="color:#b7ab00">Yellow colored fields</b> will be overwritten after each automatic update cycle.</li>
			<li>- <b style="color:green;">Green colored fields</b> are safe to customize fields and will never be overwritten.</li>
		</ul>
		<p><i>Hit the blue <b>update</b> botton to save the product data and <b>refresh</b> to see the current product preview.</i></p>
	</div>
</details>

<?php
$preview = 0;	
$post_type = get_post_type();
?>

<div class="amazingaffiliates_admin_page <?php if( 'amazing_product' == $post_type ) echo'amazingaffiliates_product_page'; ?>">
 		
	<?php for($i = 1; $i <= 2; $i++): ?>
		<div class="amazingaffiliates_custom_meta_metabox">
		<?php if( 'amazing_product' != $post_type AND $i > 1) continue; ?>
		<?php foreach( array_keys($this->custom_post_meta) as $post_type_target ) : ?>
			<?php if( get_post_type() == $post_type_target ) : ?>
				<?php foreach( array_keys($this->custom_post_meta[$post_type_target]) as $custom_post_meta_group_key ) : ?>
	
					<?php // Product block preview
					if( 'amazing_product' == $post_type AND $preview == 0 AND $i==2):
					?>
					<div class="amazingaffiliates_custom_fields_group product_preview" >

						<div id="amazingaffiliates_ajax" style="display:none;"
							 data-ajax="<?php echo esc_url( get_site_url() ); ?>/wp-admin/admin-ajax.php"
							 data-nonce="<?php echo esc_attr( wp_create_nonce( 'workshop-warehouse' ) ); ?>"
						></div>

						<?php wp_nonce_field( 'amazingaffiliates_metabox', 'amazingaffiliates_metabox_field' ); ?>

						<?php
						$preview = 1;
						$entry = get_the_ID();
						$asin = sanitize_text_field( get_post_meta( $entry, 'asin', true) );

						$default = array(
							'shortcoded'                    => 0,
							'productid' 					=> '',
							'search'						=> '',
							'customtitle' 					=> '',
							'customcontentbefore' 		    => '',
							'showdetails'					=> 3,
							'showdescription'				=> 0,
							'showtable'					    => 0,
							'customcontentafter' 			=> '',	
							'noprice' 						=> false,
							'nobuybutton' 				    => false,
							'wrappertitle' 				    => '',
							'wrappercolor' 				    => '',
							'rating' 						=> 0,
						);

						$atts = array(
							'shortcoded'                    => 0,
							'productid' 					=> $entry,
							'search'                        => '',
							'showdetails'					=> -1,
							'showdescription'				=> -1,
							'showtable'					    => -1,
							'rating' 						=> -1,
							'wrappercolor' 				    => 'steelblue;',
							'wrappertitle' 				    => 'Product Block Preview'
						);

						$inputs = shortcode_atts($default, $atts);

						?>

						<div class="product_container product_block" style="margin-top:0;">

							<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 wth the Amazon APIs"
										>
									<img alt="" width="28" height="28" src="<?php echo esc_attr( AMAZINGAFFILIATES_PLUGIN_URL . '/admin/img/update.jpg' ); ?>" >
								</button>
							</div>

							<div id="product_display">
								<?php require( AMAZINGAFFILIATES_PLUGIN_URI . '/public/blocks/product/product_block.php' );	?>
							</div>

						</div>
					</div>
					<?php endif; ?>
	
					<?php // Product pages custom looping
					if('amazing_product' == $post_type) {
						if('api' == $custom_post_meta_group_key OR 'update' == $custom_post_meta_group_key ) {							
							if( $i == 1) continue;
						}
						else {
							if( $i == 2) continue;
						}
					}
					?>
	
	
	

					<div class="amazingaffiliates_custom_fields_group <?php echo esc_attr( $this->custom_post_meta_groups[$custom_post_meta_group_key]['metabox_classes'] ); ?>" >
						<p class="amazingaffiliates_custom_fields_group_title" ><?php echo esc_html( $this->custom_post_meta_groups[$custom_post_meta_group_key]['group_name'] ); ?></p>

						<?php foreach( $this->custom_post_meta[$post_type_target][$custom_post_meta_group_key] as $custom_post_meta ) : ?>
							<?php $field_content = get_post_meta( get_the_ID(), $custom_post_meta['slug'], true ); ?>

							<div class="amazingaffiliates_custom_fields <?php echo esc_attr( $custom_post_meta['metabox_classes'] ); ?>" >
								<label for="<?php echo esc_attr( $custom_post_meta['slug'] ); ?>"><?php echo esc_html( $custom_post_meta['name'] ); ?></label>

								<?php
								switch ( $custom_post_meta['metabox'] ) {
									case 'display':
										echo '<p id="' . esc_attr( $custom_post_meta['slug'] ) . '" type="text" >';
										if( $field_content == '' ) { echo '-'; }
										else { echo wp_kses_post( $field_content ); }
										echo '</p>';
										break;
									case 'displaylink':
										echo '<a id="' . esc_attr( $custom_post_meta['slug'] ) . '" type="text" target="_blank" rel="noopener noreferrer" href="' . esc_url( $field_content ) . '" >';
										if( $field_content == '' ) { echo '[No URL]'; }
										else {  echo wp_kses_post( $field_content ); }
										echo '</a>';
										break;
									case 'displayx100':
										echo '<p id="' . esc_attr( $custom_post_meta['slug'] ) . '" type="text" >';
										if( $field_content == '' ) { echo '-'; }
										else { echo wp_kses_post( $field_content ) . '%'; }
										echo '</p>';
										break;
									case 'displaybool':
										echo '<p id="' . esc_attr( $custom_post_meta['slug'] ) . '" type="text" >';
										if( $field_content == '' ) { echo '-'; }
										else { 
											if( $field_content == 1 ) { echo 'TRUE'; }
											if( $field_content == 0 ) { echo 'FALSE'; }
										}
										echo '</p>';
										break;
									case 'displayid':
										echo '<button id="' . esc_attr( $custom_post_meta['slug'] ) . '" value="' . esc_attr( get_the_ID() ) . '"'
											. ' class="product_id input tocopy" data-tagline="ID copied!" data-prefix="ID" >';
										echo 'ID: ' . wp_kses_post( get_the_ID() );
										echo '</button>';
										break;
									case 'displayasin':
										echo '<button id="' . esc_attr( $custom_post_meta['slug'] ) . '" value="' . esc_attr( $field_content ) . '"'
											. ' class="product_asin input tocopy" data-tagline="ASIN copied!" data-prefix="ASIN" >';
										if( $field_content == '' ) { echo 'ASIN: -'; }
										else { echo 'ASIN: ' . wp_kses_post( $field_content ); }
										echo '</button>';
										break;
									case 'inputlink':
										$important_field_missing_input = '';
										$important_field_missing_label = '';
										$important_field_missing_placeholder = '';
										if(empty($field_content)) {
											$important_field_missing_input = 'style="border:2px solid red;"';
											$important_field_missing_label = 'style="color:red;font-weight:600;"';
											$important_field_missing_placeholder = "DONT LEAVE BLANK THIS FIELD or you will not monetize!";
										}

										echo    '<p ' . esc_attr( $important_field_missing_label ) . '>(please dont leave it blank)</p>';
										echo	'<input name="' . esc_attr( $custom_post_meta['slug'] ) . '" id="' . esc_attr( $custom_post_meta['slug'] ) . '" type="text" value="' . esc_attr( $field_content ) . '" ' . esc_attr( $important_field_missing_input ) . ' placeholder="' . esc_attr( $important_field_missing_placeholder ) . '">';

										break;
									case 'inputx100':
										echo    '<p>(0-100%)</p>';
										echo	'<input name="' . esc_attr( $custom_post_meta['slug'] ) . '" id="' . esc_attr( $custom_post_meta['slug'] ) . '" type="text" value="' . esc_attr( $field_content ) . '">';
										break;
									case 'inputbool':
										echo    '<p>(0 false, 1 true)</p>';
										echo	'<input name="' . esc_attr( $custom_post_meta['slug'] ) . '" id="' . esc_attr( $custom_post_meta['slug'] ) . '" type="text" value="' . esc_attr( $field_content ) . '">';
										break;
									case 'displayjson':
										echo '<pre id="' . esc_attr( $custom_post_meta['slug'] ) . '" type="text" >';
										if( $field_content == '' AND ! is_null( json_decode( $field_content ) ) ) { echo '-'; }
										else { 
											$pretty_print = wp_json_encode( json_decode( $field_content , null , 2048 , JSON_INVALID_UTF8_IGNORE ), JSON_PRETTY_PRINT );
											if( is_null($pretty_print) OR str_contains($pretty_print, "\n")) {
												echo esc_js( $field_content );
											}
											else {
												echo esc_js( $pretty_print);
											}
											// pretty print returns null with some bad api responses due to a misuse of characters in the product descriptions
											// better an unpretty print rather than a null value
										}
										echo '</pre>';
										break;
									case 'details':
									   // echo '<details id="' . esc_attr( $custom_post_meta['slug'] ) . '" closed ><summary>' . esc_attr( ucfirst( $custom_post_meta['slug'] ) ) . '</summary><p>';
										if( $field_content == '' AND ! is_null( json_decode( $field_content ) ) ) {
											echo '[No JSON data]';
										}
										elseif( $field_content == '') {
											echo '[No data]';
										}
										else {
											echo wp_kses_post( $field_content );
										}
									   // echo '</p></details>';
										break;
									case 'image':
										echo wp_get_attachment_image( $field_content , "thumbnail" );
										echo '<p id="' . esc_html( $custom_post_meta['slug'] ) . '" type="text" >';
										if( empty($field_content) ) { echo '-'; }
										else { 
											echo wp_kses_post( $field_content );
										}
										echo '</p>';
										break;
									case 'time':
										echo '<p id="' . esc_attr( $custom_post_meta['slug'] ) . '" type="text" >';
										if(is_numeric($field_content)) {
											echo esc_html( gmdate('d F Y H:i:s',$field_content) ) . ' GMT</p>';
										}
										else {
											if(!empty($field_content)) {
												echo wp_kses_post( '(' . $field_content . ')' );
											}
											else {
												echo '[No data]';
											}										
										}									

										break;
									case 'input':
										echo	'<input name="' . esc_attr( $custom_post_meta['slug'] ) . '" id="' . esc_attr( $custom_post_meta['slug'] ) . '" type="text" value="' . esc_attr( $field_content ) . '">';
										break;
									case 'textarea':
										echo	'<textarea rows="3" oninput="textareaFit(event);" onfocus="textareaFit(event);" name="' . esc_attr( $custom_post_meta['slug'] ) . '" id="' . esc_attr( $custom_post_meta['slug'] ) . '" type="text" >' . esc_attr( $field_content ) . '</textarea>';
										break;
									case 'editor':
										$editor_content = get_post_meta( get_the_ID(), $custom_post_meta['slug'], true );
										$editor_id = $custom_post_meta['slug'];
										$editor_settings = array(
											'quicktags' => true,
											'textarea_name' => $custom_post_meta['slug'],
											'textarea_rows' => 5
										 );
										wp_editor($editor_content, $editor_id, $editor_settings);
										break;
								}
								?>

								</div>

						<?php endforeach; ?>

					</div>

				<?php endforeach; ?>
			<?php endif; ?>
		<?php endforeach; ?>
	</div>
	<?php endfor; ?>
    
</div>