<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
function elex_fpf_manage_feed_template_function() {
	 $upload_dir = wp_upload_dir();
	$base        = $upload_dir['basedir'];
	$path        = $base . '/elex-facebook-feed/';
	$dir_url     = $upload_dir['baseurl'] . '/elex-facebook-feed/';

	$search    = isset( $_GET['search'] ) ? sanitize_text_field( $_GET['search'] ) : '';
	$feed_type = isset( $_GET['feed_type'] ) ? sanitize_text_field( $_GET['feed_type'] ) : 'all';
	if ( 'all' == $feed_type ) {
		$all_button_color    = 'elex-fpf-button-color-active';
		$manual_button_color = 'elex-fpf-button-color-inactive';
		$sync_button_color   = 'elex-fpf-button-color-inactive';
	} elseif ( 'manual' == $feed_type ) {
		$all_button_color    = 'elex-fpf-button-color-inactive';
		$manual_button_color = 'elex-fpf-button-color-active';
		$sync_button_color   = 'elex-fpf-button-color-inactive';
	} else {
		$all_button_color    = 'elex-fpf-button-color-inactive';
		$manual_button_color = 'elex-fpf-button-color-inactive';
		$sync_button_color   = 'elex-fpf-button-color-active';
	}
	?>
	<div class="elex-fpf-empty-div"></div>
	<div id="elex_manage_feed" class="postbox elex-fpf-manage-feed-table-box elex-fpf-manage-feed-table-box-main ">
		<h1><?php esc_html_e( 'Manage Feeds', 'elex-product-feed' ); ?></h1>
		<div style="padding-bottom: 2%; "><button onclick="elex_fpf_filter_feed_fun('all')" class="botton button-large <?php echo esc_html( $all_button_color ); ?>" >All</button> <button onclick="elex_fpf_filter_feed_fun('manual')" class="botton button-large <?php echo esc_html( $manual_button_color ); ?>" >Manual</button> <button onclick="elex_fpf_filter_feed_fun('sync')" class="botton button-large <?php echo esc_html( $sync_button_color ); ?>">Sync</button> <button onclick="elex_fpf_search_feed_fun()" style="float:right" class="botton button-large button-primary">search</button> <input value="<?php echo esc_html( $search ); ?>" style="float:right" type="text" id="elex_fpf_search_feed" placeholder="Search by Feed name.." title="Type in a name"> <br></div>
		<table id="elex_manage_feed_files" class="elex-fpf-manage-feed-settings-table">
			<tr>
				<th class='elex-fpf-manage-feed-settings-table-name'>
					<?php esc_html_e( 'Name', 'elex-product-feed' ); ?>
				</th>
				<th class='elex-fpf-manage-feed-settings-table-url'>
					<?php esc_html_e( 'URL', 'elex-product-feed' ); ?>
				</th>
				<th class='elex-fpf-manage-feed-settings-created-date'>
					<?php esc_html_e( 'Created', 'elex-product-feed' ); ?>
				</th>
				<th class='elex-fpf-manage-feed-settings-modified-datel'>
					<?php esc_html_e( 'Modified', 'elex-product-feed' ); ?>
				</th>
				<th class='elex-fpf-manage-feed-settings-modified-datel'>
					<?php esc_html_e( 'Next schedule', 'elex-product-feed' ); ?>
				</th>
				<th class='elex-fpf-manage-feed-settings-table-actions'>
					<?php esc_html_e( 'Actions', 'elex-product-feed' ); ?>
				</th>
			</tr>
			<tr></tr>
	<?php
	if ( is_dir( $path ) ) {
		global $wpdb;
		$table_name    = $wpdb->prefix . 'fpf_feeds';
		$feed_id_query = "SELECT * FROM $table_name Where feed_meta_key = 'manage_feed_data' ";
		$result        = array_reverse( $wpdb->get_results( ( $wpdb->prepare( '%1s', $feed_id_query ) ? stripslashes( $wpdb->prepare( '%1s', $feed_id_query ) ) : $wpdb->prepare( '%s', '' ) ), ARRAY_A ) );
		if ( ! isset( $_GET['feed_page'] ) ) {
			$current_page = 1;
		} else {
			$current_page = sanitize_text_field( $_GET['feed_page'] );
		}
		$max_pagination          = 3;
		$max_feeds_per_page      = 100;
		$products_each_iteration = 0;
		$start                   = ( $current_page - 1 ) * $max_feeds_per_page;
		$counter                 = 0;
		foreach ( $result as $key => $value ) {
			$val = $value['feed_meta_content'];
			$val = json_decode( $val, true );

			if ( 'manual' == $feed_type ) {
				if ( isset( $val['sync_fb_enable'] ) && 'true' == $val['sync_fb_enable'] ) {
					continue;
				}
			} elseif ( 'sync' == $feed_type ) {
				if ( ! ( isset( $val['sync_fb_enable'] ) && 'true' == $val['sync_fb_enable'] ) ) {
					continue;
				}
			}

			if ( $search && strpos( strtolower( $val['name'] ), strtolower( $search ) ) === false ) {
				continue;
			}
			if ( $counter < $start ) {
				$counter++;
				continue;
			}
			$counter++;
			if ( $products_each_iteration == $max_feeds_per_page ) {
				break;
			}
			$products_each_iteration++;
			$file_name     = '';
			$created_date  = '';
			$modified_date = '';
			$pause_or_play = '';
			$next_schedule = '-';

			$file         = $val['file'];
			$file_name    = $val['name'];
			$created_date = isset( $val['created_date'] ) ? $val['created_date'] : '';
			if ( isset( $val['modified_date'] ) ) {
				$modified_date = $val['modified_date'];
			}
					
			$pause_or_play = $val['pause_schedule'];
			if ( 'no_refresh' != $val['refresh_schedule'] ) {
				if ( 'weekly' == $val['refresh_schedule'] && isset( $val['refresh_days'] ) && $val['refresh_days'] ) {
					$str_next_day = '';
					$today        = strtolower( current_time( 'l' ) );
					if ( in_array( $today, $val['refresh_days'] ) ) {
						if ( current_time( 'G' ) >= $val['refresh_hour'] ) {
							 $index = array_search( $today, $val['refresh_days'] );
							if ( isset( $val['refresh_days'][ $index + 1 ] ) ) {
								$str_next_day = 'next ' . $val['refresh_days'][ $index + 1 ];
							} else {
								$str_next_day = 'next ' . $val['refresh_days'][0];
							}
						} else {
							$str_next_day = 'today';
						}
					} else {
						foreach ( $val['refresh_days'] as $day_index => $day_value ) {
							if ( gmdate( 'w', strtotime( $day_value ) ) > gmdate( 'w', strtotime( $today ) ) ) {
								$str_next_day = 'next ' . $day_value;
								break;
							}
						}
						$str_next_day = 'next ' . $val['refresh_days'][0];
					}
					$next_schedule = gmdate( 'd-m-Y', strtotime( $str_next_day ) );
				} elseif ( 'monthly' == $val['refresh_schedule'] ) {
					$today          = current_time( 'j' );
					$next_day       = '';
					$number_of_days = current_time( 't' );
					$next_month     = false;
					if ( in_array( $today, $val['refresh_days'] ) ) {
						if ( current_time( 'G' ) >= $val['refresh_hour'] ) {
							 $index = array_search( $today, $val['refresh_days'] );
							if ( isset( $val['refresh_days'][ $index + 1 ] ) && $val['refresh_days'][ $index + 1 ] <= $number_of_days ) {
								$next_day = $val['refresh_days'][ $index + 1 ];
							} else {
								$next_day   = $val['refresh_days'][0];
								$next_month = true;
							}
						} else {
							$next_day = $today;
						}
					} else {
						foreach ( $val['refresh_days'] as $day_index => $day_value ) {
							if ( ( $day_value > $today ) && ( $day_value <= $number_of_days ) ) {
								$next_day = $day_value;
								break;
							}
							$next_day   = $val['refresh_days'][0];
							$next_month = true;
						}
					}
					$next_day      = sprintf( '%02d', $next_day );
					$next_schedule = $next_day . '-' . current_time( 'm-Y' );
					if ( $next_month ) {
						$next_schedule = gmdate( 'd-m-Y', strtotime( $next_schedule . '+1 month' ) );
					}
				} else {
					if ( current_time( 'G' ) >= $val['refresh_hour'] ) {
						$next_schedule = gmdate( 'd-m-Y', strtotime( 'tomorrow' ) );
					} else {
						$next_schedule = gmdate( 'd-m-Y', strtotime( 'today' ) );
					}
				}
				$next_schedule .= '<br><span style="font-size: 10px;">' . sprintf( '%02d', $val['refresh_hour'] ) . ':00:00</span>';
				if ( 'paused' == $val['pause_schedule'] ) {
					$next_schedule .= '<br><span style="color:red;font-size: 10px;">(Paused)</span>';
				}
			}
			if ( ! $file_name ) {
				continue;
			}
			if ( isset( $val['sync_fb_enable'] ) && 'true' == $val['sync_fb_enable'] ) {
				$feed_sync_or_manual = '<br><span class="elex-fpf-sync-identifier">Sync</span>';
			} else {
				$feed_sync_or_manual = '<br><span class="elex-fpf-manual-identifier">Manual</span>';
			}
			?>
				<tr>
					<td class="elex-fpf-manage-feed-settings-table-name">
			<?php echo wp_kses_post( $file_name . $feed_sync_or_manual ); ?>
					</td>
					<td class="elex-fpf-manage-feed-settings-table-url">
			<?php
			if ( ! ( isset( $val['sync_fb_enable'] ) && 'true' == $val['sync_fb_enable'] ) ) {
				echo esc_html( $dir_url . $file );
			}
			?>
					</td>
					<td class="elex-fpf-manage-feed-settings-created-date">
			<?php
			$created_date = explode( ' ', $created_date );
			echo esc_html( $created_date[0] );
			if ( isset( $created_date[1] ) ) {
				echo '<br><span style="font-size: 10px;">' . esc_html( $created_date[1] ) . '</span>';
			}
			?>
					</td>
					<td class="elex-fpf-manage-feed-settings-modified-date">
			<?php
			$modified_date = explode( ' ', $modified_date );
			echo esc_html( $modified_date[0] );
			if ( isset( $modified_date[1] ) ) {
				echo '<br><span style="font-size: 10px;">' . esc_html( $modified_date[1] ) . '</span>';
			}
			?>
					</td>
					<td class="elex-fpf-manage-feed-settings-modified-date">
			<?php echo wp_kses_post( $next_schedule ); ?>
					</td>
					<td class="elex-fpf-manage-feed-settings-table-actions">
			<?php
			if ( ! ( isset( $val['sync_fb_enable'] ) && 'true' == $val['sync_fb_enable'] ) ) {
				?>
						<span class="elex-fpf-icon-view"  title="View Feed" onclick="window.open('<?php echo esc_html( $dir_url . $file ); ?>','_blank')" style="display: inline-block; margin: 0px 2px 0px;"></span>
				<?php
			} else {
				$upload_dir   = wp_upload_dir();
				$base         = $upload_dir['basedir'];
				$sync_dir_url = $upload_dir['baseurl'] . '/elex-facebook-sync-logs/';

				$file           = explode( '.', $file );
				$file_path_txt  = $sync_dir_url . 'log_' . $file[0] . '.txt';
				$view_sync_file = $file_path_txt ;

				?>
							<span class="elex-fpf-icon-log-file"  title="Log File"  style="display: inline-block;" onclick="elex_fpf_view_logfile_fun('<?php echo esc_html( $view_sync_file ); ?>')"></span>
				<?php 
			} 
			?>
						 
						<span class=" elex-fpf-icon4 elex-fpf-icon4-edit"  title="Edit Project" onclick="elex_edit_file('<?php echo esc_html( $value['feed_id'] ); ?>')"   style="display: inline-block;"></span>
						<span class=" elex-fpf-icon-copy"  title="Copy Project" onclick="elex_copy_file('<?php echo esc_html( $value['feed_id'] ); ?>')"   style="display: inline-block;"></span>
			<?php if ( 'ready' == $pause_or_play && 'no_refresh' != $val['refresh_schedule'] ) { ?>
						<span class="elex-fpf-icon-pause"  title="Pause Schedule" onclick="elex_pause_schedule('<?php echo esc_html( $value['feed_id'] ); ?>')"   style="display: inline-block;"></span>
			<?php } elseif ( 'no_refresh' != $val['refresh_schedule'] ) { ?>
						<span class="elex-fpf-icon-play"  title="Resume Schedule" onclick="elex_play_schedule('<?php echo esc_html( $value['feed_id'] ); ?>')"   style="display: inline-block;"></span>
			<?php } ?>
						<span class=" elex-fpf-icon3 elex-fpf-icon3-refresh"  title="Refresh Feed" onclick="update_file_to_latest('<?php echo esc_html( $value['feed_id'] ); ?>', '<?php echo esc_html( $file_name ); ?>')" style="display: inline-block;"></span>
						<span class="elex-fpf-icon elex-fpf-icon-report"  title="Show Report" onclick="elex_show_reports('<?php echo esc_html( $value['feed_id'] ); ?>', '<?php echo esc_html( $file_name ); ?>')"   style="display: inline-block;"></span>
			<?php
			if ( ! ( isset( $val['sync_fb_enable'] ) && 'true' == $val['sync_fb_enable'] ) ) {
				?>
							<a href= "<?php $path . esc_html( $file ); ?>"  download=<?php echo esc_html( $file ); ?> target="_blank" id="<?php echo esc_html( $file ); ?>"></a>
								<span class=" elex-fpf-icon-download"  title="Download Feed" onclick="document.getElementById('<?php echo esc_html( $file ); ?>').click();" download="<?php echo esc_html( $file ); ?>"style="display: inline-block;"></span>
				<?php
			}
			?>
						<span class=" elex-fpf-icon elex-fpf-icon-delete" onclick="elex_remove_file('<?php echo esc_html( $value['feed_id'] ); ?>', '<?php echo esc_html( $file_name ); ?>')"   title="Delete Project" style="display: inline-block;"></span>
					</td>
				</tr>
			<?php
		}
	}
	?>
		</table>
	</div>
	<?php
	$pagination = 0;

	if ( isset( $products_each_iteration ) < isset( $max_feeds_per_page ) ) {
		$pagination = ceil( $counter / $max_feeds_per_page );
	} elseif ( ! empty( $result ) ) {
		$pagination = ceil( count( $result ) / $max_feeds_per_page );
	}
	if ( $pagination > 1 ) {
		if ( $pagination == $max_pagination ) {
			$start = 1;
		} else {
			$start = $current_page;
			if ( $pagination == $current_page ) {
				$start = $current_page - 1;
			}
		}
		if ( 1 == $current_page ) {
			$left_disable = 'pag-disable';
			$left_href    = '#';
		} else {
			$left_disable = '';
			$left_href    = 'admin.php?page=elex-facebook-feed-manage&feed_page=' . ( $current_page - 1 ) . '&search=' . $search;
		}
		if ( $pagination == $current_page ) {
			$right_disable = 'pag-disable';
			$right_href    = '#';
		} else {
			$right_disable = '';
			$right_href    = 'admin.php?page=elex-facebook-feed-manage&feed_page=' . ( $current_page + 1 ) . '&search=' . $search;
		}
		?>
				<div style="padding-left: 76%;" class="pagination">
					<a href="<?php echo esc_html( $left_href ); ?> " class="<?php echo esc_html( $left_disable ); ?> ">&laquo;</a>
		<?php

		for ( $flag = $start; $flag <= $pagination; $flag++ ) {
			$active = '';
			if ( $flag == $current_page ) {
				   $active = 'pag-active';
			}
			?>
					<a href="admin.php?page=elex-facebook-feed-manage&feed_page=<?php echo esc_html( $flag ); ?>&search=<?php echo esc_html( $search ); ?>" class="<?php echo esc_html( $active ); ?> " > <?php echo esc_html( $flag ); ?> </a>
			<?php
			if ( $flag >= $start + $max_pagination - 1 ) {
				   break;
			}
		}
		?>
					<a href="<?php echo esc_html( $right_href ); ?> " class="<?php echo esc_html( $right_disable ); ?> ">&raquo;</a>
				</div>
		<?php
	}
	?>
	


	<div id="dialogBox" style="display: none;">
		<div id="chartContainer" class="dialog" style="height: 370px; width: 100%;"></div>
	</div>

	<?php
}
elex_fpf_manage_feed_template_function();
require_once ELEX_FACEBOOK_FEED_TEMPLATE_PATH . '/elex-settings-frontend.php';
