185 lines
8.4 KiB
PHP
185 lines
8.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
$error = '';
|
|
$success = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$companyName = $_POST['company_name'] ?? '';
|
|
$uprnRequired = isset($_POST['uprn_required']) ? 1 : 0;
|
|
$statuses = $_POST['statuses'] ?? [];
|
|
$folders = $_POST['folders'] ?? [];
|
|
$defaultStatusIndex = (int)($_POST['default_status'] ?? 0);
|
|
|
|
if (empty($companyName)) {
|
|
$error = "Company name is required.";
|
|
} elseif (empty($statuses)) {
|
|
$error = "At least one job status is required.";
|
|
} elseif (empty($folders)) {
|
|
$error = "At least one required folder is required.";
|
|
} else {
|
|
try {
|
|
db()->beginTransaction();
|
|
|
|
// 1. Create Company
|
|
$stmt = db()->prepare("INSERT INTO companies (name, uprn_required) VALUES (?, ?)");
|
|
$stmt->execute([$companyName, $uprnRequired]);
|
|
$companyId = db()->lastInsertId();
|
|
|
|
// 2. Insert Statuses
|
|
$stmt = db()->prepare("INSERT INTO job_statuses (company_id, name, is_default, sort_order) VALUES (?, ?, ?, ?)");
|
|
foreach ($statuses as $index => $statusName) {
|
|
if (trim($statusName) === '') continue;
|
|
$isDefault = ($index === $defaultStatusIndex) ? 1 : 0;
|
|
$stmt->execute([$companyId, $statusName, $isDefault, $index]);
|
|
}
|
|
|
|
// 3. Insert Folders
|
|
$stmt = db()->prepare("INSERT INTO required_folders (company_id, name) VALUES (?, ?)");
|
|
foreach ($folders as $folderName) {
|
|
if (trim($folderName) === '') continue;
|
|
$stmt->execute([$companyId, $folderName]);
|
|
}
|
|
|
|
// 4. Create first Admin user (simplified for demo)
|
|
$stmt = db()->prepare("INSERT INTO users (company_id, name, email, password, role) VALUES (?, ?, ?, ?, ?)");
|
|
$stmt->execute([$companyId, 'Admin User', 'admin@' . strtolower(str_replace(' ', '', $companyName)) . '.com', password_hash('password123', PASSWORD_DEFAULT), 'admin']);
|
|
|
|
db()->commit();
|
|
$success = "Company setup successfully! You can now log in.";
|
|
header('Refresh: 2; URL=index.php');
|
|
} catch (Exception $e) {
|
|
db()->rollBack();
|
|
$error = "Database error: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Company Onboarding - RepairsPro</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;700&display=swap" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?= time() ?>">
|
|
<style>
|
|
.setup-container { max-width: 600px; margin: 50px auto; }
|
|
.dynamic-row { display: flex; gap: 10px; margin-bottom: 10px; align-items: center; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="setup-container">
|
|
<div class="card p-4">
|
|
<h2 class="fw-bold mb-4">Company Onboarding</h2>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
|
|
<?php else: ?>
|
|
|
|
<form method="POST" id="onboardingForm">
|
|
<!-- Step 1: Basic Info -->
|
|
<div class="mb-4">
|
|
<label class="form-label fw-bold">Company Name</label>
|
|
<input type="text" name="company_name" class="form-control" placeholder="e.g. London Repairs Ltd" required>
|
|
</div>
|
|
|
|
<div class="mb-4">
|
|
<div class="form-check form-switch">
|
|
<input class="form-check-input" type="checkbox" name="uprn_required" id="uprnCheck">
|
|
<label class="form-check-label fw-bold" for="uprnCheck">Require Job UPRN?</label>
|
|
</div>
|
|
<small class="text-secondary">If enabled, every job must have a unique UPRN.</small>
|
|
</div>
|
|
|
|
<hr>
|
|
|
|
<!-- Step 2: Job Statuses -->
|
|
<div class="mb-4">
|
|
<label class="form-label fw-bold">Job Statuses</label>
|
|
<p class="small text-secondary">Define the workflow stages for your jobs.</p>
|
|
<div id="status-container">
|
|
<div class="dynamic-row">
|
|
<input type="radio" name="default_status" value="0" checked title="Set as default">
|
|
<input type="text" name="statuses[]" class="form-control" value="To Be Surveyed" required>
|
|
</div>
|
|
<div class="dynamic-row">
|
|
<input type="radio" name="default_status" value="1" title="Set as default">
|
|
<input type="text" name="statuses[]" class="form-control" value="Booking Required">
|
|
<button type="button" class="btn btn-sm btn-outline-danger remove-btn">×</button>
|
|
</div>
|
|
</div>
|
|
<button type="button" class="btn btn-sm btn-outline-primary mt-2" onclick="addRow('status-container', 'statuses[]', true)">+ Add Status</button>
|
|
</div>
|
|
|
|
<hr>
|
|
|
|
<!-- Step 3: Required Folders -->
|
|
<div class="mb-4">
|
|
<label class="form-label fw-bold">Required Folders</label>
|
|
<p class="small text-secondary">These folders will appear on every job automatically.</p>
|
|
<div id="folder-container">
|
|
<div class="dynamic-row">
|
|
<input type="text" name="folders[]" class="form-control" value="Photos" required>
|
|
</div>
|
|
<div class="dynamic-row">
|
|
<input type="text" name="folders[]" class="form-control" value="Quote">
|
|
<button type="button" class="btn btn-sm btn-outline-danger remove-btn">×</button>
|
|
</div>
|
|
</div>
|
|
<button type="button" class="btn btn-sm btn-outline-primary mt-2" onclick="addRow('folder-container', 'folders[]', false)">+ Add Folder</button>
|
|
</div>
|
|
|
|
<div class="d-grid gap-2 mt-5">
|
|
<button type="submit" class="btn btn-primary btn-lg">Complete Onboarding</button>
|
|
</div>
|
|
</form>
|
|
<?php endif; ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
function addRow(containerId, name, hasRadio) {
|
|
const container = document.getElementById(containerId);
|
|
const count = container.children.length;
|
|
const div = document.createElement('div');
|
|
div.className = 'dynamic-row';
|
|
|
|
let html = '';
|
|
if (hasRadio) {
|
|
html += `<input type="radio" name="default_status" value="${count}">`;
|
|
}
|
|
html += `<input type="text" name="${name}" class="form-control" required>`;
|
|
html += `<button type="button" class="btn btn-sm btn-outline-danger remove-btn">×</button>`;
|
|
|
|
div.innerHTML = html;
|
|
container.appendChild(div);
|
|
|
|
div.querySelector('.remove-btn').onclick = function() {
|
|
div.remove();
|
|
// Re-index radios if necessary
|
|
if (hasRadio) {
|
|
Array.from(container.children).forEach((row, idx) => {
|
|
const radio = row.querySelector('input[type="radio"]');
|
|
if (radio) radio.value = idx;
|
|
});
|
|
}
|
|
};
|
|
}
|
|
|
|
// Attach remove events to existing buttons
|
|
document.querySelectorAll('.remove-btn').forEach(btn => {
|
|
btn.onclick = function() { btn.parentElement.remove(); };
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|