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

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

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $rank_num = $_POST['rank_num'];
    $name = $_POST['name'];
    $overall_rating = $_POST['overall_rating'];
    $account_type = $_POST['account_type'];
    $execution_speed = $_POST['execution_speed'];
    $execution_badge = $_POST['execution_badge'] ?: NULL;
    $slippage = $_POST['slippage'];
    $trading_cost = $_POST['trading_cost'];
    $trading_cost_badge = $_POST['trading_cost_badge'] ?: NULL;
    $swap = $_POST['swap'];
    $swap_badge = $_POST['swap_badge'] ?: NULL;
    $disconnection_rating = $_POST['disconnection_rating'];
    $disconnection_badge = $_POST['disconnection_badge'] ?: NULL;

    $logo_url = '';
    if (isset($_FILES['logo_image']) && $_FILES['logo_image']['error'] === UPLOAD_ERR_OK && !empty($_FILES['logo_image']['name'])) {
        $uploadDir = '../uploads/';
        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0777, true);
        }

        $fileName = time() . '_' . basename($_FILES['logo_image']['name']);
        $targetPath = $uploadDir . $fileName;
        if (move_uploaded_file($_FILES['logo_image']['tmp_name'], $targetPath)) {
            $logo_url = $targetPath;
        }
    }

    $sql = "INSERT INTO trader_safety.trading_conditions (rank_num, name, logo_url, overall_rating, account_type, execution_speed, execution_badge, slippage, trading_cost, trading_cost_badge, swap, swap_badge, disconnection_rating, disconnection_badge) 
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    $stmt = $dbh->prepare($sql);
    $stmt->execute([$rank_num, $name, $logo_url, $overall_rating, $account_type, $execution_speed, $execution_badge, $slippage, $trading_cost, $trading_cost_badge, $swap, $swap_badge, $disconnection_rating, $disconnection_badge]);

    header("Location: view_trad_env.php");
    exit;
}

include "includes/header.php";
include "includes/sidebar.php";
?>

<style>
  .dash { max-width: 1000px; margin: 0 auto; padding: 30px 26px; z-index: 1; position: relative; }
  .panel { background: var(--dark-700); border: 1px solid rgba(255,255,255,0.05); border-radius: 14px; padding: 25px; }
  .form-group { margin-bottom: 18px; }
  .form-label { display: block; font-size: 0.85rem; color: var(--text-2); margin-bottom: 8px; font-weight: 600; }
    .form-control { width: 100%; padding: 12px 15px; 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; }
  .form-control:focus { outline: none; border-color: var(--yellow); box-shadow: 0 0 0 3px var(--yellow-glass); }
    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: var(--yellow-glass); color: var(--yellow); border-color: var(--yellow); }
  select.form-control { appearance: none; background-image: url('data:image/svg+xml;charset=US-ASCII,<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="%238a90a6" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>'); background-repeat: no-repeat; background-position: right 15px center; background-size: 14px; }
  .btn-submit { background: var(--yellow); color: var(--dark-900); padding: 12px 24px; border: none; border-radius: 8px; font-weight: 700; font-size: 1rem; cursor: pointer; width: 100%; margin-top: 10px; }
  .btn-submit:hover { background: var(--yellow-bright); }
  .grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
</style>

<main class="dash">
  <div class="panel">
    <h2 style="color: var(--text-1); margin-bottom: 25px; font-family: 'Syne', sans-serif;"><i class="fas fa-plus-circle" style="color: var(--yellow);"></i> Add Trading Condition</h2>
    
        <form method="POST" action="" enctype="multipart/form-data">
      <div class="grid-2">
        <div class="form-group">
            <label class="form-label">Rank Number</label>
            <input type="number" name="rank_num" class="form-control" required placeholder="e.g. 1">
        </div>
        <div class="form-group">
            <label class="form-label">Broker Name</label>
            <input type="text" name="name" class="form-control" required placeholder="e.g. FOREX.com">
        </div>
      </div>

      <div class="grid-2">
        <div class="form-group">
            <label class="form-label">Logo Image</label>
            <input type="file" name="logo_image" class="form-control" accept="image/*" required>
        </div>
        <div class="form-group">
            <label class="form-label">Account Type</label>
            <select name="account_type" class="form-control" required>
                <option value="Standard">Standard</option>
                <option value="ECN">ECN</option>
            </select>
        </div>
      </div>

      <div class="form-group">
          <label class="form-label">Overall Rating</label>
          <input type="text" name="overall_rating" class="form-control" required placeholder="e.g. AAA">
      </div>

      <hr style="border-color: rgba(255,255,255,0.05); margin: 25px 0;">

      <div class="grid-2">
        <div class="form-group">
            <label class="form-label">Execution Speed</label>
            <input type="text" name="execution_speed" class="form-control" placeholder="e.g. 122.4 ms">
        </div>
        <div class="form-group">
            <label class="form-label">Execution Badge</label>
            <select name="execution_badge" class="form-control">
                <option value="">None</option>
                <option value="Perfect">Perfect</option>
                <option value="Poor">Poor</option>
                <option value="Good">Good</option>
            </select>
        </div>
      </div>

      <div class="grid-2">
        <div class="form-group">
            <label class="form-label">Trading Cost</label>
            <input type="text" name="trading_cost" class="form-control" placeholder="e.g. $ 2.12/Lot">
        </div>
        <div class="form-group">
            <label class="form-label">Trading Cost Badge</label>
            <select name="trading_cost_badge" class="form-control">
                <option value="">None</option>
                <option value="Perfect">Perfect</option>
                <option value="Poor">Poor</option>
                <option value="Good">Good</option>
            </select>
        </div>
      </div>

      <div class="grid-2">
        <div class="form-group">
            <label class="form-label">Swap</label>
            <input type="text" name="swap" class="form-control" placeholder="e.g. $ -0.05/Lot">
        </div>
        <div class="form-group">
            <label class="form-label">Swap Badge</label>
            <select name="swap_badge" class="form-control">
                <option value="">None</option>
                <option value="Perfect">Perfect</option>
                <option value="Poor">Poor</option>
                <option value="Good">Good</option>
            </select>
        </div>
      </div>
      
      <div class="grid-2">
          <div class="form-group">
              <label class="form-label">Slippage</label>
              <input type="text" name="slippage" class="form-control" placeholder="e.g. -0.4">
          </div>
          <div class="form-group">
              <label class="form-label">Disconnection Rating</label>
              <input type="text" name="disconnection_rating" class="form-control" placeholder="e.g. AAA">
          </div>
      </div>

      <div class="form-group">
          <label class="form-label">Disconnection Badge</label>
          <select name="disconnection_badge" class="form-control">
              <option value="">None</option>
              <option value="Perfect">Perfect</option>
              <option value="Poor">Poor</option>
              <option value="Good">Good</option>
          </select>
      </div>

      <button type="submit" class="btn-submit">Save Record</button>
    </form>
  </div>
</main>

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