<?php
ob_start();
session_start();
include_once("../config.php");
include_once("../functions.php");
if (!check_login()) {
  header("Location: " . WEB_URL . "logout.php");
  die();
}

include_once(ROOT_PATH.'partial/report_top_common.php');
include_once(ROOT_PATH.'language/'.$lang_code_global.'/lang_tenant_info.php');
include_once(ROOT_PATH.'language/'.$lang_code_global.'/lang_common.php');
include_once(ROOT_PATH.'library/helper.php');
$ams_helper = new ams_helper();

$get_data = get_input_serilization();

// tiny html-escape helper
function e($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title><?= e($building_name); ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<?php include_once(ROOT_PATH.'/partial/header_script.php'); ?>

<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;800&display=swap" rel="stylesheet" />
<style>
  :root{
    /* Calm, readable palette */
    --bg-page:#f7f7f9;      /* very soft gray */
    --card:#ffffff;
    --ink:#111827;          /* gray-900 */
    --ink2:#374151;         /* gray-700 */
    --muted:#6b7280;        /* gray-500 */
    --line:#e5e7eb;         /* gray-200 */
    --accent:#475569;       /* slate-600 */
    --accent-soft:#eef2f7;  /* very soft slate */
    --shadow:0 10px 28px rgba(2,6,23,.08);
  }

  html, body, .content, .box, .table {
    font-family:'Inter', system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, 'Apple Color Emoji', 'Segoe UI Emoji';
    color:var(--ink2);
    background:var(--bg-page);
  }

  /* Page frame */
  .report-wrap{
    max-width: 1200px;
    margin: 20px auto 64px;
    padding: 0 16px;
  }

  /* Card */
  .card {
    background:var(--card);
    border:1px solid var(--line);
    border-radius:16px;
    box-shadow:var(--shadow);
    overflow:hidden;
  }
  .card-header {
    display:flex; align-items:center; justify-content:space-between; gap:12px;
    padding:16px 18px;
    border-bottom:1px solid var(--line);
    background:linear-gradient(180deg, var(--accent-soft), #fff);
  }
  .card-title { margin:0; font-weight:800; color:var(--ink); letter-spacing:.2px; }
  .card-sub { color:var(--muted); font-size:12px; }

  .card-body { padding:16px 18px; }

  /* Title row (top logo/title area already included via partial) */
  .under-title {
    text-decoration:underline; font-weight:800; color:var(--ink);
    margin:0; padding:8px 0 0 0;
  }

  /* Table */
  .table { width:100%; border-collapse:separate; border-spacing:0; }
  .table thead th{
    background:#fafafa;
    color:var(--ink);
    font-weight:800;
    border-bottom:1px solid var(--line) !important;
    padding:10px 12px;
    position:sticky; top:0; z-index:1; /* sticky header for long tables */
  }
  .table tbody td{
    padding:10px 12px;
    border-top:1px solid var(--line);
    vertical-align:top;
  }
  .table tbody tr:nth-child(even){ background:#fcfcfd; }
  .table tbody tr:hover{ background:#f9fafb; }

  /* Inner units table (borderless look) */
  .table-borderless { width:100%; border-collapse:collapse; }
  .table-borderless td{ border:0; padding:4px 0; }

  /* Print button (floating) */
  .print-fab {
    position:fixed; top:14px; right:14px;
    border-radius:999px;
    background:#fff;
    color:var(--ink2);
    border:1px solid var(--line);
    padding:10px 14px;
    box-shadow:0 12px 28px rgba(2,6,23,.12);
    display:inline-flex; align-items:center; gap:8px;
    font-weight:700;
    text-decoration:none;
  }
  .print-fab:hover{ background:#f9fafb; text-decoration:none; }
  .print-fab i{ opacity:.85; }

  /* Info bar below header */
  .info-bar{
    display:flex; gap:10px; align-items:center; justify-content:flex-start;
    font-size:12px; color:var(--muted);
  }
  .chip {
    display:inline-flex; align-items:center; gap:6px;
    padding:4px 10px; border-radius:999px; border:1px solid var(--line);
    background:#fff; color:var(--ink2); font-weight:700; font-size:12px;
  }

  /* Print tweaks */
  @media print {
    body, .content, .report-wrap { background:#fff; }
    .card, .box, .card-header { border:0; box-shadow:none; }
    .print-fab { display:none !important; }
    .card-body { padding:0; }
    .table thead th { position:static; }
    .under-title { margin-top:8px; }
  }
</style>

<script>
function printContent(area, title){
  // printThis config kept (logic preserved)
  $("#"+area).printThis({ pageTitle: title });
}
</script>

<!-- IE8 fallback -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>

<body>
  <!-- Floating Print Button -->
  <a class="print-fab btn_save" title="<?= e($_data['text_16']); ?>"
     href="javascript:void(0);"
     onclick="printContent('printable','Visitors Report');">
    <i class="fa fa-print"></i> <span><?= e($_data['text_16']); ?></span>
  </a>

  <section class="content">
    <div class="report-wrap" id="printable">
      <input type="hidden" id="web_url" value="<?= WEB_URL; ?>" />

      <div class="row">
        <div class="col-xs-12">

          <?php include_once(ROOT_PATH.'partial/report_top_title.php'); ?>

          <div class="card">
            <div class="card-header">
              <div>
                <h3 class="card-title under-title"><?= e($_data['text_1']); ?></h3>
                <div class="info-bar" style="margin-top:6px;">
                  <span class="chip"><i class="fa fa-building-o"></i> <?= e($building_name); ?></span>
                  <span class="chip"><i class="fa fa-calendar-o"></i> <?= e(date('j M Y, H:i')); ?></span>
                  <?php
                    $statusLabel = 'All';
                    if (isset($get_data['status'])) {
                      if ($get_data['status'] === 'active')   $statusLabel = 'Active';
                      elseif ($get_data['status'] === 'inactive') $statusLabel = 'Archived';
                    }
                  ?>
                  <span class="chip"><i class="fa fa-filter"></i> Status: <?= e($statusLabel); ?></span>
                </div>
              </div>
              <div class="card-sub">Well-formatted, print-friendly tenant summary</div>
            </div>

            <div class="card-body">
              <div style="overflow:auto;">
                <table class="table sakotable table-bordered table-striped dt-responsive" style="font-size:13px;">
                  <thead>
                    <tr>
                      <th><?= e($_data['text_5']); ?></th>
                      <th><?= e($_data['text_6']); ?></th>
                      <th><?= e($_data['text_7']); ?></th>
                      <th><?= e($_data['text_8']); ?></th>
                      <th>Business Name</th>
                      <th>Business Phone</th>
                      <th>Assigned Unit/Shop</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
                      // === Query logic unchanged ===
                      switch ($get_data["status"] ?? '') {
                        case "active":
                          $query = "SELECT * FROM current_tenants_view";
                          break;
                        case "inactive":
                          $query = "SELECT * FROM tenants WHERE r_status = 2";
                          break;
                        default:
                          $query = "SELECT * FROM tenants ORDER BY r_name ASC";
                          break;
                      }

                      $result = mysqli_query($link, $query);
                      while ($row = mysqli_fetch_array($result)) {
                        $units = [];
                    ?>
                      <tr>
                        <td><?= e($row['r_name']); ?></td>
                        <td><?= e($row['r_email']); ?></td>
                        <td><?= e($row['r_contact']); ?></td>
                        <td><?= e($row['r_address']); ?></td>
                        <td><?= e($row['business_name']); ?></td>
                        <td><?= e($row['business_phone']); ?></td>
                        <td>
                          <table class="table-borderless">
                            <?php
                              $qUnits = $link->query("SELECT * FROM occupied_units_view WHERE tenant_id = {$row['tid']}");
                              while ($unit = $qUnits->fetch_assoc()){
                                $units[] = ["uid" => $unit["uid"], "tid" => $row["tid"]];
                                echo '<tr>'.
                                       '<td>'.e($unit['b_name']).'</td>'.
                                       '<td>'.e($unit['unit_no']).'</td>'.
                                       '<td>$'.number_format((float)$unit['rate'], 2).'</td>'.
                                     '</tr>';
                              }
                            ?>
                          </table>
                        </td>
                      </tr>
                    <?php } mysqli_close($link); ?>
                  </tbody>
                </table>
              </div><!-- /overflow -->
            </div><!-- /card-body -->
          </div><!-- /card -->

        </div>
      </div>
    </div>
  </section>
</body>
</html>
