Files
wms-app/app/login/onboarding.php
T
2026-05-12 17:02:13 +07:00

298 lines
13 KiB
PHP

<?php
require '../session.php';
require '../config.php';
// Must come from email verification
if (empty($_SESSION['onboarding_user_id'])) {
header('Location: ' . $server_url . 'login/index.php');
exit;
}
// Generate CSRF token before include_header.php outputs the meta tag
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
require '../include_header.php';
$user_name = htmlspecialchars($_SESSION['onboarding_name'] ?? 'there');
?>
<body>
<div class="container py-5" style="max-width:680px;">
<!-- Header -->
<div class="text-center mb-5">
<a href="<?php echo $server_url?>login/index.php" class="d-inline-block mb-4">
<img src="<?php echo $server_url?>assets/images/favicon.png" alt="" width="40"/>
<span class="ms-2"><img src="<?php echo $server_url?>assets/images/logo.svg" alt=""></span>
</a>
<h1 class="h4 mb-1">Welcome, <?php echo $user_name ?>! 👋</h1>
<p class="text-muted">Set up your company and email before you get started.</p>
</div>
<!-- Company Info Card -->
<div class="card mb-4">
<div class="card-body p-5">
<h2 class="fs-5 mb-1"><i class="ti ti-building me-2"></i>Company Information</h2>
<p class="text-muted small mb-4">This is how your company appears across the system.</p>
<div class="row g-3">
<div class="col-md-6">
<label class="form-label">Company Name (TH) <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="company_name" placeholder="ชื่อบริษัท" required>
</div>
<div class="col-md-6">
<label class="form-label">Company Name (EN)</label>
<input type="text" class="form-control" id="company_name2" placeholder="Company Name (English)">
</div>
<div class="col-md-6">
<label class="form-label">Channel Name <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="channel_name" placeholder="e.g. my-shop">
<div class="form-text">Unique identifier. Lowercase, no spaces.</div>
</div>
<div class="col-md-3">
<label class="form-label">Branch</label>
<input type="text" class="form-control" id="branch" placeholder="สำนักงานใหญ่">
</div>
<div class="col-md-3">
<label class="form-label">Branch No.</label>
<input type="text" class="form-control" id="branch_no" placeholder="00000">
</div>
<div class="col-md-6">
<label class="form-label">Email</label>
<input type="email" class="form-control" id="company_email" placeholder="company@example.com">
</div>
<div class="col-md-6">
<label class="form-label">Phone</label>
<input type="text" class="form-control" id="company_phone" placeholder="02-xxx-xxxx">
</div>
</div>
</div>
</div>
<!-- SMTP Card -->
<div class="card mb-4">
<div class="card-body p-5">
<div class="d-flex justify-content-between align-items-start mb-1">
<h2 class="fs-5 mb-0"><i class="ti ti-mail-cog me-2"></i>SMTP / Email Setting</h2>
<span class="badge bg-label-danger">Required</span>
</div>
<p class="text-muted small mb-3">
SMTP is required to send OTP during login.
A verification email will be sent when you finish setup.
</p>
<!-- SMTP User Guide (collapsible) -->
<div class="mb-4">
<a class="small link-primary text-decoration-none" data-bs-toggle="collapse" href="#smtp_guide" role="button">
<i class="ti ti-help-circle me-1"></i>How do I get SMTP settings?
</a>
<div class="collapse mt-3" id="smtp_guide">
<div class="border rounded p-4 bg-light small">
<!-- Gmail -->
<div class="mb-4">
<div class="fw-semibold mb-2">
<i class="ti ti-brand-gmail me-1 text-danger"></i>Gmail
</div>
<p class="text-muted mb-2">Gmail requires an <strong>App Password</strong> — your regular Gmail password will not work.</p>
<ol class="mb-2 ps-3">
<li>Go to <a href="https://myaccount.google.com/security" target="_blank">myaccount.google.com/security</a></li>
<li>Enable <strong>2-Step Verification</strong> if not already on</li>
<li>Search for <strong>App passwords</strong> in the search bar</li>
<li>Create a new app password — name it <em>WMS</em></li>
<li>Copy the 16-character password shown</li>
</ol>
<div class="bg-white border rounded p-2 font-monospace small">
Host: smtp.gmail.com &nbsp;|&nbsp; Port: 587 &nbsp;|&nbsp; Encryption: TLS
</div>
</div>
<hr class="my-3">
<!-- Outlook / Office 365 -->
<div class="mb-4">
<div class="fw-semibold mb-2">
<i class="ti ti-brand-office me-1 text-primary"></i>Outlook / Office 365
</div>
<p class="text-muted mb-2">Use your Microsoft 365 email and password directly. Make sure SMTP AUTH is enabled for your account.</p>
<ol class="mb-2 ps-3">
<li>Go to <a href="https://admin.microsoft.com" target="_blank">admin.microsoft.com</a></li>
<li>Under <strong>Users → Active users</strong>, select the account</li>
<li>Go to <strong>Mail → Manage email apps</strong></li>
<li>Enable <strong>Authenticated SMTP</strong></li>
</ol>
<div class="bg-white border rounded p-2 font-monospace small">
Host: smtp.office365.com &nbsp;|&nbsp; Port: 587 &nbsp;|&nbsp; Encryption: TLS
</div>
</div>
<hr class="my-3">
<!-- Yahoo -->
<div class="mb-4">
<div class="fw-semibold mb-2">
<i class="ti ti-mail me-1 text-warning"></i>Yahoo Mail
</div>
<p class="text-muted mb-2">Yahoo also requires an <strong>App Password</strong>.</p>
<ol class="mb-2 ps-3">
<li>Go to <a href="https://login.yahoo.com/account/security" target="_blank">Yahoo Account Security</a></li>
<li>Enable <strong>Two-step verification</strong></li>
<li>Click <strong>Generate app password</strong></li>
<li>Select <em>Other App</em>, name it <em>WMS</em>, copy the password</li>
</ol>
<div class="bg-white border rounded p-2 font-monospace small">
Host: smtp.mail.yahoo.com &nbsp;|&nbsp; Port: 587 &nbsp;|&nbsp; Encryption: TLS
</div>
</div>
<hr class="my-3">
<!-- Generic / cPanel -->
<div>
<div class="fw-semibold mb-2">
<i class="ti ti-server me-1 text-secondary"></i>Web Hosting / cPanel
</div>
<p class="text-muted mb-2">If your email is hosted with a web provider (e.g. Hostinger, GoDaddy, SiteGround):</p>
<ol class="mb-2 ps-3">
<li>Log in to your hosting <strong>cPanel</strong></li>
<li>Go to <strong>Email Accounts</strong> and create or select an account</li>
<li>Click <strong>Connect Devices</strong> to see SMTP details</li>
<li>Use those exact host, port and encryption settings</li>
</ol>
<div class="text-muted">
The host is usually <code>mail.yourdomain.com</code> and port is <code>587</code>.
</div>
</div>
</div>
</div>
</div>
<!-- SMTP Form -->
<div class="row g-3">
<div class="col-md-8">
<label class="form-label">SMTP Host <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="smtp_host" placeholder="e.g. smtp.gmail.com">
</div>
<div class="col-md-4">
<label class="form-label">Port <span class="text-danger">*</span></label>
<select class="form-select" id="smtp_port">
<option value="587">587 — TLS</option>
<option value="465">465 — SSL</option>
<option value="25">25 — Plain</option>
</select>
</div>
<div class="col-md-6">
<label class="form-label">Username / Email <span class="text-danger">*</span></label>
<input type="text" class="form-control" id="smtp_username" placeholder="your@email.com">
</div>
<div class="col-md-6">
<label class="form-label">Password <span class="text-danger">*</span></label>
<div class="input-group">
<input type="password" class="form-control" id="smtp_password" placeholder="SMTP password">
<button class="btn btn-outline-secondary toggle-pw" type="button" data-target="smtp_password">
<i class="ti ti-eye"></i>
</button>
</div>
</div>
<div class="col-12">
<label class="form-label">Encryption</label>
<div class="d-flex gap-4">
<div class="form-check">
<input class="form-check-input" type="radio" name="smtp_encryption" id="enc_tls" value="tls" checked>
<label class="form-check-label" for="enc_tls">TLS</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="smtp_encryption" id="enc_ssl" value="ssl">
<label class="form-check-label" for="enc_ssl">SSL</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="smtp_encryption" id="enc_none" value="none">
<label class="form-check-label" for="enc_none">None</label>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Actions -->
<div class="d-flex justify-content-end align-items-center">
<button class="btn btn-primary px-5" id="btn_finish" onclick="finish_onboarding()">
<i class="ti ti-rocket me-1"></i>Finish Setup
</button>
</div>
</div>
<script>
$(function () {
$(document).on('click', '.toggle-pw', function () {
const $input = $('#' + $(this).data('target'));
const isText = $input.attr('type') === 'text';
$input.attr('type', isText ? 'password' : 'text');
$(this).find('i').toggleClass('ti-eye ti-eye-off');
});
});
function finish_onboarding() {
const company_name = $('#company_name').val().trim();
const channel_name = $('#channel_name').val().trim();
if (!company_name || !channel_name) {
bootbox.alert('Company name and channel name are required.');
return;
}
if (!$('#smtp_host').val().trim() || !$('#smtp_username').val().trim() || !$('#smtp_password').val()) {
bootbox.alert('SMTP host, username and password are required.');
return;
}
const $btn = $('#btn_finish');
$btn.prop('disabled', true).html('<i class="ti ti-loader-2 me-1"></i>Verifying SMTP…');
const encryption = $('input[name="smtp_encryption"]:checked').val();
ajax_request({
url: '<?php echo $server_url?>login/api/engine/onboarding.php',
autoPrepare: false,
data: { json: JSON.stringify({
action: 'create',
company_name: company_name,
company_name2: $('#company_name2').val().trim(),
channel_name: channel_name,
branch: $('#branch').val().trim() || 'สำนักงานใหญ่',
branch_no: $('#branch_no').val().trim() || '00000',
email: $('#company_email').val().trim(),
phone: $('#company_phone').val().trim(),
smtp_host: $('#smtp_host').val().trim(),
smtp_port: $('#smtp_port').val(),
smtp_username: $('#smtp_username').val().trim(),
smtp_password: $('#smtp_password').val(),
smtp_encryption: encryption,
})},
onSuccess: function (r) {
bootbox.alert('Setup complete! Please sign in to get started.', function () {
window.location.href = '<?php echo $server_url?>login/index.php';
});
},
onError: function (r) {
$btn.prop('disabled', false).html('<i class="ti ti-rocket me-1"></i>Finish Setup');
},
});
}
</script>
</body>
</html>