<?php
session_start();
require './inn/connect.php';
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $error = false;
    $errormsg = [];
    $captchaerror = false;
    if(!empty($_SESSION['captcha_code']) &&  $_POST['captcha'] == $_SESSION['captcha_code']){
        $captchaerror = true;
    }
    if($captchaerror){
        //print_r($_POST);
        if(empty($_POST['verifytoken'])){
            $error = true;
            $errormsg['otp_error'] = "Email not verified";            
        }else{
            $stmt = $conn->prepare("SELECT status FROM verify_email where vtoken = :vtoken and status = 1");
            $stmt->execute(['vtoken'=>$_POST['verifytoken']]);
            if($stmt->fetchColumn() == 0){
                $error = true;
                $errormsg['otp_error'] = "Email not verified.";
            }
        }
        if(empty(trim($_POST['name']))){
            $error = true;
            $errormsg['first_name_error'] = "First name required";
        }
        if(empty(trim($_POST['captcha']))){
            $error = true;
            $errormsg['captcha_error'] = "Captcha required";
        }elseif(empty($_SESSION['captcha_code'])){
            $error = true;
            $errormsg['captcha_error'] = "Captcha required";
        }elseif($_POST['captcha'] != $_SESSION['captcha_code']){
            $error = true;
            $errormsg['captcha_error'] = "Captcha not matched";
        }
        if(empty(trim($_POST['email']))){
            $error = true;
            $errormsg['email_error'] = "Email required";
        }else{
            $stmt = $conn->prepare("SELECT id FROM users where email = :email");
            $stmt->execute(['email'=>trim($_POST['email'])]);
            if($stmt->fetchColumn() > 0){
                $error = true;
                $errormsg['email_error'] = "Email already exists";
            }
        }        
    
        if(empty($_POST['acceptance-4'])){
            $error = true;
            $errormsg['captcha_error'] = "Confirm checkbox";
        }
        $insertData = [
            'first_name' => htmlspecialchars($_POST['name']),
            'last_name' => htmlspecialchars($_POST['last_name']),
            'email' => htmlspecialchars($_POST['email']),
            'phone' => htmlspecialchars($_POST['phone']),
            'gender' => htmlspecialchars($_POST['gender']),
            'dob' => htmlspecialchars($_POST['dob_date']),
            'country' => htmlspecialchars($_POST['country']),
            'zip' => htmlspecialchars($_POST['zipcode']),
            'fb_url' => htmlspecialchars($_POST['fb_url']),
            'in_url' => htmlspecialchars($_POST['in_url']),
            'tw_url' => htmlspecialchars($_POST['tw_url']),
            'ln_url' => htmlspecialchars($_POST['ln_url']),
            'email_verify' => 0,
            'accept_18' => 0,
            'accept_14' => 0,
            'accept_member' => 0,
            'parent_name' => htmlspecialchars($_POST['parentname']),
            'parent_phone' => htmlspecialchars($_POST['parentphone']),
            'parent_email' => htmlspecialchars($_POST['parentemail']),
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s')
        ];
        if(!$error){
            $stmt = "INSERT INTO users (first_name, last_name, email, phone, gender, dob, country, zip, fb_url, in_url, tw_url, ln_url, email_verify, accept_18, accept_14, accept_member, parent_name, parent_phone, parent_email, created_at, updated_at) VALUES (:first_name, :last_name, :email, :b_email, :phone, :gender, :dob, :country, :state, :city, :zip, :fb_url, :in_url, :tw_url, :ln_url, :email_verify, :accept_18, :accept_14, :accept_member, :parent_name, :parent_phone, :parent_email, :created_at, :updated_at) ";
            $stmt = $conn->prepare($stmt);
            $stmt->execute($insertData);
            unset($_SESSION['captcha_code']);
            $_SESSION['success_msg'] = "Member created successfully!";
            $response = [
                'status' => 'true',
                'message' => 'Data save successfully!',
                'data' => $insertData
            ];
        }else{
            $response = [
                'status' => 'false',
                'message' => 'error',
                'data' => $errormsg
            ];
        }

    }else{
        $errormsg['captcha_error'] = 'Security code required';
        $response = [
            'status' => 'false',
            'message' => 'error',
            'data' => $errormsg
        ];
    }
    
   
    
    // unset($_SESSION['captcha_code']);
    // Step 4: Send the JSON response
    echo json_encode($response);
}