<?php
/**
 * WebEngine CMS - TikTok Streamer Widget
 * Configuración del botón lateral y previsualización TikTok (template nexoraves6).
 *
 * @version 1.0
 * @author Gabo Dev <https://www.gabodevelop.com/>
 */

if (!defined('access')) die('Direct access not permitted.');

$moduleName = 'tiktok_streamer';
$xmlPath = __PATH_MODULE_CONFIGS__ . $moduleName . '.xml';

function saveTiktokStreamerChanges($xmlPath) {
	if (!is_string($xmlPath) || $xmlPath === '' || !is_writable($xmlPath)) {
		message('error', 'The configuration file is not writable.');
		return;
	}
	$xml = simplexml_load_file($xmlPath);
	if (!$xml) {
		message('error', 'Error loading config file.');
		return;
	}
	$xml->active = isset($_POST['setting_1']) && $_POST['setting_1'] == '1' ? '1' : '0';
	$xml->username = isset($_POST['setting_2']) ? trim($_POST['setting_2']) : '';
	$xml->button_label = isset($_POST['setting_3']) ? trim($_POST['setting_3']) : 'TikTok';
	$save = $xml->asXML($xmlPath);
	if ($save) {
		message('success', 'TikTok Streamer settings saved.');
	} else {
		message('error', 'Error saving changes.');
	}
}

if (isset($_POST['submit_changes'])) {
	saveTiktokStreamerChanges($xmlPath);
}

loadModuleConfigs($moduleName);
$tiktok_active = mconfig('active');
$tiktok_active = is_array($tiktok_active) ? 0 : (int)$tiktok_active;
$tiktok_username = mconfig('username');
$tiktok_username = is_array($tiktok_username) ? '' : (string)$tiktok_username;
$tiktok_label = mconfig('button_label');
$tiktok_label = is_array($tiktok_label) ? 'TikTok' : (string)$tiktok_label;
?>
<h2>TikTok Streamer Settings</h2>
<p>Show a lateral button above the footer (nexoraves6). When clicked, a panel slides to the left with the TikTok live preview.</p>
<form action="" method="post">
	<table class="table table-striped table-bordered table-hover module_config_tables">
		<tr>
			<th>Status</th>
			<td><?php enabledisableCheckboxes('setting_1', $tiktok_active, 'Enabled', 'Disabled'); ?></td>
		</tr>
		<tr>
			<th>TikTok username</th>
			<td>
				<input type="text" name="setting_2" class="form-control" value="<?php echo htmlspecialchars($tiktok_username); ?>" placeholder="e.g. username" />
				<span class="help-block">The TikTok @username (without @). Used for live page: tiktok.com/@username/live</span>
			</td>
		</tr>
		<tr>
			<th>Button label</th>
			<td>
				<input type="text" name="setting_3" class="form-control" value="<?php echo htmlspecialchars($tiktok_label); ?>" placeholder="TikTok" />
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<button type="submit" name="submit_changes" class="btn btn-primary">Save changes</button>
			</td>
		</tr>
	</table>
</form>
