<?php

function dhwcpl_admin_menu(){
	add_submenu_page( 'edit.php?post_type=product', __( 'Labels', 'dhwcpl' ), __( 'Labels', 'dhwcpl' ), 'manage_product_terms', 'edit-tags.php?taxonomy=product_label&post_type=product');
}

add_action('admin_menu', 'dhwcpl_admin_menu', 10);

/**
 * dhwcpl_enqueue_scripts function
 */
function dhwcpl_enqueue_scripts(){
	wp_enqueue_style('dhwcpl-admin', plugins_url('dhwc-product-labels/assets/css/admin.css') );
	wp_enqueue_media();
}
add_action( 'admin_enqueue_scripts','dhwcpl_enqueue_scripts');

/**
 * dhwcpl_form_description function
 */
function dhwcpl_form_description() {
	echo wpautop( __( 'Product Labels for your store can be managed here.', 'dhwcpl' ) );
}

add_action( 'product_label_pre_add_form', 'dhwcpl_form_description' );

/**
 * dhwcpl_add_images function
 */
function dhwcpl_add_images(){
	global $woocommerce;
	?>
	<div class="form-field">
		<label><?php _e( 'Display', 'dhwcpl' ); ?></label>
		<select name="product_label_display" id="product_label_display">
		<?php 
			foreach (dhwcpl_get_display() as $key=>$text){
				echo '<option value="'.$key.'">'.$text.'</option>';
			}
		?>
		</select>
		<p><?php echo __('The config Label is display in loop product or single product on your site.','dhwcpl') ?></p>
	</div>
	<div class="form-field">
		<label><?php _e( 'Format', 'dhwcpl' ); ?></label>
		<select name="product_label_format" id="product_label_format">
		<?php 
			foreach (dhwcpl_get_format() as $key=>$text){
				echo '<option value="'.$key.'">'.$text.'</option>';
			}
		?>
		</select>
		<script type="text/javascript">
			jQuery(document).ready(function($){
				function format_control(){
					if($('#product_label_format').val() == 'custom'){
						$('#custom_content').show();
						$('#product_label_image_field').hide();
					}else{
						$('#custom_content').hide();
						$('#product_label_image_field').show();
					}
				}
				format_control();
				$('#product_label_format').on('change',function(){
					format_control();
				});
			});
		</script>
	</div>
	<div id="custom_content" class="form-field">
		<label><?php _e( 'Content', 'dhwcpl' ); ?></label>
		<textarea id="product_label_content" cols="40" rows="5" name="product_label_content"></textarea>
		<p><?php echo _e('Arbitrary text or HTML.','dhwcpl')?></p>
	</div>
	<div id="product_label_image_field" class="form-field">
		<label><?php _e( 'Image', 'dhwcpl' ); ?></label>
		<div id="product_label_image" style="float:left;margin-right:10px;">
			<img src="<?php echo woocommerce_placeholder_img_src(); ?>" style="width:60px"/>
		</div>
		<div style="line-height:60px;">
			<input type="hidden" id="product_label_image_id" name="product_label_image_id" />
			<button type="submit" class="upload_image_button button"><?php _e('Upload/Add image', 'dhwcpl'); ?></button>
			<button type="submit" class="remove_image_button button"><?php _e('Remove image', 'dhwcpl'); ?></button>
		</div>
		<script type="text/javascript">
	
			 // Only show the "remove image" button when needed
			 if ( ! jQuery('#product_label_image_id').val() )
				 jQuery('.remove_image_button').hide();
	
			// Uploading files
			var file_frame;
	
			jQuery(document).on( 'click', '.upload_image_button', function( event ){
	
				event.preventDefault();
	
				// If the media frame already exists, reopen it.
				if ( file_frame ) {
					file_frame.open();
					return;
				}
	
				// Create the media frame.
				file_frame = wp.media.frames.downloadable_file = wp.media({
					title: '<?php _e( 'Choose an image', 'dhwcpl' ); ?>',
					button: {
						text: '<?php _e( 'Use image', 'dhwcpl' ); ?>',
					},
					multiple: false
				});
	
				// When an image is selected, run a callback.
				file_frame.on( 'select', function() {
					attachment = file_frame.state().get('selection').first().toJSON();
	
					jQuery('#product_label_image_id').val( attachment.id );
					jQuery('#product_label_image img').attr('src', attachment.url );
					jQuery('.remove_image_button').show();
				});
	
				// Finally, open the modal.
				file_frame.open();
			});
	
			jQuery(document).on( 'click', '.remove_image_button', function( event ){
				jQuery('#product_label_image img').attr('src', '<?php echo woocommerce_placeholder_img_src(); ?>');
				jQuery('#product_label_image_id').val('');
				jQuery('.remove_image_button').hide();
				return false;
			});
	
		</script>
		<div class="clear"></div>
	</div>	
	<div class="form-field">
		<label><?php _e( 'Style', 'dhwcpl' ); ?></label>
		<div>
			<label for="top" style="display: block;"><?php echo __('Top','dhwcpl')?></label>
			<input id="top" class="small-text" type="text" value="" name="top">
			<p><?php echo __('For absolutely positioned elements, example: auto, inherit or the top edge position in px, cm,% etc.','dhwcpl')?></p>
		</div>
		<div>
			<label for="right" style="display:block;"><?php echo __('Right','dhwcpl')?></label>
			<input id="right" class="small-text" type="text" value="" name="right">
			<p><?php echo __('For absolutely positioned elements, example: auto, inherit or the top edge position in px, cm,% etc.','dhwcpl')?></p>
		</div>
		<div>
			<label for="bottom" style="display:block;"><?php echo __('Bottom','dhwcpl')?></label>
			<input id="bottom" class="small-text" type="text" value=""  name="bottom">
			<p><?php echo __('For absolutely positioned elements, example: auto, inherit or the top edge position in px, cm,% etc.','dhwcpl')?></p>
		</div>
		<div>
			<label for="left" style="display:block; "><?php echo __('Left','dhwcpl')?></label>
			<input id="left" class="small-text" type="text" size="50" value="" name="left">
			<p><?php echo __('For absolutely positioned elements, example: auto, inherit or the top edge position in px, cm,% etc.','dhwcpl')?></p>
		</div>
	</div>
<?php 
}

add_action('product_label_add_form_fields','dhwcpl_add_images');

/**
 * dhwcpl_edit_images function
 */
function dhwcpl_edit_images($term, $taxonomy ){
	global $woocommerce;
	$image 			= '';
	$image_id 	= get_woocommerce_term_meta( $term->term_id, 'image_id', true );
	$format_id = get_woocommerce_term_meta($term->term_id, 'format', true);
	
	$display_id = get_woocommerce_term_meta($term->term_id,'display',true);
	
	$top = get_woocommerce_term_meta($term->term_id, 'top', true);
	$right = get_woocommerce_term_meta($term->term_id, 'right', true);
	$bottom = get_woocommerce_term_meta($term->term_id, 'bottom', true);
	$left = get_woocommerce_term_meta($term->term_id, 'left', true);
	
	if ($image_id) :
		$image = wp_get_attachment_url( $image_id );
	else :
		$image = woocommerce_placeholder_img_src();
	endif;
	?>
	<tr class="form-field">
		<th scope="row" valign="top"><label><?php _e( 'Display', 'dhwcpl' ); ?></label></th>
		<td>
			<select name="product_label_display" id="product_label_display">
			<?php 
				foreach (dhwcpl_get_display() as $key=>$text){
					echo '<option value="'.$key.'" '.selected($key,$display_id,true).'>'.$text.'</option>';
				}
			?>
			</select>
			<p class="description"><?php echo __('The config Label is display in loop product or single product on your site.','dhwcpl') ?></p>
		</td>
	</tr>
	<tr class="form-field">
		<th scope="row" valign="top"><label><?php _e('Format', 'dhwcpl'); ?></label></th>
		<td>
			<select name="product_label_format" id="product_label_format">
			<?php 
				foreach (dhwcpl_get_format() as $key=>$text){
					echo '<option value="'.$key.'" '.selected($key,$format_id,true).'>'.$text.'</option>';
				}
			?>
			</select>
			<script type="text/javascript">
				jQuery(document).ready(function($){
					function format_control(){
						if($('#product_label_format').val() == 'custom'){
							$('#custom_content').show();
							$('#product_label_image_field').hide();
						}else{
							$('#custom_content').hide();
							$('#product_label_image_field').show();
						}
					}
					format_control();
					$('#product_label_format').on('change',function(){
						format_control();
					});
				});
			</script>
		</td>
	</tr>
	<tr  id="custom_content" class="form-field">
		<th scope="row" valign="top"><label><?php _e( 'Content', 'dhwcpl' ); ?></label></th>
		<td>
			<textarea id="product_label_content" cols="40" rows="5" name="product_label_content"><?php echo esc_textarea(get_woocommerce_term_meta($term->term_id, 'product_label_content', true ));?></textarea>
			<p class="description"><?php echo _e('Arbitrary text or HTML.','dhwcpl')?></p>
		</td>
	</tr>
	<tr id="product_label_image_field" class="form-field">
		<th scope="row" valign="top"><label><?php _e('Image', 'dhwcpl'); ?></label></th>
		<td>
			<div id="product_label_image" style="float:left;margin-right:10px;">
			<img src="<?php echo $image; ?>" style="width:60px" />
			</div>
			<div style="line-height:60px;">
				<input type="hidden" id="product_label_image_id" name="product_label_image_id" value="<?php echo $image_id; ?>" />
				<button type="submit" class="upload_image_button button"><?php _e('Upload/Add image', 'dhwcpl'); ?></button>
				<button type="submit" class="remove_image_button button"><?php _e('Remove image', 'dhwcpl'); ?></button>
			</div>
			<script type="text/javascript">

				jQuery(function(){

					 // Only show the "remove image" button when needed
					 if ( ! jQuery('#product_label_image_id').val() )
						 jQuery('.remove_image_button').hide();

					// Uploading files
					var file_frame;

					jQuery(document).on( 'click', '.upload_image_button', function( event ){

						event.preventDefault();

						// If the media frame already exists, reopen it.
						if ( file_frame ) {
							file_frame.open();
							return;
						}

						// Create the media frame.
						file_frame = wp.media.frames.downloadable_file = wp.media({
							title: '<?php _e( 'Choose an image', 'dhwcpl' ); ?>',
							button: {
								text: '<?php _e( 'Use image', 'dhwcpl' ); ?>',
							},
							multiple: false
						});

						// When an image is selected, run a callback.
						file_frame.on( 'select', function() {
							attachment = file_frame.state().get('selection').first().toJSON();

							jQuery('#product_label_image_id').val( attachment.id );
							jQuery('#product_label_image img').attr('src', attachment.url );
							jQuery('.remove_image_button').show();
						});

						// Finally, open the modal.
						file_frame.open();
					});

					jQuery(document).on( 'click', '.remove_image_button', function( event ){
						jQuery('#product_label_image img').attr('src', '<?php echo woocommerce_placeholder_img_src(); ?>');
						jQuery('#product_label_image_id').val('');
						jQuery('.remove_image_button').hide();
						return false;
					});
				});

			</script>
			<div class="clear"></div>
		</td>
	</tr>
	<tr class="form-field">
		<th scope="row" valign="top"><label><?php _e('Style', 'dhwcpl'); ?></label></th>
		<td>
			<div>
				<label for="top" style="display: block;"><?php echo __('Top','dhwcpl')?></label>
				<input id="top" class="small-text" type="text" value="<?php echo $top ?>" name="top">
				<p class="description"><?php echo __('For absolutely positioned elements, example: auto, inherit or the top edge position in px, cm,% etc.','dhwcpl')?></p>
			</div>
			<div style="margin-top:10px">
				<label for="right" style="display:block;"><?php echo __('Right','dhwcpl')?></label>
				<input id="right" class="small-text" type="text" value="<?php echo $right ?>"name="right">
				<p class="description"><?php echo __('For absolutely positioned elements, example: auto, inherit or the top edge position in px, cm,% etc.','dhwcpl')?></p>
			</div>
			<div style="margin-top:10px">
				<label for="bottom" style="display:block;"><?php echo __('Bottom','dhwcpl')?></label>
				<input id="bottom" class="small-text" type="text" value="<?php echo $bottom ?>"  name="bottom">
				<p class="description"><?php echo __('For absolutely positioned elements, example: auto, inherit or the top edge position in px, cm,% etc.','dhwcpl')?></p>
			</div>
			<div style="margin-top:10px">
				<label for="left" style="display:block; "><?php echo __('Left','dhwcpl')?></label>
				<input id="left" class="small-text" type="text" size="50" value="<?php echo $left ?>" name="left">
				<p class="description"><?php echo __('For absolutely positioned elements, example: auto, inherit or the top edge position in px, cm,% etc.','dhwcpl')?></p>
			</div>
		</td>
	</tr>
<?php 
}
add_action('product_label_edit_form_fields','dhwcpl_edit_images',10,3);

/**
 * dhwcpl_save_images function
 */
function dhwcpl_save_images( $term_id, $tt_id, $taxonomy ) {
	if (isset($_POST['product_label_display']))
		update_woocommerce_term_meta($term_id,'display',$_POST['product_label_display']);

	if (isset($_POST['product_label_format']))
		update_woocommerce_term_meta($term_id,'format',$_POST['product_label_format']);
	
	if (isset($_POST['product_label_content']))
		update_woocommerce_term_meta($term_id,'product_label_content',$_POST['product_label_content']);
	
	if ( isset( $_POST['product_label_image_id'] ) )
		update_woocommerce_term_meta( $term_id, 'image_id', absint( $_POST['product_label_image_id'] ) );
	if (isset($_POST['top'])) {
		update_woocommerce_term_meta( $term_id, 'top', $_POST['top'] );
	}
	if (isset($_POST['right'])) {
		update_woocommerce_term_meta( $term_id, 'right', $_POST['right'] );
	}
	if (isset($_POST['bottom'])) {
		update_woocommerce_term_meta( $term_id, 'bottom', $_POST['bottom'] );
	}
	if (isset($_POST['left'])) {
		update_woocommerce_term_meta( $term_id, 'left',  $_POST['left']);
	}

}

add_action( 'created_term', 'dhwcpl_save_images', 10,3 );
add_action( 'edit_term', 'dhwcpl_save_images', 10,3 );

/**
 * dhwcpl_columns function
 */
function dhwcpl_columns( $columns ) {
	$new_columns = array();
	$new_columns['cb'] = $columns['cb'];
	$new_columns['content'] = __( 'Content', 'dhwcpl' );
	$new_columns['format'] = __('Format','dhwcpl');
	$new_columns['display'] = __('Display','dhwcpl');
	unset( $columns['cb'] );
	unset($columns['slug']);
	unset($columns['posts']);
	
	return array_merge( $new_columns, $columns );
}

add_filter( 'manage_edit-product_label_columns', 'dhwcpl_columns' );

/**
 * dhwcpl_custom_column function
 */
function dhwcpl_custom_column( $columns, $column, $id ) {
	global $woocommerce;
	if ( $column == 'content' ) {
		if (get_woocommerce_term_meta( $id, 'format', true ) == 'custom') {
			$columns .= wpautop(get_woocommerce_term_meta( $id, 'product_label_content', true));
		}else {
			$image 			= '';
			$image_id 	= get_woocommerce_term_meta( $id, 'image_id', true );
			
			if ($image_id)
				$image = wp_get_attachment_thumb_url( $image_id );
			else
				$image = woocommerce_placeholder_img_src();
			$columns .= '<img src="' . $image . '" alt="Image" class="wp-post-image" style="max-width:60px" />';

		}
	}elseif ($column == 'format'){
		echo get_woocommerce_term_meta( $id, 'format', true );
	}elseif ($column == 'display'){
		$displayArr = dhwcpl_get_display();
		$display_id = get_woocommerce_term_meta( $id, 'display', true );
		if (isset($displayArr[$display_id])) {
			echo $displayArr[$display_id];
		}else {
			echo __('All','dhwcpl');
		}
	}
	return $columns;
}

add_filter( 'manage_product_label_custom_column', 'dhwcpl_custom_column', 10, 3 );

/**
 * dhwcpl_product_write_panel_tabs function
 */
function dhwcpl_product_write_panel_tabs(){
	echo '<li class="dhwcpl dhwcpl_options"><a href="#dhwcpl_data">'. __( 'Labels','dhwcpl').'</a></li>';
}

add_action('woocommerce_product_write_panel_tabs','dhwcpl_product_write_panel_tabs');

/**
 * dhwcpl_product_write_panels function
 */
function dhwcpl_product_write_panels(){
	global $post;
	$product_labels = (array)  get_post_meta( $post->ID, 'product_labels', true );
	?>
	<div id="dhwcpl_data" class="panel woocommerce_options_panel">
		<div class="options_group">
			<p class="form-field">
				<label for="product_labels"><?php echo __('Labels','dhwcpl')?></label>
				<select name="product_labels[]" id="product_labels"  class="chosen_select" multiple="multiple" data-placeholder="<?php echo __('Select labels','dhwcpl')?>">
					<?php 
						$labels = (array) get_terms('product_label', array( 'hide_empty' => 0, 'orderby' => 'id' ));
						foreach ($labels as $label){

					
					echo '<option value="'.esc_attr($label->term_id).'"'. selected( in_array( $label->term_id, $product_labels ), true, false ) . '>'.esc_html__($label->name).'</option>';
					
					 }?>
				</select>
			</p>
		</div>
	</div>
	<?php 
}

add_action( 'woocommerce_product_write_panels', 'dhwcpl_product_write_panels');

/**
 * dhwcpl_product_label_save function
 */
function dhwcpl_product_label_save($post_id, $post){
	$product_labels 		= isset( $_POST['product_labels'] ) ? array_map( 'intval', $_POST['product_labels'] ) : array();
	update_post_meta( $post_id, 'product_labels', $product_labels );
}
add_action( 'woocommerce_process_product_meta', 'dhwcpl_product_label_save',10,3);

