<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
require_once __DIR__ . '/../../app/core/theme.php';
auth_require_login();
$pdo = crm_pdo();
$page_title = 'Branding & Theme';

$fields = [
    'brand_app_name' => gp_theme_setting($pdo, 'brand_app_name', 'GoPools CRM'),
    'brand_logo' => gp_theme_setting($pdo, 'brand_logo', ''),
    'brand_favicon' => gp_theme_setting($pdo, 'brand_favicon', ''),
    'theme_mode' => gp_theme_setting($pdo, 'theme_mode', 'light'),
    'theme_primary' => gp_theme_setting($pdo, 'theme_primary', '#2563eb'),
    'theme_secondary' => gp_theme_setting($pdo, 'theme_secondary', '#0f172a'),
    'theme_glass' => gp_theme_setting($pdo, 'theme_glass', '1'),
    'background_type' => gp_theme_setting($pdo, 'background_type', 'gradient'),
    'background_color' => gp_theme_setting($pdo, 'background_color', '#f8fafc'),
    'background_image' => gp_theme_setting($pdo, 'background_image', ''),
    'background_gradient' => gp_theme_setting($pdo, 'background_gradient', 'linear-gradient(135deg,#f8fafc 0%,#e2e8f0 100%)'),
    'background_overlay_opacity' => gp_theme_setting($pdo, 'background_overlay_opacity', '0.10'),
];

$ok = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    foreach (array_keys($fields) as $k) {
        $fields[$k] = ($k === 'theme_glass') ? (isset($_POST[$k]) ? '1' : '0') : trim((string)($_POST[$k] ?? ''));
    }
    foreach ($fields as $k => $v) {
        $stmt = $pdo->prepare("INSERT INTO settings (setting_key, setting_value) VALUES (:k,:v) ON DUPLICATE KEY UPDATE setting_value = VALUES(setting_value)");
        $stmt->execute([':k' => $k, ':v' => $v]);
    }
    $ok = 'Branding settings saved.';
}

include __DIR__ . '/../includes/header.php';
?>
<div class="page-header">
  <div class="page-header-title">
    <h1>Branding & Theme</h1>
    <span>Logo, favicon, colors, light/dark, gradient backgrounds</span>
  </div>
  <div class="page-header-actions"><a class="btn btn-secondary btn-sm" href="/public/settings/index.php">Back to settings</a></div>
</div>

<div class="grid-2">
  <div class="card">
    <?php if ($ok): ?><div class="alert-danger" style="background:rgba(22,163,74,.15);color:#166534;border-color:rgba(22,163,74,.25)"><?php echo h($ok); ?></div><?php endif; ?>
    <form method="post">
      <div class="form-grid">
        <div class="form-group"><label>App name</label><input type="text" name="brand_app_name" value="<?php echo h($fields['brand_app_name']); ?>"></div>
        <div class="form-group"><label>Logo URL / path</label><input type="text" name="brand_logo" value="<?php echo h($fields['brand_logo']); ?>" placeholder="/public/uploads/logo.png"></div>
        <div class="form-group"><label>Favicon URL / path</label><input type="text" name="brand_favicon" value="<?php echo h($fields['brand_favicon']); ?>" placeholder="/public/uploads/favicon.ico"></div>
        <div class="form-group"><label>Theme mode</label><select name="theme_mode"><option value="light" <?php echo $fields['theme_mode']==='light'?'selected':''; ?>>Light premium</option><option value="dark" <?php echo $fields['theme_mode']==='dark'?'selected':''; ?>>Dark premium</option><option value="auto" <?php echo $fields['theme_mode']==='auto'?'selected':''; ?>>Auto</option></select></div>
        <div class="form-group"><label>Primary color</label><input type="color" name="theme_primary" value="<?php echo h($fields['theme_primary']); ?>"></div>
        <div class="form-group"><label>Secondary color</label><input type="color" name="theme_secondary" value="<?php echo h($fields['theme_secondary']); ?>"></div>
        <div class="form-group"><label>Glass cards</label><div><input type="checkbox" name="theme_glass" <?php echo $fields['theme_glass']==='1'?'checked':''; ?>></div></div>
        <div class="form-group"><label>Background type</label><select name="background_type"><option value="color" <?php echo $fields['background_type']==='color'?'selected':''; ?>>Solid color</option><option value="image" <?php echo $fields['background_type']==='image'?'selected':''; ?>>Image</option><option value="gradient" <?php echo $fields['background_type']==='gradient'?'selected':''; ?>>Gradient</option></select></div>
        <div class="form-group"><label>Background color</label><input type="color" name="background_color" value="<?php echo h($fields['background_color']); ?>"></div>
        <div class="form-group"><label>Background image URL / path</label><input type="text" name="background_image" value="<?php echo h($fields['background_image']); ?>" placeholder="/public/uploads/background.jpg"></div>
        <div class="form-group" style="grid-column:1/-1"><label>Gradient CSS</label><input type="text" name="background_gradient" value="<?php echo h($fields['background_gradient']); ?>"></div>
        <div class="form-group"><label>Background overlay opacity</label><input type="number" step="0.01" min="0" max="0.90" name="background_overlay_opacity" value="<?php echo h($fields['background_overlay_opacity']); ?>"></div>
      </div>
      <div class="form-actions"><button class="btn btn-primary" type="submit">Save branding</button></div>
    </form>
  </div>

  <div class="card">
    <div class="page-header-title"><h1 style="font-size:1.05rem">What else to add next</h1><span>Recommended next settings</span></div>
    <div class="small" style="line-height:1.7">
      <div>• upload-based logo/background manager</div>
      <div>• email templates for dispatch / reminders</div>
      <div>• notification center</div>
      <div>• checklist templates by project type</div>
      <div>• role permissions panel</div>
      <div>• customer portal branding</div>
      <div>• live preview theme switcher</div>
    </div>
  </div>
</div>

<?php include __DIR__ . '/../includes/footer.php'; ?>
