262 lines
13 KiB
PHP
262 lines
13 KiB
PHP
<?php
|
|
require_once __DIR__ . '/includes/helpers.php';
|
|
|
|
// Placeholder for company ID - in a real app, this would come from the session after login
|
|
$company_id = get_current_company_id();
|
|
$current_user = getCurrentAppUser();
|
|
|
|
if (!$company_id) {
|
|
// Redirect to login or error page if no company is identified
|
|
header('Location: /login.php'); // Assuming a login.php exists
|
|
exit();
|
|
}
|
|
|
|
$step = isset($_GET['step']) ? (int)$_GET['step'] : 1;
|
|
$message = '';
|
|
$message_type = ''; // 'success' or 'error'
|
|
|
|
// Check if onboarding is already complete
|
|
$onboarding_complete = get_company_onboarding_status($company_id);
|
|
if ($onboarding_complete) {
|
|
header('Location: index.php');
|
|
exit();
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
if ($step === 1) {
|
|
// Handle Job Status Setup submission
|
|
$statuses = $_POST['statuses'] ?? [];
|
|
$default_status_index = $_POST['default_status'] ?? null;
|
|
|
|
if (empty($statuses)) {
|
|
$message = 'Please add at least one job status.';
|
|
$message_type = 'error';
|
|
} elseif (!isset($default_status_index) || !array_key_exists($default_status_index, $statuses)) {
|
|
$message = 'Please select a default job status.';
|
|
$message_type = 'error';
|
|
} else {
|
|
try {
|
|
$pdo = db();
|
|
$pdo->beginTransaction();
|
|
|
|
// Clear existing job statuses for this company to avoid duplicates on re-submission
|
|
$stmt = $pdo->prepare("DELETE FROM job_statuses WHERE company_id = ?");
|
|
$stmt->execute([$company_id]);
|
|
|
|
$insert_stmt = $pdo->prepare("INSERT INTO job_statuses (company_id, name, is_default) VALUES (?, ?, ?)");
|
|
foreach ($statuses as $index => $status_name) {
|
|
$is_default = (int)($index == $default_status_index); // Explicitly cast to int (0 or 1)
|
|
$insert_stmt->execute([$company_id, $status_name, $is_default]);
|
|
}
|
|
|
|
$pdo->commit();
|
|
header('Location: onboarding.php?step=2&message=Job statuses saved successfully!&message_type=success');
|
|
exit();
|
|
} catch (PDOException $e) {
|
|
$pdo->rollBack();
|
|
$message = 'Error saving job statuses: ' . $e->getMessage();
|
|
$message_type = 'error';
|
|
}
|
|
}
|
|
} elseif ($step === 2) {
|
|
// Handle Required Job Folder Setup submission
|
|
$folders = $_POST['folders'] ?? [];
|
|
|
|
if (empty($folders)) {
|
|
$message = 'Please add at least one required folder.';
|
|
$message_type = 'error';
|
|
} else {
|
|
try {
|
|
$pdo = db();
|
|
$pdo->beginTransaction();
|
|
|
|
// Clear existing job folders for this company
|
|
$stmt = $pdo->prepare("DELETE FROM job_folders WHERE company_id = ? AND is_required = TRUE");
|
|
$stmt->execute([$company_id]);
|
|
|
|
$insert_stmt = $pdo->prepare("INSERT INTO job_folders (company_id, name, is_required) VALUES (?, ?, TRUE)");
|
|
foreach ($folders as $folder_name) {
|
|
$insert_stmt->execute([$company_id, $folder_name]);
|
|
}
|
|
|
|
// Mark onboarding as complete
|
|
$stmt = $pdo->prepare("UPDATE companies SET onboarding_complete = TRUE WHERE id = ?");
|
|
$stmt->execute([$company_id]);
|
|
|
|
$pdo->commit();
|
|
header('Location: index.php?message=Onboarding complete!&message_type=success');
|
|
exit();
|
|
} catch (PDOException $e) {
|
|
$pdo->rollBack();
|
|
$message = 'Error saving job folders: ' . $e->getMessage();
|
|
$message_type = 'error';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Get messages from URL for display
|
|
if (isset($_GET['message'])) {
|
|
$message = htmlspecialchars($_GET['message']);
|
|
$message_type = htmlspecialchars($_GET['message_type'] ?? '');
|
|
}
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Onboarding Wizard - Repairs Pro</title>
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
<style>
|
|
body { font-family: 'Inter', system-ui, -apple-system, sans-serif; background-color: #F9FAFB; color: #111827; }
|
|
.container { max-width: 800px; margin: 50px auto; padding: 30px; background-color: #FFFFFF; border-radius: 4px; box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
|
|
h1 { color: #111827; border-bottom: 1px solid #E5E7EB; padding-bottom: 15px; margin-bottom: 30px; }
|
|
h2 { color: #3B82F6; margin-bottom: 20px; }
|
|
.form-group { margin-bottom: 20px; }
|
|
label { display: block; margin-bottom: 8px; font-weight: 500; }
|
|
input[type="text"] { width: calc(100% - 22px); padding: 10px; border: 1px solid #D1D5DB; border-radius: 4px; }
|
|
button { padding: 10px 20px; background-color: #3B82F6; color: #FFFFFF; border: none; border-radius: 4px; cursor: pointer; font-size: 16px; }
|
|
button:hover { background-color: #2563EB; }
|
|
.status-item, .folder-item { display: flex; align-items: center; margin-bottom: 10px; }
|
|
.status-item input[type="radio"] { margin-right: 10px; }
|
|
.status-item input[type="text"], .folder-item input[type="text"] { flex-grow: 1; margin-right: 10px; }
|
|
.add-button { background-color: #10B981; margin-top: 10px; }
|
|
.add-button:hover { background-color: #059669; }
|
|
.remove-button { background-color: #EF4444; margin-left: 10px; }
|
|
.remove-button:hover { background-color: #DC2626; }
|
|
.message { padding: 15px; border-radius: 4px; margin-bottom: 20px; }
|
|
.message.success { background-color: #D1FAE5; color: #065F46; border: 1px solid #34D399; }
|
|
.message.error { background-color: #FEE2E2; color: #991B1B; border: 1px solid #F87171; }
|
|
.pagination { margin-top: 30px; text-align: center; }
|
|
.pagination button { margin: 0 10px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<h1>Company Onboarding Wizard</h1>
|
|
|
|
<?php if ($message): ?>
|
|
<div class="message <?php echo $message_type; ?>">
|
|
<?php echo $message; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($step === 1): // Job Status Setup ?>
|
|
<h2>Step 1: Job Status Setup</h2>
|
|
<p>Define the different statuses a job can have within your company. One status must be set as default.</p>
|
|
<form method="POST" action="onboarding.php?step=1">
|
|
<div id="status-list">
|
|
<div class="form-group status-item">
|
|
<input type="radio" name="default_status" value="0" id="default_status_0" required>
|
|
<input type="text" name="statuses[]" value="To Be Surveyed" placeholder="e.g., To Be Surveyed" required>
|
|
<label for="default_status_0">Default</label>
|
|
</div>
|
|
<div class="form-group status-item">
|
|
<input type="radio" name="default_status" value="1" id="default_status_1">
|
|
<input type="text" name="statuses[]" value="Booking Required" placeholder="e.g., Booking Required" required>
|
|
<label for="default_status_1">Default</label>
|
|
<button type="button" class="remove-button" onclick="removeStatus(this)">Remove</button>
|
|
</div>
|
|
<div class="form-group status-item">
|
|
<input type="radio" name="default_status" value="2" id="default_status_2">
|
|
<input type="text" name="statuses[]" value="Completed" placeholder="e.g., Completed" required>
|
|
<label for="default_status_2">Default</label>
|
|
<button type="button" class="remove-button" onclick="removeStatus(this)">Remove</button>
|
|
</div>
|
|
</div>
|
|
<button type="button" class="add-button" onclick="addStatus()">Add Another Status</button>
|
|
<div class="pagination">
|
|
<button type="submit">Next Step</button>
|
|
</div>
|
|
</form>
|
|
|
|
<script>
|
|
let statusCount = 3;
|
|
function addStatus() {
|
|
const statusList = document.getElementById('status-list');
|
|
const newStatusItem = document.createElement('div');
|
|
newStatusItem.classList.add('form-group', 'status-item');
|
|
newStatusItem.innerHTML = `
|
|
<input type="radio" name="default_status" value="${statusCount}" id="default_status_${statusCount}">
|
|
<input type="text" name="statuses[]" placeholder="e.g., In Progress" required>
|
|
<label for="default_status_${statusCount}">Default</label>
|
|
<button type="button" class="remove-button" onclick="removeStatus(this)">Remove</button>
|
|
`;
|
|
statusList.appendChild(newStatusItem);
|
|
statusCount++;
|
|
updateRadioValues();
|
|
}
|
|
|
|
function removeStatus(button) {
|
|
button.closest('.status-item').remove();
|
|
updateRadioValues();
|
|
}
|
|
|
|
function updateRadioValues() {
|
|
const statusItems = document.querySelectorAll('.status-item');
|
|
statusItems.forEach((item, index) => {
|
|
item.querySelector('input[type="radio"]').value = index;
|
|
item.querySelector('input[type="radio"]').id = `default_status_${index}`;
|
|
item.querySelector('label').htmlFor = `default_status_${index}`;
|
|
});
|
|
}
|
|
</script>
|
|
|
|
<?php elseif ($step === 2): // Required Job Folder Setup ?>
|
|
<h2>Step 2: Required Job Folder Setup</h2>
|
|
<p>Define folders that will automatically appear on every job. These cannot be deleted by users.</p>
|
|
<form method="POST" action="onboarding.php?step=2">
|
|
<div id="folder-list">
|
|
<div class="form-group folder-item">
|
|
<input type="text" name="folders[]" value="PO" placeholder="e.g., PO" required>
|
|
<button type="button" class="remove-button" onclick="removeFolder(this)">Remove</button>
|
|
</div>
|
|
<div class="form-group folder-item">
|
|
<input type="text" name="folders[]" value="Quote" placeholder="e.g., Quote" required>
|
|
<button type="button" class="remove-button" onclick="removeFolder(this)">Remove</button>
|
|
</div>
|
|
<div class="form-group folder-item">
|
|
<input type="text" name="folders[]" value="Photos" placeholder="e.g., Photos" required>
|
|
<button type="button" class="remove-button" onclick="removeFolder(this)">Remove</button>
|
|
</div>
|
|
<div class="form-group folder-item">
|
|
<input type="text" name="folders[]" value="RAMS" placeholder="e.g., RAMS" required>
|
|
<button type="button" class="remove-button" onclick="removeFolder(this)">Remove</button>
|
|
</div>
|
|
<div class="form-group folder-item">
|
|
<input type="text" name="folders[]" value="Invoices" placeholder="e.g., Invoices" required>
|
|
<button type="button" class="remove-button" onclick="removeFolder(this)">Remove</button>
|
|
</div>
|
|
</div>
|
|
<button type="button" class="add-button" onclick="addFolder()">Add Another Folder</button>
|
|
<div class="pagination">
|
|
<button type="button" onclick="window.location.href='onboarding.php?step=1'" class="">Previous Step</button>
|
|
<button type="submit">Complete Onboarding</button>
|
|
</div>
|
|
</form>
|
|
|
|
<script>
|
|
let folderCount = 5;
|
|
function addFolder() {
|
|
const folderList = document.getElementById('folder-list');
|
|
const newFolderItem = document.createElement('div');
|
|
newFolderItem.classList.add('form-group', 'folder-item');
|
|
newFolderItem.innerHTML = `
|
|
<input type="text" name="folders[]" placeholder="e.g., Documents" required>
|
|
<button type="button" class="remove-button" onclick="removeFolder(this)">Remove</button>
|
|
`;
|
|
folderList.appendChild(newFolderItem);
|
|
folderCount++;
|
|
}
|
|
|
|
function removeFolder(button) {
|
|
button.closest('.folder-item').remove();
|
|
}
|
|
</script>
|
|
|
|
<?php endif; ?>
|
|
</div>
|
|
</body>
|
|
</html>
|