<?php
$activePage = basename($_SERVER['PHP_SELF'], '.php');
$titles = [
    'index' => 'Makro-Dashboard',
    'makro-analyse' => 'Makro-Analyse',
    'globale-liquiditaet' => 'Globale Liquidität',
    'sentiment-score' => 'Sentiment-Score',
    'zyklus' => 'Zyklus',
    'sektoren' => 'Sektoren',
    'forecast' => 'Forecast',
    'minervini-kriterien' => 'Minvervini-Kriterien',
    'aktien-scanner' => 'Aktien-Scanner',
    'aktien-screener' => 'Aktien-Screener',
    'watchlist' => 'Watchlist',
    'depot' => 'Depot',
    'saisonalitaet' => 'Saisonalität',
    'saisonale-aktien' => 'Saisonale Aktien',
    'korrelationen' => 'Korrelationen',
    'korrelation-heatmap' => 'Korrelation Heatmap',
    'aktienbewertung' => 'Aktienbewerung',
    'strategic-profile' => 'Strategisches Profil',
    'chart-whale-patterns' => 'Chart Whale Pattners',
    'insider-trades' => 'Insider Trades',
    'market-movers' => 'Market Movers',
    'tickers' => 'Tickers',
    'money-management' => 'Money Management',
    'risikoprofil' => 'Risikoprofil'
];
$pageTitle = $titles[$activePage] ?? 'Makro-Dashboard';
?>
<!DOCTYPE html>
<html lang="de">
  <head>
    <meta charset="UTF-8">
    <title><?= $pageTitle ?> | Trade Station</title>
    <link href="assets/css/plugins.css" rel="stylesheet">
    <link href="assets/css/style.css" rel="stylesheet">
    <script src="assets/js/bootstrap.bundle.min.js"></script>
    <script src="assets/js/jquery-3.7.1.min.js"></script>
    
    <?php if (!empty($requires_chartjs)): ?>
      <script src="assets/js/chart.js"></script>
      <script src="assets/js/chartjs-adapter-date-fns.js"></script>
      <script src="assets/js/chartjs-plugin-annotation@1.4.0.js"></script>
      <script src="assets/js/chartjs-plugin-datalabels@2.js"></script>
      <script src="assets/js/chart.boxplot.js"></script>
      <script>
        if (typeof ChartBoxPlot !== 'undefined') {
          const components = [
            ChartBoxPlot.BoxPlotController,
            ChartBoxPlot.BoxAndWhiskers,
            ChartBoxPlot.ViolinController,
            ChartBoxPlot.Violin,
            ChartBoxPlot.BoxPlot,
            ChartBoxPlot.ViolinPlot
          ].filter(Boolean);
      
          Chart.register(...components);
        } else {
          console.warn('ChartBoxPlot nicht geladen.');
        }
      </script>
    <?php endif; ?>
    
    <?php if (!empty($requires_dataTables)): ?>
      <script src="assets/js/jquery.dataTables.min.js"></script>
      <script src="assets/js/dataTables.bootstrap5.min.js"></script>
    <?php endif; ?>
    
    <?php if (!empty($requires_search)): ?>
      <link href="assets/css/jquery-ui.css" rel="stylesheet">
      <script src="assets/js/jquery-ui.min.js"></script>
      <script>
      document.addEventListener("DOMContentLoaded", function () {
        let tickers = [];
      
        fetch('/views/suche/tickers.json')
          .then(res => res.json())
          .then(data => {
            tickers = data.map(t => ({
              label: `<strong>${t.symbol}</strong><br><small class="text-muted">${t.name} (${t.exchangeShortName})</small>`,
              value: t.symbol
            }));
      
            document.querySelectorAll('.symbol-autocomplete').forEach(input => {
              $(input).autocomplete({
                source: tickers,
                minLength: 2
              });
            });
      
            $.ui.autocomplete.prototype._renderItem = function (ul, item) {
              return $("<li>")
                .append("<div>" + item.label + "</div>")
                .appendTo(ul);
            };
          })
          .catch(err => {
            console.error("Ticker-Daten konnten nicht geladen werden:", err);
          });
      });
      </script>
      <script>
      document.addEventListener("DOMContentLoaded", function () {
        // URL-Parameter ins Feld übernehmen
        const params = new URLSearchParams(window.location.search);
        const preset = params.get('symbol');
        const form = document.getElementById('symbolSearchForm');
        const input = document.getElementById('symbolInput');
        const actionSelect = document.getElementById('actionSelect');
      
        if (preset) input.value = preset;
      
        form.addEventListener('submit', function (e) {
          e.preventDefault();
      
          const symbol = input.value.trim();
          const action = actionSelect.value;
      
          if (!symbol || !action) {
            alert("Bitte ein Symbol eingeben und eine Aktion wählen.");
            return;
          }
      
          const targets = {
            fundamental: 'aktienbewertung.php',
            strategic_profile: 'strategic-profile.php',
            saisonalitaet: 'saisonalitaet.php',
            minervini: 'minervini-kriterien.php',
            chart_whale_patterns: 'chart-whale-patterns.php'
          };
      
          const targetUrl = `${targets[action]}?symbol=${encodeURIComponent(symbol)}`;
          window.location.href = targetUrl;
        });
      });
      </script>
    <?php endif; ?>
    
  </head>
  <body id="top" class="d-flex flex-column min-vh-100">