<?php 
include_once('../header.php');
if(!check_login()){
  header("Location: " . WEB_URL . "logout.php");
  die();
}
?>
<?php
include_once(ROOT_PATH.'language/'.$lang_code_global.'/lang_floor_list.php');

$delinfo = 'none';
$addinfo = 'none';
$msg = "";

/* Delete */
if (isset($_GET['id']) && $_GET['id'] != '' && (int)$_GET['id'] > 0){
  $id = (int)$_GET['id'];
  $sqlx = "DELETE FROM `blocks` WHERE bid = {$id}";
  mysqli_query($link,$sqlx); 
  $delinfo = 'block';
}

/* Flash messages */
if(isset($_GET['m']) && $_GET['m'] == 'add'){
  $addinfo = 'block';
  $msg = $_data['add_msg'];
}
if(isset($_GET['m']) && $_GET['m'] == 'up'){
  $addinfo = 'block';
  $msg = $_data['update_msg'];
}
?>

<!-- Page Header -->
<section class="content-header">
  <h1 style="font-weight:800;margin-bottom:8px;">
    <i class="fa fa-building"></i> <?= $_data['floor_list_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"><?= $_data['floor_list_title']; ?></li>
  </ol>
</section>

<!-- Friendly 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; --soft-2:#eef2ff; --line:#e2e8f0;
    --brand:#2563eb; --ok:#16a34a; --danger:#dc2626; --warn:#d97706;
  }
  .content-wrapper, .box, .table, .btn, .alert, label {
    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,.10);
  }
  .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); }

  .toolbar{
    display:flex; gap:8px; justify-content:flex-end; margin-bottom:10px;
  }
  .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; }
  .btn-danger{ background:#dc2626; border-color:#dc2626; }
  .btn-danger:hover{ background:#b91c1c; border-color:#b91c1c; }

  .table thead th{
    background:#f1f5f9; border-bottom:1px solid var(--line); color:var(--ink-1);
    font-weight:700;
  }
  .table tbody tr:hover{ background:#f8fafc; }
  .alert{ border-radius:12px; }
</style>

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

      <!-- Alerts -->
      <div id="me" class="alert alert-danger alert-dismissable" style="display:<?= $delinfo; ?>">
        <button aria-hidden="true" data-dismiss="alert" class="close" type="button"><i class="fa fa-close"></i></button>
        <h4 style="margin-top:0;"><i class="icon fa fa-ban"></i> <?= $_data['delete_text'];?>!</h4>
        <?= $_data['delete_floor_information']; ?>
      </div>

      <div id="you" class="alert alert-success alert-dismissable" style="display:<?= $addinfo; ?>">
        <button aria-hidden="true" data-dismiss="alert" class="close" type="button"><i class="fa fa-close"></i></button>
        <h4 style="margin-top:0;"><i class="icon fa fa-check"></i> <?= $_data['success'];?>!</h4>
        <?= htmlspecialchars($msg); ?>
      </div>

      <!-- Toolbar -->
      <div class="toolbar">
        <a class="btn btn-success" data-toggle="tooltip" href="<?= WEB_URL; ?>block/addblock.php" title="<?= $_data['add_floor']; ?>">
          <i class="fa fa-plus"></i> <?= $_data['add_floor']; ?>
        </a>
        <a class="btn btn-warning" data-toggle="tooltip" href="<?= WEB_URL; ?>dashboard.php" title="<?= $_data['home_breadcam']; ?>">
          <i class="fa fa-dashboard"></i> <?= $_data['home_breadcam']; ?>
        </a>
      </div>

      <!-- List card -->
      <div class="box box-success">
        <div class="box-header">
          <h3 class="box-title"><i class="fa fa-list-ul"></i> <?= $_data['floor_list_title']; ?></h3>
        </div>
        <div class="box-body">
          <table class="table sakotable table-bordered table-striped dt-responsive" id="floorTable">
            <thead>
              <tr>
                <th style="width:70%;"><?= $_data['b_name']; ?></th>
                <th style="width:30%; text-align:center;"><?= $_data['action_text']; ?></th>
              </tr>
            </thead>
            <tbody>
              <?php
                $result = mysqli_query($link,"SELECT * FROM blocks ORDER BY b_name");
                while($row = mysqli_fetch_array($result)){
                  $bid   = (int)$row['bid'];
                  $bname = htmlspecialchars($row['b_name']);
              ?>
                <tr>
                  <td><?= $bname; ?></td>
                  <td style="text-align:center; white-space:nowrap;">
                    <a class="btn btn-warning ams_btn_special" data-toggle="tooltip"
                       href="<?= WEB_URL;?>block/addblock.php?id=<?= $bid; ?>"
                       title="<?= $_data['edit_text']; ?>">
                      <i class="fa fa-pencil"></i>
                    </a>
                    <a class="btn btn-danger ams_btn_special" data-toggle="tooltip"
                       href="javascript:;" onclick="deleteFloor(<?= $bid; ?>);"
                       title="<?= $_data['delete_text']; ?>">
                      <i class="fa fa-trash-o"></i>
                    </a>
                  </td>
                </tr>
              <?php } mysqli_close($link); $link = NULL; ?>
            </tbody>
          </table>
        </div>
      </div>

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

<script type="text/javascript">
  function deleteFloor(Id){
    const proceed = function(){
      window.location = '<?= WEB_URL; ?>block/blocklist.php?id=' + Id;
    };
    // Uses your global sweetConfirmDelete helper
    if (typeof sweetConfirmDelete === 'function') {
      sweetConfirmDelete(proceed);
    } else {
      if(confirm("<?= $_data['delete_text']; ?>?")) proceed();
    }
  }

  // Auto-hide flash alerts
  (function(){
    setTimeout(function(){
      var a = document.getElementById('me'); if(a) a.style.display = 'none';
      var b = document.getElementById('you'); if(b) b.style.display = 'none';
    }, 3200);
  })();
</script>

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