<?php
require_once("/home/steelmin/public_html/inc/config.php");
require_once("/home/steelmin/public_html/hf.php");
include_once('/home/steelmin/public_html/steeladmin/hf_360.php');  
include_once "/home/steelmin/public_html/plantbee/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['email'])){    
    // Validate if all required fields are present
    $required_fields = [
        'name', 'email', 'mobile', 'message'        
    ];
    
    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['name']), ENT_QUOTES);   
    $email = htmlentities(trim($_POST['email']), ENT_QUOTES);
    $mobile = trim($_POST['mobile']);
    $message = htmlentities(trim($_POST['message']), ENT_QUOTES); 
    
    $utm_campaign =htmlentities(trim($_POST['utm_campaign']), ENT_QUOTES); 
    $utm_medium =htmlentities(trim($_POST['utm_medium']), ENT_QUOTES); 
    $currDateTime = date("Y-m-d H:i:s");
    
   
    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;
    }
  
    $camp_code ='CAMP205';
    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'];        
                    
        $arr['tbl_members']=array(
            'name'=>$first_name,           
            'billing_name'=>$first_name,            
            'email'=>$email, 
            'username'=>$email,           
            'phoneno'=>$mobile, 
            'ipaddress'=>$ip, 				 
            'date'=>$currDateTime, 
            'main_source'=>'Marcom Paid Campaign',
            'source_from'=>"New",
            'password'=>$mobile,
            'ol_be'=>'Paid Campaign',
            '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,      
        'ip' => $ip,
        'refral_url' => ''
    );
    addCampaignHistory($field_array);
    
    
    $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 .=  "Message => ".$message." <br /> ";
    $comments .=  "utm_campaign => ".$utm_campaign." <br /> ";
    $comments .=  "utm_medium => ".$utm_medium;
   
  
						
    mysql_query("INSERT INTO tbl_comments(memid, `f_date`, `f_time`, `comments`, `created_by`, `comments_for`) VALUES ($memid, '$dt', '$tm', '$comments', 'Created from report campaign page', 'SM')") or die(mysql_error());   
   
  
    $lead_type='A1';$mood='';$fudate='';       
    $leadtypeArr = getLastLeadType($memid,'W',$dflag='true');
  
    if(isset($leadtypeArr['lead_type']) && $leadtypeArr['lead_type']!='')
        $lead_type = $leadtypeArr['lead_type'];
    if(isset($leadtypeArr['mood'])  && $leadtypeArr['lead_type']!='')
        $mood = $leadtypeArr['mood'];
    if(isset($leadtypeArr['fudate']) && $leadtypeArr['fudate']!='')
        $fudate = $leadtypeArr['fudate']; 
    
    if($lead_type=='A5' || $lead_type=='B5')
        $lead_type='A11';
    
    $rm_arr =getRMForRoutingProcess($memid, 'steelmint', $lead_type); 
    
    if(empty($rm_arr)){
        $rm_arr['rm_id'] = 0;
        $mood = 0;
        $rm_arr['subrm_id'] = 0;
        $rm_arr['first_log_id'] = 0;
    }
    $arr_reg['tbl_reg_logs'] =
        array(
                'memid'=>$memid,
                'startdate'=>$dt,
                'enddate'=>$dt,
                'f_time'=>$tm,
                'f_product'=>'W',
                'status'=>'Incomplete',
                'selected_products'=>'',
                'created_on'=>$currDateTime,
                'lead_type'=>$lead_type,
                'fudate'=>'',
                'rm'=>$rm_arr['rm_id'],
                'subrm'=>$rm_arr['subrm_id'],
                'mood'=>'',
                'pipeline_status'=>'New',
                'routing_status'=>$lead_type,
                'first_log_id' => $rm_arr['first_log_id']
        );
    $res_arr_reg = create_logs_from_array_of_array($arr_reg);
    $reg_id=$res_arr_reg['tbl_reg_logs'];
    if(isset($arr_reg['tbl_reg_logs']) && $rm_arr['first_log_id']=='' && !empty($reg_id)){
        $_newUpdtArr['tbl_reg_logs']['first_log_id']=$reg_id;
        update_table_from_array_of_array($_newUpdtArr,$reg_id);
    }
    updateTbltbl_updated_reg_logs($memid);    
   //Successfully registered message
  
    $data['status']=true;
    $data['message']="Your message has been sent successfully!";   
    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");
?>
