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

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

$user_id = $_SESSION['user_id'];

// Get vendor ID
$sql_vendor = "SELECT id FROM vendors WHERE user_id = ?";
$stmt_vendor = $conn->prepare($sql_vendor);
$stmt_vendor->bind_param("i", $user_id);
$stmt_vendor->execute();
$result_vendor = $stmt_vendor->get_result();
$vendor = $result_vendor->fetch_assoc();

if (!$vendor) {
    header('Location: index.php');
    exit();
}

$vendor_id = $vendor['id'];
$status_filter = $_GET['status'] ?? '';

$sql = "SELECT mr.*, p.name as property_name, u.unit_number,
               t.first_name, t.last_name, t.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 users t ON mr.requester_id = t.id
        WHERE mr.assigned_vendor_id = ?";

$params = [$vendor_id];
$types = 'i';

if (!empty($status_filter)) {
    $sql .= " AND mr.status = ?";
    $params[] = $status_filter;
    $types .= 's';
}

$sql .= " ORDER BY mr.created_at DESC";

$stmt = $conn->prepare($sql);
$stmt->bind_param($types, ...$params);
$stmt->execute();
$result = $stmt->get_result();
?>
<?php include 'layouts/main.php'; ?>

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

                    <div class="row mb-3">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-body">
                                    <form method="GET" class="d-flex gap-2">
                                        <select class="form-select" name="status" style="width: 200px;" onchange="this.form.submit()">
                                            <option value="">All Status</option>
                                            <option value="Assigned" <?php echo $status_filter == 'Assigned' ? 'selected' : ''; ?>>Assigned</option>
                                            <option value="In Progress" <?php echo $status_filter == 'In Progress' ? 'selected' : ''; ?>>In Progress</option>
                                            <option value="Completed" <?php echo $status_filter == 'Completed' ? 'selected' : ''; ?>>Completed</option>
                                        </select>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">All My Jobs</h4>
                                </div>
                                <div class="card-body">
                                    <div class="table-responsive">
                                        <table class="table table-nowrap align-middle mb-0">
                                            <thead>
                                                <tr>
                                                    <th>Ticket #</th>
                                                    <th>Property/Unit</th>
                                                    <th>Category</th>
                                                    <th>Priority</th>
                                                    <th>Status</th>
                                                    <th>Cost</th>
                                                    <th>Action</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if ($result && $result->num_rows > 0): ?>
                                                    <?php while($job = $result->fetch_assoc()): ?>
                                                        <tr>
                                                            <td><?php echo htmlspecialchars($job['ticket_number']); ?></td>
                                                            <td>
                                                                <div>
                                                                    <h5 class="fs-14 mb-0"><?php echo htmlspecialchars($job['property_name'] ?? 'N/A'); ?></h5>
                                                                    <p class="text-muted mb-0 small">Unit: <?php echo htmlspecialchars($job['unit_number'] ?? 'N/A'); ?></p>
                                                                </div>
                                                            </td>
                                                            <td>
                                                                <span class="badge bg-soft-info text-info">
                                                                    <?php echo htmlspecialchars($job['category']); ?>
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <span class="badge bg-soft-warning text-warning">
                                                                    <?php echo htmlspecialchars($job['priority']); ?>
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <span class="badge bg-soft-primary text-primary">
                                                                    <?php echo htmlspecialchars($job['status']); ?>
                                                                </span>
                                                            </td>
                                                            <td>
                                                                <?php if ($job['final_cost']): ?>
                                                                    KES <?php echo number_format($job['final_cost'], 2); ?>
                                                                <?php else: ?>
                                                                    <span class="text-muted">Pending</span>
                                                                <?php endif; ?>
                                                            </td>
                                                            <td>
                                                                <a href="work_order_details.php?id=<?php echo $job['id']; ?>" 
                                                                   class="btn btn-soft-secondary btn-sm">
                                                                    <i class="ri-eye-line"></i>
                                                                </a>
                                                            </td>
                                                        </tr>
                                                    <?php endwhile; ?>
                                                <?php else: ?>
                                                    <tr>
                                                        <td colspan="7" class="text-center py-4">
                                                            <div class="text-muted">No jobs found</div>
                                                        </td>
                                                    </tr>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

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

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