<?php
include '../../connection.php';
session_start();

if (!isset($_GET['page'])) {
    die('Missing report ID.');
}

$report_id = intval($_GET['page']);

// Fetch report details
$query = "
SELECT 
    r.report_id,
    r.notes,
    r.date_created AS report_date,
    t.UniqueID,
    t.description,
    t.status,
    t.date_created AS complaint_date,
    t.incident_address,
    u.first_name AS kag_fname,
    u.middle_name AS kag_mname,
    u.last_name AS kag_lname,
    res.first_name AS res_fname,
    res.middle_name AS res_mname,
    res.last_name AS res_lname,
    c.complain_name
FROM tbl_reports r
LEFT JOIN tbl_transaction t ON r.UniqueID = t.UniqueID
LEFT JOIN tbl_user u ON r.user_id = u.user_id
LEFT JOIN tbl_resident res ON t.resident_id = res.resident_id
LEFT JOIN tbl_complaint_types c ON t.complaint_type_id = c.complaint_type_id
WHERE r.report_id = '$report_id'
";

$result = $conn->query($query);
if ($result->num_rows === 0) {
    die('Report not found.');
}

$row = $result->fetch_assoc();

// Prepare dynamic variables
$kagawad_name = trim($row['kag_fname'] . ' ' . $row['kag_mname'] . ' ' . $row['kag_lname']);
$resident_name = trim($row['res_fname'] . ' ' . $row['res_mname'] . ' ' . $row['res_lname']);
$complaint_type = ucfirst($row['complain_name']);
$status = ucfirst($row['status']);
$complaint_date = date('F d, Y', strtotime($row['complaint_date']));
$report_date = date('F d, Y', strtotime($row['report_date']));
$notes = !empty($row['notes']) ? nl2br(htmlspecialchars($row['notes'])) : 'No additional remarks recorded.';
?>

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" integrity="sha384-xOolHFLEh07PJGoPkLv1IbcEPTNtaed2xpHsD9ESMhqIYd0nLMwNLD69Npy4HI+N" crossorigin="anonymous">

    <title>Official Report Letter</title>
  </head>
  <style>
    body{
      font-family: 'Arial','Helvetica', sans-serif;
      font-size: 20px;
    }
    @media print {
      .no-print { display: none; }
    }
  </style>
  <body>
    
    <!-- Print Button -->
    <div class="text-center mt-4 no-print">
      <button class="btn btn-primary" onclick="window.print();">
        <i class="fa fa-print"></i> Print Report
      </button>
    </div>
    <div class="container">
    <div class="row">
      <div class="col-4" align="end">
        <img src="../../images/logo.jfif" style="width: 150px; height: 150px;">
      </div>
      <div class="col-4 text-center">
        <br>
         <p>Republic of the Philippines <br> 
        Province of Sultan Kudarat <br>
        Municipality of President Quirino <br>
        <span style="font-weight: bold;">BARANGAY TUAL</span> 
       </p> 
      </div>
      <div class="col-4">
      </div>
    </div>

    <div class="row">
      <div class="col-lg-12">
        <p style="font-weight: bold; text-align: center;">OFFICE OF THE LUPONG TAGAPAMAYAPA</p>
        <hr style="height: 2px; background-color: #000; border: none;">
      </div>
    </div>

    <div class="row">
      <div class="col-lg-12">
      <p style="font-weight: bold;">
        Republic of the Philippines <br>
      Province of Sultan Kudarat <br>
      Municipality of President Quirino <br>
      Barangay Tual
      </p>
      </div>
    </div>

    <div class="row">
      <div class="col-lg-12">
        <p style="font-weight: bold;">OFFICIAL REPORT LETTER</p>
        <p>Date: <?= $report_date ?></p>
      </div>

      <div class="col-lg-12">
        <p>
          To: <strong>Hon. Elmer Janolino</strong><br>
        Punong Barangay <br>
        Barangay Tual
        </p>

        <p>
          Subject: <span style="font-weight: bold;">Report on the Status of Complaint Resolution</span> 
        </p>

        <p style="font-weight: bold;">
          Sir/Madam,
        </p>

        <br>
        <p>
          This is to formally report the status of the complaint filed on <strong><?= $complaint_date ?></strong> concerning <strong>"<?= htmlspecialchars($complaint_type) ?>"</strong>. 
          The matter was attended to by <strong><?= htmlspecialchars($kagawad_name) ?></strong>. 
          After the conducted mediation/settlement, the issue has been <strong><?= htmlspecialchars($status) ?></strong>.
          <br><br>
          <strong>Remarks / Summary:</strong><br>
          <?= $notes ?>
        </p>

        <p>
          Respectfully submitted,
        </p>

        <br>
        <p>
          <strong><?= htmlspecialchars($kagawad_name) ?></strong><br>                                                                        
        Position: Barangay Kagawad  <br>     
        Barangay Tual, President Quirino
        </p>

        <br>
        <p>Noted by:</p>
        <br>
      <span style="font-weight:bold;">Hon. Elmer Janolino</span> <br> 
      Punong Barangay
      </div>
    </div>
    </div>


    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
  </body>
</html>
