<?php
if ( !defined('ABSPATH') ){ exit; }
function wpctp_edit_fields( $instance, $this, $defaults, $textdomain, $others = false ){
	if( $instance['background_image'] ) :
		$instance['default_bg'] = wp_get_attachment_image_url( $instance['background_image'], 'full' );
	endif;
	if( $instance['thumbnail'] ) :
		$instance['default_thumbnail'] = wp_get_attachment_image_url( $instance['thumbnail'], 'thumbnail' );
	endif;
	$enable_background_image = (bool) $instance['enable_background_image'];
	?>
    <div id="<?php wpctp_e( 'edit' ); ?>"><span><?php _e( 'CLICK TO EDIT FIELDS', $textdomain ); ?> </span></div>
    <div id="<?php wpctp_e( 'fields' ); ?>" style="display:none;">
    <div class="<?php wpctp_e( 'group' ); ?>">
	<?php if( in_array( 'title', $defaults ) ) : ?>
		<p>
			<label for="<?php echo $this['title']['id']; ?>"><?php _e( 'Title', $textdomain ); ?> : </label>
			<input class="widefat" id="<?php echo $this['title']['id']; ?>" name="<?php echo $this['title']['name']; ?>" type="text" value="<?php echo esc_attr( sanitize_text_field( $instance['title'] ) ); ?>" />
		</p>
	<?php endif; ?>
	<?php if( in_array( 'description', $defaults ) ) : ?>
		<p>
			<label for="<?php echo $this['description']['id']; ?>"><?php _e( 'Description', $textdomain ); ?> : </label><br />
			<textarea class="widefat" rows="5" cols="20" id="<?php echo $this['description']['id']; ?>" name="<?php echo $this['description']['name']; ?>"><?php echo esc_textarea( $instance['description'] ); ?></textarea>
		</p>
	<?php endif; ?>
	<div class="widget-column-box">
		<?php if( in_array( 'page_ids', $defaults ) ) : ?>
			<p>
				<label for="<?php echo $this['page_ids']['id']; ?>"><?php _e( 'Select pages where to display this section.', $textdomain ); ?> : </label>
				<?php
					wpctp_checkbox_pages( array(
						'name'     => esc_attr( $this['page_ids']['name'] ),
						'id'       => $this['page_ids']['id'],
						'exclude'  => get_option( 'page_for_posts' ),
						'selected' => (array) $instance['page_ids'],
					) );
				?>
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'background_image', $defaults ) ) : ?>
			<p>
				<label for="<?php echo $this['background_image']['id']; ?>"><?php _e( 'Background Image', $textdomain ); ?> : </label>
				<div id="<?php wpctp_e( 'upload' ); ?>" data-id="#<?php echo $this['background_image']['id']; ?>">
					<img id="<?php echo $this['background_image']['id']; ?>-img" src="<?php echo $instance['default_bg']; ?>" style=" max-width: 100%; " />
				</div>
				<input id="<?php echo $this['background_image']['id']; ?>-input" name="<?php echo $this['background_image']['name'] ?>" type="hidden" value="<?php echo absint( $instance['background_image'] ); ?>" />       
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'enable_background_image', $defaults ) ) : ?>
			<p>
				<label for="<?php echo $this['enable_background_image']['id']; ?>">
					<input class="checkbox" type="checkbox"<?php checked( true, $enable_background_image ); ?> id="<?php echo $this['enable_background_image']['id']; ?>" name="<?php echo $this['enable_background_image']['name']; ?>" value="1" />
					<?php _e( 'Enable Background Image', $textdomain ); ?>
				</label>
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'background_color', $defaults ) ) : ?>
			<p>
				<label for="<?php echo $this['background_color']['id']; ?>"><?php _e( 'Background Color', $textdomain ); ?> : </label>
				<input 
					type="text" 
					name="<?php echo $this['background_color']['name']; ?>" 
					id="<?php echo $this['background_color']['id']; ?>" 
					class="anton-color-picker" 
					value="<?php echo $instance['background_color']; ?>"
					style="display:none;"                    
				/>  
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'overlay_bc', $defaults ) ) : ?>
			<p>
				<label for="<?php echo $this['overlay_bc']['id']; ?>"><?php _e( 'Overlay Background Color', $textdomain ); ?> : </label>
				<input 
					type="text" 
					name="<?php echo $this['overlay_bc']['name']; ?>" 
					id="<?php echo $this['overlay_bc']['id']; ?>" 
					class="anton-color-picker" 
					value="<?php echo $instance['overlay_bc']; ?>"
					style="display:none;"                    
				/>  
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'heading_color', $defaults ) ) : ?>
			<p>
				<label for="<?php echo $this['heading_color']['id']; ?>"><?php _e( 'Title Text Color', $textdomain ); ?> : </label>
				<input 
					type="text" 
					name="<?php echo $this['heading_color']['name']; ?>" 
					id="<?php echo $this['heading_color']['id']; ?>" 
					class="anton-color-picker" 
					value="<?php echo $instance['heading_color']; ?>"
					style="display:none;"                    
				/>  
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'separator_color', $defaults ) ) : ?>
			<p>
				<label for="<?php echo $this['separator_color']['id']; ?>"><?php _e( 'Separator Color', $textdomain ); ?> : </label>
				<input 
					type="text" 
					name="<?php echo $this['separator_color']['name']; ?>" 
					id="<?php echo $this['separator_color']['id']; ?>" 
					class="anton-color-picker" 
					value="<?php echo $instance['separator_color']; ?>"
					style="display:none;"                    
				/>  
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'color', $defaults ) ) : ?>
			<p>
				<label for="<?php echo $this['color']['id']; ?>"><?php _e( 'Content Text Color', $textdomain ); ?> : </label>
				<input 
					type="text" 
					name="<?php echo $this['color']['name']; ?>" 
					id="<?php echo $this['color']['id']; ?>" 
					class="anton-color-picker" 
					value="<?php echo $instance['color']; ?>"
					style="display:none;"                    
				/>  
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'thumbnail', $defaults ) ) : ?>
			<p>
				<label for="<?php echo $this['thumbnail']['id']; ?>"><?php _e( 'Default Thumbnail Image', $textdomain ); ?> : </label>
				<div id="<?php wpctp_e( 'upload' ); ?>" class="<?php wpctp_e( 'center' ); ?>" data-id="#<?php echo $this['thumbnail']['id']; ?>">
					<img id="<?php echo $this['thumbnail']['id']; ?>-img" src="<?php echo $instance['default_thumbnail']; ?>" style=" max-width: 100%; " />
				</div>
				<input id="<?php echo $this['thumbnail']['id']; ?>-input" name="<?php echo $this['thumbnail']['name']; ?>" type="hidden" value="<?php echo absint( $instance['thumbnail'] ); ?>" />       
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'layout', $defaults ) ) : ?>
			<p>
				<label for="<?php echo esc_attr( $this['layout']['id'] ); ?>"><?php _e( 'Layout', $textdomain ); ?> : </label>
				<select id="<?php echo esc_attr( $this['layout']['id'] ); ?>" name="<?php echo esc_attr( $this['layout']['name'] ); ?>">
                    <?php if( isset( $others['layout'] ) ) : ?>
						<?php for( $x = 1; $x <= $others['layout']; $x++ ){ ?>
                        	<option value="layout-<?php echo $x; ?>" <?php selected( "layout-$x", $instance['layout'] ); ?>><?php _e( "Layout $x", $textdomain ); ?></option>
						<?php } ?>
                    <?php endif; ?>
				</select>
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'category', $defaults ) ) : ?>
			<p>
				<label for="<?php echo $this['category']['id']; ?>"><?php _e( 'Category', $textdomain ); ?>:</label>
				<?php
					$categories_args = array(
						'name'            	=> $this['category']['name'],
						'id'              	=> $this['category']['id'],
						'selected'        	=> $instance['category'],
						'orderby'         	=> 'Name',
						'hierarchical'    	=> 1,
						'show_option_all' 	=> __( 'All Categories', $textdomain ),
						'taxonomy'   		=> isset( $others['taxonomy'] ) ? $others['taxonomy'] : '',
					);
					wp_dropdown_categories( $categories_args );
				?>
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'posts_num', $defaults ) ) : ?>
			<p>
				<label for="<?php echo $this['posts_num']['id']; ?>"><?php _e( 'Post Per Page', $textdomain ); ?> : </label>
				<input class="tiny-text" id="<?php echo $this['posts_num']['id']; ?>" name="<?php echo $this['posts_num']['name']; ?>" type="number" step="1" min="1" value="<?php echo absint( $instance['posts_num'] ); ?>" size="3" />
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'content_limit', $defaults ) ) : ?>
			<p>
				<label for="<?php echo $this['content_limit']['id']; ?>"><?php _e( 'Content Limit', $textdomain ); ?> : </label>
				<input id="<?php echo $this['content_limit']['id']; ?>" name="<?php echo $this['content_limit']['name']; ?>" type="number" step="1" min="1" value="<?php echo absint( $instance['content_limit'] ); ?>" />
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'column', $defaults ) ) : ?>
			<p>
				<label for="<?php echo esc_attr( $this['column']['id'] ); ?>"><?php _e( 'Column', $textdomain ); ?> : </label>
				<select id="<?php echo esc_attr( $this['column']['id'] ); ?>" name="<?php echo esc_attr( $this['column']['name'] ); ?>">
					<option value="1" <?php selected( '1', $instance['column'] ); ?>><?php _e( '1', $textdomain ); ?></option>
					<option value="2" <?php selected( '2', $instance['column'] ); ?>><?php _e( '2', $textdomain ); ?></option>
					<option value="3" <?php selected( '3', $instance['column'] ); ?>><?php _e( '3', $textdomain ); ?></option>
					<option value="4" <?php selected( '4', $instance['column'] ); ?>><?php _e( '4', $textdomain ); ?></option>
					<option value="5" <?php selected( '5', $instance['column'] ); ?>><?php _e( '5', $textdomain ); ?></option>
					<option value="6" <?php selected( '6', $instance['column'] ); ?>><?php _e( '6', $textdomain ); ?></option>
				</select>
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'read_more_text', $defaults ) ) : ?>
			<p>
				<label for="<?php echo $this['read_more_text']['id']; ?>"><?php _e( 'Read More Button Text' ); ?> : </label>
				<input 
					type="url" 
					name="<?php echo $this['read_more_text']['name']; ?>" 
					id="<?php echo $this['read_more_text']['id']; ?>"
                    class="widefat"
					value="<?php echo $instance['read_more_text']; ?>"
                    placeholder="<?php _e( 'Leave it empty to disabled', $textdomain ); ?>"                   
				/>  
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'view_more_text', $defaults ) ) : ?>
			<p>
				<label for="<?php echo $this['view_more_text']['id']; ?>"><?php _e( 'View More Button Text' ); ?> : </label>
				<input 
					type="url" 
					name="<?php echo $this['view_more_text']['name']; ?>" 
					id="<?php echo $this['view_more_text']['id']; ?>"
                    class="widefat"
					value="<?php echo $instance['view_more_text']; ?>"
                    placeholder="<?php _e( 'Leave it empty to disabled', $textdomain ); ?>"                   
				/>  
			</p>
    	<?php endif; ?>
		<?php if( in_array( 'view_more_url', $defaults ) ) : ?>
			<p>
				<label for="<?php echo $this['view_more_url']['id']; ?>"><?php _e( 'View More Button URL' ); ?> : </label>
				<input 
					type="url" 
					name="<?php echo $this['view_more_url']['name']; ?>" 
					id="<?php echo $this['view_more_url']['id']; ?>"
                    class="widefat"
					value="<?php echo $instance['view_more_url']; ?>"
                    placeholder="<?php _e( 'http://demo-extended-pro.4nton.com/', $textdomain ); ?>"                   
				/>  
			</p>
    	<?php endif; ?>
 	</div>
 	<div id="<?php wpctp_e( 'space' ); ?>" style="height:30px;"></div>
 	<div id="<?php wpctp_e( 'close' ); ?>" class="button">Close</div>
    </div>
    </div>
 	<?php
}