<?php
session_start();
if (!isset($_SESSION['panel_user'])) { header('Location: index.php'); exit; }
if (($_SESSION['tipo_user'] ?? '') !== 'admin') { header('Location: dashboard.php'); exit; }
require_once dirname(__DIR__) . '/api/_auth_common.php';
$db = new PDO('sqlite:' . dirname(__DIR__) . '/api/db.db');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
auth_ensure_schema($db);
$alert = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $mode = strtolower(trim((string)($_POST['active_login_mode'] ?? 'normal')));
    if (!in_array($mode, ['normal','mac','activation_code'], true)) $mode = 'normal';
    $qrcode = isset($_POST['qrcode_app_enabled']) ? 1 : 0;
    $renew = isset($_POST['renew_button_enabled']) ? 1 : 0;
    $testButton = isset($_POST['test_button_enabled']) ? 1 : 0;
    $vencimentoModal = isset($_POST['vencimento_modal_enabled']) ? 1 : 0;
    $st = $db->prepare('UPDATE app_config SET active_login_mode = ?, qrcode_app_enabled = ?, renew_button_enabled = ?, test_button_enabled = ?, vencimento_modal_enabled = ?, updated_at = datetime(\'now\') WHERE id = 1');
    $st->execute([$mode, $qrcode, $renew, $testButton, $vencimentoModal]);
    $alert = 'Configurações salvas com sucesso.';
}
$cfg = $db->query('SELECT * FROM app_config WHERE id = 1 LIMIT 1')->fetch(PDO::FETCH_ASSOC) ?: [];
include './includes/header.php';
function h($v){ return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }
?>
<div class="card radius-10">
<div class="card-body">
<h4>Configuração do Login do App</h4>
<?php if ($alert): ?><div class="alert alert-success"><?= h($alert) ?></div><?php endif; ?>
<form method="post">
<div class="form-group mb-3">
<label>Modo de login ativo no app</label>
<select name="active_login_mode" class="form-control">
<option value="normal" <?= (($cfg['active_login_mode'] ?? 'normal') === 'normal') ? 'selected' : '' ?>>Login normal com usuário e senha</option>
<option value="mac" <?= (($cfg['active_login_mode'] ?? '') === 'mac') ? 'selected' : '' ?>>Login por MAC automático</option>
<option value="activation_code" <?= (($cfg['active_login_mode'] ?? '') === 'activation_code') ? 'selected' : '' ?>>Login por código de ativação</option>
</select>
<small class="text-muted">O auto login por MAC também funciona no modo normal quando o MAC já possui DNS, usuário e senha válidos no cadastro.</small>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-check form-switch mb-3">
<input class="form-check-input" type="checkbox" name="qrcode_app_enabled" id="qrcode_app_enabled" <?= ((int)($cfg['qrcode_app_enabled'] ?? 1) === 1) ? 'checked' : '' ?>>
<label class="form-check-label" for="qrcode_app_enabled">Mostrar QR Code e card de ativação na LoginActivity</label>
</div>
</div>
<div class="col-md-6">
<div class="form-check form-switch mb-3">
<input class="form-check-input" type="checkbox" name="test_button_enabled" id="test_button_enabled" <?= ((int)($cfg['test_button_enabled'] ?? 1) === 1) ? 'checked' : '' ?>>
<label class="form-check-label" for="test_button_enabled">Mostrar botão Testar grátis na LoginActivity</label>
</div>
</div>
<div class="col-md-6">
<div class="form-check form-switch mb-3">
<input class="form-check-input" type="checkbox" name="vencimento_modal_enabled" id="vencimento_modal_enabled" <?= ((int)($cfg['vencimento_modal_enabled'] ?? 1) === 1) ? 'checked' : '' ?>>
<label class="form-check-label" for="vencimento_modal_enabled">Ativar modal de vencimento na HomeActivity</label>
</div>
</div>
<div class="col-md-6">
<div class="form-check form-switch mb-3">
<input class="form-check-input" type="checkbox" name="renew_button_enabled" id="renew_button_enabled" <?= ((int)($cfg['renew_button_enabled'] ?? 1) === 1) ? 'checked' : '' ?>>
<label class="form-check-label" for="renew_button_enabled">Mostrar botão Renovar na HomeActivity</label>
</div>
</div>
</div>
<button class="btn btn-primary">Salvar</button>
</form>
</div>
</div>
<?php include './includes/footer.php'; ?>
