<?php
function sm_campaigns() {

	global $wpdb;
	$setstatus = plugins_url('setStatus.php', __FILE__);

	echo "<script type = 'text/javascript'>
	jQuery(document).ready(function () {
		jQuery('.ToggleSwitchSample').toggleSwitch();
	});

	function validateMyForm(){
			var name = jQuery('#campaignname').val();

			if (name =='') {
				alert('please enter campaign name!');
				return false;
			}
			return true;
	}

	function changeStatus(id){
		request = jQuery.ajax({
				type: 'post',
				  url: '" . $setstatus . "',
				  data: { id: id}
				  })
				    .done(function( msg ) {
				  });
	}
	</script>";

	if (isset($_POST['smNewCampaign'])) {
		$table_name = $wpdb->prefix . "smcampaigns";
		$results = $wpdb->get_results("SELECT *	FROM $table_name WHERE name != ''"
			, ARRAY_A);

		$wpdb->insert($table_name, array(
			'name' => '',
		));
		$id = $wpdb->insert_id;

		$_GET['edit'] = $id;

		echo "<script>
		window.location.replace('?page=sm_campaigns&edit=" . $id . "');
		</script>";
	}

	if (isset($_GET['edit'])) {
		sm_editcampaigns($_GET['edit']);
	} else {
		if (isset($_GET['delete'])) {
			$table_name = $wpdb->prefix . "smcampaigns";
			$wpdb->delete($table_name, array('id' => $_GET['delete']));
		}

		echo '<div class = "wrap">
				<div class = "smhead">
				' . SOCIALTRAFFICBOT . '  </div>
				<h1> Campaigns</h1>
				<hr />';

		echo "
				<div style ='float:left;'>
					<form method='post' action=''>
						<input name='smNewCampaign' type='submit' value='Add New Campaign' class = 'button button-primary' id ='smNewCampaign' >
					</form>
				</div>";
		echo '<form id="pagedata" method="post" action="">';

		$wp_list_table = new smCampaigns();
		$wp_list_table->prepare_items();
		$wp_list_table->display();

		echo '</form>';
	}
}

function sm_editcampaigns($id) {

	global $pagenow;
	global $wpdb;
	$wpdb->show_errors();

	$table_name = $wpdb->prefix . "smcampaigns";
	$query = "SELECT name FROM $table_name WHERE id = $id";

	$querydata = $wpdb->get_row($query);
	$name = $querydata->name;

	echo '<div class = "wrap">
				<div class = "smhead">
				' . SOCIALTRAFFICBOT . ' </div>
				<h1>' . $name . '</h1>
				<hr />';

	//generic HTML and code goes here

	if (isset($_GET['tab'])) {
		sm_admin_tabs($_GET['tab'], $id);
	} else {
		sm_admin_tabs('settings', $id);
	}
}

function sm_admin_tabs($current = 'settings', $campaign) {
	$campaign = $_GET['edit'];

	$tabs = array(
		'settings' => 'Setup',
		'titlesets' => 'Create Headlines',
		'getcontent' => 'All Content',
		'getcontent_facebook' => 'Facebook Content',
		'getcontent_instagram' => 'Instagram Content',
		'getcontent_pinterest' => 'Pinterest Content',
			'getcontent_reddit' => 'Reddit Content',
				'getcontent_twitter' => 'Twitter Content',
						'custom' => 'Custom Promotions',
		'viral_spiral' => 'Viral Spiral',
		'log' => 'Log',
	);

	echo '<div id="icon-themes" class="icon32"><br></div>';
	echo '<h2 class="nav-tab-wrapper">';
	foreach ($tabs as $tab => $name) {
		$class = ($tab == $current) ? ' nav-tab-active' : '';
		echo "<a class='nav-tab$class' href='?page=sm_campaigns&tab=$tab&edit=" . $campaign . "'>$name</a>";
	}

	echo '</h2>';
	if (isset($_GET['tab'])) {
		$tab = $_GET['tab'];
	} else {
		$tab = 'settings';
	}

	switch ($tab) {
	case 'settings':
		sm_campaign_settings($campaign);
		break;
	case 'titlesets':
		sm_titlesets($campaign);
		break;
	case 'getcontent':
	case 'getcontent_facebook':
	case 'getcontent_instagram':
	case 'getcontent_pinterest':
	case 'getcontent_twitter':
	case 'getcontent_reddit':
		sm_feeds($campaign);
		break;
	
		case 'custom':
		sm_custom($campaign);
		break;
	case 'viral_spiral':
		sm_viral_spiral($campaign);
		break;
	case 'log':
		sm_log($campaign);
		break;
	}
}

function sm_campaign_settings($campaign) {

	global $wpdb;

	if (isset($_POST['smSaveSettings'])) {

		$campaignname = trim($_POST['campaignname']);
		$id = trim($_POST['smFbID']);
		$secret = trim($_POST['smFbSecret']);
			$page = trim($_POST['smFbPage']);
		
		
		update_post_meta('111111113', $campaign . 'campaignname', $campaignname);

		update_post_meta(111111113, $campaign . 'smFbID', $id);
		update_post_meta(111111113, $campaign . 'smFbSecret', $secret);
			update_post_meta(111111113, $campaign . 'smFbPage', $page);
	

		update_post_meta(111111113, $campaign . 'smIGUser', trim($_POST['ig-user']));
		update_post_meta(111111113, $campaign . 'smIGPassword', trim($_POST['ig-password']));

		update_post_meta(111111113, $campaign . 'smPnEmail', trim($_POST['pinterest_email']));
	
			update_post_meta(111111113, $campaign . 'smPnPassword', trim($_POST['pinterest_password']));
		
		update_post_meta(111111113, $campaign . 'smPnBoard', trim($_POST['selected_pinterest_board']));

		update_post_meta(111111113, $campaign . 'smRdPage', trim($_POST['selected_reddit_subpage']));

		update_post_meta(111111113, $campaign . 'smRdID', trim($_POST['reddit_app_id']));
		update_post_meta(111111113, $campaign . 'smRdSecret', trim($_POST['reddit_app_secret']));

		$table_name = $wpdb->prefix . "smcampaigns";
		$wpdb->update(
			$table_name,
			array(
				'name' => $campaignname, // string
			),
			array('id' => $campaign));

		$successmessage = " <strong>Settings Saved</strong>";
	}

	$campaignname = get_post_meta(111111113, $campaign . 'campaignname', true);
	$id = get_post_meta(111111113, $campaign . 'smFbID', true);
	$secret = get_post_meta(111111113, $campaign . 'smFbSecret', true);
	$page = get_post_meta(111111113, $campaign . 'smFbPage', true);

	$smIGUser = get_post_meta(111111113, $campaign . 'smIGUser', true);
	$smIGPassword = get_post_meta(111111113, $campaign . 'smIGPassword', true);

	$pinterest_email = get_post_meta(111111113, $campaign . 'smPnEmail', TRUE);
	$pinterest_password = get_post_meta(111111113, $campaign . 'smPnPassword', TRUE);
	$selected_pinterest_board = get_post_meta(111111113, $campaign . 'smPnBoard', true);

	$reddit_app_id = get_post_meta(111111113, $campaign . 'smRdID', TRUE);
	$reddit_app_secret = get_post_meta(111111113, $campaign . 'smRdSecret', TRUE);
	$selected_reddit_subpage = get_post_meta(111111113, $campaign . 'smRdPage', true);

	echo "<br />
			<br />
			<form method='post' action='' onsubmit= 'return validateMyForm();'>
				<table>
					<tr class='table-gap'><td>Campaign Name: </td><td ><input type='text' name='campaignname' value='" . $campaignname . "' id ='campaignname'><input name='smSaveSettings' type='submit' value='Save' class = 'button button-primary' id ='smSaveSettings' >" . $successmessage . "</td></tr>";

	echo "
	<tr><td>&nbsp;</td></tr>
	<tr><td>Facebook User ID: </td><td ><input type='text' name='smFbID' value='" . $id . "'></td></tr>
	<tr><td>Facebook Session ID: </td><td ><input type='text' name='smFbSecret' value='" . $secret . "'></td></tr>
	<tr><td>Facebook Page ID: </td><td ><input type='text' name='smFbPage' value='" . $page . "'></td></tr>
	<tr><td>&nbsp;</td></tr>

	<tr><td>Instagram User: </td><td ><input type='text' name='ig-user' value='" . $smIGUser . "'></td></tr>
	<tr><td>Instagram Password: </td><td ><input type='password' name='ig-password' value='" . $smIGPassword . "'></td></tr>
	<tr><td>&nbsp;</td></tr>

	<tr><td>Pinterest Email: </td><td ><input type='text' name='pinterest_email' value='" . $pinterest_email . "' size='40'></td></tr>
	<tr><td>Pinterest Password: </td><td ><input type='password' name='pinterest_password' value='" . $pinterest_password . "' value='' size='40'></td></tr>
	<tr><td>&nbsp;</td></tr>";
	
	
	echo '<tr><td><h3>Autoblogging Settings</h3></td></tr>';
	echo '<tr><td>Post In Draft Mode? </td>
				<td>';

	if ($smdraft == "yes") {
		echo '<input type="radio" name="smdraft" value="yes" checked = "checked"> Yes
				<input type="radio" name="smdraft" value="no" > No';
	} else {
		echo '<input type="radio" name="smdraft" value="yes"> Yes
				<input type="radio" name="smdraft" value="no" checked = "checked"> No';
	}
	echo '</td></tr><tr><td>Show Source? </td><td>';

	if ($showsource == "yes") {
		echo '<input type="radio" name="smshowsource" value="yes" checked = "checked"> Yes
			<input type="radio" name="smshowsource" value="no" > No';
	} else {
		echo '<input type="radio" name="smshowsource" value="yes"> Yes
			<input type="radio" name="smshowsource" value="no" checked = "checked"> No';
	}
	echo '</td></tr>
			<tr><td>Source Label:  </td><td><input type="text" name="smsourcelabel" size = "60" value ="' . $sourcelabel . '"> </td></tr>
			<tr><td>Choose Category: </td><td>
			<select name="smcategory" >';

	$args = array(
		'type' => 'post',
		'child_of' => 0,
		'parent' => '',
		'orderby' => 'name',
		'order' => 'ASC',
		'hide_empty' => 0,
		'hierarchical' => 1,
		'exclude' => '',
		'include' => '',
		'number' => '',
		'taxonomy' => 'category',
		'pad_counts' => false,

	);
	$categories = get_categories($args);
	echo "categories =" . print_r($categories);

	foreach ($categories as $categoryi) {
		if ($category == $categoryi->name) {
			echo '<option value= "' . $categoryi->name . '" selected = "selected">' . $categoryi->name . '</option>';
		} else {
			echo '<option value="' . $categoryi->name . '">' . $categoryi->name . '</option>';
		}
	}

	echo '
			</select>
			</td>
		</tr>
		<tr>
			<td>Maximum Post Length:  </td><td><input type="text" name="smpostlength" size = "10" value ="' . $smpostlength . '">Leave empty for whole post </td>
		</tr>
		<tr>
			<td>Intro (spintax enabled):  </td><td><textarea name="smintro" rows = "8" cols = "80" >' . $smintro . '</textarea> </td>
		</tr>
		<tr>
			<td>Outro (spintax enabled):  </td><td><textarea name="smoutro"  rows = "8" cols = "80">' . $smoutro . '</textarea> </td>
		</tr>
		</table>
	</form>';



	echo'</table>
	</form>';
}
