<?php
require 'config.php';

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (isset($_SESSION['user_id'])) {
    redirect('index.php');
}

$errors = [];
$name = $email = $phone = "";

if (is_post()) {
    if(function_exists('verify_csrf')) verify_csrf();
    
    $name     = trim($_POST['name'] ?? '');
    $email    = trim($_POST['email'] ?? '');
    $phone    = trim($_POST['phone'] ?? ''); 
    $password = $_POST['password'] ?? '';
    $confirm  = $_POST['confirm'] ?? '';

    if (!$name || !$email || !$phone || !$password || !$confirm) {
        $errors[] = "অনুগ্রহ করে সকল ঘর পূরণ করুন।";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $errors[] = "আপনার ইমেইলটি সঠিক নয়।";
    } elseif (strlen($phone) < 11) {
        $errors[] = "সঠিক মোবাইল নম্বর প্রদান করুন।";
    } elseif (strlen($password) < 6) {
        $errors[] = "পাসওয়ার্ড কমপক্ষে ৬ অক্ষরের হতে হবে।";
    } elseif ($password !== $confirm) {
        $errors[] = "পাসওয়ার্ড দুটি মিলেনি, আবার চেষ্টা করুন।";
    } else {
        $stmt = $pdo->prepare("SELECT id FROM users WHERE email=?");
        $stmt->execute([$email]);
        if ($stmt->fetch()) {
            $errors[] = "এই ইমেইল দিয়ে ইতিমধ্যে নিবন্ধন করা হয়েছে।";
        } else {
            $hash = password_hash($password, PASSWORD_DEFAULT);
            try {
                $stmt = $pdo->prepare("INSERT INTO users (name, email, phone, password) VALUES (?, ?, ?, ?)");
                if ($stmt->execute([$name, $email, $phone, $hash])) {
                    if(function_exists('set_flash')) {
                        set_flash("নিবন্ধন সফল হয়েছে! এখন আপনি লগইন করতে পারেন।", "success");
                    }
                    header("Location: login.php");
                    exit;
                } else {
                    $errors[] = "দুঃখিত, কিছু ভুল হয়েছে। আবার চেষ্টা করুন।";
                }
            } catch (PDOException $e) {
                $errors[] = "ডেটাবেজ ত্রুটি: " . $e->getMessage();
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="bn">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>নিবন্ধন | ডিজিটাল সার্ভিস পোর্টাল</title>
  <link href="https://fonts.googleapis.com/css2?family=Inter:opsz,wght@14..32,300;14..32,400;14..32,500;14..32,600;14..32,700&family=Hind+Siliguri:wght@300;400;500;600;700&display=swap" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    body {
      font-family: 'Inter', 'Hind Siliguri', sans-serif;
      background: linear-gradient(125deg, #E0E7FF 0%, #F3E8FF 100%);
      min-height: 100vh;
      display: flex;
      align-items: center;
      justify-content: center;
      padding: 1.5rem;
      position: relative;
    }

    /* Abstract shapes */
    .shape {
      position: fixed;
      z-index: 0;
      filter: blur(60px);
      opacity: 0.4;
    }
    .shape-1 {
      top: -100px;
      left: -100px;
      width: 300px;
      height: 300px;
      background: #8B5CF6;
      border-radius: 50%;
    }
    .shape-2 {
      bottom: -80px;
      right: -80px;
      width: 400px;
      height: 400px;
      background: #F97316;
      border-radius: 50%;
    }
    .shape-3 {
      top: 30%;
      right: 10%;
      width: 200px;
      height: 200px;
      background: #10B981;
      border-radius: 50%;
      opacity: 0.3;
    }

    .container {
      width: 100%;
      max-width: 1200px;
      position: relative;
      z-index: 2;
    }

    .register-grid {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 2rem;
      background: rgba(255, 255, 255, 0.2);
      backdrop-filter: blur(12px);
      border-radius: 2.5rem;
      padding: 1.5rem;
      box-shadow: 0 25px 45px rgba(0,0,0,0.1);
      border: 1px solid rgba(255,255,255,0.3);
    }

    /* Left side - brand area */
    .brand-side {
      background: linear-gradient(145deg, #4F46E5, #7C3AED);
      border-radius: 2rem;
      padding: 2.5rem;
      color: white;
      display: flex;
      flex-direction: column;
      justify-content: center;
      box-shadow: 0 20px 35px -10px rgba(0,0,0,0.2);
    }
    .brand-side h1 {
      font-size: 2.5rem;
      font-weight: 800;
      margin-bottom: 1rem;
      line-height: 1.2;
    }
    .brand-side p {
      font-size: 1rem;
      opacity: 0.9;
      margin-bottom: 2rem;
      line-height: 1.5;
    }
    .feature-list {
      list-style: none;
    }
    .feature-list li {
      margin-bottom: 1rem;
      display: flex;
      align-items: center;
      gap: 0.75rem;
      font-size: 1rem;
    }
    .feature-list i {
      font-size: 1.3rem;
      background: rgba(255,255,255,0.2);
      padding: 0.4rem;
      border-radius: 50%;
    }

    /* Right side - form card */
    .form-card {
      background: white;
      border-radius: 2rem;
      padding: 2.2rem;
      box-shadow: 0 20px 35px -12px rgba(0,0,0,0.15);
      transition: transform 0.2s;
    }
    .form-card:hover {
      transform: translateY(-5px);
    }
    .form-card h2 {
      font-size: 1.8rem;
      font-weight: 700;
      color: #1E1B4B;
      margin-bottom: 0.5rem;
    }
    .form-card .sub {
      color: #6B7280;
      margin-bottom: 2rem;
      border-left: 3px solid #7C3AED;
      padding-left: 0.75rem;
    }

    /* Form styles */
    .input-group {
      margin-bottom: 1.5rem;
    }
    .input-group label {
      display: flex;
      align-items: center;
      gap: 0.5rem;
      font-weight: 600;
      font-size: 0.85rem;
      color: #374151;
      margin-bottom: 0.5rem;
    }
    .input-group label i {
      color: #7C3AED;
    }
    .input-group input {
      width: 100%;
      padding: 0.9rem 1rem;
      font-size: 0.95rem;
      border: 1px solid #E5E7EB;
      border-radius: 1rem;
      outline: none;
      transition: all 0.2s;
      background: #F9FAFB;
      font-family: inherit;
    }
    .input-group input:focus {
      border-color: #7C3AED;
      background: white;
      box-shadow: 0 0 0 3px rgba(124,58,237,0.1);
    }
    .password-wrapper {
      position: relative;
    }
    .password-wrapper input {
      padding-right: 2.5rem;
    }
    .toggle-eye {
      position: absolute;
      right: 1rem;
      top: 50%;
      transform: translateY(-50%);
      cursor: pointer;
      color: #9CA3AF;
    }

    .btn-register {
      width: 100%;
      background: linear-gradient(95deg, #4F46E5, #7C3AED);
      border: none;
      padding: 0.9rem;
      border-radius: 1.5rem;
      font-weight: 700;
      font-size: 1rem;
      color: white;
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 0.5rem;
      cursor: pointer;
      transition: all 0.25s;
      margin-top: 0.5rem;
      font-family: inherit;
    }
    .btn-register:hover {
      background: linear-gradient(95deg, #4338CA, #6D28D9);
      transform: scale(1.01);
      box-shadow: 0 8px 20px rgba(79,70,229,0.3);
    }

    .login-link {
      text-align: center;
      margin-top: 1.8rem;
      font-size: 0.9rem;
      color: #4B5563;
    }
    .login-link a {
      color: #7C3AED;
      font-weight: 700;
      text-decoration: none;
    }
    .login-link a:hover {
      text-decoration: underline;
    }

    .error-alert {
      background: #FEF2F2;
      border-left: 5px solid #EF4444;
      padding: 1rem;
      border-radius: 1rem;
      margin-bottom: 1.5rem;
      font-size: 0.85rem;
      color: #991B1B;
    }
    .error-alert ul {
      margin-left: 1.2rem;
    }

    /* WhatsApp button */
    .whatsapp-float {
      position: fixed;
      bottom: 24px;
      right: 24px;
      background: #25D366;
      width: 56px;
      height: 56px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 1.8rem;
      color: white;
      box-shadow: 0 5px 18px rgba(37,211,102,0.3);
      transition: 0.2s;
      z-index: 99;
      text-decoration: none;
    }
    .whatsapp-float:hover {
      transform: scale(1.08);
      background: #128C7E;
    }

    /* Responsive */
    @media (max-width: 900px) {
      .register-grid {
        grid-template-columns: 1fr;
        gap: 1rem;
      }
      .brand-side {
        text-align: center;
        padding: 2rem;
      }
      .feature-list li {
        justify-content: center;
      }
    }
    @media (max-width: 500px) {
      .form-card {
        padding: 1.5rem;
      }
    }
  </style>
</head>
<body>
  <div class="shape shape-1"></div>
  <div class="shape shape-2"></div>
  <div class="shape shape-3"></div>

  <div class="container">
    <div class="register-grid">
      <!-- Left side: Brand & features -->
      <div class="brand-side">
       <h1>BEST SEBA<br>PORTAL</h1>
        <p>এক প্ল্যাটফর্মে সব সেবা — সহজ, দ্রুত ও নিরাপদ</p>
        <ul class="feature-list">
          <li><i class="bi bi-check-lg"></i> সহজ নিবন্ধন</li>
          <li><i class="bi bi-shield-check"></i> নিরাপদ ডাটা</li>
          <li><i class="bi bi-headset"></i> ২৪/৭ সাপোর্ট</li>
          <li><i class="bi bi-whatsapp"></i> হোয়াটসঅ্যাপে সংযুক্ত</li>
        </ul>
      </div>

      <!-- Right side: Registration Form -->
      <div class="form-card">
        <h2>অ্যাকাউন্ট খুলুন</h2>
        <div class="sub">আজই যোগ দিন, সীমাহীন সুবিধা পান</div>

        <?php if (!empty($errors)): ?>
          <div class="error-alert">
            <ul>
              <?php foreach ($errors as $err): ?>
                <li><?php echo htmlspecialchars($err); ?></li>
              <?php endforeach; ?>
            </ul>
          </div>
        <?php endif; ?>

        <form method="post">
          <?php if(function_exists('csrf_field')) csrf_field(); ?>

          <div class="input-group">
            <label><i class="bi bi-person"></i> পূর্ণ নাম</label>
            <input type="text" name="name" placeholder="আপনার নাম" value="<?php echo htmlspecialchars($name); ?>" required>
          </div>

          <div class="input-group">
            <label><i class="bi bi-envelope"></i> ইমেইল</label>
            <input type="email" name="email" placeholder="ইমেইল ঠিকানা" value="<?php echo htmlspecialchars($email); ?>" required>
          </div>

          <div class="input-group">
            <label><i class="bi bi-phone"></i> মোবাইল</label>
            <input type="text" name="phone" placeholder="01XXXXXXXXX" value="<?php echo htmlspecialchars($phone); ?>" required>
          </div>

          <div class="input-group">
            <label><i class="bi bi-lock"></i> পাসওয়ার্ড</label>
            <div class="password-wrapper">
              <input type="password" name="password" id="password" placeholder="কমপক্ষে ৬ অক্ষর" required>
              <i class="bi bi-eye-slash toggle-eye" onclick="togglePass('password', this)"></i>
            </div>
          </div>

          <div class="input-group">
            <label><i class="bi bi-check-circle"></i> পাসওয়ার্ড নিশ্চিত</label>
            <div class="password-wrapper">
              <input type="password" name="confirm" id="confirm" placeholder="আবার লিখুন" required>
            </div>
          </div>

          <button type="submit" class="btn-register">
            <i class="bi bi-arrow-right-circle"></i> নিবন্ধন করুন
          </button>

          <div class="login-link">
            ইতিমধ্যে অ্যাকাউন্ট আছে? <a href="login.php">লগইন করুন</a>
          </div>
        </form>
      </div>
    </div>
  </div>

  <!-- WhatsApp Button -->
  <a href="https://wa.me/8801880845221" class="whatsapp-float" target="_blank" rel="noopener noreferrer">
    <i class="bi bi-whatsapp"></i>
  </a>

  <script>
    function togglePass(fieldId, icon) {
      const field = document.getElementById(fieldId);
      if (field.type === "password") {
        field.type = "text";
        icon.classList.remove("bi-eye-slash");
        icon.classList.add("bi-eye");
      } else {
        field.type = "password";
        icon.classList.remove("bi-eye");
        icon.classList.add("bi-eye-slash");
      }
    }
  </script>
</body>
</html>