<?php
require_once("/home/steelmin/public_html/inc/config.php");
require_once("/home/steelmin/public_html/steeladmin/commonfunc.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);
// Set content type to JSON
header('Content-Type: application/json');

// Check if the request method is POST
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get the JSON input
    $input = json_decode(file_get_contents('php://input'), true);   
    
    if(isset($input['insert_tbl_ref_page']) && strtoupper($input['insert_tbl_ref_page'])=='Y'){
        // Validate input
        if (isset($input['f_date'], $input['referred_page'], $input['referring_page'])) {
            $dt = mysql_real_escape_string($input['f_date']);
            $curpage = mysql_real_escape_string($input['referred_page']);
            $referring_page = mysql_real_escape_string($input['referring_page']);
            $event_id = $input['event_id'];
            // Prepare the SQL query
            $qry = "INSERT INTO `tbl_ref_page` (`f_date`, `referred_page`, `referring_page`,`event_id`) VALUES ('$dt', '$curpage', '$referring_page', '$event_id' )";

            // Execute the query
            if (mysql_query($qry)) {
                // Successful insert
                echo json_encode(array("status" => "success", "message" => "Record inserted successfully."));
            } else {
                // Error inserting
                echo json_encode(array("status" => "error", "message" => "Error inserting record: " . mysql_error()));
            }
        } else {
            // Missing input parameters
            echo json_encode(array("status" => "error", "message" => "Missing required parameters."));
        }
    }elseif(isset($input['create_combo']) && strtoupper($input['create_combo'])=='Y'){
        // Validate input
        if (isset($input['dept_sql']) && !empty($input['dept_sql'])) {
            $dept_sql = trim($input['dept_sql']);
            
            $sel_box_html = create_combo($dept_sql,'companytype','id','name','Company Type *','','','js-custom-select u-select-v1 g-brd-gray-light-v3 g-color-gray-dark-v5 rounded g-py-12',false,'');
           
            if (!empty($sel_box_html)) {                
                echo json_encode(array("status" => "success", "data" => $sel_box_html));
            } else {               
                echo json_encode(array("status" => "error", "message" => "Error inserting record: " . mysql_error()));
            }
        } else {            
            echo json_encode(array("status" => "error", "message" => "Missing required parameters."));
        }
    }elseif(isset($input['getCityOrCountry']) && strtoupper($input['getCityOrCountry'])=='Y'){
        // Validate input
        if (isset($input['ip_countryName'])) {            
            $ip_countryName = trim($input['ip_countryName']);
            $city_country = trim($input['city_country']);
            $other_city = trim($input['other_city']);
            $label = isset($input['label']) ? trim($input['label']) : '';
            $disabled = isset($input['disabled']) ? trim($input['disabled']) : '';
            $combo_name_arr = isset($input['combo_name_arr']) ? $input['combo_name_arr'] : '';
            $no = isset($input['no']) ? trim($input['no']) : '';

            // Call the function with the gathered parameters            
           getCityOrCountry($ip_countryName, $city_country, $other_city, $label, $disabled, $combo_name_arr, $no);

        } else {            
            echo "Missing required parameters..";
        }
    }
}else{
    if (isset($_REQUEST['getActiveEventID']) && strtoupper($_REQUEST['getActiveEventID']) == 'Y'){
        $eventId = qryToCommaSepString("SELECT id FROM tbl_sm_events_events ORDER BY other_event_name, id DESC LIMIT 1");
        if (!empty($eventId)) {
            // Create an associative array with status and event_id
            $response = array(
                "status" => "success",
                "event_id" => $eventId
            );
        } else {
            // Handle case where $eventId is empty
            $response = array(
                "status" => "error",
                "message" => "No active event found"
            );
        }    
        // Output JSON
        echo json_encode($response);
        
    }
    elseif (isset($_REQUEST['get_latest_event_details']) && strtoupper($_REQUEST['get_latest_event_details']) == 'Y' && isset($_REQUEST['event_id']) &&  !empty($_REQUEST['event_id']) ){
        
        $eventId = TRIM($_REQUEST['event_id']);
        $event_details = '';
        if(!empty($eventId))
            $event_details = get_latest_event_details($eventId);
       
        if (!empty($event_details)) {
            // Create an associative array with status and event_id
            $response = array(
                "status" => "success",
                "event_details" => $event_details
            );
        } else {
            // Handle case where $eventId is empty
            $response = array(
                "status" => "error",
                "message" => "No active event found."
            );
        }    
        // Output JSON
        echo json_encode($response);
        
    }if (isset($_REQUEST['getState']) && strtoupper($_REQUEST['getState']) == 'Y'  && isset($_REQUEST['city_id']) &&  !empty($_REQUEST['city_id'])){
        
        
        $stcnt=mysql_fetch_array(mysql_query("select state_id from tbl_state_city where city_id='".$_REQUEST['city_id']."' and country_id=113 limit 1"));
        $state = $stcnt['state_id'];
            
        if (!empty($state)) {
            // Create an associative array with status and event_id
            $response = array(
                "status" => "success",
                "state" => $state
            );
        } else {
            // Handle case where $eventId is empty
            $response = array(
                "status" => "error",
                "message" => "No active event found"
            );
        }    
        // Output JSON
        echo json_encode($response);
        
    }
}
?>
