<?php 
include_once('../header.php');
include_once(ROOT_PATH.'language/'.$lang_code_global.'/lang_add_floor.php');

if(!check_login()){
  header("Location: " . WEB_URL . "logout.php");
  die();
}

/* -------------------------------
   Defaults
-------------------------------- */
$success      = "none";
$b_name       = '';
$title        = $_data['add_new_floor_top_title'];
$button_text  = $_data['save_button_text'];
$form_url     = WEB_URL . "block/addblock.php";
$hdnid        = "0";
$readOnly     = (isset($_GET['mode']) && $_GET['mode'] === 'view');

/* -------------------------------
   Create / Update
-------------------------------- */
if (isset($_POST['txtFloor'])) {
  if (isset($_POST['hdn']) && $_POST['hdn'] === '0') {
    $sql = "INSERT INTO `blocks`(b_name) VALUES ('{$_POST['txtFloor']}')";
    mysqli_query($link, $sql);
    mysqli_close($link);
    header("Location: " . WEB_URL . 'block/blocklist.php?m=add');
    exit;
  } else {
    $id  = isset($_GET['id']) ? (int)$_GET['id'] : 0;
    $sql = "UPDATE `blocks` SET `b_name`='{$_POST['txtFloor']}' WHERE bid='{$id}'";
    mysqli_query($link, $sql);
    header("Location: " . WEB_URL . 'block/blocklist.php?m=up');
    exit;
  }
}

/* -------------------------------
   Edit / View mode load
-------------------------------- */
if (isset($_GET['id']) && $_GET['id'] !== '') {
  $id     = (int)$_GET['id'];
  $result = mysqli_query($link, "SELECT * FROM blocks WHERE bid = '{$id}' LIMIT 1");
  if ($row = mysqli_fetch_array($result)) {
    $b_name      = $row['b_name'];
    $hdnid       = $_GET['id'];
    $title       = $_data['update_floor_top_title'];
    $button_text = $_data['update_button_text'];
    $form_url    = WEB_URL . "block/addblock.php?id=" . $_GET['id'];
  }
}
?>
<!-- Page Header -->
<section class="content-header">
  <h1 style="font-weight:800;margin-bottom:8px;">
    <i class="fa fa-building"></i> <?= htmlspecialchars($title); ?>
  </h1>
  <ol class="breadcrumb">
    <li><a href="<?= WEB_URL?>dashboard.php"><i class="fa fa-dashboard"></i> <?= $_data['home_breadcam']; ?></a></li>
    <li class="active"><?= $_data['add_new_floor_information_breadcam']; ?></li>
    <li class="active"><?= htmlspecialchars($title); ?></li>
  </ol>
</section>

<!-- Soft, readable styling -->
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;800&display=swap" rel="stylesheet">
<style>
  :root{
    --ink-1:#0f172a; --ink-2:#334155; --muted:#64748b;
    --soft:#f8fafc; --line:#e2e8f0; --brand:#2563eb;
    --ok:#16a34a; --warn:#d97706; --danger:#dc2626;
  }
  .content-wrapper, .box, .table, .btn, label, input, .help-text{
    font-family:'Inter', system-ui, Segoe UI, Roboto, Helvetica, Arial;
    color:var(--ink-2);
  }
  .box.box-success{
    border:0; border-radius:16px; overflow:hidden;
    box-shadow:0 14px 36px rgba(2,6,23,.12);
  }
  .box.box-success .box-header{
    background: linear-gradient(135deg, #eaf2ff, #f8fafc);
    border-bottom:1px solid var(--line);
  }
  .box .box-title{ font-weight:800; color:var(--ink-1); }
  .form-control{
    height:44px; border-radius:12px; border:1px solid var(--line);
    box-shadow:none !important;
  }
  .input-group-addon{
    background:#fff; border:1px solid var(--line);
    border-right:0; border-radius:12px 0 0 12px; color:#64748b;
  }
  .with-icon .form-control{ border-left:0; border-radius:0 12px 12px 0; }
  .help-text{ font-size:12px; color:var(--muted); margin-top:6px; }
  .btn{ border-radius:10px; }
  .btn-success{ background:#16a34a; border-color:#16a34a; }
  .btn-success:hover{ background:#15803d; border-color:#15803d; }
  .btn-warning{ background:#f59e0b; border-color:#f59e0b; }
  .btn-warning:hover{ background:#d97706; border-color:#d97706; }
  .errorStar{ color:var(--danger); }
</style>

<!-- Main content -->
<section class="content">
  <div class="row">
    <div class="col-md-12">

      <div class="box box-success">
        <div class="box-header">
          <h3 class="box-title">
            <i class="fa fa-plus-square-o"></i> <?= $_data['add_new_floor_entry_text']; ?>
          </h3>
        </div>

        <form onsubmit="return validateMe();" action="<?= $form_url; ?>" method="post" enctype="multipart/form-data">
          <div class="box-body" style="padding:18px;">
            <div class="form-group">
              <label for="txtFloor"><span class="errorStar">*</span> Block Name</label>
              <div class="input-group with-icon">
                <span class="input-group-addon"><i class="fa fa-building-o"></i></span>
                <input
                  type="text"
                  name="txtFloor"
                  id="txtFloor"
                  class="form-control"
                  placeholder="e.g., Block A, Tower 3, West Wing"
                  value="<?= htmlspecialchars($b_name); ?>"
                  <?= $readOnly ? 'readonly' : '' ?>
                />
              </div>
              <div class="help-text">Use a short, clear name. Examples: “Block A”, “Tower 3”, “Wing B”.</div>
            </div>
          </div>

          <div class="box-footer" style="padding:16px 18px;">
            <div class="form-group pull-right">
              <?php if (!$readOnly): ?>
                <button type="submit" name="submit" class="btn btn-success">
                  <i class="fa fa-floppy-o"></i> <?= htmlspecialchars($button_text); ?>
                </button>
              <?php endif; ?>
              <a class="btn btn-warning" href="<?= WEB_URL; ?>block/blocklist.php">
                <i class="fa fa-reply"></i> <?= $_data['back_text']; ?>
              </a>
            </div>
          </div>

          <input type="hidden" value="<?= htmlspecialchars($hdnid); ?>" name="hdn"/>
        </form>
      </div>

    </div>
  </div>
</section>

<script type="text/javascript">
  function validateMe(){
    var el = document.getElementById('txtFloor');
    if(!el || el.hasAttribute('readonly')) return true;

    var v = (el.value || '').trim().replace(/\s+/g,' ');
    if(v.length < 2){
      alert("<?= $_data['floor_required']; ?>");
      el.focus();
      return false;
    }
    el.value = v;
    return true;
  }

  // Autofocus nicely
  (function(){
    var el = document.getElementById('txtFloor');
    if(el && !el.hasAttribute('readonly')) el.focus();
  })();
</script>

<?php include_once('../footer.php'); ?>
