<?php
require_once("/home/steelmin/public_html/inc/config.php");
require_once("/home/steelmin/public_html/hf.php");
/*
ini_set('display_errors',1);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);*/
$data=[];
header('Content-Type: application/json');
if(isset($_POST['event_id'])){    
    // Validate if all required fields are present
    $required_fields = [
        'fullName', 'event_id', 'jobTitle', 'companyName', 
        'emailAddress', 'phoneNumber', 'agreement'
    ];
    
    foreach ($required_fields as $field){
        if (!isset($_POST[$field]) || empty($_POST[$field])) {
            $data['status'] = false;
            $data['message'] = "Missing or empty field: " . $field;
            echo json_encode($data);
            require_once("/home/steelmin/public_html/inc/desconfig.php");
            die();
        }
    }
    
     // Sanitize and trim the input fields
    $first_name = htmlentities(trim($_POST['fullName']), ENT_QUOTES);
    $event_id = trim($_POST['event_id']);
    $country = trim($_POST['country']);
    $job_title = htmlentities(trim($_POST['jobTitle']), ENT_QUOTES);
    $company_name = htmlentities(trim($_POST['companyName']), ENT_QUOTES);
    $email = htmlentities(trim($_POST['emailAddress']), ENT_QUOTES);
    $mobile = htmlentities(trim($_POST['phoneNumber']), ENT_QUOTES);
    $currDateTime = date("Y-m-d H:i:s");
    $agreement = trim($_POST['agreement']);
   
    if(empty($email) || empty($mobile)){
        $data['status']=false;
        $data['message']="Mobile and email are mandatory, Please try again.";
        echo json_encode($data);
        require_once("/home/steelmin/public_html/inc/desconfig.php");
        die;
    }
   
    $country_details = qryToCommaSepString("select concat(country,'_',std_code) as country_details from tbl_country where id=$country limit 1");
    if($country_details!=''){
        $tem_det = explode("_",$country_details);
        $country_name = $tem_det[0]; 
        $isdcode = $tem_det[1]; 
    }
    $camp_code ='CAMP204';
    if(trim($_POST['camp_code'])!='')
        $camp_code = $_POST['camp_code'];

    $sql1     = "select * from tbl_members where email='$email' or username='$email' or addtnl_email='$email' or phoneno='$mobile' or alternate_mob_no='$mobile' or username='$mobile' "; 	
    $resu1     =  mysql_query($sql1);
    
    if(mysql_num_rows($resu1)==0){
        $ip =  $_SERVER['REMOTE_ADDR'];        
        include_once('/home/steelmin/public_html/steeladmin/hf_360.php');                
        $arr['tbl_members']=array(
            'name'=>$first_name,
            'job_title'=>$job_title,
            'billing_name'=>$first_name,
            'company'=>$company_name, 
            'email'=>$email, 
            'username'=>$email,
            'isd_code'=>$isdcode,
            'phoneno'=>$mobile, 
            'ipaddress'=>$ip, 				 
            'date'=>$currDateTime, 
            'main_source'=>'events website url',
            'source_from'=>"New",           
            'country'=>$country,           
            'password'=>$mobile,
            'ol_be'=>'EVT FE',
            'primary_source' =>$camp_code,
            'lead_type' => 'A1'
        );        
        $res = create_logs_from_array_of_array($arr);        
        $memid = $res['tbl_members'];        
    }else{			
        $res = mysql_fetch_array($resu1,MYSQL_ASSOC);
        $memid = $res['id'];        
    }
    if(empty($memid))
    {
        $data['status']=false;
        $data['message']="There is some issue in registration, Please try again.";
        echo json_encode($data);
        require_once("/home/steelmin/public_html/inc/desconfig.php");
        die;
    }
    
    $field_array = array();
    $field_array = array(
        'camp_code' => $camp_code,
        'memid' => $memid,
        'eventId' => $event_id,
        'ip' => $ip,
        'refral_url' => 'https://www.bigmint.co/events/'
    );
    addCampaignHistory($field_array);
    
    makeEntryInTableFinalStatus($memid, $event_id);   
    $dt=date('Y-m-d');
    $tm=date('H:i:s');       
    $comments      ='';
    $comments .=  "Name => ".$first_name." <br /> ";
    $comments .=  "Email => ".$email." <br /> ";
    $comments .=  "Phone No. => ".$mobile." <br /> ";
    $comments .=  "Company => ".$company_name." <br /> ";
    $comments .=  "Country => ".$country_name." <br /> ";
    $comments .=  "Job Title => ".$job_title." <br /> ";
    $comments .=  "Agreement => ".$agreement." <br /> ";
   
  
						
    mysql_query("INSERT INTO tbl_comments(memid, `f_date`, `f_time`, `comments`, `created_by`, `comments_for`, event_id) VALUES ($memid, '$dt', '$tm', '$comments', 'Created from webinar registration page  $plan_value', 'EV', ".$event_id.")") or die(mysql_error());   
   
    
   //Successfully registered message
    $data['status']=true;
    $data['message']="Successfully registered";
    $data['video_link']='';
    echo json_encode($data);
    require_once("/home/steelmin/public_html/inc/desconfig.php");
    die;
   
}else{
            $data['status'] = false;
            $data['message'] = "Missing event id";
            echo json_encode($data);
            require_once("/home/steelmin/public_html/inc/desconfig.php");
            die();
}
require_once("/home/steelmin/public_html/inc/desconfig.php");
?>
