<?php
session_start();

// ✅ FIX: Path detection logic (Try both common paths)
$configPath = __DIR__ . "/includes/config.php";
if (!file_exists($configPath)) {
    $configPath = __DIR__ . "/../includes/config.php"; // Fallback if file is in subfolder
}
if (file_exists($configPath)) {
    include $configPath;
} else {
    die("Error: Config file not found.");
}

if (!isset($_SESSION['admin_id'])) {
    header("Location: login.php");
    exit();
}

$success = "";
$error = "";

// ✅ Save notice
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title    = trim($_POST['title'] ?? '');
    $message  = trim($_POST['message'] ?? '');
    $telegram = trim($_POST['telegram'] ?? '');

    if ($title && $message) {
        // Insert new notice (History maintain karne ke liye)
        $stmt = $conn->prepare("INSERT INTO notices (title, message, telegram_link, updated_at) VALUES (?, ?, ?, NOW())");
        $stmt->bind_param("sss", $title, $message, $telegram);
        
        if($stmt->execute()){
            $success = "✅ Notice updated successfully!";
        } else {
            $error = "❌ Database Error: " . $stmt->error;
        }
        $stmt->close();
    } else {
        $error = "⚠️ Please fill all required fields.";
    }
}

// ✅ Fetch last notice
$res = $conn->query("SELECT * FROM notices ORDER BY updated_at DESC LIMIT 1");
$notice = $res->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Admin Notice Settings</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <style>
    :root {
      --primary: #003366;
      --accent: #2196f3;
      --bg: #f4f7fb;
      --white: #fff;
    }
    body {
      font-family: "Segoe UI", sans-serif;
      background: var(--bg);
      margin: 0;
      padding: 0;
      display: flex;
      justify-content: center;
      align-items: flex-start;
      min-height: 100vh;
    }
    .container {
      background: var(--white);
      width: 95%;
      max-width: 850px;
      margin: 20px auto;
      border-radius: 16px;
      padding: 25px;
      box-shadow: 0 5px 25px rgba(0,0,0,0.12);
      position: relative;
    }
    /* Back Button */
    .back-btn {
        position: absolute;
        top: 20px;
        left: 20px;
        text-decoration: none;
        background: #eef5ff;
        color: var(--primary);
        padding: 8px 12px;
        border-radius: 8px;
        font-weight: bold;
        font-size: 14px;
        transition: 0.3s;
    }
    .back-btn:hover { background: #dceaff; }

    h1 {
      text-align: center;
      color: var(--primary);
      margin-bottom: 15px;
      margin-top: 10px;
    }
    form {
      display: flex;
      flex-direction: column;
      gap: 18px;
    }
    label {
      font-weight: 600;
      color: var(--primary);
      margin-bottom: 5px;
      display: block;
    }
    input, textarea {
      padding: 12px;
      font-size: 15px;
      border-radius: 10px;
      border: 1px solid #ccc;
      outline: none;
      width: 100%;
      transition: 0.2s;
      box-sizing: border-box; 
    }
    input:focus, textarea:focus {
      border-color: var(--accent);
      box-shadow: 0 0 6px rgba(33,150,243,0.4);
    }
    textarea {
      resize: vertical;
      min-height: 120px;
    }
    button {
      padding: 14px;
      background: linear-gradient(90deg,#2196f3,#003366);
      border: none;
      color: #fff;
      font-size: 16px;
      font-weight: 600;
      border-radius: 12px;
      cursor: pointer;
      transition: 0.3s;
    }
    button:hover {
      background: linear-gradient(90deg,#003366,#2196f3);
    }
    .msg {
      text-align: center;
      padding: 12px;
      margin-bottom: 15px;
      border-radius: 8px;
      font-weight: 500;
    }
    .success { background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
    .error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }

    /* Preview Section */
    .preview {
      background: #eef5ff;
      padding: 20px;
      border-radius: 12px;
      margin-top: 30px;
      border: 1px dashed #2196f3;
      text-align: center;
    }
    .preview h3 { margin: 0 0 15px; color: #003366; font-size: 16px; text-transform: uppercase; letter-spacing: 1px; }
    
    /* Simulate User Popup */
    .preview-box {
        background: #fff;
        padding: 20px;
        border-radius: 12px;
        box-shadow: 0 4px 20px rgba(0,0,0,0.1);
        display: inline-block;
        width: 100%;
        max-width: 400px;
        text-align: left;
    }
    .preview-box strong { display: block; font-size: 18px; margin-bottom: 10px; color: #003366; }
    .preview-box p { margin: 0 0 15px; color: #444; line-height: 1.5; font-size: 14px; }
    .preview-btn {
      display: inline-block;
      width: 100%;
      text-align: center;
      padding: 10px 0;
      border-radius: 8px;
      background: linear-gradient(90deg,#007bff,#0056b3);
      color: #fff;
      text-decoration: none;
      font-weight: bold;
      font-size: 14px;
      box-sizing: border-box;
    }
    
    @media(max-width: 600px){
      .container { padding: 18px; }
      button { font-size: 14px; padding: 12px; }
      input, textarea { font-size: 14px; }
    }
  </style>
</head>
<body>
  <div class="container">
    <a href="dashboard.php" class="back-btn">← Back</a>
    
    <h1>📢 Manage Notice</h1>

    <?php if (!empty($success)): ?>
      <div class="msg success"><?= $success ?></div>
    <?php elseif (!empty($error)): ?>
      <div class="msg error"><?= $error ?></div>
    <?php endif; ?>

    <form method="post">
      <div>
        <label>Notice Title</label>
        <input type="text" name="title" placeholder="e.g. System Maintenance" value="<?= htmlspecialchars($notice['title'] ?? '') ?>" required>
      </div>
      <div>
        <label>Notice Message</label>
        <textarea name="message" placeholder="Write your message here..." required><?= htmlspecialchars($notice['message'] ?? '') ?></textarea>
      </div>
      <div>
        <label>Telegram/Contact Link (Optional)</label>
        <input type="url" name="telegram" placeholder="https://t.me/yourchannel" value="<?= htmlspecialchars($notice['telegram_link'] ?? '') ?>">
      </div>
      <button type="submit">💾 Update Notice</button>
    </form>

    <?php if($notice): ?>
    <div class="preview">
      <h3>🔍 Live Preview (User View)</h3>
      
      <!-- Mockup of the User Popup -->
      <div class="preview-box">
          <strong><?= htmlspecialchars($notice['title']) ?></strong>
          <p><?= nl2br(htmlspecialchars($notice['message'])) ?></p>
          <?php if(!empty($notice['telegram_link'])): ?>
            <a href="<?= htmlspecialchars($notice['telegram_link']) ?>" target="_blank" class="preview-btn">✈ Contact via Telegram</a>
          <?php endif; ?>
      </div>
      
    </div>
    <?php endif; ?>
  </div>
</body>
</html>