<?php
require_once __DIR__ . '/../includes/header_customer.php';
require_once __DIR__ . '/../includes/auth.php';
$pdo = require __DIR__ . '/../config/pdo.php';

$errors = [];
$values = [
    'name' => '',
    'email' => '',
    'phone' => '',
    'address' => '',
];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $values['name'] = trim($_POST['name'] ?? '');
    $values['email'] = trim($_POST['email'] ?? '');
    $values['phone'] = trim($_POST['phone'] ?? '');
    $values['address'] = trim($_POST['address'] ?? '');
    $password = $_POST['password'] ?? '';
    $passwordConfirm = $_POST['password_confirm'] ?? '';

    if ($values['name'] === '') {
        $errors['name'] = 'Nama wajib diisi.';
    }

    if ($values['email'] === '' || !filter_var($values['email'], FILTER_VALIDATE_EMAIL)) {
        $errors['email'] = 'Email tidak valid.';
    }

    if ($values['phone'] === '') {
        $errors['phone'] = 'No HP wajib diisi.';
    }

    if ($values['address'] === '') {
        $errors['address'] = 'Alamat wajib diisi.';
    }

    if (strlen($password) < 6) {
        $errors['password'] = 'Password minimal 6 karakter.';
    }

    if ($password !== $passwordConfirm) {
        $errors['password_confirm'] = 'Konfirmasi password tidak sama.';
    }

    if (!$errors) {
        $stmt = $pdo->prepare('SELECT id FROM users WHERE email = ? LIMIT 1');
        $stmt->execute([$values['email']]);
        if ($stmt->fetch()) {
            $errors['email'] = 'Email sudah terdaftar.';
        }
    }

    if (!$errors) {
        $hash = password_hash($password, PASSWORD_BCRYPT);
        $username = $values['email'];
        $stmt = $pdo->prepare('INSERT INTO users (name, email, username, phone, address, password_hash, role, is_active) VALUES (?, ?, ?, ?, ?, ?, "customer", 1)');
        $stmt->execute([
            $values['name'],
            $values['email'],
            $username,
            $values['phone'],
            $values['address'],
            $hash,
        ]);

        flash_message('success', 'Registrasi berhasil. Silakan login.');
        redirect(base_url('pages/login.php'));
    }
}

require_once __DIR__ . '/../includes/header_customer.php';
?>

<section class="auth-card">
    <h2>Registrasi Customer</h2>

    <?php if ($message = flash_message('success')): ?>
        <div class="alert success"><?= sanitize($message); ?></div>
    <?php endif; ?>

    <form method="post">
        <label>Nama Lengkap</label>
        <input type="text" name="name" value="<?= sanitize($values['name']); ?>" required>
        <?php if (!empty($errors['name'])): ?><small class="error"><?= sanitize($errors['name']); ?></small><?php endif; ?>

        <label>Email</label>
        <input type="email" name="email" value="<?= sanitize($values['email']); ?>" required>
        <?php if (!empty($errors['email'])): ?><small class="error"><?= sanitize($errors['email']); ?></small><?php endif; ?>

        <label>No HP</label>
        <input type="text" name="phone" value="<?= sanitize($values['phone']); ?>" required>
        <?php if (!empty($errors['phone'])): ?><small class="error"><?= sanitize($errors['phone']); ?></small><?php endif; ?>

        <label>Alamat</label>
        <textarea name="address" rows="3" required><?= sanitize($values['address']); ?></textarea>
        <?php if (!empty($errors['address'])): ?><small class="error"><?= sanitize($errors['address']); ?></small><?php endif; ?>

        <label>Password</label>
        <input type="password" name="password" required>
        <?php if (!empty($errors['password'])): ?><small class="error"><?= sanitize($errors['password']); ?></small><?php endif; ?>

        <label>Konfirmasi Password</label>
        <input type="password" name="password_confirm" required>
        <?php if (!empty($errors['password_confirm'])): ?><small class="error"><?= sanitize($errors['password_confirm']); ?></small><?php endif; ?>

        <button type="submit">Daftar</button>
        <p>Sudah punya akun? <a href="<?= base_url('pages/login.php'); ?>">Login</a></p>
    </form>
</section>

<?php require_once __DIR__ . '/../includes/footer_customer.php'; ?>
