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

include '_inc/dbconn.php';

// 1. Capture and Sanitize Inputs
$name     = 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']);
$address  = mysqli_real_escape_string($con, $_POST['customer_address']);
$balance  = mysqli_real_escape_string($con, $_POST['initial']);
$pin      = mysqli_real_escape_string($con, $_POST['customer_pin']);
$status   = mysqli_real_escape_string($con, $_POST['account_status']); // 1 for Active, 0 for Pending

// Generate a random Account Number
$account_no = "309" . rand(1000000, 9999999);

// Password Handling
$raw_password = $_POST['customer_pwd'];
$hashed_password = password_hash($raw_password, PASSWORD_DEFAULT); 

// 2. Insert into 'users' table (Matching your SQL structure)
$sql = "INSERT INTO users (fullname, email, password, account_number, account_pin, balance, verified) 
        VALUES ('$name', '$email', '$hashed_password', '$account_no', '$pin', '$balance', '$status')";

if(mysqli_query($con, $sql)) {
    $user_id = mysqli_insert_id($con);

    // 3. Record Initial Deposit in 'transactions' table
    $date = date('Y-m-d H:i:s');
    $ref = "REG-" . strtoupper(bin2hex(random_bytes(4)));
    $sql_trans = "INSERT INTO transactions (user_id, amount, type, description, status, reference_id, created_at) 
                  VALUES ('$user_id', '$balance', 'credit', 'Initial Account Opening Deposit', 'completed', '$ref', '$date')";
    mysqli_query($con, $sql_trans);

    // 4. Send Welcome Email
    $to = $email;
    $subject = "Your Account Details - Welcome to Equisfon Bank";
    
    $message = "
    <html>
    <head><title>Welcome to Equisfon Bank</title></head>
    <body style='font-family: Arial, sans-serif; line-height: 1.6;'>
        <div style='max-width: 600px; margin: auto; border: 1px solid #eee; padding: 20px;'>
            <h2 style='color: #0052FF;'>Welcome, $name!</h2>
            <p>Your online banking account has been successfully created. Here are your login credentials:</p>
            <table style='width: 100%;'>
                <tr><td><strong>Account Number:</strong></td><td>$account_no</td></tr>
                <tr><td><strong>Login Email:</strong></td><td>$email</td></tr>
                <tr><td><strong>Password:</strong></td><td>$raw_password</td></tr>
                <tr><td><strong>Account PIN:</strong></td><td>$pin</td></tr>
            </table>
            <p style='margin-top: 20px;'>Please log in and change your password immediately for security.</p>
            <br>
            <p>Regards,<br>Equisfon Bank Team</p>
        </div>
    </body>
    </html>
    ";

    // Email Headers
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= "From: Equisfon Support <support@equisfon.com>" . "\r\n";

    mail($to, $subject, $message, $headers);

    // Redirect with success
    header('location:display_customer.php?msg=success');
} else {
    echo "Error: " . mysqli_error($con);
}
?>
