<?php
require_once __DIR__.'/includes/functions.php';
verify_csrf();
if($_SERVER['REQUEST_METHOD']==='POST'){
 try{
   $name=trim($_POST['name']??''); $email=trim($_POST['email']??''); $pass=(string)($_POST['password']??'');
   if(!$name||!filter_var($email,FILTER_VALIDATE_EMAIL)||strlen($pass)<8) throw new Exception('Please enter valid name, email and password minimum 8 characters.');
   q('INSERT INTO mj_users(name,email,mobile,password_hash,country_id,timezone,preferred_currency,created_at) VALUES(?,?,?,?,?,?,?,NOW())',[$name,$email,$_POST['mobile']??'',password_hash($pass,PASSWORD_DEFAULT),$_POST['country_id']?:null,$_POST['timezone']?:'UTC',$_POST['currency']?:'USD']);
   $_SESSION['user_id']=(int)db()->lastInsertId(); flash('success','Welcome to MJ Marketplace.'); redirect('my-account.php');
 }catch(Throwable $e){ flash('error',$e->getMessage()); }
}
$countries=all_rows('SELECT * FROM mj_countries WHERE is_active=1 ORDER BY name'); $pageTitle='Register'; include __DIR__.'/includes/header.php';
?>
<section class="mj-auth-shell"><div class="container"><div class="row justify-content-center"><div class="col-xl-11"><div class="card mj-auth-card"><div class="row g-0"><div class="col-lg-5"><div class="mj-auth-visual h-100"><span class="badge text-bg-light text-primary rounded-pill mb-3"><i class="bi bi-stars me-1"></i>Join free</span><h1 class="fw-bold display-5">Start buying, selling and listing your company.</h1><p class="text-white-50">Post products, services, request quotes, manage businesses and chat safely.</p><img src="<?=asset('images/company-hero.svg')?>" alt="Register illustration"></div></div><div class="col-lg-7"><div class="p-4 p-lg-5"><h2 class="fw-bold mb-1">Create account</h2><p class="text-muted mb-4">Your account works for all countries in MJ Marketplace.</p><form method="post"><?=csrf_input()?><div class="row g-3"><div class="col-md-6"><label class="form-label fw-semibold">Full name</label><input class="form-control" name="name" required></div><div class="col-md-6"><label class="form-label fw-semibold">Email</label><input class="form-control" type="email" name="email" required></div><div class="col-md-6"><label class="form-label fw-semibold">Mobile / WhatsApp</label><input class="form-control" name="mobile"></div><div class="col-md-6"><label class="form-label fw-semibold">Country</label><select class="form-select" name="country_id"><option value="">Select country</option><?php foreach($countries as $c): ?><option value="<?=e($c['id'])?>" data-tz="<?=e($c['timezone_default'])?>" data-currency="<?=e($c['currency_code'])?>"><?=e($c['flag_emoji'].' '.$c['name'])?></option><?php endforeach; ?></select></div><div class="col-md-6"><label class="form-label fw-semibold">Timezone</label><input class="form-control" name="timezone" value="Asia/Singapore"></div><div class="col-md-6"><label class="form-label fw-semibold">Currency</label><input class="form-control" name="currency" value="SGD"></div><div class="col-12"><label class="form-label fw-semibold">Password</label><input class="form-control" type="password" name="password" minlength="8" required><div class="form-text">Minimum 8 characters.</div></div></div><button class="btn btn-primary btn-lg w-100 mt-4"><i class="bi bi-person-plus me-1"></i>Create Account</button></form><div class="text-center mt-4 text-muted">Already have account? <a href="login.php" class="fw-bold text-decoration-none">Login</a></div></div></div></div></div></div></div></div></section>
<?php include __DIR__.'/includes/footer.php'; ?>
