<?php
session_start();
include 'layouts/dbconn.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$request_id = $_GET['id'] ?? '';

if (empty($request_id)) {
    header('Location: list_maintenance.php');
    exit();
}

// Get maintenance request details
$sql = "SELECT mr.*, p.name as property_name, u.unit_number,
               t.first_name, t.last_name, t.email, t.phone,
               v.company_name as vendor_name, v.phone as vendor_phone
        FROM maintenance_requests mr
        LEFT JOIN properties p ON mr.property_id = p.id
        LEFT JOIN units u ON mr.unit_id = u.id
        LEFT JOIN tenants t ON mr.tenant_id = t.tenant_id
        LEFT JOIN vendors v ON mr.assigned_vendor_id = v.id
        WHERE mr.request_id = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $request_id);
$stmt->execute();
$result = $stmt->get_result();
$request = $result->fetch_assoc();

if (!$request) {
    header('Location: list_maintenance.php');
    exit();
}
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Maintenance Request Details')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <?php includeFileWithVariables('layouts/page-title.php', array('pagetitle' => 'Maintenance', 'title' => 'Request Details')); ?>

                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="list_maintenance.php" class="btn btn-light">
                                <i class="ri-arrow-left-line me-1"></i>Back to Requests
                            </a>
                            <?php if ($request['status'] == 'Open' || $request['status'] == 'NEW'): ?>
                                <a href="assign_vendor.php?id=<?php echo $request_id; ?>" class="btn btn-primary ms-2">
                                    <i class="ri-user-add-line me-1"></i>Assign Vendor
                                </a>
                            <?php endif; ?>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-xl-8">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">
                                        Ticket: <?php echo htmlspecialchars($request['ticket_number'] ?? $request['request_code']); ?>
                                    </h4>
                                </div>
                                <div class="card-body">
                                    <div class="row mb-3">
                                        <div class="col-md-6">
                                            <p class="mb-2"><strong>Property:</strong> <?php echo htmlspecialchars($request['property_name'] ?? 'N/A'); ?></p>
                                            <p class="mb-2"><strong>Unit:</strong> <?php echo htmlspecialchars($request['unit_number'] ?? 'N/A'); ?></p>
                                            <p class="mb-2"><strong>Category:</strong> 
                                                <span class="badge bg-soft-info"><?php echo htmlspecialchars($request['category']); ?></span>
                                            </p>
                                            <p class="mb-2"><strong>Priority:</strong> 
                                                <?php
                                                $priority_colors = [
                                                    'Emergency' => 'danger',
                                                    'High' => 'warning',
                                                    'Medium' => 'info',
                                                    'Low' => 'secondary'
                                                ];
                                                $priority_color = $priority_colors[$request['priority']] ?? 'secondary';
                                                ?>
                                                <span class="badge bg-soft-<?php echo $priority_color; ?>">
                                                    <?php echo htmlspecialchars($request['priority']); ?>
                                                </span>
                                            </p>
                                        </div>
                                        <div class="col-md-6">
                                            <p class="mb-2"><strong>Status:</strong> 
                                                <span class="badge bg-soft-primary"><?php echo htmlspecialchars($request['status']); ?></span>
                                            </p>
                                            <p class="mb-2"><strong>Created:</strong> <?php echo date('M d, Y H:i', strtotime($request['created_at'])); ?></p>
                                            <?php if ($request['scheduled_date']): ?>
                                                <p class="mb-2"><strong>Scheduled:</strong> <?php echo date('M d, Y H:i', strtotime($request['scheduled_date'])); ?></p>
                                            <?php endif; ?>
                                            <?php if ($request['completed_date']): ?>
                                                <p class="mb-2"><strong>Completed:</strong> <?php echo date('M d, Y H:i', strtotime($request['completed_date'])); ?></p>
                                            <?php endif; ?>
                                        </div>
                                    </div>

                                    <div class="mb-3">
                                        <h6 class="text-muted mb-2">Subject</h6>
                                        <p><?php echo htmlspecialchars($request['subject']); ?></p>
                                    </div>

                                    <div class="mb-3">
                                        <h6 class="text-muted mb-2">Description</h6>
                                        <p><?php echo nl2br(htmlspecialchars($request['description'])); ?></p>
                                    </div>

                                    <?php if ($request['assigned_vendor_id']): ?>
                                        <div class="mb-3">
                                            <h6 class="text-muted mb-2">Assigned Vendor</h6>
                                            <p class="mb-1"><strong><?php echo htmlspecialchars($request['vendor_name']); ?></strong></p>
                                            <p class="mb-0 text-muted"><?php echo htmlspecialchars($request['vendor_phone'] ?? 'N/A'); ?></p>
                                        </div>
                                    <?php endif; ?>

                                    <?php if ($request['final_cost']): ?>
                                        <div class="mb-3">
                                            <h6 class="text-muted mb-2">Final Cost</h6>
                                            <h4 class="text-success">KES <?php echo number_format($request['final_cost'], 2); ?></h4>
                                        </div>
                                    <?php endif; ?>

                                    <?php if ($request['notes']): ?>
                                        <div class="mb-3">
                                            <h6 class="text-muted mb-2">Notes</h6>
                                            <p><?php echo nl2br(htmlspecialchars($request['notes'])); ?></p>
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>

                        <div class="col-xl-4">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">Requester Information</h4>
                                </div>
                                <div class="card-body">
                                    <p class="mb-2"><strong><?php echo htmlspecialchars($request['first_name'] . ' ' . $request['last_name']); ?></strong></p>
                                    <p class="mb-2"><?php echo htmlspecialchars($request['email'] ?? 'N/A'); ?></p>
                                    <p class="mb-0"><?php echo htmlspecialchars($request['phone'] ?? 'N/A'); ?></p>
                                </div>
                            </div>

                            <div class="card mt-3">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">Quick Actions</h4>
                                </div>
                                <div class="card-body">
                                    <?php if ($request['status'] == 'Open' || $request['status'] == 'NEW'): ?>
                                        <a href="assign_vendor.php?id=<?php echo $request_id; ?>" class="btn btn-primary w-100 mb-2">
                                            <i class="ri-user-add-line me-1"></i>Assign Vendor
                                        </a>
                                    <?php endif; ?>
                                    <?php if ($request['property_id']): ?>
                                        <a href="property_details.php?id=<?php echo $request['property_id']; ?>" class="btn btn-secondary w-100 mb-2">
                                            <i class="ri-building-line me-1"></i>View Property
                                        </a>
                                    <?php endif; ?>
                                    <?php if ($request['unit_id']): ?>
                                        <a href="unit_details.php?id=<?php echo $request['unit_id']; ?>" class="btn btn-info w-100">
                                            <i class="ri-home-4-line me-1"></i>View Unit
                                        </a>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
