<?php

/**
 * Returns the FTSE Index
 *
 * This function returns the FTSE index market
 * data from the database. The function returns
 * the data in a <table>. The database is updated
 * once a month, usually on the 1st of the month.
 *
 * @function add_ftse_index()
 *
 * @uses $iris_db::get_results()
 *
 * @param N/A
 *
 * @return (string) the FTSE index market data in a HTML <table>
 *
 * @since 1.0.0 
 *
 */
function add_ftse_index() {
	
	global $iris_db;
	
	$ftse_index = $iris_db->get_results( 'SELECT * FROM ftse_index ORDER BY datestamp DESC LIMIT 24' );
	$table = '<h2>FTSE 100 Index</h2>';
	$table .= '<p>Figures shown are the closing index for the month.</p>';
	$table .= '<table><tr><th>Date</th><th>FTSE 100 Index</th></tr>';
	
	foreach ( $ftse_index as $index ) {
		
		$table .= '<tr>';
		$table .= '<td>' . date( 'M Y', strtotime( $index->datestamp ) ) . '</td>';
		$table .= '<td>' . $index->index . '</td>';
		$table .= '</tr>';
	}
	$table .= '</table>';
	return $table;
}
add_shortcode( 'ftse_index', 'add_ftse_index' );



/**
 * Returns the Bank Of England Base Rate
 *
 * This function returns the Bank Of England Base
 * Rate market data from the database. The
 * function returns the data in a <table>. The
 * database is updated once a month, usually on
 * the 1st of the month.
 *
 * @function add_base_rate()
 *
 * @uses $iris_db::get_results()
 *
 * @param N/A
 *
 * @return (string) the Bank Of England Base Rate market data in a HTML <table>
 *
 * @since 1.0.18 
 *
 */
function add_base_rate() {
	
	global $iris_db;
	
	$base_rate = $iris_db->get_results( 'SELECT * FROM bank_of_england_base_rate ORDER BY datestamp DESC LIMIT 24' );
	$current = $iris_db->get_row( 'SELECT * FROM bank_of_england_base_rate ORDER BY datestamp DESC LIMIT 1' );
	$table = '<h2>Base Rate</h2>';
	$table .= '<p>The present Bank of England Base Rate is ' . $current->rate . '%.</p>';
	$table .= '<table><tr><th>Date</th><th>Base Rate %</th></tr>';
	
	foreach ( $base_rate as $rate ) {
		
		$table .= '<tr>';
		$table .= '<td>' . date( 'M Y', strtotime( $rate->datestamp ) ) . '</td>';
		$table .= '<td>' . number_format( $rate->rate, 2 ) . '</td>';
		$table .= '</tr>';
	}
	$table .= '</table>';
	return $table;
}
add_shortcode( 'base_rate', 'add_base_rate' );



/**
 * Returns Average Earnings
 *
 * This function returns Average Earnings
 * market data from the database. The function
 * returns the data in a <table>. The database
 * is updated once a month, usually on the
 * 1st of the month.
 *
 * @function add_average_earnings()
 *
 * @uses $iris_db::get_results()
 *
 * @param N/A
 *
 * @return (string) Average Earnings market data in a HTML <table>
 *
 * @since 1.0.18 
 *
 */
function add_average_earnings() {
	
	global $iris_db;
	
	$rpi = $iris_db->get_results( 'SELECT * FROM average_earnings ORDER BY datestamp DESC LIMIT 24' );
	$table = '<h2>Average Earnings</h2>';
	$table .= '<p>Seasonally adjusted average weekly earnings.</p>';
	$table .= '<table><tr><th>Date</th><th>Average Earnings Index</th><th>% change (year on year)</th></tr>';
	
	foreach ( $rpi as $index ) {
		
		$table .= '<tr>';
		$table .= '<td>' . date( 'M Y', strtotime( $index->datestamp ) ) . '</td>';
		$table .= '<td>' . $index->index . '</td>';
		$table .= '<td>' . number_format( $index->percentage, 1 ) . '</td>';
		$table .= '</tr>';
	}
	$table .= '</table>';
	return $table;
}
add_shortcode( 'average_earnings', 'add_average_earnings' );



/**
 * Returns Retail Price Index
 *
 * This function returns Retail Price Index
 * market data from the database. The function
 * returns the data in a <table>. The database
 * is updated once a month, usually on the
 * 1st of the month.
 *
 * @function add_retail_price_index()
 *
 * @uses $iris_db::get_results()
 *
 * @param N/A
 *
 * @return (string) Retail Price Index market data in a HTML <table>
 *
 * @since 1.0.18 
 *
 */
function add_retail_price_index() {
	
	global $iris_db;
	
	$rpi = $iris_db->get_results( 'SELECT * FROM retail_price_index ORDER BY datestamp DESC LIMIT 24' );
	$table = '<h2>Retail Price Index</h2>';
	$table .= '<p>All RPI figures use the January 1987 base of 100.</p>';
	$table .= '<table><tr><th>Date</th><th>Retail Price Index</th><th>Consumer Price Inflation</th></tr>';
	
	foreach ( $rpi as $index ) {
		$table .= '<tr>';
		$table .= '<td>' . date( 'M Y', strtotime( $index->datestamp ) ) . '</td>';
		$table .= '<td>' . number_format( $index->rpi, 1 ) . '</td>';
		$table .= '<td>' . number_format( $index->cpi, 1 ) . '</td>';
		$table .= '</tr>';
	}
	$table .= '</table>';
	return $table;
}
add_shortcode( 'retail_price_index', 'add_retail_price_index' );



/**
 * Returns Exchange Rates
 *
 * This function returns Exchange Rates
 * market data from the database. The function
 * returns the data in a <table>. The database
 * is updated once a month, usually on the
 * 1st of the month.
 *
 * @function add_exchange_rates()
 *
 * @uses $iris_db::get_results()
 *
 * @param N/A
 *
 * @return (string) Exchange Rates market data in a HTML <table>
 *
 * @since 1.2.1 
 *
 */
function add_exchange_rates() {
	
	global $iris_db;
	
	$exchange_rates = $iris_db->get_results( 'SELECT * FROM exchange_rates ORDER BY datestamp DESC LIMIT 24' );
	$table = '<h2>Exchange Rates</h2>';
	$table .= '<p>The following exchange rates are based on an average for the month.</p>';
	$table .= '<table><tr><th>Date</th><th>&dollar; US Dollar</th><th>&euro; Euro</th><th>&yen; Yen</th></tr>';
	//$table .= '<tr><td>Today</td><td id="ex-rates-dollar"></td><td id="ex-rates-euro"></td><td id="ex-rates-yen"></td></tr>';
	
	foreach ( $exchange_rates as $rate ) {
		
		$table .= '<tr>';
		$table .= '<td>' . date( 'M Y', strtotime( $rate->datestamp ) ) . '</td>';
		$table .= '<td>' . number_format( $rate->dollar, 4 ) . '</td>';
		$table .= '<td>' . number_format( $rate->euro, 4 ) . '</td>';
		$table .= '<td>' . number_format( $rate->yen, 2 ) . '</td>';
		$table .= '</tr>';
	}
	$table .= '</table>';
	return $table;
}
add_shortcode( 'exchange_rates', 'add_exchange_rates' );



/**
 * Returns Market Data
 *
 * This function returns Market Data in
 * seperate <table> tags. The tables use
 * jQuery to allow toggling of each market
 * data table.
 *
 * @function add_market_data()
 *
 * @uses add_exchange_rates(), add_average_earnings(), add_base_rate(), add_ftse_index(), add_retail_price_index()
 *
 * @param N/A
 *
 * @return (string) Market Data in tables, with toggle buttons
 *
 * @since 1.0.0 
 *
 */
function add_market_data() {
	
	$content = '<button class="tab-button" data-type="tab" data-target="exchange-rates">Exchange Rates</button>';
	$content .= '<button class="tab-button" data-type="tab" data-target="average-earnings">Average Earnings</button>';
	$content .= '<button class="tab-button" data-type="tab" data-target="base-rate">Base Rate</button>';
	$content .= '<button class="tab-button" data-type="tab" data-target="ftse-index">FTSE 100 Index</button>';
	$content .= '<button class="tab-button" data-type="tab" data-target="retail-price-index">Retail Price Index</button>';
	$content .= '<div class="tab active" data-tab-id="exchange-rates">' . add_exchange_rates() . '</div>';
	$content .= '<div class="tab" data-tab-id="average-earnings">' . add_average_earnings() . '</div>';
	$content .= '<div class="tab" data-tab-id="base-rate">' . add_base_rate() . '</div>';
	$content .= '<div class="tab" data-tab-id="ftse-index">' . add_ftse_index() . '</div>';
	$content .= '<div class="tab" data-tab-id="retail-price-index">' . add_retail_price_index() . '</div>';
	return $content;	
}
add_shortcode('market_data', 'add_market_data');