132 lines
5.5 KiB
PHP
132 lines
5.5 KiB
PHP
<?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'; ?>
|