<?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_work_orders.php');
    exit();
}

// Get work order details (same as maintenance request)
$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();
$work_order = $result->fetch_assoc();

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Work Order 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' => 'Work Orders', 'title' => 'Work Order Details')); ?>

                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="list_work_orders.php" class="btn btn-light">
                                <i class="ri-arrow-left-line me-1"></i>Back to Work Orders
                            </a>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-xl-8">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">
                                        Work Order: <?php echo htmlspecialchars($work_order['ticket_number'] ?? $work_order['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($work_order['property_name'] ?? 'N/A'); ?></p>
                                            <p class="mb-2"><strong>Unit:</strong> <?php echo htmlspecialchars($work_order['unit_number'] ?? 'N/A'); ?></p>
                                            <p class="mb-2"><strong>Category:</strong> 
                                                <span class="badge bg-soft-info"><?php echo htmlspecialchars($work_order['category']); ?></span>
                                            </p>
                                            <p class="mb-2"><strong>Priority:</strong> 
                                                <span class="badge bg-soft-warning"><?php echo htmlspecialchars($work_order['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($work_order['status']); ?></span>
                                            </p>
                                            <p class="mb-2"><strong>Created:</strong> <?php echo date('M d, Y H:i', strtotime($work_order['created_at'])); ?></p>
                                            <?php if ($work_order['scheduled_date']): ?>
                                                <p class="mb-2"><strong>Scheduled:</strong> <?php echo date('M d, Y H:i', strtotime($work_order['scheduled_date'])); ?></p>
                                            <?php endif; ?>
                                            <?php if ($work_order['completed_date']): ?>
                                                <p class="mb-2"><strong>Completed:</strong> <?php echo date('M d, Y H:i', strtotime($work_order['completed_date'])); ?></p>
                                            <?php endif; ?>
                                        </div>
                                    </div>

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

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

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

                                    <?php if ($work_order['final_cost']): ?>
                                        <div class="mb-3">
                                            <h6 class="text-muted mb-2">Final Cost</h6>
                                            <h4 class="text-success">KES <?php echo number_format($work_order['final_cost'], 2); ?></h4>
                                        </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</h4>
                                </div>
                                <div class="card-body">
                                    <p class="mb-2"><strong><?php echo htmlspecialchars($work_order['first_name'] . ' ' . $work_order['last_name']); ?></strong></p>
                                    <p class="mb-2"><?php echo htmlspecialchars($work_order['email'] ?? 'N/A'); ?></p>
                                    <p class="mb-0"><?php echo htmlspecialchars($work_order['phone'] ?? 'N/A'); ?></p>
                                </div>
                            </div>
                        </div>
                    </div>

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

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