<?php
function sm_titlesets($campaign) {
	if (isset($_POST['smAddTitleSet'])) {
		global $wpdb;
		$wpdb->show_errors();
		$table_name = $wpdb->prefix . "smtitlesets";

		$title = $_POST['title'];
		if (!is_array($title)) {
			$title = [$title];
		}
		$wpdb->insert($table_name, array(
			'setname' => trim($_POST['setname']),
			'title' => implode('|:|', $title),
		));
	}

	$formfields = '<tr><td style="width: 100px">Set Name:</td><td><input type="text" name="setname" size="50"></td></tr>
      <tr><td style="width: 100px">Title:</td><td><input type="text" name="title[0]" size="50"></td></tr>';

	echo '<div>
    <form method="post" action="">
    <table style="width: 100%">' . $formfields;

	echo '<tr><td></td><td><a class="add-more-title button button-secondary">Add More</a>&nbsp;<input name="smAddTitleSet" type="submit" value="Save" class="button button-primary"></td></tr>
    </table>
    </form>
    </div>
    <form id="pagedata" method="post" action="">';

	$wp_list_table = new smTitlesets($campaign);
	$wp_list_table->prepare_items();
	$wp_list_table->display();

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

	echo '<div class="inline-edit-tts-form hidden">
    <input type="hidden" name="id" />
    <table style="width: 100%; border-top: 1px solid #cccccc; border-bottom: 1px solid #cccccc;">' . $formfields .
		'<tr><td></td><td><a class="add-more-title button button-secondary">Add More</a> &nbsp; <a href="#" class="button inline-edit-tts-update button-primary">Update</a> &nbsp; <a href="#" class="button inline-edit-cancel button-secondary">Cancel</a></td></tr>
    </table>
  </div>';
}
