<?php
function sm_getCustomTitleOptions() {
	global $wpdb;
	$wpdb->show_errors();
	$table_name = $wpdb->prefix . "smtitlesets";

	$query = "SELECT * FROM $table_name ORDER BY setname ASC";
	$results = $wpdb->get_results($query);

	$out = "";
	foreach ($results as $res) {
		$out .= "<option value=\"{$res->id}\">{$res->setname}</option>";
	}

	return $out;
}

function sm_getCustomIOtroOptions() {
	global $wpdb;
	$wpdb->show_errors();
	$table_name = $wpdb->prefix . "smintrosoutros";

	$query = "SELECT * FROM $table_name ORDER BY setname ASC";
	$results = $wpdb->get_results($query);

	$out = "";
	foreach ($results as $res) {
		$out .= "<option value=\"{$res->id}\">{$res->setname}</option>";
	}

	return $out;
}

function sm_feeds($campaign) {
	if (isset($_POST['smAddFeed'])) {
		$smFeedPeriod = trim($_POST['smFeedPeriod']);
		$smhashtags = trim($_POST['smhashtags']);

		global $wpdb;
		$wpdb->show_errors();
		$table_name = $wpdb->prefix . "smfeeds";

		$wpdb->insert($table_name, array(
			'contentType'	=> trim($_POST['content-type']),
			'titlesetId' 	=> trim($_POST['titlesetId']),
			'ioId' 				=> trim($_POST['ioId']),
			'campaign' 		=> $campaign,
			'hashtags' 		=> $smhashtags,
			'url' 				=> trim($_POST['feedurl']),
		));
	}

	echo '<div>
		<form method="post" action="">
		<table style="width: 100%">
			<tr><td style="width: 100px">URL:</td><td><input type="text" name="feedurl" size = "50"></td></tr>
			<tr><td>Content Type</td><td><select name="content-type">
				<option value="rss-feed">RSS Feed</option>
				<option value="youtube-channel">Youtube Channel</option>
				<option value="youtube-playlist">Youtube Playlist</option>
			</select></td></tr>';

	echo '<tr><td></td><td><input name="smAddFeed" type="submit" value="Add" class="button button-primary" /></td></tr>
		</table>
		</form>
		</div>
		<form id="pagedata" method="post" action="">';

	$wp_list_table = new smFeeds($campaign);
	$wp_list_table->prepare_items();
	$wp_list_table->display();
	echo '</form>';

	echo '<div id="inline-edit-link" class="hidden" data-url="' . plugins_url('inline-edit.php', __FILE__) . '" />';

	echo '<div class="inline-edit-feed-form hidden">
		<input type="hidden" name="id" />
		<table style="width: 100%; border-top: 1px solid #cccccc; border-bottom: 1px solid #cccccc;">
			<tr><td>URL:</td><td><input type="text" name="url" size="50"></td></tr>
			<tr><td>Content Type:</td><td><select name="contentType">
				<option value="rss-feed">RSS Feed</option>
				<option value="youtube-channel">Youtube Channel</option>
				<option value="youtube-playlist">Youtube Playlist</option>
			</select></td></tr>
			<tr><td></td><td><a href="#" class="button inline-edit-feed-update button-primary">Update</a> &nbsp; <a href="#" class="button inline-edit-cancel button-secondary">Cancel</a></td></tr>
		</table>
	</div>';
}
