<?php
session_start();
include('config.php');

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

// Enable error reporting
ini_set('display_errors', 1);
error_reporting(E_ALL);

$error_message = "";

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name']);
    $score = trim($_POST['score']);
    $regulation_status = trim($_POST['regulation_status']);
    $risk_labels = trim($_POST['risk_labels']);
    $years_in_business = trim($_POST['years_in_business']);
    $flag_url = trim($_POST['flag_url']);
    $detail_url = trim($_POST['detail_url']);
    $image_url = null;

    // Handle image upload
    if (!empty($_FILES['image_url']['name'])) {
        $uploadDir = 'uploads/';
        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0777, true);
        }
        $filename = uniqid() . '_' . basename($_FILES['image_url']['name']);
        $targetPath = $uploadDir . $filename;
        if (move_uploaded_file($_FILES['image_url']['tmp_name'], $targetPath)) {
            $image_url = $targetPath;
        } else {
            $error_message = "Image upload failed!";
        }
    }

    // Insert into DB
    if(empty($error_message)) {
        try {
            $stmt = $dbh->prepare("INSERT INTO blackbrokers 
                (name, image_url, score, regulation_status, risk_labels, years_in_business, flag_url, detail_url, created_at) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, NOW())");
            $success = $stmt->execute([
                $name,
                $image_url,
                $score,
                $regulation_status,
                $risk_labels,
                $years_in_business,
                $flag_url,
                $detail_url
            ]);

            if ($success) {
                echo "<script>alert('Blackbroker added successfully.'); window.location.href='list_blackbrokers.php';</script>";
                exit;
            } else {
                $error_message = "Failed to insert data.";
            }
        } catch (PDOException $e) {
            $error_message = "Error: " . $e->getMessage();
        }
    }
}

// Include Premium Admin Header & Sidebar
include 'includes/header.php';
include 'includes/sidebar.php';
?>

<style>
  /* ── Layout & Page Header ── */
  .dash { max-width: 900px; margin: 0 auto; padding: 30px 26px; z-index: 1; position: relative; }
  
  .page-header { display: flex; align-items: flex-end; justify-content: space-between; margin-bottom: 28px; flex-wrap: wrap; gap: 14px; }
  .ph-eyebrow { font-family: 'JetBrains Mono', monospace; font-size: 0.6rem; letter-spacing: 3.5px; text-transform: uppercase; color: var(--red); margin-bottom: 6px; display: flex; align-items: center; gap: 7px; }
  .ph-eyebrow::before { content: ''; display: inline-block; width: 20px; height: 2px; background: var(--red); border-radius: 2px; }
  .ph-title { font-family: 'Syne', sans-serif; font-size: 2rem; font-weight: 800; color: var(--text-1); letter-spacing: -0.5px; line-height: 1.1; }

  /* ── Alerts ── */
  .custom-alert-error { background: rgba(239,79,74,0.1); border: 1px solid rgba(239,79,74,0.2); color: var(--red); padding: 14px 20px; border-radius: 10px; margin-bottom: 24px; display: flex; align-items: center; gap: 10px; font-weight: 500; font-size: 0.9rem; }
  
  /* ── Form Panel ── */
  .form-panel { background: var(--dark-700); border: 1px solid rgba(255,255,255,0.05); border-radius: 14px; padding: 30px; }
  
  .section-title { font-family: 'Syne', sans-serif; font-size: 1.1rem; font-weight: 700; color: var(--text-1); border-bottom: 1px solid rgba(255,255,255,0.05); padding-bottom: 10px; margin-bottom: 20px; margin-top: 30px; }
  .section-title:first-child { margin-top: 0; }
  .section-title i { color: var(--red); margin-right: 8px; font-size: 1rem; }

  .grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }

  .form-group { margin-bottom: 18px; }
  .form-label { display: block; font-family: 'JetBrains Mono', monospace; font-size: 0.7rem; text-transform: uppercase; letter-spacing: 1px; color: var(--text-2); margin-bottom: 8px; font-weight: 600; }
  
  .form-control { width: 100%; padding: 12px 16px; background: var(--dark-800); border: 1px solid rgba(255,255,255,0.08); border-radius: 8px; color: var(--text-1); font-size: 0.9rem; transition: 0.3s; font-family: 'Manrope', sans-serif; }
  .form-control:focus { outline: none; border-color: var(--red); box-shadow: 0 0 0 3px rgba(239,79,74,0.15); }
  .form-control::placeholder { color: rgba(255,255,255,0.2); }
  
  textarea.form-control { min-height: 100px; resize: vertical; }

  input[type="file"].form-control { padding: 9px 16px; color: var(--text-2); }
  input[type="file"]::file-selector-button { background: var(--dark-600); color: var(--text-1); border: 1px solid rgba(255,255,255,0.1); padding: 5px 12px; border-radius: 6px; margin-right: 15px; cursor: pointer; font-family: 'JetBrains Mono', monospace; font-size: 0.7rem; transition: 0.2s; }
  input[type="file"]::file-selector-button:hover { background: rgba(239,79,74,0.1); color: var(--red); border-color: var(--red); }

  /* ── Action Buttons ── */
  .form-actions { display: flex; gap: 15px; margin-top: 30px; padding-top: 20px; border-top: 1px solid rgba(255,255,255,0.05); }
  .btn-submit { background: var(--red); color: #fff; padding: 12px 28px; border: none; border-radius: 8px; font-weight: 700; font-size: 0.95rem; cursor: pointer; transition: 0.2s; display: flex; align-items: center; gap: 8px; }
  .btn-submit:hover { background: #d9433e; transform: translateY(-2px); box-shadow: 0 4px 12px rgba(239,79,74,0.3); }
  .btn-cancel { background: rgba(255,255,255,0.05); color: var(--text-1); padding: 12px 28px; border: 1px solid rgba(255,255,255,0.1); border-radius: 8px; font-weight: 600; font-size: 0.95rem; cursor: pointer; text-decoration: none; transition: 0.2s; display: flex; align-items: center; gap: 8px; }
  .btn-cancel:hover { background: rgba(255,255,255,0.1); color: #fff; }

  @media (max-width: 768px) {
      .grid-2 { grid-template-columns: 1fr; gap: 10px; }
      .form-actions { flex-direction: column; }
      .btn-submit, .btn-cancel { width: 100%; justify-content: center; }
  }
</style>

<main class="dash">
    <!-- Page Header -->
    <div class="page-header">
        <div>
            <div class="ph-eyebrow">Scam Alert</div>
            <div class="ph-title">Add <strong>Blacklisted Broker</strong></div>
        </div>
    </div>

    <!-- Error Alert -->
    <?php if (!empty($error_message)): ?>
        <div class="custom-alert-error">
            <i class="fas fa-exclamation-triangle"></i> <?= $error_message ?>
        </div>
    <?php endif; ?>

    <div class="form-panel">
        <form method="POST" enctype="multipart/form-data">
            
            <h3 class="section-title"><i class="fas fa-ban"></i> Broker Details</h3>
            
            <div class="grid-2">
                <div class="form-group">
                    <label class="form-label">Broker Name</label>
                    <input type="text" name="name" class="form-control" placeholder="e.g. ScamBrokerFX" required>
                </div>
                <div class="form-group">
                    <label class="form-label">Score</label>
                    <input type="number" step="0.01" name="score" class="form-control" placeholder="e.g. 1.23" required>
                </div>
            </div>

            <div class="grid-2">
                <div class="form-group">
                    <label class="form-label">Regulation Status</label>
                    <input type="text" name="regulation_status" class="form-control" placeholder="e.g. Unregulated / Fake License" required>
                </div>
                <div class="form-group">
                    <label class="form-label">Years in Business</label>
                    <input type="text" name="years_in_business" class="form-control" placeholder="e.g. < 1 Year" required>
                </div>
            </div>

            <div class="form-group">
                <label class="form-label">Risk Labels (Comma Separated)</label>
                <textarea name="risk_labels" class="form-control" placeholder="e.g. Clone Firm, Withdrawal Issues, No License" required></textarea>
            </div>

            <h3 class="section-title"><i class="fas fa-link"></i> Media & URLs</h3>
            
            <div class="grid-2">
                <div class="form-group">
                    <label class="form-label">Flag URL (Optional)</label>
                    <input type="text" name="flag_url" class="form-control" placeholder="https://example.com/flag.png">
                </div>
                <div class="form-group">
                    <label class="form-label">Detail Page URL (Optional)</label>
                    <input type="text" name="detail_url" class="form-control" placeholder="https://example.com/broker-details">
                </div>
            </div>

            <div class="form-group">
                <label class="form-label">Broker Logo (Image Upload)</label>
                <input type="file" name="image_url" class="form-control">
            </div>

            <!-- Action Buttons -->
            <div class="form-actions">
                <button type="submit" class="btn-submit">
                    <i class="fas fa-plus-circle"></i> Add to Blacklist
                </button>
                <a href="list_blackbrokers.php" class="btn-cancel">
                    <i class="fas fa-arrow-left"></i> Back to List
                </a>
            </div>

        </form>
    </div>
</main>

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