<?php 
include '../../connection.php';

$id = $_GET['page'];
$year = date("y");

$query = "
SELECT
    tbl_transaction.UniqueID,
    tbl_transaction.resident_id,
    tbl_transaction.description,
    tbl_transaction.status,
    tbl_transaction.date_created,
    tbl_transaction.supported_evidence,
    tbl_resident.first_name,
    tbl_resident.middle_name,
    tbl_resident.last_name,
    tbl_complaint_types.complain_name,
    tbl_assignment.assignment_id,
    tbl_assignment.assigned_person,
    tbl_assignment.department_id,
    tbl_assignment.status AS assigned_status,
    tbl_assignment.deadline,
    tbl_assignment.date_created AS assigned_date,
    tbl_user.first_name AS user_fname,
    tbl_user.middle_name AS user_mname,
    tbl_user.last_name AS user_lname,
    tbl_user.contact AS user_contact,
    tbl_department.department_name
FROM tbl_transaction
LEFT JOIN tbl_resident 
    ON tbl_transaction.resident_id = tbl_resident.resident_id
LEFT JOIN tbl_complaint_types 
    ON tbl_transaction.complaint_type_id = tbl_complaint_types.complaint_type_id
LEFT JOIN tbl_assignment 
    ON tbl_assignment.UniqueID = tbl_transaction.UniqueID
LEFT JOIN tbl_user 
    ON tbl_assignment.assigned_person = tbl_user.user_id
LEFT JOIN tbl_department 
    ON tbl_assignment.department_id = tbl_department.department_id
WHERE tbl_transaction.UniqueID = '$id'
";

$result = $conn->query($query);
$row = $result->fetch_assoc();
?>

<div class="container">
    <button class="btn btn-secondary mb-3 btn-md click_page" data-page="complaints.php">
        Back <i class="fa fa-arrow-left"></i>
    </button>

    <div class="row">
        <!-- Main Complaint Information -->
        <div class="col-lg-8">
            <!-- Basic Information -->
            <div class="card complaint-card mb-3">
                <div class="card-header">
                    <i class="fas fa-info-circle mr-2"></i>Basic Information
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="detail-item">
                                <h6><i class="fas fa-hashtag text-primary mr-2"></i>Case Number</h6>
                                <p><?= htmlspecialchars($row['UniqueID']) ?> - <?= $year ?></p>
                            </div>
                            <div class="detail-item">
                                <h6><i class="fas fa-calendar-alt text-primary mr-2"></i>Date Filed</h6>
                                <p><?= date('F d, Y', strtotime($row['date_created'])) ?></p>
                            </div>
                            <div class="detail-item">
                                <h6><i class="fas fa-gavel text-primary mr-2"></i>Nature of Case</h6>
                                <p><?= htmlspecialchars($row['complain_name']) ?></p>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="detail-item">
                                <h6><i class="fas fa-user text-primary mr-2"></i>Assigned Officer</h6>
                                <p><?= htmlspecialchars($row['user_fname'] . ' ' . $row['user_mname'] . ' ' . $row['user_lname']) ?></p>
                                <p><i class="fas fa-phone mr-2"></i><?= htmlspecialchars($row['user_contact']) ?></p>
                            </div>
                            <div class="detail-item">
                                <h6><i class="fas fa-building text-primary mr-2"></i>Department</h6>
                                <p><?= htmlspecialchars($row['department_name']) ?></p>
                            </div>
                            <div class="detail-item">
                                <h6><i class="fas fa-flag text-primary mr-2"></i>Status</h6>
                                <p>
                                    <span class="badge 
                                        <?php 
                                            if ($row['status'] == 'new') echo 'status-new';
                                            elseif ($row['status'] == 'in-progress') echo 'status-in-progress';
                                            elseif ($row['status'] == 'resolved') echo 'status-resolved';
                                        ?>">
                                        <?= ucfirst($row['status']) ?>
                                    </span>
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Case Details -->
            <div class="card complaint-card mb-3">
                <div class="card-header">
                    <i class="fas fa-clipboard-list mr-2"></i>Case Details
                </div>
                <div class="card-body">
                <?php
                    $file = $row['supported_evidence'];
                    $path = "../uploads/evidence/" . $file;

                    // get file extension
                    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));

                    // define image and video extensions
                    $image_ext = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
                    $video_ext = ['mp4', 'mov', 'avi', 'mkv', 'webm'];

                    if (in_array($ext, $image_ext)): ?>
                        <!-- Display Image -->
                        <div class="detail-item">
                            <h6>Supporting Evidence (Image)</h6>
                            <img src="<?= $path ?>" alt="Evidence Image" class="img-fluid rounded shadow-sm" style="max-height:300px; object-fit:contain;">
                        </div>

                    <?php elseif (in_array($ext, $video_ext)): ?>
                        <!-- Display Video -->
                        <div class="detail-item">
                            <p>Description: <?= $row['description'] ?></p>
                            <h6>Supporting Evidence (Video)</h6>
                            <video controls class="w-100 rounded shadow-sm" style="max-height:350px;">
                                <source src="<?= $path ?>" type="video/<?= $ext ?>">
                                Your browser does not support the video tag.
                            </video>
                        </div>

                    <?php elseif (!empty($file)): ?>
                        <!-- Other File Types (PDF, DOCX, etc.) -->
                        <div class="detail-item">
                            <h6>Supporting Evidence</h6>
                            <a href="<?= $path ?>" target="_blank" class="btn btn-outline-primary btn-sm">
                                View Evidence
                            </a>
                        </div>

                    <?php endif; ?>

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

        <!-- Sidebar -->
        <div class="col-lg-4">
            <div class="card complaint-card">
                <div class="card-header">
                    <i class="fas fa-history mr-2"></i>Case Timeline
                </div>
                <div class="card-body">
                    <div class="timeline">
                        <div class="timeline-item">
                            <h6>Complaint Filed</h6>
                            <p class="text-muted small"><?= date('F d, Y', strtotime($row['date_created'])) ?></p>
                            <p>Complaint submitted by <?= htmlspecialchars($row['first_name'] . ' ' . $row['last_name']) ?></p>
                        </div>

                        <?php if (!empty($row['assigned_date'])): ?>
                        <div class="timeline-item">
                            <h6>Case Assigned</h6>
                            <p class="text-muted small"><?= date('F d, Y', strtotime($row['assigned_date'])) ?></p>
                            <p>Assigned to <?= htmlspecialchars($row['user_fname'] . ' ' . $row['user_lname']) ?></p>
                        </div>
                        <?php endif; ?>

                        <?php if (!empty($row['deadline'])): ?>
                        <div class="timeline-item">
                            <h6>Deadline</h6>
                            <p class="text-muted small"><?= date('F d, Y', strtotime($row['deadline'])) ?></p>
                            <p>Expected resolution date.</p>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<!-- Print Button -->
<button class="btn btn-primary print-btn" onclick="window.print()">
    <i class="fas fa-print fa-lg"></i>
</button>
