<?php
function get_city_name($city_id)
{
	global $conn;
	if($city_id){
	 // echo "select tbl_city_name from tbl_city where 1 and tbl_city_flag=0 and tbl_city_id='".$city_id."'";
    	$sql_usr=mysqli_fetch_array(mysqli_query($conn,"select tbl_city_name from tbl_city where 1 and tbl_city_flag=0 and tbl_city_id='".$city_id."'"));
    	return $sql_usr['tbl_city_name'];  
	}else
        {
            	return '';  
        }


	
}

function get_source_name($source_id)
{
	global $conn;
	$sql_usr=mysqli_fetch_array(mysqli_query($conn,"select source_name from tbl_lead_source where 1 and flag=0 and tbl_lead_source_id='".$source_id."'"));
	return $sql_usr['source_name'];
}

function get_source_id($source_code)
{
	global $conn;
	$sql_usr=mysqli_fetch_array(mysqli_query($conn,"select tbl_lead_source_id  from tbl_lead_source where 1 and flag=0 and source_code='".$source_code."'"));
	return $sql_usr['tbl_lead_source_id '];
}
function get_project_name($lead_user_id)
{
	global $conn;
//echo "select tbl_lead_user_prj_id from tbl_lead_user_projects where 1 and	tbl_lead_user_id='".$lead_user_id."'";
$sql_usr=mysqli_query($conn,"select tbl_lead_user_prj_id from tbl_lead_user_projects where 1 and	tbl_lead_user_id='".$lead_user_id."'");
	$project_ids = [];
    while ($row = mysqli_fetch_array($sql_usr)) {
        $project_ids[] = $row['tbl_lead_user_prj_id'];
    }
   // print_r($project_ids);
    
    $project_names_str='';
    if (!empty($project_ids)) {
        $project_ids_str = implode(',', $project_ids); // Convert array to comma-separated values
    //echo $project_ids_str;
        // Fetch project names based on project IDs
       // echo "SELECT tbl_project_name FROM tbl_project WHERE tbl_project_id IN ($project_ids_str)";
        $sql_projects = mysqli_query($conn, "SELECT tbl_project_name FROM tbl_project WHERE tbl_project_id IN ($project_ids_str)");
        if($sql_projects){
            $project_names = [];
            while ($row = mysqli_fetch_array($sql_projects)) {
                $project_names[] = $row['tbl_project_name'];
            }
        
            // Convert project names into a comma-separated string
            if (!empty($project_names)) {
                $project_names_str = implode(', ', $project_names);
            } 
        }
        else{
            $project_names_str='';
        }
        
    }
    
	return $project_names_str;
}

function get_lead_project_id($lead_user_id)
{
	global $conn;
    $sql_usr=mysqli_fetch_array(mysqli_query($conn,"select tbl_lead_user_prj_id from tbl_lead_user_projects where 1 and	tbl_lead_user_id='".$lead_user_id."'"));
    
    if($sql_usr){
        $project_id = $sql_usr['tbl_lead_user_prj_id'];
    }
    else {
        $project_id = 0;
    }
    
    
	return $project_id;
}

function get_project_id($lead_user_id)
{
	global $conn;
//echo "select tbl_lead_user_prj_id from tbl_lead_user_projects where 1 and	tbl_lead_user_id='".$lead_user_id."'";
$sql_usr=mysqli_query($conn,"select tbl_lead_user_prj_id from tbl_lead_user_projects where 1 and	tbl_lead_user_id='".$lead_user_id."'");
	$project_ids = [];
    while ($row = mysqli_fetch_array($sql_usr)) {
        $project_ids[] = $row['tbl_lead_user_prj_id'];
    }
   // print_r($project_ids);
    
    $project_names_str='';
    if (!empty($project_ids)) {
        $project_ids_str = implode(',', $project_ids); // Convert array to comma-separated values
    //echo $project_ids_str;
        // Fetch project names based on project IDs
       // echo "SELECT tbl_project_name FROM tbl_project WHERE tbl_project_id IN ($project_ids_str)";
        $sql_projects = mysqli_query($conn, "SELECT tbl_project_name FROM tbl_project WHERE tbl_project_id IN ($project_ids_str)");
        if($sql_projects){
            $project_names = [];
            while ($row = mysqli_fetch_array($sql_projects)) {
                $project_names[] = $row['tbl_project_name'];
            }
        
            // Convert project names into a comma-separated string
            if (!empty($project_names)) {
                $project_names_str = implode(', ', $project_names);
            } 
        }
        else{
            $project_names_str='';
        }
        
    }
    
	return $project_names_str;
}

function get_user_project_name($user_id)
{
	global $conn;
	$sql_usr=mysqli_query($conn,"select tbl_lead_user_access_projects_project_id from tbl_lead_user_access_projects where 1 and	tbl_lead_user_access_projects_user_id='".$user_id."'");
	$project_ids = [];
    while ($row = mysqli_fetch_array($sql_usr)) {
        $project_ids[] = $row['tbl_lead_user_access_projects_project_id'];
    }
    
    $project_names_str='';
    if (!empty($project_ids)) {
        $project_ids_str = implode(',', $project_ids); // Convert array to comma-separated values
    
        // Fetch project names based on project IDs
        $sql_projects = mysqli_query($conn, "SELECT tbl_project_name FROM tbl_project WHERE tbl_project_id IN ($project_ids_str)");
    
        $project_names = [];
        while ($row = mysqli_fetch_array($sql_projects)) {
            $project_names[] = $row['tbl_project_name'];
        }
    
        // Convert project names into a comma-separated string
        if (!empty($project_names)) {
            $project_names_str = implode(', ', $project_names);
        }
    }
    
	return $project_names_str;
}

function get_assigned_user_name($as_user_id)
{
	global $conn;
	$sql_usr=mysqli_fetch_array(mysqli_query($conn,"select name from tbl_invite_user where 1 and id='".$as_user_id."'"));
	if($sql_usr){
	    return $sql_usr['name'];
	}
	else{
	    return '';
	}
	
}

function get_assigned_user_mobile($as_user_id)
{
	global $conn;
	$sql_usr=mysqli_fetch_array(mysqli_query($conn,"select mobile_no from tbl_invite_user where 1 and id='".$as_user_id."'"));
	if($sql_usr){
	    return $sql_usr['mobile_no'];
	}
	else{
	    return '';
	}
	
}

function getproject_name($id)
{
	global $conn;
	$sql_usr=mysqli_fetch_array(mysqli_query($conn,"select tbl_project_name from tbl_project where 1 and tbl_project_id='".$id."'"));
	if($sql_usr){
	    return $sql_usr['tbl_project_name'];
	}
	else{
	    return '';
	}
	
}

function get_assigned_user($assigned_usr_id)
{
	global $conn;
	$sql_usr=mysqli_fetch_array(mysqli_query($conn,"select 	tbl_lead_user_assigned_user_id from tbl_lead_user where 1 and tbl_lead_user_id='".$assigned_usr_id."'"));
	return $sql_usr['tbl_lead_user_assigned_user_id'];
}

function send_sms($mobile_number,$message){
   
    // Build API URL with proper encoding
    $sms_url = "http://sms.wishsolution.com/api/sms/SendSMS.aspx?" . http_build_query([
    	'usr' => 'Vbuzz',
    	'key' => '9374CC90-6932-4B5C-B2C1-5C7B13DF96BA',
    	'smstype' => 'TextSMS',
    	'to' => $mobile_number,
    	'msg' => $message,
    	'rout' => 'Transactional',
    	'from' => 'VBuzzz',
    	'templateid' => '1607100000000347990'
    ]);
    
    // Send SMS using cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $sms_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    
    // Handle errors
    if (curl_errno($ch)) {
    	echo 'cURL error: ' . curl_error($ch);
    	curl_close($ch);
    	return false;
    } else {
    	echo 'SMS API Response: ' . $response;
    	curl_close($ch);
    	return true;
    	
    }
    
    
}

function send_sms_inp($mobile_number,$message){
   
    // Build API URL with proper encoding
    $sms_url = "http://sms.wishsolution.com/api/sms/SendSMS.aspx?" . http_build_query([
    	'usr' => 'Vbuzz',
    	'key' => '9374CC90-6932-4B5C-B2C1-5C7B13DF96BA',
    	'smstype' => 'TextSMS',
    	'to' => $mobile_number,
    	'msg' => $message,
    	'rout' => 'Transactional',
    	'from' => 'VBuzzz',
    	'templateid' => '1607100000000348049'
    ]);
    
    // Send SMS using cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $sms_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    
    // Handle errors
    if (curl_errno($ch)) {
    	echo 'cURL error: ' . curl_error($ch);
    	curl_close($ch);
    	return false;
    } else {
    	echo 'SMS API Response: ' . $response;
    	curl_close($ch);
    	return true;
    	
    }
    
    
}

function send_sms_eoi($mobile_number,$message){
   
    // Build API URL with proper encoding
    $sms_url = "http://sms.wishsolution.com/api/sms/SendSMS.aspx?" . http_build_query([
    	'usr' => 'Vbuzz',
    	'key' => '9374CC90-6932-4B5C-B2C1-5C7B13DF96BA',
    	'smstype' => 'TextSMS',
    	'to' => $mobile_number,
    	'msg' => $message,
    	'rout' => 'Transactional',
    	'from' => 'VBuzzz',
    	'templateid' => '1607100000000348043'
    ]);
    
    // Send SMS using cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $sms_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    
    // Handle errors
    if (curl_errno($ch)) {
    	echo 'cURL error: ' . curl_error($ch);
    	curl_close($ch);
    	return false;
    } else {
    	echo 'SMS API Response: ' . $response;
    	curl_close($ch);
    	return true;
    	
    }
    
    
}

function send_sms_site_visit_completed($mobile_number,$message){
   
    // Build API URL with proper encoding
    $sms_url = "http://sms.wishsolution.com/api/sms/SendSMS.aspx?" . http_build_query([
    	'usr' => 'Vbuzz',
    	'key' => '9374CC90-6932-4B5C-B2C1-5C7B13DF96BA',
    	'smstype' => 'TextSMS',
    	'to' => $mobile_number,
    	'msg' => $message,
    	'rout' => 'Transactional',
    	'from' => 'VBuzzz',
    	'templateid' => '1607100000000348045'
    ]);
    
    // Send SMS using cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $sms_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    
    // Handle errors
    if (curl_errno($ch)) {
    	echo 'cURL error: ' . curl_error($ch);
    	curl_close($ch);
    	return false;
    } else {
    	echo 'SMS API Response: ' . $response;
    	curl_close($ch);
    	return true;
    	
    }
    
    
}
function send_sms_site_scheduled($mobile_number,$message){
   
    // Build API URL with proper encoding
    $sms_url = "http://sms.wishsolution.com/api/sms/SendSMS.aspx?" . http_build_query([
    	'usr' => 'Vbuzz',
    	'key' => '9374CC90-6932-4B5C-B2C1-5C7B13DF96BA',
    	'smstype' => 'TextSMS',
    	'to' => $mobile_number,
    	'msg' => $message,
    	'rout' => 'Transactional',
    	'from' => 'VBuzzz',
    	'templateid' => '1607100000000348046'
    ]);
    
    // Send SMS using cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $sms_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    
    // Handle errors
    if (curl_errno($ch)) {
    	echo 'cURL error: ' . curl_error($ch);
    	curl_close($ch);
    	return false;
    } else {
    	echo 'SMS API Response: ' . $response;
    	curl_close($ch);
    	return true;
    	
    }
    
    
}
function send_sms_site_followup($mobile_number,$message){
   
    // Build API URL with proper encoding
    $sms_url = "http://sms.wishsolution.com/api/sms/SendSMS.aspx?" . http_build_query([
    	'usr' => 'Vbuzz',
    	'key' => '9374CC90-6932-4B5C-B2C1-5C7B13DF96BA',
    	'smstype' => 'TextSMS',
    	'to' => $mobile_number,
    	'msg' => $message,
    	'rout' => 'Transactional',
    	'from' => 'VBuzzz',
    	'templateid' => '1607100000000348049'
    ]);
    
    // Send SMS using cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $sms_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    
    // Handle errors
    if (curl_errno($ch)) {
    	echo 'cURL error: ' . curl_error($ch);
    	curl_close($ch);
    	return false;
    } else {
    	echo 'SMS API Response: ' . $response;
    	curl_close($ch);
    	return true;
    	
    }
    
    
}

?>