Login and superadmin setup

This commit is contained in:
Flatlogic Bot 2026-03-01 22:53:30 +00:00
parent d49002c4b1
commit 7527f17da8
15 changed files with 1364 additions and 389 deletions

View File

@ -1,84 +1,126 @@
<?php
require_once __DIR__ . '/header.php';
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
$message = '';
require_once 'db/config.php';
$msp_id = $_SESSION['msp_id'];
// Fetch available templates
$stmt = db()->prepare("SELECT * FROM onboarding_templates WHERE msp_id = ? ORDER BY name ASC");
$stmt->execute([$msp_id]);
$templates = $stmt->fetchAll();
$success = '';
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'] ?? '';
$email = $_POST['email'] ?? '';
$template_id = $_POST['template_id'] ?: null;
if (empty($name)) {
$error = "Customer name is required.";
} else {
if ($name && $email) {
try {
$stmt = db()->prepare("INSERT INTO customers (name, contact_email, status) VALUES (?, ?, 'onboarding')");
$stmt->execute([$name, $email]);
db()->beginTransaction();
$stmt = db()->prepare("INSERT INTO customers (msp_id, template_id, name, contact_email, status) VALUES (?, ?, ?, ?, 'onboarding')");
$stmt->execute([$msp_id, $template_id, $name, $email]);
$customer_id = db()->lastInsertId();
// Seed onboarding tasks
$tasks = [
'Initial Kick-off Meeting',
'Discovery & Asset Inventory',
'Agent Deployment (RMM)',
'Network Security Audit',
'Backup & Disaster Recovery Setup',
'Microsoft 365 Tenant Review',
'Final Configuration Handover'
];
$stmt_task = db()->prepare("INSERT INTO onboarding_tasks (customer_id, task_name) VALUES (?, ?)");
foreach ($tasks as $task) {
$stmt_task->execute([$customer_id, $task]);
if ($template_id) {
// Seed tasks from template steps
$stmt = db()->prepare("SELECT title FROM onboarding_template_steps WHERE template_id = ? ORDER BY order_index ASC");
$stmt->execute([$template_id]);
$steps = $stmt->fetchAll();
foreach ($steps as $step) {
$stmt = db()->prepare("INSERT INTO onboarding_tasks (customer_id, task_name) VALUES (?, ?)");
$stmt->execute([$customer_id, "Complete Step: " . $step['title']]);
}
} else {
// Default tasks if no template
$default_tasks = ['Initial Call', 'Service Agreement signed', 'Access credentials received', 'Network audit completed'];
foreach ($default_tasks as $task) {
$stmt = db()->prepare("INSERT INTO onboarding_tasks (customer_id, task_name) VALUES (?, ?)");
$stmt->execute([$customer_id, $task]);
}
}
$message = "Customer added successfully! <a href='onboarding.php?id=$customer_id' style='color: var(--primary); text-decoration: underline;'>View Onboarding Checklist</a>";
db()->commit();
$success = "Customer added successfully!";
} catch (PDOException $e) {
db()->rollBack();
$error = "Error adding customer: " . $e->getMessage();
}
} else {
$error = "Please fill in all required fields.";
}
}
include 'header.php';
?>
<div class="container" style="max-width: 600px;">
<div style="margin-bottom: 2rem;">
<h2 style="font-size: 1.5rem; font-weight: 700; margin-bottom: 0.5rem;">Add New Customer</h2>
<p style="color: var(--text-muted); font-size: 0.875rem;">Enter details to start the onboarding process.</p>
<div class="container py-5">
<div class="header-section mb-5">
<h1>Add New Customer</h1>
<p class="text-muted">Register a new client and start the onboarding process.</p>
</div>
<?php if ($message): ?>
<div style="background: #dcfce7; color: #166534; padding: 1rem; border-radius: var(--radius); margin-bottom: 1.5rem; border: 1px solid #bbf7d0;">
<i class="bi bi-check-circle-fill" style="margin-right: 0.5rem;"></i> <?php echo $message; ?>
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card shadow-sm border-0">
<div class="card-body p-4 p-md-5">
<?php if ($success): ?>
<div class="alert alert-success d-flex align-items-center mb-4">
<i class="bi bi-check-circle-fill me-2"></i>
<div><?php echo $success; ?> <a href="customers.php" class="alert-link">View customer list.</a></div>
</div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger d-flex align-items-center mb-4">
<i class="bi bi-exclamation-triangle-fill me-2"></i>
<div><?php echo $error; ?></div>
</div>
<?php endif; ?>
<form method="POST" action="">
<div class="row g-4">
<div class="col-md-6">
<label for="name" class="form-label fw-bold">Customer Name</label>
<input type="text" name="name" id="name" class="form-control" placeholder="e.g. Acme Corporation" required>
</div>
<div class="col-md-6">
<label for="email" class="form-label fw-bold">Primary Contact Email</label>
<input type="email" name="email" id="email" class="form-control" placeholder="e.g. contact@acme.com" required>
</div>
<div class="col-12">
<label for="template_id" class="form-label fw-bold">Onboarding Template</label>
<select name="template_id" id="template_id" class="form-select">
<option value="">-- No template (use default tasks) --</option>
<?php foreach ($templates as $template): ?>
<option value="<?php echo $template['id']; ?>"><?php echo htmlspecialchars($template['name']); ?></option>
<?php endforeach; ?>
</select>
<div class="form-text mt-2 small text-muted">Choosing a template will automatically generate onboarding tasks based on that template.</div>
</div>
<div class="col-12 mt-5">
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary btn-lg fw-bold">
<i class="bi bi-plus-circle me-2"></i> Create & Start Onboarding
</button>
<a href="customers.php" class="btn btn-link text-muted">Cancel</a>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<?php endif; ?>
<?php if ($error): ?>
<div style="background: #fee2e2; color: #991b1b; padding: 1rem; border-radius: var(--radius); margin-bottom: 1.5rem; border: 1px solid #fecaca;">
<i class="bi bi-exclamation-circle-fill" style="margin-right: 0.5rem;"></i> <?php echo $error; ?>
</div>
<?php endif; ?>
<div class="card">
<form method="POST">
<div class="form-group">
<label class="form-label" for="name">Company / Customer Name</label>
<input type="text" id="name" name="name" class="form-control" placeholder="Acme Corp" required>
</div>
<div class="form-group">
<label class="form-label" for="email">Primary Contact Email</label>
<input type="email" id="email" name="email" class="form-control" placeholder="admin@acmecorp.com">
</div>
<div style="display: flex; gap: 1rem; margin-top: 2rem;">
<button type="submit" class="btn btn-primary" style="flex: 1;">Create Customer Profile</button>
<a href="customers.php" class="btn btn-outline" style="flex: 1;">Cancel</a>
</div>
</form>
</div>
<div style="margin-top: 2rem; padding: 1rem; background: #f1f5f9; border-radius: var(--radius); font-size: 0.875rem; color: var(--text-muted);">
<i class="bi bi-info-circle" style="margin-right: 0.5rem; color: var(--primary);"></i>
Adding a customer will automatically generate a standard 7-step onboarding checklist to track progress.
</div>
</div>
<?php require_once __DIR__ . '/footer.php'; ?>
<?php include 'footer.php'; ?>

128
customer_view.php Normal file
View File

@ -0,0 +1,128 @@
<?php
require_once 'db/config.php';
$customer_id = $_GET['id'] ?? null;
$step_index = (int)($_GET['step'] ?? 0);
if (!$customer_id) {
die("Invalid access.");
}
$stmt = db()->prepare("SELECT c.*, m.name as msp_name FROM customers c JOIN msps m ON c.msp_id = m.id WHERE c.id = ?");
$stmt->execute([$customer_id]);
$customer = $stmt->fetch();
if (!$customer || !$customer['template_id']) {
die("No onboarding template assigned to this customer.");
}
// Fetch steps
$stmt = db()->prepare("SELECT * FROM onboarding_template_steps WHERE template_id = ? ORDER BY order_index ASC");
$stmt->execute([$customer['template_id']]);
$steps = $stmt->fetchAll();
if (empty($steps)) {
die("This onboarding flow has no steps.");
}
$current_step = $steps[$step_index] ?? null;
if (!$current_step) {
$step_index = 0;
$current_step = $steps[0];
}
// Fetch fields for current step
$stmt = db()->prepare("SELECT * FROM onboarding_template_fields WHERE step_id = ? ORDER BY order_index ASC");
$stmt->execute([$current_step['id']]);
$fields = $stmt->fetchAll();
$is_last_step = ($step_index === count($steps) - 1);
include 'header.php'; // We use the same header for simplicity, but we could make a "customer header"
?>
<div class="container py-5 mt-4">
<div class="row justify-content-center">
<div class="col-md-9">
<div class="text-center mb-5">
<h6 class="text-primary fw-bold text-uppercase mb-2">Welcome to <?php echo htmlspecialchars($customer['msp_name']); ?></h6>
<h1>Customer Onboarding</h1>
<div class="d-flex justify-content-center mt-4">
<?php foreach ($steps as $idx => $step): ?>
<div class="px-2">
<div class="rounded-pill <?php echo $idx === $step_index ? 'bg-primary' : ($idx < $step_index ? 'bg-success' : 'bg-light'); ?>" style="width: 40px; height: 10px;"></div>
</div>
<?php endforeach; ?>
</div>
</div>
<div class="card shadow border-0 overflow-hidden">
<div class="card-body p-5">
<h2 class="fw-bold mb-3"><?php echo htmlspecialchars($current_step['title']); ?></h2>
<p class="text-muted mb-5 lead"><?php echo nl2br(htmlspecialchars($current_step['description'])); ?></p>
<form method="POST" action="customer_view.php?id=<?php echo $customer_id; ?>&step=<?php echo $step_index + 1; ?>">
<div class="row g-4">
<?php foreach ($fields as $field): ?>
<div class="col-12">
<?php if ($field['type'] === 'text'): ?>
<div class="p-4 bg-light rounded-3 mb-3">
<?php echo nl2br($field['content']); ?>
</div>
<?php elseif ($field['type'] === 'form_input'): ?>
<label class="form-label fw-bold"><?php echo htmlspecialchars($field['label']); ?><?php echo $field['is_required'] ? ' <span class="text-danger">*</span>' : ''; ?></label>
<input type="text" class="form-control form-control-lg" <?php echo $field['is_required'] ? 'required' : ''; ?>>
<?php elseif ($field['type'] === 'form_textarea'): ?>
<label class="form-label fw-bold"><?php echo htmlspecialchars($field['label']); ?><?php echo $field['is_required'] ? ' <span class="text-danger">*</span>' : ''; ?></label>
<textarea class="form-control" rows="4" <?php echo $field['is_required'] ? 'required' : ''; ?>></textarea>
<?php elseif ($field['type'] === 'form_select'): ?>
<label class="form-label fw-bold"><?php echo htmlspecialchars($field['label']); ?><?php echo $field['is_required'] ? ' <span class="text-danger">*</span>' : ''; ?></label>
<select class="form-select form-select-lg" <?php echo $field['is_required'] ? 'required' : ''; ?>>
<option value="">Select an option...</option>
<?php foreach (explode(',', $field['content']) as $opt): ?>
<option value="<?php echo trim($opt); ?>"><?php echo trim($opt); ?></option>
<?php endforeach; ?>
</select>
<?php elseif ($field['type'] === 'download'): ?>
<div class="d-flex align-items-center p-3 border rounded-3 bg-light">
<i class="bi bi-file-earmark-pdf display-6 text-danger me-3"></i>
<div class="flex-grow-1">
<div class="fw-bold"><?php echo htmlspecialchars($field['label']); ?></div>
<small class="text-muted">Please download and review this document.</small>
</div>
<a href="<?php echo htmlspecialchars($field['content']); ?>" target="_blank" class="btn btn-primary btn-sm"><i class="bi bi-download"></i> Download</a>
</div>
<?php elseif ($field['type'] === 'upload'): ?>
<label class="form-label fw-bold"><?php echo htmlspecialchars($field['label']); ?><?php echo $field['is_required'] ? ' <span class="text-danger">*</span>' : ''; ?></label>
<input type="file" class="form-control" <?php echo $field['is_required'] ? 'required' : ''; ?>>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
<div class="mt-5 d-flex justify-content-between">
<?php if ($step_index > 0): ?>
<a href="customer_view.php?id=<?php echo $customer_id; ?>&step=<?php echo $step_index - 1; ?>" class="btn btn-outline-secondary btn-lg px-5">Back</a>
<?php else: ?>
<div></div>
<?php endif; ?>
<?php if ($is_last_step): ?>
<button type="submit" class="btn btn-success btn-lg px-5 fw-bold">Finish Onboarding</button>
<?php else: ?>
<button type="submit" class="btn btn-primary btn-lg px-5 fw-bold">Next Step</button>
<?php endif; ?>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<style>
body { background-color: #f8fafc; }
.card { border-radius: 1rem; }
</style>
<?php include 'footer.php'; ?>

View File

@ -1,83 +1,97 @@
<?php
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
require_once __DIR__ . '/header.php';
$msp_id = $_SESSION['msp_id'];
$customers = [];
try {
$stmt = db()->query("SELECT * FROM customers ORDER BY name ASC");
$stmt = db()->prepare("SELECT * FROM customers WHERE msp_id = ? ORDER BY name ASC");
$stmt->execute([$msp_id]);
$customers = $stmt->fetchAll();
} catch (PDOException $e) {
// Handle error
}
?>
<div class="container">
<div style="display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 2rem;">
<div class="container py-5">
<div class="d-flex justify-content-between align-items-end mb-4">
<div>
<h2 style="font-size: 1.5rem; font-weight: 700; margin-bottom: 0.5rem;">Managed Customers</h2>
<p style="color: var(--text-muted); font-size: 0.875rem;">List of all customers and their current status.</p>
<h1>Managed Customers</h1>
<p class="text-muted">List of all customers and their current status.</p>
</div>
<a href="add_customer.php" class="btn btn-primary">
<i class="bi bi-person-plus" style="margin-right: 0.5rem;"></i> Add Customer
<i class="bi bi-person-plus"></i> Add Customer
</a>
</div>
<div class="card">
<table class="table">
<thead>
<tr>
<th>Customer Name</th>
<th>Email</th>
<th>Status</th>
<th>Onboarding Progress</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($customers)): ?>
<tr>
<td colspan="5" style="text-align: center; color: var(--text-muted); padding: 4rem;">
No customers found. <a href="add_customer.php" style="color: var(--primary);">Add your first customer.</a>
</td>
</tr>
<?php else: ?>
<?php foreach ($customers as $customer): ?>
<div class="card shadow-sm border-0">
<div class="card-body p-0">
<table class="table mb-0">
<thead class="bg-light">
<tr>
<td style="font-weight: 600;"><?php echo htmlspecialchars($customer['name']); ?></td>
<td><?php echo htmlspecialchars($customer['contact_email'] ?? 'N/A'); ?></td>
<td><span class="badge badge-<?php echo $customer['status']; ?>"><?php echo $customer['status']; ?></span></td>
<td>
<?php
$progress = 0;
try {
$stmt = db()->prepare("SELECT COUNT(*) as total, SUM(CASE WHEN is_completed = 1 THEN 1 ELSE 0 END) as completed FROM onboarding_tasks WHERE customer_id = ?");
$stmt->execute([$customer['id']]);
$row = $stmt->fetch();
if ($row && $row['total'] > 0) {
$progress = round(($row['completed'] / $row['total']) * 100);
}
} catch (PDOException $e) {}
?>
<div style="width: 100px; height: 8px; background: #e2e8f0; border-radius: 4px; overflow: hidden; display: inline-block; vertical-align: middle;">
<div style="width: <?php echo (int)$progress; ?>%; height: 100%; background: var(--success);"></div>
</div>
<span style="font-size: 0.75rem; margin-left: 0.5rem; font-weight: 600;"><?php echo (int)$progress; ?>%</span>
</td>
<td>
<div style="display: flex; gap: 0.5rem;">
<a href="onboarding.php?id=<?php echo $customer['id']; ?>" class="btn btn-outline" style="padding: 0.25rem 0.5rem;" title="View Onboarding">
<i class="bi bi-list-check"></i>
</a>
<a href="#" class="btn btn-outline" style="padding: 0.25rem 0.5rem;" title="Edit Details">
<i class="bi bi-pencil-square"></i>
</a>
</div>
<th class="ps-4 py-3">Customer Name</th>
<th class="py-3">Email</th>
<th class="py-3">Status</th>
<th class="py-3">Onboarding Progress</th>
<th class="pe-4 py-3">Actions</th>
</tr>
</thead>
<tbody>
<?php if (empty($customers)): ?>
<tr>
<td colspan="5" class="text-center text-muted py-5">
No customers found. <a href="add_customer.php" class="text-primary fw-bold">Add your first customer.</a>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
<?php else: ?>
<?php foreach ($customers as $customer): ?>
<tr>
<td class="ps-4 fw-bold"><?php echo htmlspecialchars($customer['name']); ?></td>
<td><?php echo htmlspecialchars($customer['contact_email'] ?? 'N/A'); ?></td>
<td>
<span class="badge <?php echo $customer['status'] == 'active' ? 'bg-success-subtle text-success' : 'bg-primary-subtle text-primary'; ?> rounded-pill">
<?php echo ucfirst($customer['status']); ?>
</span>
</td>
<td>
<?php
$progress = 0;
try {
$stmt = db()->prepare("SELECT COUNT(*) as total, SUM(CASE WHEN is_completed = 1 THEN 1 ELSE 0 END) as completed FROM onboarding_tasks WHERE customer_id = ?");
$stmt->execute([$customer['id']]);
$row = $stmt->fetch();
if ($row && $row['total'] > 0) {
$progress = round(($row['completed'] / $row['total']) * 100);
}
} catch (PDOException $e) {}
?>
<div class="progress" style="height: 8px; width: 120px; display: inline-flex;">
<div class="progress-bar bg-success" role="progressbar" style="width: <?php echo (int)$progress; ?>%;"></div>
</div>
<span class="small ms-2 fw-bold text-muted"><?php echo (int)$progress; ?>%</span>
</td>
<td class="pe-4">
<div class="btn-group">
<a href="onboarding.php?id=<?php echo $customer['id']; ?>" class="btn btn-outline-primary btn-sm" title="View Onboarding">
<i class="bi bi-list-check"></i>
</a>
<a href="#" class="btn btn-outline-secondary btn-sm" title="Edit Details">
<i class="bi bi-pencil-square"></i>
</a>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php require_once __DIR__ . '/footer.php'; ?>
<?php require_once __DIR__ . '/footer.php'; ?>

79
db/migrations_002.sql Normal file
View File

@ -0,0 +1,79 @@
-- Plans table
CREATE TABLE IF NOT EXISTS plans (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price DECIMAL(10, 2) DEFAULT 0.00,
max_customers INT DEFAULT 10,
can_custom_domain BOOLEAN DEFAULT FALSE,
is_trial BOOLEAN DEFAULT FALSE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Seed default plans
INSERT INTO plans (name, price, max_customers, can_custom_domain, is_trial) VALUES
('Free Trial', 0.00, 5, FALSE, TRUE),
('Pro', 49.00, 50, TRUE, FALSE),
('Enterprise', 199.00, 1000, TRUE, FALSE);
-- MSPs table (Tenants)
CREATE TABLE IF NOT EXISTS msps (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
plan_id INT NOT NULL,
subdomain VARCHAR(255) UNIQUE,
custom_domain VARCHAR(255) UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (plan_id) REFERENCES plans(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Users table
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
msp_id INT NULL, -- NULL for superadmins
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
role ENUM('superadmin', 'msp_admin', 'msp_staff') NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (msp_id) REFERENCES msps(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Onboarding Templates
CREATE TABLE IF NOT EXISTS onboarding_templates (
id INT AUTO_INCREMENT PRIMARY KEY,
msp_id INT NOT NULL,
name VARCHAR(255) NOT NULL,
description TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (msp_id) REFERENCES msps(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Onboarding Template Steps (Pages)
CREATE TABLE IF NOT EXISTS onboarding_template_steps (
id INT AUTO_INCREMENT PRIMARY KEY,
template_id INT NOT NULL,
title VARCHAR(255) NOT NULL,
description TEXT,
order_index INT DEFAULT 0,
FOREIGN KEY (template_id) REFERENCES onboarding_templates(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Onboarding Template Fields/Content
CREATE TABLE IF NOT EXISTS onboarding_template_fields (
id INT AUTO_INCREMENT PRIMARY KEY,
step_id INT NOT NULL,
type ENUM('text', 'form_input', 'form_textarea', 'form_select', 'download', 'upload') NOT NULL,
label VARCHAR(255) NOT NULL,
content TEXT, -- Used for 'text' or 'download' URL or 'options' for select
is_required BOOLEAN DEFAULT FALSE,
order_index INT DEFAULT 0,
FOREIGN KEY (step_id) REFERENCES onboarding_template_steps(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- Update customers table to link to MSP
ALTER TABLE customers ADD COLUMN msp_id INT NOT NULL AFTER id;
ALTER TABLE customers ADD CONSTRAINT fk_customer_msp FOREIGN KEY (msp_id) REFERENCES msps(id) ON DELETE CASCADE;
-- Create a default superadmin (password: admin123)
-- In a real app, this would be handled via a setup script or CLI.
INSERT INTO users (name, email, password_hash, role) VALUES
('Super Admin', 'admin@msp-portal.com', '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'superadmin');

View File

@ -2,9 +2,14 @@
require_once __DIR__ . '/config.php';
try {
$sql = file_get_contents(__DIR__ . '/migrations_001.sql');
db()->exec($sql);
$migrations = glob(__DIR__ . '/migrations_*.sql');
sort($migrations);
foreach ($migrations as $migration) {
$sql = file_get_contents($migration);
db()->exec($sql);
echo "Executed migration: " . basename($migration) . "\n";
}
echo "Database setup successful.";
} catch (PDOException $e) {
echo "Error setting up database: " . $e->getMessage();
}
}

249
edit_template.php Normal file
View File

@ -0,0 +1,249 @@
<?php
session_start();
if (!isset($_SESSION['user_id']) || !isset($_SESSION['msp_id'])) {
header('Location: login.php');
exit;
}
require_once 'db/config.php';
$msp_id = $_SESSION['msp_id'];
$template_id = $_GET['id'] ?? null;
if (!$template_id) {
header('Location: templates.php');
exit;
}
// Security: Check if template belongs to the current MSP
$stmt = db()->prepare("SELECT * FROM onboarding_templates WHERE id = ? AND msp_id = ?");
$stmt->execute([$template_id, $msp_id]);
$template = $stmt->fetch();
if (!$template) {
header('Location: templates.php');
exit;
}
// Handle Form Submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
if ($_POST['action'] === 'add_step') {
$title = $_POST['title'];
$description = $_POST['description'];
$stmt = db()->prepare("SELECT MAX(order_index) FROM onboarding_template_steps WHERE template_id = ?");
$stmt->execute([$template_id]);
$max_order = $stmt->fetchColumn() ?: 0;
$stmt = db()->prepare("INSERT INTO onboarding_template_steps (template_id, title, description, order_index) VALUES (?, ?, ?, ?)");
$stmt->execute([$template_id, $title, $description, $max_order + 1]);
header("Location: edit_template.php?id=$template_id");
exit;
}
if ($_POST['action'] === 'add_field') {
$step_id = $_POST['step_id'];
$type = $_POST['type'];
$label = $_POST['label'];
$content = $_POST['content'] ?? '';
$is_required = isset($_POST['is_required']) ? 1 : 0;
$stmt = db()->prepare("SELECT MAX(order_index) FROM onboarding_template_fields WHERE step_id = ?");
$stmt->execute([$step_id]);
$max_order = $stmt->fetchColumn() ?: 0;
$stmt = db()->prepare("INSERT INTO onboarding_template_fields (step_id, type, label, content, is_required, order_index) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->execute([$step_id, $type, $label, $content, $is_required, $max_order + 1]);
header("Location: edit_template.php?id=$template_id");
exit;
}
if ($_POST['action'] === 'delete_step') {
$step_id = $_POST['step_id'];
$stmt = db()->prepare("DELETE FROM onboarding_template_steps WHERE id = ? AND template_id = ?");
$stmt->execute([$step_id, $template_id]);
header("Location: edit_template.php?id=$template_id");
exit;
}
}
// Fetch all steps and their fields
$stmt = db()->prepare("SELECT * FROM onboarding_template_steps WHERE template_id = ? ORDER BY order_index ASC");
$stmt->execute([$template_id]);
$steps = $stmt->fetchAll();
foreach ($steps as &$step) {
$stmt = db()->prepare("SELECT * FROM onboarding_template_fields WHERE step_id = ? ORDER BY order_index ASC");
$stmt->execute([$step['id']]);
$step['fields'] = $stmt->fetchAll();
}
include 'header.php';
?>
<div class="container py-5">
<div class="mb-4">
<a href="templates.php" class="btn btn-outline-secondary btn-sm mb-3"><i class="bi bi-arrow-left"></i> Back to Templates</a>
<div class="d-flex justify-content-between align-items-end">
<div>
<h1 class="mb-1">Editing Template: <?php echo htmlspecialchars($template['name']); ?></h1>
<p class="text-muted"><?php echo htmlspecialchars($template['description']); ?></p>
</div>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addStepModal">
<i class="bi bi-plus-circle"></i> Add Step/Page
</button>
</div>
</div>
<?php if (empty($steps)): ?>
<div class="alert alert-info py-5 text-center">
<h4 class="mb-3">This template has no steps yet.</h4>
<p class="text-muted">Start by adding a new page to your onboarding flow.</p>
<button type="button" class="btn btn-primary mt-3" data-bs-toggle="modal" data-bs-target="#addStepModal">
<i class="bi bi-plus-circle"></i> Add First Step
</button>
</div>
<?php endif; ?>
<div class="onboarding-flow mt-5">
<?php foreach ($steps as $index => $step): ?>
<div class="card shadow-sm border-0 mb-5">
<div class="card-header bg-white p-4 border-bottom-0 d-flex justify-content-between align-items-center">
<h3 class="fw-bold mb-0">Step <?php echo $index + 1; ?>: <?php echo htmlspecialchars($step['title']); ?></h3>
<div>
<button type="button" class="btn btn-outline-primary btn-sm me-2" data-bs-toggle="modal" data-bs-target="#addFieldModal<?php echo $step['id']; ?>">
<i class="bi bi-plus-lg"></i> Add Item
</button>
<form method="POST" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this entire step?');">
<input type="hidden" name="action" value="delete_step">
<input type="hidden" name="step_id" value="<?php echo $step['id']; ?>">
<button type="submit" class="btn btn-outline-danger btn-sm"><i class="bi bi-trash"></i></button>
</form>
</div>
</div>
<div class="card-body p-4 pt-0">
<p class="text-muted mb-4"><?php echo htmlspecialchars($step['description']); ?></p>
<div class="list-group list-group-flush border rounded">
<?php foreach ($step['fields'] as $field): ?>
<div class="list-group-item p-3">
<div class="d-flex justify-content-between align-items-center">
<div>
<span class="badge bg-light text-dark border me-2"><?php echo strtoupper(str_replace('_', ' ', $field['type'])); ?></span>
<span class="fw-bold"><?php echo htmlspecialchars($field['label']); ?></span>
<?php if ($field['is_required']): ?>
<span class="text-danger">*</span>
<?php endif; ?>
</div>
<div>
<button class="btn btn-link btn-sm p-0 text-muted me-2"><i class="bi bi-pencil"></i></button>
<button class="btn btn-link btn-sm p-0 text-danger"><i class="bi bi-x-lg"></i></button>
</div>
</div>
<?php if ($field['type'] === 'text'): ?>
<div class="mt-2 small text-muted border-start ps-3 py-2 italic"><?php echo nl2br(htmlspecialchars($field['content'])); ?></div>
<?php elseif ($field['type'] === 'download'): ?>
<div class="mt-2"><a href="<?php echo htmlspecialchars($field['content']); ?>" target="_blank" class="btn btn-light btn-sm"><i class="bi bi-download"></i> Download Resource</a></div>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php if (empty($step['fields'])): ?>
<div class="list-group-item p-5 text-center text-muted italic">
No items added to this step yet.
</div>
<?php endif; ?>
</div>
</div>
</div>
<!-- Add Field Modal for this step -->
<div class="modal fade" id="addFieldModal<?php echo $step['id']; ?>" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<div class="modal-header">
<h5 class="modal-title">Add Item to Step: <?php echo htmlspecialchars($step['title']); ?></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" name="action" value="add_field">
<input type="hidden" name="step_id" value="<?php echo $step['id']; ?>">
<div class="mb-3">
<label class="form-label">Type</label>
<select name="type" class="form-select" required onchange="toggleFieldOptions(this, 'options_<?php echo $step['id']; ?>')">
<option value="text">Informational Text (HTML Allowed)</option>
<option value="form_input">Form Input (Single Line)</option>
<option value="form_textarea">Form Textarea (Multi-line)</option>
<option value="form_select">Dropdown Menu</option>
<option value="download">Download Link/File</option>
<option value="upload">Customer Upload Field</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">Label/Title</label>
<input type="text" name="label" class="form-control" placeholder="e.g. Please enter your VAT number" required>
</div>
<div id="options_<?php echo $step['id']; ?>">
<div class="mb-3">
<label class="form-label">Content/Options</label>
<textarea name="content" class="form-control" rows="3" placeholder="Enter text content, download URL, or comma-separated options for dropdown."></textarea>
</div>
</div>
<div class="mb-3 form-check">
<input type="checkbox" name="is_required" class="form-check-input" id="req_<?php echo $step['id']; ?>">
<label class="form-check-label" for="req_<?php echo $step['id']; ?>">Is this field required?</label>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Add Item</button>
</div>
</form>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<!-- Add Step Modal -->
<div class="modal fade" id="addStepModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<div class="modal-header">
<h5 class="modal-title">Add New Page/Step</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" name="action" value="add_step">
<div class="mb-3">
<label class="form-label">Step Title</label>
<input type="text" name="title" class="form-control" placeholder="e.g. Company Details" required>
</div>
<div class="mb-3">
<label class="form-label">Description/Instruction</label>
<textarea name="description" class="form-control" rows="2" placeholder="Tell the customer what this step is about."></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Add Step</button>
</div>
</form>
</div>
</div>
</div>
<script>
function toggleFieldOptions(select, optionsId) {
// We could hide/show specific inputs based on type here
}
</script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<?php include 'footer.php'; ?>

View File

@ -1,7 +1,17 @@
<?php
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
require_once __DIR__ . '/db/config.php';
$current_page = basename($_SERVER['PHP_SELF']);
// Auth Check for most pages (excluding login/register)
$public_pages = ['login.php', 'register.php'];
if (!isset($_SESSION['user_id']) && !in_array($current_page, $public_pages)) {
header('Location: login.php');
exit;
}
// Read project preview data from environment
$projectDescription = $_SERVER['PROJECT_DESCRIPTION'] ?? 'MSP Customer Success Platform for onboarding and management.';
$projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
@ -14,17 +24,12 @@ $projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
<title>MSP Connect | Customer Success Platform</title>
<?php if ($projectDescription): ?>
<!-- Meta description -->
<meta name="description" content='<?= htmlspecialchars($projectDescription) ?>' />
<!-- Open Graph meta tags -->
<meta property="og:description" content="<?= htmlspecialchars($projectDescription) ?>" />
<!-- Twitter meta tags -->
<meta property="twitter:description" content="<?= htmlspecialchars($projectDescription) ?>" />
<?php endif; ?>
<?php if ($projectImageUrl): ?>
<!-- Open Graph image -->
<meta property="og:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
<!-- Twitter image -->
<meta property="twitter:image" content="<?= htmlspecialchars($projectImageUrl) ?>" />
<?php endif; ?>
@ -32,7 +37,6 @@ $projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<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=<?php echo time(); ?>">
<!-- Bootstrap Icons -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
</head>
<body>
@ -41,14 +45,29 @@ $projectImageUrl = $_SERVER['PROJECT_IMAGE_URL'] ?? '';
<i class="bi bi-shield-check"></i> MSP Connect
</a>
<div class="nav-links">
<a href="index.php" class="nav-link <?php echo $current_page == 'index.php' ? 'active' : ''; ?>">Dashboard</a>
<a href="customers.php" class="nav-link <?php echo $current_page == 'customers.php' ? 'active' : ''; ?>">Customers</a>
<a href="#" class="nav-link">QBRs</a>
<a href="#" class="nav-link">Tickets</a>
<a href="#" class="nav-link">Integrations</a>
<?php if (isset($_SESSION['user_id'])): ?>
<?php if ($_SESSION['role'] === 'superadmin'): ?>
<a href="superadmin_dashboard.php" class="nav-link <?php echo $current_page == 'superadmin_dashboard.php' ? 'active' : ''; ?>">Superadmin</a>
<a href="manage_msps.php" class="nav-link <?php echo $current_page == 'manage_msps.php' ? 'active' : ''; ?>">MSPs</a>
<a href="plans.php" class="nav-link <?php echo $current_page == 'plans.php' ? 'active' : ''; ?>">Plans</a>
<?php else: ?>
<a href="index.php" class="nav-link <?php echo $current_page == 'index.php' ? 'active' : ''; ?>">Dashboard</a>
<a href="customers.php" class="nav-link <?php echo $current_page == 'customers.php' ? 'active' : ''; ?>">Customers</a>
<a href="templates.php" class="nav-link <?php echo $current_page == 'templates.php' ? 'active' : ''; ?>">Templates</a>
<a href="#" class="nav-link">QBRs</a>
<?php endif; ?>
<?php endif; ?>
</div>
<div>
<span style="font-size: 0.875rem; color: var(--text-muted); margin-right: 1rem;">Admin</span>
<a href="#" class="btn btn-outline" style="padding: 0.25rem 0.5rem;"><i class="bi bi-person-circle"></i></a>
<?php if (isset($_SESSION['user_id'])): ?>
<span style="font-size: 0.875rem; color: var(--text-muted); margin-right: 1rem;">
<?php echo htmlspecialchars($_SESSION['user_name']); ?>
(<?php echo strtoupper($_SESSION['role']); ?>)
</span>
<a href="logout.php" class="btn btn-outline" style="padding: 0.25rem 0.5rem;" title="Logout"><i class="bi bi-box-arrow-right"></i></a>
<?php else: ?>
<a href="login.php" class="nav-link d-inline-block">Login</a>
<a href="register.php" class="btn btn-primary btn-sm ms-2">Register MSP</a>
<?php endif; ?>
</div>
</nav>
</nav>

195
index.php
View File

@ -1,138 +1,109 @@
<?php
require_once __DIR__ . '/header.php';
// Fetch some stats
$stats = [
'total_customers' => 0,
'active_onboarding' => 0,
'qbrs_scheduled' => 4,
'open_tickets' => 12
];
try {
$stmt = db()->query("SELECT COUNT(*) FROM customers");
$stats['total_customers'] = $stmt->fetchColumn();
$stmt = db()->query("SELECT COUNT(*) FROM customers WHERE status = 'onboarding'");
$stats['active_onboarding'] = $stmt->fetchColumn();
} catch (PDOException $e) {
// Handle error gracefully
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
// Fetch recent customers
$recent_customers = [];
try {
$stmt = db()->query("SELECT * FROM customers ORDER BY created_at DESC LIMIT 5");
$recent_customers = $stmt->fetchAll();
} catch (PDOException $e) {
// Handle error
require_once 'db/config.php';
// If superadmin, redirect to superadmin dashboard
if ($_SESSION['role'] === 'superadmin') {
header('Location: superadmin_dashboard.php');
exit;
}
$msp_id = $_SESSION['msp_id'];
// Stats for current MSP
$total_customers = db()->prepare("SELECT COUNT(*) FROM customers WHERE msp_id = ?");
$total_customers->execute([$msp_id]);
$total_customers = $total_customers->fetchColumn();
$onboarding_count = db()->prepare("SELECT COUNT(*) FROM customers WHERE msp_id = ? AND status = 'onboarding'");
$onboarding_count->execute([$msp_id]);
$onboarding_count = $onboarding_count->fetchColumn();
$active_count = db()->prepare("SELECT COUNT(*) FROM customers WHERE msp_id = ? AND status = 'active'");
$active_count->execute([$msp_id]);
$active_count = $active_count->fetchColumn();
// Recent Customers
$stmt = db()->prepare("SELECT * FROM customers WHERE msp_id = ? ORDER BY created_at DESC LIMIT 5");
$stmt->execute([$msp_id]);
$recent_customers = $stmt->fetchAll();
include 'header.php';
?>
<div class="container">
<div style="margin-bottom: 2rem;">
<h2 style="font-size: 1.5rem; font-weight: 700; margin-bottom: 0.5rem;">MSP Operations Dashboard</h2>
<p style="color: var(--text-muted); font-size: 0.875rem;">Welcome back, Admin. Here's what's happening today.</p>
<div class="container py-5">
<div class="header-section">
<h1>Dashboard</h1>
<p class="text-muted">Welcome back, <?php echo htmlspecialchars($_SESSION['user_name']); ?>. Here's what's happening with your clients.</p>
</div>
<div class="stats-grid">
<div class="stat-card">
<div class="stat-label">Total Customers</div>
<div class="stat-value"><?php echo (int)$stats['total_customers']; ?></div>
<div class="stat-value"><?php echo $total_customers; ?></div>
<div class="stat-label">Total Clients</div>
</div>
<div class="stat-card">
<div class="stat-label">Active Onboarding</div>
<div class="stat-value" style="color: var(--primary);"><?php echo (int)$stats['active_onboarding']; ?></div>
<div class="stat-value text-primary"><?php echo $onboarding_count; ?></div>
<div class="stat-label">In Onboarding</div>
</div>
<div class="stat-card">
<div class="stat-label">QBRs This Quarter</div>
<div class="stat-value"><?php echo (int)$stats['qbrs_scheduled']; ?></div>
</div>
<div class="stat-card">
<div class="stat-label">Open Support Tickets</div>
<div class="stat-value" style="color: var(--danger);"><?php echo (int)$stats['open_tickets']; ?></div>
<div class="stat-value text-success"><?php echo $active_count; ?></div>
<div class="stat-label">Active Managed</div>
</div>
</div>
<div class="card">
<div class="card-title">
<span>Recent Customers</span>
<a href="customers.php" class="btn btn-outline">View All</a>
</div>
<table class="table">
<thead>
<tr>
<th>Customer Name</th>
<th>Email Address</th>
<th>Status</th>
<th>Added On</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php if (empty($recent_customers)): ?>
<tr>
<td colspan="5" style="text-align: center; color: var(--text-muted); padding: 2rem;">
<i class="bi bi-inbox" style="font-size: 2rem; display: block; margin-bottom: 1rem;"></i>
No customers added yet. <a href="add_customer.php" style="color: var(--primary);">Add your first customer.</a>
</td>
</tr>
<?php else: ?>
<?php foreach ($recent_customers as $customer): ?>
<tr>
<td style="font-weight: 600;"><?php echo htmlspecialchars($customer['name']); ?></td>
<td><?php echo htmlspecialchars($customer['contact_email'] ?? 'N/A'); ?></td>
<td><span class="badge badge-<?php echo $customer['status']; ?>"><?php echo $customer['status']; ?></span></td>
<td><?php echo date('M d, Y', strtotime($customer['created_at'])); ?></td>
<td>
<?php if ($customer['status'] == 'onboarding'): ?>
<a href="onboarding.php?id=<?php echo $customer['id']; ?>" class="btn btn-outline" style="padding: 0.25rem 0.5rem;">
Onboarding
</a>
<?php else: ?>
<a href="#" class="btn btn-outline" style="padding: 0.25rem 0.5rem;">Details</a>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1.5rem;">
<div class="card">
<div class="card-title">Quick Actions</div>
<div style="display: flex; flex-direction: column; gap: 0.75rem;">
<a href="add_customer.php" class="btn btn-primary" style="justify-content: flex-start;">
<i class="bi bi-person-plus" style="margin-right: 0.75rem;"></i> Onboard New Customer
</a>
<button class="btn btn-outline" style="justify-content: flex-start;">
<i class="bi bi-calendar-event" style="margin-right: 0.75rem;"></i> Schedule QBR Meeting
</button>
<button class="btn btn-outline" style="justify-content: flex-start;">
<i class="bi bi-ticket-perforated" style="margin-right: 0.75rem;"></i> Create Internal Ticket
</button>
<div class="row mt-5">
<div class="col-md-8">
<div class="card shadow-sm border-0 mb-4">
<div class="card-body p-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h3 class="fw-bold mb-0">Recent Customers</h3>
<a href="customers.php" class="btn btn-outline btn-sm">View All</a>
</div>
<div class="list-group list-group-flush">
<?php foreach ($recent_customers as $customer): ?>
<a href="onboarding.php?id=<?php echo $customer['id']; ?>" class="list-group-item list-group-item-action border-0 px-0 py-3 d-flex justify-content-between align-items-center">
<div>
<div class="fw-bold text-primary"><?php echo htmlspecialchars($customer['name']); ?></div>
<small class="text-muted"><?php echo htmlspecialchars($customer['contact_email']); ?></small>
</div>
<span class="badge <?php echo $customer['status'] == 'active' ? 'bg-success-subtle text-success' : 'bg-primary-subtle text-primary'; ?> rounded-pill">
<?php echo ucfirst($customer['status']); ?>
</span>
</a>
<?php endforeach; ?>
<?php if (empty($recent_customers)): ?>
<p class="text-muted text-center py-4">No customers yet. <a href="add_customer.php">Add your first one!</a></p>
<?php endif; ?>
</div>
</div>
</div>
</div>
<div class="card">
<div class="card-title">Integration Status</div>
<div style="display: flex; flex-direction: column; gap: 0.75rem;">
<div style="display: flex; justify-content: space-between; align-items: center; font-size: 0.875rem;">
<span>ConnectWise PSA</span>
<span style="color: var(--success);"><i class="bi bi-check-circle-fill"></i> Connected</span>
<div class="col-md-4">
<div class="card shadow-sm border-0 mb-4 bg-primary text-white">
<div class="card-body p-4">
<h4 class="fw-bold mb-3">Onboarding Help</h4>
<p class="small opacity-75">Need to streamline your client intake? Use our template builder to create custom onboarding flows.</p>
<a href="templates.php" class="btn btn-light btn-sm fw-bold">Manage Templates</a>
</div>
<div style="display: flex; justify-content: space-between; align-items: center; font-size: 0.875rem;">
<span>Microsoft 365</span>
<span style="color: var(--success);"><i class="bi bi-check-circle-fill"></i> Connected</span>
</div>
<div style="display: flex; justify-content: space-between; align-items: center; font-size: 0.875rem;">
<span>Datto RMM</span>
<span style="color: var(--warning);"><i class="bi bi-exclamation-triangle"></i> Configure</span>
</div>
<div class="card shadow-sm border-0">
<div class="card-body p-4">
<h4 class="fw-bold mb-3">System Updates</h4>
<div class="small">
<div class="mb-2"><strong>v2.0</strong> - Multi-tenancy & Templates <span class="text-muted float-end">Mar 1</span></div>
<div class="mb-2"><strong>v1.5</strong> - QBR Support <span class="text-muted float-end">Feb 20</span></div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php require_once __DIR__ . '/footer.php'; ?>
<?php include 'footer.php'; ?>

72
login.php Normal file
View File

@ -0,0 +1,72 @@
<?php
session_start();
require_once 'db/config.php';
if (isset($_SESSION['user_id'])) {
header('Location: index.php');
exit;
}
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';
$stmt = db()->prepare("SELECT * FROM users WHERE email = ?");
$stmt->execute([$email]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password_hash'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['msp_id'] = $user['msp_id'];
$_SESSION['role'] = $user['role'];
$_SESSION['user_name'] = $user['name'];
header('Location: index.php');
exit;
} else {
$error = 'Invalid email or password.';
}
}
include 'header.php';
?>
<div class="container py-5 mt-5">
<div class="row justify-content-center">
<div class="col-md-5">
<div class="card shadow-sm border-0">
<div class="card-body p-5">
<div class="text-center mb-4">
<h2 class="fw-bold text-primary">MSP Portal Login</h2>
<p class="text-muted">Welcome back! Please enter your details.</p>
</div>
<?php if ($error): ?>
<div class="alert alert-danger mb-4"><?php echo $error; ?></div>
<?php endif; ?>
<form method="POST" action="">
<div class="mb-3">
<label for="email" class="form-label">Email Address</label>
<input type="email" name="email" id="email" class="form-control py-2" required>
</div>
<div class="mb-4">
<label for="password" class="form-label">Password</label>
<input type="password" name="password" id="password" class="form-control py-2" required>
</div>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary py-2 fw-bold">Sign In</button>
</div>
</form>
<div class="text-center mt-4">
<p class="mb-0 text-muted small">Don't have an account? <a href="register.php" class="text-primary fw-bold text-decoration-none">Register your MSP</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include 'footer.php'; ?>

5
logout.php Normal file
View File

@ -0,0 +1,5 @@
<?php
session_start();
session_destroy();
header('Location: login.php');
exit;

132
manage_msps.php Normal file
View File

@ -0,0 +1,132 @@
<?php
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'superadmin') {
header('Location: index.php');
exit;
}
require_once 'db/config.php';
$success = '';
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
if ($_POST['action'] === 'update_msp') {
$msp_id = $_POST['msp_id'];
$subdomain = $_POST['subdomain'];
$custom_domain = $_POST['custom_domain'];
$plan_id = $_POST['plan_id'];
try {
$stmt = db()->prepare("UPDATE msps SET subdomain = ?, custom_domain = ?, plan_id = ? WHERE id = ?");
$stmt->execute([$subdomain ?: null, $custom_domain ?: null, $plan_id, $msp_id]);
$success = "MSP configuration updated successfully.";
} catch (PDOException $e) {
$error = "Error updating MSP: " . $e->getMessage();
}
}
}
$stmt = db()->query("SELECT m.*, p.name as plan_name FROM msps m JOIN plans p ON m.plan_id = p.id ORDER BY m.name ASC");
$msps = $stmt->fetchAll();
$stmt = db()->query("SELECT * FROM plans");
$plans = $stmt->fetchAll();
include 'header.php';
?>
<div class="container py-5">
<div class="header-section">
<h1>Manage MSPs</h1>
<p class="text-muted">Manage tenant subdomains, custom domains, and subscription plans.</p>
</div>
<?php if ($success): ?>
<div class="alert alert-success"><?php echo $success; ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<div class="card shadow-sm border-0">
<div class="card-body p-4">
<table class="table">
<thead>
<tr>
<th>MSP Name</th>
<th>Current Plan</th>
<th>Subdomain</th>
<th>Custom Domain</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($msps as $msp): ?>
<tr>
<td class="fw-bold"><?php echo htmlspecialchars($msp['name']); ?></td>
<td><span class="badge bg-light text-dark border"><?php echo htmlspecialchars($msp['plan_name']); ?></span></td>
<td><?php echo $msp['subdomain'] ? htmlspecialchars($msp['subdomain']) . '.mspconnect.com' : 'N/A'; ?></td>
<td><?php echo $msp['custom_domain'] ? htmlspecialchars($msp['custom_domain']) : 'N/A'; ?></td>
<td>
<button type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#editMspModal<?php echo $msp['id']; ?>">
<i class="bi bi-pencil-square"></i> Edit
</button>
</td>
</tr>
<!-- Edit MSP Modal -->
<div class="modal fade" id="editMspModal<?php echo $msp['id']; ?>" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<div class="modal-header">
<h5 class="modal-title">Configure MSP: <?php echo htmlspecialchars($msp['name']); ?></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" name="action" value="update_msp">
<input type="hidden" name="msp_id" value="<?php echo $msp['id']; ?>">
<div class="mb-3">
<label class="form-label">Subscription Plan</label>
<select name="plan_id" class="form-select">
<?php foreach ($plans as $plan): ?>
<option value="<?php echo $plan['id']; ?>" <?php echo $plan['id'] == $msp['plan_id'] ? 'selected' : ''; ?>>
<?php echo $plan['name']; ?>
</option>
<?php endforeach; ?>
</select>
</div>
<div class="mb-3">
<label class="form-label">Subdomain</label>
<div class="input-group">
<input type="text" name="subdomain" class="form-control" value="<?php echo htmlspecialchars($msp['subdomain'] ?? ''); ?>">
<span class="input-group-text">.mspconnect.com</span>
</div>
</div>
<div class="mb-3">
<label class="form-label">Custom Domain</label>
<input type="text" name="custom_domain" class="form-control" placeholder="e.g. portal.acme.com" value="<?php echo htmlspecialchars($msp['custom_domain'] ?? ''); ?>">
<small class="text-muted">Only available for Pro and Enterprise plans.</small>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save Changes</button>
</div>
</form>
</div>
</div>
</div>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<?php include 'footer.php'; ?>

View File

@ -1,163 +1,107 @@
<?php
require_once __DIR__ . '/header.php';
session_start();
if (!isset($_SESSION['user_id'])) {
header('Location: login.php');
exit;
}
require_once 'db/config.php';
$msp_id = $_SESSION['msp_id'];
$customer_id = $_GET['id'] ?? null;
if (!$customer_id) {
echo "<div class='container' style='text-align: center; margin-top: 5rem;'><h3 style='color: var(--danger);'>Customer Not Found</h3><a href='customers.php' class='btn btn-outline'>Back to Customer List</a></div>";
require_once __DIR__ . '/footer.php';
header('Location: customers.php');
exit;
}
// Fetch customer
$customer = null;
try {
$stmt = db()->prepare("SELECT * FROM customers WHERE id = ?");
$stmt->execute([$customer_id]);
$customer = $stmt->fetch();
} catch (PDOException $e) {}
// Security: Check if customer belongs to the current MSP
$stmt = db()->prepare("SELECT * FROM customers WHERE id = ? AND msp_id = ?");
$stmt->execute([$customer_id, $msp_id]);
$customer = $stmt->fetch();
if (!$customer) {
echo "<div class='container' style='text-align: center; margin-top: 5rem;'><h3 style='color: var(--danger);'>Customer Not Found</h3><a href='customers.php' class='btn btn-outline'>Back to Customer List</a></div>";
require_once __DIR__ . '/footer.php';
header('Location: customers.php');
exit;
}
// Fetch tasks
$tasks = [];
try {
$stmt = db()->prepare("SELECT * FROM onboarding_tasks WHERE customer_id = ? ORDER BY id ASC");
$stmt->execute([$customer_id]);
$tasks = $stmt->fetchAll();
} catch (PDOException $e) {}
// Fetch tasks for the customer
$stmt = db()->prepare("SELECT * FROM onboarding_tasks WHERE customer_id = ? ORDER BY id ASC");
$stmt->execute([$customer_id]);
$tasks = $stmt->fetchAll();
// Calculate progress
$total_tasks = count($tasks);
$completed_tasks = count(array_filter($tasks, function($t) { return $t['is_completed']; }));
$progress = $total_tasks > 0 ? round(($completed_tasks / $total_tasks) * 100) : 0;
include 'header.php';
?>
<div class="container" style="max-width: 800px;">
<div style="display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 2rem;">
<div class="container py-5">
<div class="d-flex justify-content-between align-items-end mb-4">
<div>
<h2 style="font-size: 1.5rem; font-weight: 700; margin-bottom: 0.5rem;">Onboarding Checklist: <?php echo htmlspecialchars($customer['name']); ?></h2>
<p style="color: var(--text-muted); font-size: 0.875rem;">Manage and track setup tasks for this customer.</p>
<h1>Onboarding: <?php echo htmlspecialchars($customer['name']); ?></h1>
<p class="text-muted">Track and manage the setup process for this client.</p>
</div>
<a href="customers.php" class="btn btn-outline">
<i class="bi bi-arrow-left" style="margin-right: 0.5rem;"></i> Back to List
</a>
</div>
<div class="stats-grid">
<div class="stat-card" style="grid-column: span 1 / -1;">
<div style="display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 0.5rem;">
<div class="stat-label">Overall Onboarding Progress</div>
<div class="stat-value" style="font-size: 1.25rem;"><?php echo (int)$progress; ?>%</div>
</div>
<div style="width: 100%; height: 12px; background: #e2e8f0; border-radius: 6px; overflow: hidden;">
<div id="progress-bar" style="width: <?php echo (int)$progress; ?>%; height: 100%; background: var(--success); transition: width 0.3s ease;"></div>
</div>
</div>
</div>
<div class="card">
<div class="card-title">Tasks & Milestones</div>
<div id="tasks-list">
<?php foreach ($tasks as $task): ?>
<div class="checklist-item <?php echo $task['is_completed'] ? 'completed' : ''; ?>" data-id="<?php echo $task['id']; ?>">
<input type="checkbox" class="task-checkbox" <?php echo $task['is_completed'] ? 'checked' : ''; ?> data-task-id="<?php echo $task['id']; ?>">
<span style="flex: 1; font-weight: 500;"><?php echo htmlspecialchars($task['task_name']); ?></span>
<?php if ($task['is_completed']): ?>
<span style="font-size: 0.75rem; color: var(--text-muted);"><?php echo date('M d, Y', strtotime($task['updated_at'])); ?></span>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
</div>
<div style="display: flex; gap: 1rem; margin-top: 2rem;">
<?php if ($progress === 100 && $customer['status'] === 'onboarding'): ?>
<button id="mark-active" class="btn btn-primary" style="flex: 1;" data-id="<?php echo $customer['id']; ?>">
<i class="bi bi-person-check" style="margin-right: 0.75rem;"></i> Complete Onboarding & Mark Active
<div class="d-flex gap-2">
<a href="customer_view.php?id=<?php echo $customer_id; ?>" class="btn btn-outline-primary" target="_blank">
<i class="bi bi-eye"></i> Customer View
</a>
<button class="btn btn-success" onclick="updateCustomerStatus(<?php echo $customer_id; ?>, 'active')">
<i class="bi bi-check-all"></i> Mark as Active
</button>
<?php endif; ?>
<button class="btn btn-outline" style="flex: 1;" onclick="window.print()">
<i class="bi bi-printer" style="margin-right: 0.75rem;"></i> Print Summary
</button>
</div>
</div>
<div class="row g-4">
<div class="col-md-8">
<div class="card shadow-sm border-0">
<div class="card-body p-4">
<h4 class="fw-bold mb-4">Onboarding Checklist</h4>
<div class="list-group list-group-flush">
<?php foreach ($tasks as $task): ?>
<div class="list-group-item px-0 py-3 border-0 d-flex align-items-center">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="task_<?php echo $task['id']; ?>"
<?php echo $task['is_completed'] ? 'checked' : ''; ?>
onchange="updateTask(<?php echo $task['id']; ?>, this.checked)">
<label class="form-check-label ms-3 <?php echo $task['is_completed'] ? 'text-decoration-line-through text-muted' : 'fw-bold'; ?>" for="task_<?php echo $task['id']; ?>">
<?php echo htmlspecialchars($task['name']); ?>
</label>
</div>
</div>
<?php endforeach; ?>
<?php if (empty($tasks)): ?>
<div class="text-center py-5 text-muted">
<i class="bi bi-clipboard-x display-4"></i>
<p class="mt-2">No tasks assigned to this customer.</p>
</div>
<?php endif; ?>
</div>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card shadow-sm border-0 mb-4">
<div class="card-body p-4">
<h5 class="fw-bold mb-3">Customer Information</h5>
<div class="mb-3">
<label class="small text-muted d-block">Contact Email</label>
<span class="fw-bold"><?php echo htmlspecialchars($customer['contact_email']); ?></span>
</div>
<div class="mb-3">
<label class="small text-muted d-block">Onboarding Status</label>
<span class="badge <?php echo $customer['status'] == 'active' ? 'bg-success' : 'bg-primary'; ?> rounded-pill">
<?php echo ucfirst($customer['status']); ?>
</span>
</div>
<div class="mb-0">
<label class="small text-muted d-block">Started On</label>
<span class="fw-bold"><?php echo date('M d, Y', strtotime($customer['created_at'])); ?></span>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const checkboxes = document.querySelectorAll('.task-checkbox');
const progressBar = document.getElementById('progress-bar');
const progressText = document.querySelector('.stat-value');
checkboxes.forEach(checkbox => {
checkbox.addEventListener('change', function() {
const taskId = this.dataset.taskId;
const completed = this.checked ? 1 : 0;
const item = this.closest('.checklist-item');
if (completed) {
item.classList.add('completed');
} else {
item.classList.remove('completed');
}
// Update database via AJAX
fetch('api/update_task.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `id=${taskId}&completed=${completed}`
})
.then(response => response.json())
.then(data => {
if (data.success) {
// Update progress bar
const total = checkboxes.length;
const completedCount = document.querySelectorAll('.task-checkbox:checked').length;
const newProgress = Math.round((completedCount / total) * 100);
progressBar.style.width = `${newProgress}%`;
progressText.textContent = `${newProgress}%`;
// Show toast if 100%
if (newProgress === 100) {
alert('Onboarding checklist complete! You can now mark this customer as Active.');
location.reload(); // To show the "Mark Active" button
}
} else {
alert('Error updating task: ' + data.message);
}
});
});
});
const markActiveBtn = document.getElementById('mark-active');
if (markActiveBtn) {
markActiveBtn.addEventListener('click', function() {
const customerId = this.dataset.id;
fetch('api/update_customer_status.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: `id=${customerId}&status=active`
})
.then(response => response.json())
.then(data => {
if (data.success) {
alert('Customer is now active!');
window.location.href = 'customers.php';
} else {
alert('Error: ' + data.message);
}
});
});
}
});
</script>
<?php require_once __DIR__ . '/footer.php'; ?>
<script src="assets/js/main.js"></script>
<?php include 'footer.php'; ?>

131
register.php Normal file
View File

@ -0,0 +1,131 @@
<?php
session_start();
require_once 'db/config.php';
if (isset($_SESSION['user_id'])) {
header('Location: index.php');
exit;
}
$stmt = db()->query("SELECT * FROM plans");
$plans = $stmt->fetchAll();
$error = '';
$success = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$msp_name = $_POST['msp_name'] ?? '';
$plan_id = $_POST['plan_id'] ?? '';
$user_name = $_POST['user_name'] ?? '';
$email = $_POST['email'] ?? '';
$password = $_POST['password'] ?? '';
$confirm_password = $_POST['confirm_password'] ?? '';
if ($password !== $confirm_password) {
$error = 'Passwords do not match.';
} else {
try {
db()->beginTransaction();
$stmt = db()->prepare("INSERT INTO msps (name, plan_id) VALUES (?, ?)");
$stmt->execute([$msp_name, $plan_id]);
$msp_id = db()->lastInsertId();
$password_hash = password_hash($password, PASSWORD_DEFAULT);
$stmt = db()->prepare("INSERT INTO users (msp_id, name, email, password_hash, role) VALUES (?, ?, ?, ?, 'msp_admin')");
$stmt->execute([$msp_id, $user_name, $email, $password_hash]);
db()->commit();
$_SESSION['user_id'] = db()->lastInsertId();
$_SESSION['msp_id'] = $msp_id;
$_SESSION['role'] = 'msp_admin';
$_SESSION['user_name'] = $user_name;
header('Location: index.php');
exit;
} catch (PDOException $e) {
db()->rollBack();
if ($e->getCode() == 23000) {
$error = 'Email already registered.';
} else {
$error = 'Error during registration: ' . $e->getMessage();
}
}
}
}
include 'header.php';
?>
<div class="container py-5 mt-5">
<div class="row justify-content-center">
<div class="col-md-7">
<div class="card shadow-sm border-0">
<div class="card-body p-5">
<div class="text-center mb-4">
<h2 class="fw-bold text-primary">MSP Registration</h2>
<p class="text-muted">Start managing your customer onboarding today.</p>
</div>
<?php if ($error): ?>
<div class="alert alert-danger mb-4"><?php echo $error; ?></div>
<?php endif; ?>
<form method="POST" action="">
<div class="row">
<div class="col-md-6 mb-3">
<label for="msp_name" class="form-label">MSP Name</label>
<input type="text" name="msp_name" id="msp_name" class="form-control py-2" placeholder="e.g. Acme Managed Services" required>
</div>
<div class="col-md-6 mb-3">
<label for="plan_id" class="form-label">Select Plan</label>
<select name="plan_id" id="plan_id" class="form-select py-2" required>
<?php foreach ($plans as $plan): ?>
<option value="<?php echo $plan['id']; ?>">
<?php echo $plan['name']; ?> - <?php echo $plan['price'] > 0 ? '$'.$plan['price'].'/mo' : 'Free Trial'; ?>
</option>
<?php endforeach; ?>
</select>
</div>
</div>
<hr class="my-4">
<div class="row">
<div class="col-md-6 mb-3">
<label for="user_name" class="form-label">Full Name</label>
<input type="text" name="user_name" id="user_name" class="form-control py-2" placeholder="Admin Name" required>
</div>
<div class="col-md-6 mb-3">
<label for="email" class="form-label">Email Address</label>
<input type="email" name="email" id="email" class="form-control py-2" placeholder="admin@domain.com" required>
</div>
</div>
<div class="row">
<div class="col-md-6 mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" name="password" id="password" class="form-control py-2" required>
</div>
<div class="col-md-6 mb-3">
<label for="confirm_password" class="form-label">Confirm Password</label>
<input type="password" name="confirm_password" id="confirm_password" class="form-control py-2" required>
</div>
</div>
<div class="d-grid gap-2 mt-4">
<button type="submit" class="btn btn-primary py-2 fw-bold">Create Account</button>
</div>
</form>
<div class="text-center mt-4">
<p class="mb-0 text-muted small">Already have an account? <a href="login.php" class="text-primary fw-bold text-decoration-none">Sign In</a></p>
</div>
</div>
</div>
</div>
</div>
</div>
<?php include 'footer.php'; ?>

77
superadmin_dashboard.php Normal file
View File

@ -0,0 +1,77 @@
<?php
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'superadmin') {
header('Location: index.php');
exit;
}
require_once 'db/config.php';
// Stats for Superadmin
$total_msps = db()->query("SELECT COUNT(*) FROM msps")->fetchColumn();
$total_customers = db()->query("SELECT COUNT(*) FROM customers")->fetchColumn();
$total_users = db()->query("SELECT COUNT(*) FROM users")->fetchColumn();
// Recent MSPs
$stmt = db()->query("SELECT m.*, p.name as plan_name FROM msps m JOIN plans p ON m.plan_id = p.id ORDER BY m.created_at DESC LIMIT 5");
$recent_msps = $stmt->fetchAll();
include 'header.php';
?>
<div class="container py-5">
<div class="header-section">
<h1>Superadmin Dashboard</h1>
<p class="text-muted">Overview of all MSPs and system health.</p>
</div>
<div class="stats-grid">
<div class="stat-card">
<div class="stat-value"><?php echo $total_msps; ?></div>
<div class="stat-label">Total MSPs</div>
</div>
<div class="stat-card">
<div class="stat-value"><?php echo $total_customers; ?></div>
<div class="stat-label">Total Customers</div>
</div>
<div class="stat-card">
<div class="stat-value"><?php echo $total_users; ?></div>
<div class="stat-label">Total Users</div>
</div>
</div>
<div class="card mt-5 shadow-sm border-0">
<div class="card-body p-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h3 class="fw-bold mb-0">Recent MSP Registrations</h3>
<a href="manage_msps.php" class="btn btn-outline btn-sm">Manage All MSPs</a>
</div>
<table class="table">
<thead>
<tr>
<th>MSP Name</th>
<th>Plan</th>
<th>Subdomain</th>
<th>Created At</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($recent_msps as $msp): ?>
<tr>
<td class="fw-bold text-primary"><?php echo htmlspecialchars($msp['name']); ?></td>
<td><span class="badge bg-info text-dark"><?php echo htmlspecialchars($msp['plan_name']); ?></span></td>
<td><?php echo $msp['subdomain'] ? htmlspecialchars($msp['subdomain']) . '.mspconnect.com' : 'N/A'; ?></td>
<td class="text-muted"><?php echo date('Y-m-d', strtotime($msp['created_at'])); ?></td>
<td>
<a href="manage_msps.php?id=<?php echo $msp['id']; ?>" class="btn btn-outline btn-sm"><i class="bi bi-gear"></i></a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php include 'footer.php'; ?>

107
templates.php Normal file
View File

@ -0,0 +1,107 @@
<?php
session_start();
if (!isset($_SESSION['user_id']) || !isset($_SESSION['msp_id'])) {
header('Location: login.php');
exit;
}
require_once 'db/config.php';
$msp_id = $_SESSION['msp_id'];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action'])) {
if ($_POST['action'] === 'create_template') {
$name = $_POST['name'];
$description = $_POST['description'];
$stmt = db()->prepare("INSERT INTO onboarding_templates (msp_id, name, description) VALUES (?, ?, ?)");
$stmt->execute([$msp_id, $name, $description]);
$template_id = db()->lastInsertId();
header("Location: edit_template.php?id=$template_id");
exit;
}
}
$stmt = db()->prepare("SELECT * FROM onboarding_templates WHERE msp_id = ? ORDER BY created_at DESC");
$stmt->execute([$msp_id]);
$templates = $stmt->fetchAll();
include 'header.php';
?>
<div class="container py-5">
<div class="d-flex justify-content-between align-items-center mb-5">
<div>
<h1>Onboarding Templates</h1>
<p class="text-muted">Create multi-page onboarding flows for your customers.</p>
</div>
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#createTemplateModal">
<i class="bi bi-plus-circle"></i> Create New Template
</button>
</div>
<div class="row g-4">
<?php foreach ($templates as $template): ?>
<div class="col-md-4">
<div class="card h-100 shadow-sm border-0 template-card">
<div class="card-body p-4">
<h4 class="fw-bold text-primary mb-2"><?php echo htmlspecialchars($template['name']); ?></h4>
<p class="text-muted small mb-4"><?php echo htmlspecialchars($template['description'] ?: 'No description provided.'); ?></p>
<?php
$stmt = db()->prepare("SELECT COUNT(*) FROM onboarding_template_steps WHERE template_id = ?");
$stmt->execute([$template['id']]);
$steps_count = $stmt->fetchColumn();
?>
<div class="d-flex justify-content-between align-items-center mt-auto">
<span class="badge bg-light text-dark border"><i class="bi bi-layers"></i> <?php echo $steps_count; ?> Steps</span>
<a href="edit_template.php?id=<?php echo $template['id']; ?>" class="btn btn-outline btn-sm">Manage Template</a>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
<?php if (empty($templates)): ?>
<div class="col-12 text-center py-5">
<div class="bg-light p-5 rounded-4 border">
<i class="bi bi-file-earmark-plus display-4 text-muted mb-3"></i>
<h3>No Templates Found</h3>
<p class="text-muted">You haven't created any onboarding templates yet. Click the button above to start.</p>
</div>
</div>
<?php endif; ?>
</div>
</div>
<!-- Create Template Modal -->
<div class="modal fade" id="createTemplateModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<form method="POST">
<div class="modal-header">
<h5 class="modal-title">New Onboarding Template</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
</div>
<div class="modal-body">
<input type="hidden" name="action" value="create_template">
<div class="mb-3">
<label class="form-label">Template Name</label>
<input type="text" name="name" class="form-control" placeholder="e.g. Standard Customer Onboarding" required>
</div>
<div class="mb-3">
<label class="form-label">Description (Optional)</label>
<textarea name="description" class="form-control" rows="3" placeholder="Briefly describe what this template covers."></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary">Create & Continue</button>
</div>
</form>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<?php include 'footer.php'; ?>