<?php 
session_start();
if(!isset($_SESSION['admin_login'])) {
    header('location:adminlogin.php');
    exit;
}
include '_inc/dbconn.php';

// Success/Error Messages
$msg = "";

if(isset($_POST['submit'])) {
    // 1. Collect and Sanitize Input
    $fullname = mysqli_real_escape_string($con, $_POST['customer_name']);
    $email    = mysqli_real_escape_string($con, $_POST['customer_email']);
    $mobile   = mysqli_real_escape_string($con, $_POST['customer_mobile']);
    $balance  = mysqli_real_escape_string($con, $_POST['initial']);
    $status   = mysqli_real_escape_string($con, $_POST['account_status']); // 1 for Active, 0 for Pending
    
    // 2. Generate Credentials
    $account_no = "309" . rand(1000000, 9999999); // Generates a 10-digit account number
    $raw_password = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyz"), 0, 8); // Random 8-char password
    $hashed_password = password_hash($raw_password, PASSWORD_DEFAULT);
    $pin = rand(1000, 9999);

    // 3. Check if email already exists
    $check_email = mysqli_query($con, "SELECT id FROM users WHERE email='$email'");
    if(mysqli_num_rows($check_email) > 0) {
        $msg = "<div class='alert alert-danger'>Email already registered!</div>";
    } else {
        // 4. Insert into 'users' table (Matching your SQL schema)
        $sql = "INSERT INTO users (fullname, email, password, account_number, account_pin, balance, verified) 
                VALUES ('$fullname', '$email', '$hashed_password', '$account_no', '$pin', '$balance', '$status')";
        
        if(mysqli_query($con, $sql)) {
            // 5. Send Email to User
            $to = $email;
            $subject = "Your Account Details - Online Banking";
            $message = "
            <html>
            <head><title>Welcome to Our Bank</title></head>
            <body>
                <h2>Hello $fullname,</h2>
                <p>Your online banking account has been successfully created.</p>
                <p><b>Account Number:</b> $account_no</p>
                <p><b>Temporary Password:</b> $raw_password</p>
                <p><b>Transaction PIN:</b> $pin</p>
                <p>Please log in and change your password immediately for security.</p>
            </body>
            </html>";
            
            $headers = "MIME-Version: 1.0" . "\r\n";
            $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
            $headers .= "From: support@yourbank.com" . "\r\n";

            mail($to, $subject, $message, $headers);
            
            $msg = "<div class='alert alert-success'>Account Created! Credentials sent to $email</div>";
        } else {
            $msg = "<div class='alert alert-danger'>Database Error: " . mysqli_error($con) . "</div>";
        }
    }
}

include 'admin_header.php'; 
include 'admin_navbar.php'; 
?>

<style>
    .content-wrapper { margin-left: 260px; transition: 0.3s; background: #f8fafc; min-height: 100vh; padding: 20px; }
    @media (max-width: 992px) { .content-wrapper { margin-left: 0 !important; } }
    .card { border-radius: 15px; border: none; }
</style>

<div class="content-wrapper">
    <div class="container-fluid">
        <div class="row justify-content-center">
            <div class="col-lg-8">
                <div class="card shadow-sm">
                    <div class="card-header bg-white py-3">
                        <h5 class="mb-0 font-weight-bold text-primary">Register New Customer</h5>
                    </div>
                    <div class="card-body p-4">
                        <?php echo $msg; ?>
                        <form method="POST" action="">
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label class="small font-weight-bold">Full Name</label>
                                    <input type="text" name="customer_name" class="form-control" placeholder="John Doe" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="small font-weight-bold">Email Address</label>
                                    <input type="email" name="customer_email" class="form-control" placeholder="john@example.com" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="small font-weight-bold">Phone Number</label>
                                    <input type="text" name="customer_mobile" class="form-control" placeholder="+234..." required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="small font-weight-bold">Initial Deposit ($)</label>
                                    <input type="number" name="initial" class="form-control" value="0" required>
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label class="small font-weight-bold">Account Status</label>
                                    <select name="account_status" class="form-control">
                                        <option value="1">Active</option>
                                        <option value="0">Pending/Block</option>
                                    </select>
                                </div>
                            </div>
                            <hr>
                            <p class="text-muted small">Note: System will automatically generate a secure 10-digit account number and password.</p>
                            <button type="submit" name="submit" class="btn btn-primary btn-block py-2 font-weight-bold">Create Account & Send Email</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?>
