<br><br>
<?php 
include('header.php'); 
include('connect.php'); 
?>

<main id="main" class="mt-5">
  <div class="container mt-4">
      <h1 class="text-center fw-bold mb-3" style="word-wrap: break-word; font-size: 2rem;">ข่าวประชาสัมพันธ์อาจาร์ย</h1> <!-- เพิ่ม word-wrap และปรับขนาดฟอนต์ -->
      <p class="text-center text-muted">
         
      </p>

      <!-- ปุ่มเพิ่มข่าว -->
      <div class="text-end mb-3">
          <a href="?page=appnews" class="btn btn-success">+ เพิ่มข่าว</a>
      </div>

      <!-- แสดงข่าวจากฐานข้อมูล -->
      <div class="table-responsive">
          <table class="table table-bordered table-striped">
              <thead class="table-dark">
                  <tr>
                      <th>หัวข้อข่าว</th>
                      <th>เนื้อหา</th>
                      <th>วันที่</th>
                      <th>จัดการ</th> <!-- เพิ่มคอลัมน์สำหรับปุ่มแก้ไขและลบ -->
                  </tr>
              </thead>
              <tbody>
                  <?php
                  $sql = "SELECT * FROM news ORDER BY time_news DESC";
                  $result = $con->query($sql);
                  if ($result && $result->num_rows > 0) {
                      while($row = $result->fetch_assoc()){
                          echo "<tr>";
                          echo "<td>".htmlspecialchars($row['headline'])."</td>";
                          echo "<td>".nl2br(htmlspecialchars($row['content']))."</td>";
                          echo "<td>".$row['time_news']."</td>";
                          echo "<td>
                                  <a href='?page=edit_news&id=".$row['id']."' class='btn btn-warning btn-sm'>แก้ไข</a>
                                  <a href='?page=delete_news&id=".$row['id']."' class='btn btn-danger btn-sm' onclick='return confirm(\"คุณแน่ใจไหมที่จะลบข่าวนี้?\")'>ลบ</a>
                                </td>";  // เพิ่มปุ่มแก้ไขและลบ
                          echo "</tr>";
                      }
                  } else {
                      echo "<tr><td colspan='4' class='text-center text-muted'>ยังไม่มีข่าว</td></tr>";
                  }
                  ?>
              </tbody>
          </table>
      </div>
  </div>
</main>

<?php include('footer.php'); ?>
