<?php
include('session.php');

if (isset($_GET['delete'])) {
    $id = (int)$_GET['delete'];
    $conn->query("UPDATE building_list SET isDeleted='1' WHERE bid='$id'");
    header("Location: building.php");
    exit;
}

if (isset($_GET['edit'])) {
    header("Location: addBuilding.php?edit=".(int)$_GET['edit']);
    exit;
}

$buildings = $conn->query("
    SELECT 
        b.*,
        d.name AS division_name,
        dep.name AS department_name,
        c.name AS colony_name
    FROM building_list b
    LEFT JOIN divisions d ON d.id = b.division_id
    LEFT JOIN departments dep ON dep.id = b.department_id
    LEFT JOIN colonies c ON c.id = b.colony_id
    WHERE b.isDeleted='0'
    ORDER BY b.bid DESC
");

if (isset($_GET['export']) && $_GET['export'] === 'excel') {
    header('Content-Type: text/csv; charset=UTF-8');
    header('Content-Disposition: attachment; filename="building-list-'.date('Y-m-d').'.csv"');
    echo "\xEF\xBB\xBF";

    $output = fopen('php://output', 'w');
    fputcsv($output, ['Sr No.', 'Division', 'Sub Division', 'Colony', 'Building Category', 'Building No', 'Login Pin']);

    $serial = 0;
    while ($row = $buildings->fetch_assoc()) {
        fputcsv($output, [
            ++$serial,
            $row['division_name'] ?? '',
            $row['department_name'] ?? '',
            $row['colony_name'] ?? '',
            $row['bcategory'] ?? '',
            $row['bNo'] ?? '',
            $row['mpin'] ?? ''
        ]);
    }
    fclose($output);
    exit;
}
?>
<!DOCTYPE html>
<html lang="en">
  <head>
	<title>ECIMS ADMIN | ALL BUILDINGS</title>
    <?php include('includes/head.php'); ?>
    <style>
        th,td {
            text-align:center;
        }
        .report-actions {
            display: flex;
            gap: 8px;
        }
        @media print {
            @page {
                size: landscape;
                margin: 8mm;
            }
            .horizontal-menu, .no-print, footer {
                display: none !important;
            }
            .content-wrapper, .card, .card-body {
                margin: 0 !important;
                padding: 0 !important;
                background: #fff !important;
                box-shadow: none !important;
                border: 0 !important;
            }
            .table-responsive {
                overflow: visible !important;
            }
            #dataTable {
                width: 100% !important;
                table-layout: fixed;
                font-size: 9px;
            }
            #dataTable th, #dataTable td {
                padding: 4px !important;
                white-space: normal !important;
                overflow-wrap: anywhere;
            }
        }
    </style>
  </head>
  <body>
    <div class="container-scroller">
      <div class="horizontal-menu">
        <?php include('includes/header.php'); ?>
      </div>
      <div class="container-fluid page-body-wrapper">
        <div class="main-panel">
          <div class="content-wrapper">
            <div class="page-header">
              <h3 class="page-title"> All Buildings </h3>
              <div class="report-actions no-print">
                <button type="button" class="btn btn-sm btn-primary" onclick="window.print()">Print</button>
                <a href="building.php?export=excel" class="btn btn-sm btn-success">Export Excel</a>
              </div>
            </div>
            <div class="card grid-margin">
              <div class="card-body">
                <div class="row">
                  <div class="col-12">
                    <div class="table-responsive">
                      <table id="dataTable" class="table table-striped table-bordered" width="100%">
                        <thead>
                          <tr>
                            <th>Sr No.</th>
                            <th>Division</th>
                            <th>Sub Division</th>
                            <th>Colony</th>
                            <th>Building Category</th>
                            <th>Building No</th>
                            <th>Login Pin</th>
                            <th class="no-print">Update</th>
                            <th class="no-print">Delete</th>
                          </tr>
                        </thead>
                        <tbody>
                            <?php $count=0;
                            if($buildings && $buildings->num_rows > 0) {
                            while($row = $buildings->fetch_assoc()) { $count++;
                            ?>
                              <tr>
                                <td><?php echo $count; ?></td>
                                <td><?php echo htmlspecialchars($row['division_name'] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($row['department_name'] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($row['colony_name'] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($row['bcategory'] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($row['bNo'] ?? ''); ?></td>
                                <td><?php echo htmlspecialchars($row['mpin'] ?? ''); ?></td>
                                <td class="no-print"><a href="addBuilding.php?edit=<?php echo $row['bid']; ?>" class="btn btn-xs btn-outline-primary">Update</a></td>
                                <td class="no-print"><a onClick="javascript: return confirm('Are you Sure?');" href="building.php?delete=<?php echo $row['bid']; ?>" class="btn btn-xs btn-outline-danger">Delete</a></td>
                              </tr>
                          <?php }} else {
                          echo '<tr><td colspan="9"><center>No Records Found!</center></td></tr>';
                          } ?>
                        </tbody>
                      </table>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
		  <?php include('includes/footer.php'); ?>
        </div>
      </div>
    </div>
    </div>
    <?php include('includes/foot.php'); ?>
  </body>
</html>
