37748-vm/index.php
2026-01-23 15:54:08 +00:00

188 lines
8.4 KiB
PHP

<?php
require_once 'includes/helpers.php';
// Get current user and company ID
$user = getCurrentAppUser();
// If no user or company ID, redirect to a login/error page (to be implemented later)
if (!$user || !($company_id = get_current_company_id())) {
// For now, if no user context, redirect to onboarding if not already there,
// assuming onboarding creates the initial admin user.
// In a real app, this would be a proper login flow.
if (basename($_SERVER['PHP_SELF']) !== 'onboarding.php') {
header('Location: onboarding.php');
exit();
}
// If we're already on onboarding.php and there's no user, let it proceed
// to allow the first company/user setup.
}
// Check if company onboarding is complete
if ($company_id && !get_company_onboarding_status($company_id)) {
// If not complete, redirect to the onboarding wizard
header('Location: onboarding.php');
exit();
}
// Handle Job Creation
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'create_job') {
$job_ref = $_POST['job_ref'];
$address_1 = $_POST['address_1'];
$description = $_POST['description'];
$status_id = $_POST['status_id'];
$client_id = $_POST['client_id'];
try {
$stmt = db()->prepare("INSERT INTO jobs (company_id, job_ref, address_1, description, status_id, client_id) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([$company_id, $job_ref, $address_1, $description, $status_id, $client_id]);
$job_id = db()->lastInsertId();
logActivity($job_id, 'job_created', null, null, "Job Ref: $job_ref");
header("Location: job_detail.php?id=" . $job_id);
exit;
} catch (Exception $e) {
$error = "Error creating job: " . $e->getMessage();
}
}
$jobs = db()->prepare("SELECT j.*, s.name as status_name, c.name as client_name
FROM jobs j
LEFT JOIN job_statuses s ON j.status_id = s.id
LEFT JOIN clients c ON j.client_id = c.id
WHERE j.company_id = ?
ORDER BY j.created_at DESC");
$jobs->execute([$company_id]);
$jobList = $jobs->fetchAll();
$statuses = getJobStatuses($company_id);
$clients = getClients($company_id);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dashboard - Repairs Pro</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css">
</head>
<body>
<nav class="navbar navbar-expand-lg py-3">
<div class="container">
<a class="navbar-brand fw-bold" href="index.php">Repairs Pro</a>
<div class="d-flex align-items-center">
<span class="me-3 text-secondary"><?php echo htmlspecialchars($user['name'] ?? ''); ?></span>
<button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#createJobModal">New Job</button>
</div>
</div>
</nav>
<main class="container py-5">
<div class="d-flex justify-content-between align-items-center mb-4">
<h1 class="h4 fw-bold m-0">All Jobs</h1>
</div>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<div class="card">
<div class="table-responsive">
<table class="table mb-0">
<thead>
<tr>
<th>Job Ref</th>
<th>Client</th>
<th>Address</th>
<th>Status</th>
<th>Approved</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($jobList as $job): ?>
<tr>
<td class="fw-medium text-primary"><?php echo htmlspecialchars($job['job_ref']); ?></td>
<td><?php echo htmlspecialchars($job['client_name']); ?></td>
<td class="text-secondary"><?php echo htmlspecialchars($job['address_1']); ?></td>
<td>
<span class="badge bg-light text-dark border">
<?php echo htmlspecialchars($job['status_name']); ?>
</span>
</td>
<td>
<?php if ($job['works_approved']): ?>
<span class="text-success">Yes</span>
<?php else: ?>
<span class="text-muted">No</span>
<?php endif; ?>
</td>
<td>
<a href="job_detail.php?id=<?php echo $job['id']; ?>" class="btn btn-outline-secondary btn-sm">View</a>
</td>
</tr>
<?php endforeach; if (empty($jobList)): ?>
<tr>
<td colspan="6" class="text-center py-5 text-secondary">No jobs found. Start by creating one.</td>
</tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</main>
<!-- Create Job Modal -->
<div class="modal fade" id="createJobModal" tabindex="-1">
<div class="modal-dialog">
<form class="modal-content" method="POST">
<input type="hidden" name="action" value="create_job">
<div class="modal-header">
<h5 class="modal-title">Create New Job</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label class="form-label">Job Reference *</label>
<input type="text" name="job_ref" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">Client *</label>
<select name="client_id" class="form-select" required>
<?php foreach ($clients as $client): ?>
<option value="<?php echo $client['id']; ?>"><?php echo htmlspecialchars($client['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Address Line 1</label>
<input type="text" name="address_1" class="form-control">
</div>
<div class="mb-3">
<label class="form-label">Status</label>
<select name="status_id" class="form-select">
<?php foreach ($statuses as $status): ?>
<option value="<?php echo $status['id']; ?>" <?php echo $status['is_default'] ? 'selected' : ''; ?>>
<?php echo htmlspecialchars($status['name']); ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Description</label>
<textarea name="description" class="form-control" rows="3"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Create Job</button>
</div>
</form>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>