<?php
header('Content-Type: text/html; charset=utf-8');
session_start();
if (!isset($_SESSION['unohs'])) {
    header("location:index.php?msg=unauthorized");
    exit;
}
date_default_timezone_set("Asia/Kolkata");
include 'conn.php';

mysqli_query($conn, "SET NAMES 'utf8mb4'");
mysqli_query($conn, "SET CHARACTER SET utf8mb4");
mysqli_query($conn, "SET SESSION collation_connection = 'utf8mb4_unicode_ci'");

// Add Entry
if (isset($_POST['add'])) {
    $lang_code = $_POST['lang_code'];
    $lang_name = $_POST['lang_name'];
    $flag_url = $_POST['flag_url'];
    $currency_code = $_POST['currency_code'];
    $currency_symbol = $_POST['currency_symbol'];
    $currency_name = $_POST['currency_name'];
    $is_default = isset($_POST['is_default']) ? 1 : 0;
    $status = isset($_POST['status']) ? 1 : 0;

    if ($is_default == 1) {
        mysqli_query($conn, "UPDATE langcurrency SET is_default = 0");
    }

    $sql = "INSERT INTO langcurrency 
            (lang_code, lang_name, flag_url, currency_code, currency_symbol, currency_name, is_default, status)
            VALUES 
            ('$lang_code', '$lang_name', '$flag_url', '$currency_code', '$currency_symbol', '$currency_name', $is_default, $status)";
    mysqli_query($conn, $sql);
    header("Location: manage_langcurrency.php");
    exit;
}

if (isset($_GET['set_default'])) {
    $id = $_GET['set_default'];
    mysqli_query($conn, "UPDATE langcurrency SET is_default = 0");
    mysqli_query($conn, "UPDATE langcurrency SET is_default = 1 WHERE id = $id");
    header("Location: manage_langcurrency.php");
    exit;
}

if (isset($_GET['toggle_status'])) {
    $id = $_GET['toggle_status'];
    mysqli_query($conn, "UPDATE langcurrency SET status = IF(status=1,0,1) WHERE id = $id");
    header("Location: manage_langcurrency.php");
    exit;
}

$result = mysqli_query($conn, "SELECT * FROM langcurrency ORDER BY id ASC");
?>

<?php include 'header.php'; ?>

<style>
  .toggle-small {
    position: relative;
    display: inline-block;
    width: 38px;
    height: 20px;
  }
  .toggle-small input {
    opacity: 0;
    width: 0;
    height: 0;
  }
  .slider-small {
    position: absolute;
    cursor: pointer;
    top: 0; left: 0; right: 0; bottom: 0;
    background-color: #ccc;
    transition: .4s;
    border-radius: 20px;
  }
  .slider-small:before {
    position: absolute;
    content: "";
    height: 14px;
    width: 14px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: .4s;
    border-radius: 50%;
  }
  input:checked + .slider-small {
    background-color: #6f42c1;
  }
  input:checked + .slider-small:before {
    transform: translateX(18px);
  }
</style>

<div class="container my-4">
  <h4 class="mb-4">🌍 Language & Currency Control</h4>

  <form method="POST" class="row g-3 mb-4 bg-white p-4 border rounded shadow-sm">
    <div class="col-md-2">
      <label class="form-label">Lang Code</label>
      <input type="text" name="lang_code" class="form-control" required>
    </div>
    <div class="col-md-2">
      <label class="form-label">Lang Name</label>
      <input type="text" name="lang_name" class="form-control" required>
    </div>
    <div class="col-md-2">
      <label class="form-label">Flag URL</label>
      <input type="text" name="flag_url" class="form-control" placeholder="/flags/en.png" required>
    </div>
    <div class="col-md-2">
      <label class="form-label">Currency Code</label>
      <input type="text" name="currency_code" class="form-control" placeholder="USD" required>
    </div>
    <div class="col-md-1">
      <label class="form-label">Symbol</label>
      <input type="text" name="currency_symbol" class="form-control" placeholder="$" required>
    </div>
    <div class="col-md-3">
      <label class="form-label">Currency Name</label>
      <input type="text" name="currency_name" class="form-control" placeholder="US Dollar" required>
    </div>

    <!-- Toggle switches -->
    <div class="col-12 d-flex align-items-center gap-4 mt-2">
      <label class="toggle-small">
        <input type="checkbox" name="is_default">
        <span class="slider-small"></span>
      </label>
      <span style="font-size: 14px;">Default</span>

      <label class="toggle-small">
        <input type="checkbox" name="status" checked>
        <span class="slider-small"></span>
      </label>
      <span style="font-size: 14px;">Active</span>
    </div>

    <div class="col-12 mt-3">
      <button type="submit" name="add" class="btn btn-primary w-100">Add Entry</button>
    </div>
  </form>

  <table class="table table-bordered bg-white shadow-sm">
    <thead class="table-light">
      <tr>
        <th>ID</th>
        <th>Flag</th>
        <th>Lang</th>
        <th>Code</th>
        <th>Currency</th>
        <th>Symbol</th>
        <th>Default</th>
        <th>Status</th>
        <th>Actions</th>
      </tr>
    </thead>
    <tbody>
      <?php while($row = mysqli_fetch_assoc($result)) { ?>
        <tr>
          <td><?= $row['id'] ?></td>
          <td><img src="<?= htmlspecialchars($row['flag_url']) ?>" width="30"></td>
          <td><?= htmlspecialchars($row['lang_name']) ?></td>
          <td><?= $row['lang_code'] ?></td>
          <td><?= htmlspecialchars($row['currency_name']) ?> (<?= $row['currency_code'] ?>)</td>
          <td><?= htmlspecialchars($row['currency_symbol'], ENT_QUOTES, 'UTF-8') ?></td>
          <td><?= $row['is_default'] ? '✅' : '' ?></td>
          <td>
            <label class="toggle-small">
              <input type="checkbox" onchange="location.href='?toggle_status=<?= $row['id'] ?>'" <?= $row['status'] ? 'checked' : '' ?>>
              <span class="slider-small"></span>
            </label>
          </td>
          <td>
            <a href="?set_default=<?= $row['id'] ?>" class="btn btn-sm btn-outline-success">Make Default</a>
          </td>
        </tr>
      <?php } ?>
    </tbody>
  </table>
</div>
