<?php 


//Sign in
function login($email,$password) {

    global $conn;

    $user_email = strip_tags(mysqli_real_escape_string($conn,trim($email)));
    $user_password = strip_tags(mysqli_real_escape_string($conn,trim($password)));

    $login_query = "SELECT * FROM admin WHERE email = '".$user_email."'";

    $login_res = mysqli_query($conn, $login_query);
    if (mysqli_num_rows($login_res) == 1) {
        $rows = mysqli_fetch_array($login_res);
        $password_hash = $rows['password'];
        //session_start();

    	if(password_verify($user_password,$password_hash)){
            //if(in_array($_SERVER['REMOTE_ADDR'], explode(',', $rows['ip_address']))){
                $_SESSION['email'] = $rows['email'];
                $_SESSION['admin_id'] = $rows['admin_id'];
                $_SESSION['name'] = $rows['name'];

                $query = "INSERT INTO admin_log (`admin_id`, `name`, `ip_address`) VALUES('".$rows['admin_id']."','".$rows['name']."','".$_SERVER['REMOTE_ADDR']."')";

                mysqli_query($conn, $query);

                echo "<script>window.location='dashboard.php'</script>";
            //} else {
             //   echo "<script>window.location='index.php?login=fail'</script>";
            //}
    	}
    	else{
    		echo "<script>window.location='index.php?login=fail'</script>";
    	}
	   echo "<script>window.location='dashboard.php'</script>";

    } else {
        echo "<script>window.location='index.php?login=failed'</script>";
    }
}

function search_company(){

    global $conn;

    $fleet_size_from = explode("-", $_REQUEST['fleet_size'])[0];
    $fleet_size_to = explode("-", $_REQUEST['fleet_size'])[1];
    $query = "SELECT co.*, SUM(oc.vehicles) as total_fleet_size FROM company co JOIN operating_centres oc ON co.licence_number=oc.licence_number WHERE co.postcode LIKE '".$_REQUEST['postcode']."%' GROUP BY licence_number HAVING SUM(oc.vehicles) > ".$fleet_size_from." AND SUM(oc.vehicles) < ".$fleet_size_to." AND phone!=''";

    return mysqli_query($conn, $query);
}

function update_company_status($status, $company_id){

    global $conn;

    if($status == 'no_status'){
        $query = "UPDATE company SET status = NULL WHERE company_id=".$company_id;
    }else{
        $query = "UPDATE company SET status = '".$status."' WHERE company_id=".$company_id;
    }

    return mysqli_query($conn, $query);
}

function sales_users(){
    global $conn;

    $query = "SELECT * from sales WHERE is_active = 1";

    return mysqli_query($conn, $query);
}

function save_searching($fleet_size, $postcode, $sales_id){
    global $conn;

    $query = "INSERT INTO saved_search (fleet_size, postcode, sales_id) VALUES('".$fleet_size."', '".$postcode."','".$sales_id."')";

    return mysqli_query($conn, $query);
}

function get_saved_search(){
    global $conn;

    $query = "SELECT saved_search.*, CONCAT(sales.first_name, ' ', sales.last_name) AS sales_name from saved_search JOIN sales 
            ON sales.sales_id = saved_search.sales_id ORDER BY saved_search.saved_search_id DESC";

    return mysqli_query($conn, $query);

     //mysqli_fetch_assoc($result);
}

function get_details($company_id){
    global $conn;

    $query = "SELECT co.*,lc.*, (SELECT SUM(oc.vehicles) FROM operating_centres oc WHERE oc.licence_number=co.licence_number) as total_fleet_size from company co
    JOIN licence lc ON lc.company_id=co.company_id
    WHERE co.company_id = ".$company_id;

    $result = mysqli_query($conn, $query);

    return mysqli_fetch_assoc($result);
}
/*function get_directors($company_id){
    global $conn;

    $query = "SELECT CONCAT(first_name, ' ', middle_name, ' ', surname) AS fullname, email from directors WHERE company_id = '".$company_id."' ";

    return mysqli_query($conn, $query);

    // return mysqli_fetch_assoc($result);   
}*/

function get_operat_center ($licence_number){
    global $conn;

    $query = "SELECT * from operating_centres WHERE licence_number = '".$licence_number."' ";

    return mysqli_query($conn, $query);
}

function get_transport_managers ($licence_number){
    global $conn;

    $query = "SELECT *, CONCAT(first_name, ' ', middle_name, ' ', surname) AS fullname, email from transport_managers WHERE licence_number = '".$licence_number."' ";

    return mysqli_query($conn, $query);
}

function get_licence_directors ($company_id){
    global $conn;

    $query = "SELECT *, CONCAT(first_name, ' ', middle_name, ' ', surname) AS fullname, email from  directors WHERE company_id = '".$company_id."' ";

    return mysqli_query($conn, $query);
}

function get_stats($postcode = ''){
    
    $count_array = array();

    global $conn;

    if(isset($postcode) && $postcode != ''){
        $total_query = "SELECT count(*) as cnt from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE vol.postcode LIKE '%".$postcode."%'";
    }else{
        $total_query = "SELECT count(*) as cnt from company";
    }
    $total_reult = mysqli_query($conn, $total_query);
    $count_array['total_count'] = mysqli_fetch_assoc($total_reult)['cnt'];
    $count_array['total_count'];


    if(isset($postcode) && $postcode != ''){
        $no_phone_no_website_query = "SELECT count(*) as cnt from company vol WHERE vol.postcode LIKE '%".$postcode."%' AND phone='' AND website=''";
    }else{
        $no_phone_no_website_query = "SELECT count(*) as cnt from company vol WHERE phone='' AND website=''";
    }
    $no_phone_no_website_result = mysqli_query($conn, $no_phone_no_website_query);
    $count_array['no_phone_no_website_count'] = mysqli_fetch_assoc($no_phone_no_website_result)['cnt'];



    if(isset($postcode) && $postcode != ''){
        $no_phone_yes_website_query = "SELECT count(*) as cnt from company vol WHERE vol.postcode LIKE '%".$postcode."%' AND phone='' AND website!=''";
    }else{
        $no_phone_yes_website_query = "SELECT count(*) as cnt from company vol WHERE phone='' AND website!=''";
    }
    $no_phone_yes_website_result = mysqli_query($conn, $no_phone_yes_website_query);
    $count_array['no_phone_yes_website_count'] = mysqli_fetch_assoc($no_phone_yes_website_result)['cnt'];
    

    if(isset($postcode) && $postcode != ''){
        $yes_phone_no_website_query = "SELECT count(*) as cnt from company vol WHERE vol.postcode LIKE '%".$postcode."%' AND phone!='' AND website=''";
    }else{
        $yes_phone_no_website_query = "SELECT count(*) as cnt from company vol WHERE phone!='' AND website=''";
    }
    $yes_phone_no_website_result = mysqli_query($conn, $yes_phone_no_website_query);
    $count_array['yes_phone_no_website_count'] = mysqli_fetch_assoc($yes_phone_no_website_result)['cnt'];


    if(isset($postcode) && $postcode != ''){
        $yes_phone_no_query = "SELECT count(*) as cnt from company vol WHERE vol.postcode LIKE '%".$postcode."%' AND phone!=''";
    }else{
        $yes_phone_no_query = "SELECT count(*) as cnt from company vol WHERE phone!=''";
    }
    $yes_phone_result = mysqli_query($conn, $yes_phone_no_query);
    $count_array['yes_phone_result'] = mysqli_fetch_assoc($yes_phone_result)['cnt'];

    return json_encode($count_array);
}

function get_stats_fleet_size($postcode = ''){
    $count_array = array();

    global $conn;

    if(isset($postcode) && $postcode != ''){
        $total_query = "SELECT count(*) as total_1_9 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 1 AND (voloc.vehicles) < 9 AND vol.postcode LIKE '%".$postcode."%'";
        $total_reult = mysqli_query($conn, $total_query);
        $total_1_9 = mysqli_fetch_assoc($total_reult)['total_1_9'];

        $total_query = "SELECT count(*) as total_10_19 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 10 AND (voloc.vehicles) < 19 AND vol.postcode LIKE '%".$postcode."%'";
        $total_reult = mysqli_query($conn, $total_query);
        $total_10_19 = mysqli_fetch_assoc($total_reult)['total_10_19'];

        $total_query = "SELECT count(*) as total_20_39 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 20 AND (voloc.vehicles) < 39 AND vol.postcode LIKE '%".$postcode."%'";
        $total_reult = mysqli_query($conn, $total_query);
        $total_20_39 = mysqli_fetch_assoc($total_reult)['total_20_39'];

        $total_query = "SELECT count(*) as total_40_99 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 40 AND (voloc.vehicles) < 99 AND vol.postcode LIKE '%".$postcode."%'";
        $total_reult = mysqli_query($conn, $total_query);
        $total_40_99 = mysqli_fetch_assoc($total_reult)['total_40_99'];

        $total_query = "SELECT count(*) as total_100 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 100 AND vol.postcode LIKE '%".$postcode."%'";
        $total_reult = mysqli_query($conn, $total_query);
        $total_100 = mysqli_fetch_assoc($total_reult)['total_100'];

    }else{

        $total_query = "SELECT count(*) as total_1_9 from operating_centres voloc WHERE (voloc.vehicles) > 1 AND (voloc.vehicles) < 9";
        $total_reult = mysqli_query($conn, $total_query);
        $total_1_9 = mysqli_fetch_assoc($total_reult)['total_1_9'];

        $total_query = "SELECT count(*) as total_10_19 from operating_centres voloc WHERE (voloc.vehicles) > 10 AND (voloc.vehicles) < 19";
        $total_reult = mysqli_query($conn, $total_query);
        $total_10_19 = mysqli_fetch_assoc($total_reult)['total_10_19'];

        $total_query = "SELECT count(*) as total_20_39 from operating_centres voloc WHERE (voloc.vehicles) > 20 AND (voloc.vehicles) < 39";
        $total_reult = mysqli_query($conn, $total_query);
        $total_20_39 = mysqli_fetch_assoc($total_reult)['total_20_39'];

        $total_query = "SELECT count(*) as total_40_99 from operating_centres voloc WHERE (voloc.vehicles) > 40 AND (voloc.vehicles) < 99";
        $total_reult = mysqli_query($conn, $total_query);
        $total_40_99 = mysqli_fetch_assoc($total_reult)['total_40_99'];

        $total_query = "SELECT count(*) as total_100 from operating_centres voloc WHERE (voloc.vehicles) > 100";
        $total_reult = mysqli_query($conn, $total_query);
        $total_100 = mysqli_fetch_assoc($total_reult)['total_100'];
    }
    
    return json_encode(array('total_1_9' => $total_1_9, 'total_10_19' => $total_10_19, 'total_20_39' => $total_20_39, 'total_40_99' => $total_40_99, 'total_100' => $total_100));
}

function get_stats_fleet_size_no_web_phone($postcode = ''){
    $count_array = array();

    global $conn;

    if(isset($postcode) && $postcode != ''){
        $total_query = "SELECT count(*) as total_1_9 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 1 AND (voloc.vehicles) < 9 AND vol.postcode LIKE '%".$postcode."%' AND phone='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_1_9 = mysqli_fetch_assoc($total_reult)['total_1_9'];

        $total_query = "SELECT count(*) as total_10_19 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 10 AND (voloc.vehicles) < 19 AND vol.postcode LIKE '%".$postcode."%' AND phone='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_10_19 = mysqli_fetch_assoc($total_reult)['total_10_19'];

        $total_query = "SELECT count(*) as total_20_39 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 20 AND (voloc.vehicles) < 39 AND vol.postcode LIKE '%".$postcode."%' AND phone='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_20_39 = mysqli_fetch_assoc($total_reult)['total_20_39'];

        $total_query = "SELECT count(*) as total_40_99 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 40 AND (voloc.vehicles) < 99 AND vol.postcode LIKE '%".$postcode."%' AND phone='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_40_99 = mysqli_fetch_assoc($total_reult)['total_40_99'];

        $total_query = "SELECT count(*) as total_100 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 100 AND vol.postcode LIKE '%".$postcode."%' AND phone='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_100 = mysqli_fetch_assoc($total_reult)['total_100'];

    } else {

        $total_query = "SELECT count(*) as total_1_9 from operating_centres voloc WHERE (voloc.vehicles) > 1 AND (voloc.vehicles) < 9 AND phone='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_1_9 = mysqli_fetch_assoc($total_reult)['total_1_9'];

        $total_query = "SELECT count(*) as total_10_19 from operating_centres voloc WHERE (voloc.vehicles) > 10 AND (voloc.vehicles) < 19 AND phone='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_10_19 = mysqli_fetch_assoc($total_reult)['total_10_19'];

        $total_query = "SELECT count(*) as total_20_39 from operating_centres voloc WHERE (voloc.vehicles) > 20 AND (voloc.vehicles) < 39 AND phone='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_20_39 = mysqli_fetch_assoc($total_reult)['total_20_39'];

        $total_query = "SELECT count(*) as total_40_99 from operating_centres voloc WHERE (voloc.vehicles) > 40 AND (voloc.vehicles) < 99 AND phone='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_40_99 = mysqli_fetch_assoc($total_reult)['total_40_99'];

        $total_query = "SELECT count(*) as total_100 from operating_centres voloc WHERE (voloc.vehicles) > 100 AND phone='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_100 = mysqli_fetch_assoc($total_reult)['total_100'];
    }
    
    return json_encode(array('total_1_9' => $total_1_9, 'total_10_19' => $total_10_19, 'total_20_39' => $total_20_39, 'total_40_99' => $total_40_99, 'total_100' => $total_100));
}

function get_stats_fleet_size_no_phone($postcode = ''){
    $count_array = array();

    global $conn;

    if(isset($postcode) && $postcode != ''){
        $total_query = "SELECT count(*) as total_1_9 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 1 AND (voloc.vehicles) < 9 AND vol.postcode LIKE '%".$postcode."%' AND phone='' AND website!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_1_9 = mysqli_fetch_assoc($total_reult)['total_1_9'];

        $total_query = "SELECT count(*) as total_10_19 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 10 AND (voloc.vehicles) < 19 AND vol.postcode LIKE '%".$postcode."%' AND phone='' AND website!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_10_19 = mysqli_fetch_assoc($total_reult)['total_10_19'];

        $total_query = "SELECT count(*) as total_20_39 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 20 AND (voloc.vehicles) < 39 AND vol.postcode LIKE '%".$postcode."%' AND phone='' AND website!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_20_39 = mysqli_fetch_assoc($total_reult)['total_20_39'];

        $total_query = "SELECT count(*) as total_40_99 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 40 AND (voloc.vehicles) < 99 AND vol.postcode LIKE '%".$postcode."%' AND phone='' AND website!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_40_99 = mysqli_fetch_assoc($total_reult)['total_40_99'];

        $total_query = "SELECT count(*) as total_100 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 100 AND vol.postcode LIKE '%".$postcode."%' AND phone='' AND website!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_100 = mysqli_fetch_assoc($total_reult)['total_100'];

    } else {

        $total_query = "SELECT count(*) as total_1_9 from operating_centres voloc WHERE (voloc.vehicles) > 1 AND (voloc.vehicles) < 9 AND phone='' AND website!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_1_9 = mysqli_fetch_assoc($total_reult)['total_1_9'];

        $total_query = "SELECT count(*) as total_10_19 from operating_centres voloc WHERE (voloc.vehicles) > 10 AND (voloc.vehicles) < 19 AND phone='' AND website!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_10_19 = mysqli_fetch_assoc($total_reult)['total_10_19'];

        $total_query = "SELECT count(*) as total_20_39 from operating_centres voloc WHERE (voloc.vehicles) > 20 AND (voloc.vehicles) < 39 AND phone='' AND website!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_20_39 = mysqli_fetch_assoc($total_reult)['total_20_39'];

        $total_query = "SELECT count(*) as total_40_99 from operating_centres voloc WHERE (voloc.vehicles) > 40 AND (voloc.vehicles) < 99 AND phone='' AND website!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_40_99 = mysqli_fetch_assoc($total_reult)['total_40_99'];

        $total_query = "SELECT count(*) as total_100 from operating_centres voloc WHERE (voloc.vehicles) > 100 AND phone='' AND website!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_100 = mysqli_fetch_assoc($total_reult)['total_100'];
    }
    
    return json_encode(array('total_1_9' => $total_1_9, 'total_10_19' => $total_10_19, 'total_20_39' => $total_20_39, 'total_40_99' => $total_40_99, 'total_100' => $total_100));
}

function get_stats_fleet_size_no_web($postcode = ''){
    $count_array = array();

    global $conn;

    if(isset($postcode) && $postcode != ''){
        $total_query = "SELECT count(*) as total_1_9 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 1 AND (voloc.vehicles) < 9 AND vol.postcode LIKE '%".$postcode."%' AND phone!='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_1_9 = mysqli_fetch_assoc($total_reult)['total_1_9'];

        $total_query = "SELECT count(*) as total_10_19 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 10 AND (voloc.vehicles) < 19 AND vol.postcode LIKE '%".$postcode."%' AND phone!='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_10_19 = mysqli_fetch_assoc($total_reult)['total_10_19'];

        $total_query = "SELECT count(*) as total_20_39 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 20 AND (voloc.vehicles) < 39 AND vol.postcode LIKE '%".$postcode."%' AND phone!='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_20_39 = mysqli_fetch_assoc($total_reult)['total_20_39'];

        $total_query = "SELECT count(*) as total_40_99 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 40 AND (voloc.vehicles) < 99 AND vol.postcode LIKE '%".$postcode."%' AND phone!='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_40_99 = mysqli_fetch_assoc($total_reult)['total_40_99'];

        $total_query = "SELECT count(*) as total_100 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 100 AND vol.postcode LIKE '%".$postcode."%' AND phone!='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_100 = mysqli_fetch_assoc($total_reult)['total_100'];

    } else {

        $total_query = "SELECT count(*) as total_1_9 from operating_centres voloc WHERE (voloc.vehicles) > 1 AND (voloc.vehicles) < 9 AND phone!='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_1_9 = mysqli_fetch_assoc($total_reult)['total_1_9'];

        $total_query = "SELECT count(*) as total_10_19 from operating_centres voloc WHERE (voloc.vehicles) > 10 AND (voloc.vehicles) < 19 AND phone!='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_10_19 = mysqli_fetch_assoc($total_reult)['total_10_19'];

        $total_query = "SELECT count(*) as total_20_39 from operating_centres voloc WHERE (voloc.vehicles) > 20 AND (voloc.vehicles) < 39 AND phone!='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_20_39 = mysqli_fetch_assoc($total_reult)['total_20_39'];

        $total_query = "SELECT count(*) as total_40_99 from operating_centres voloc WHERE (voloc.vehicles) > 40 AND (voloc.vehicles) < 99 AND phone!='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_40_99 = mysqli_fetch_assoc($total_reult)['total_40_99'];

        $total_query = "SELECT count(*) as total_100 from operating_centres voloc WHERE (voloc.vehicles) > 100 AND phone!='' AND website=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_100 = mysqli_fetch_assoc($total_reult)['total_100'];
    }
    
    return json_encode(array('total_1_9' => $total_1_9, 'total_10_19' => $total_10_19, 'total_20_39' => $total_20_39, 'total_40_99' => $total_40_99, 'total_100' => $total_100));
}

function get_stats_phone_no($postcode = ''){
    $count_array = array();

    global $conn;

    if(isset($postcode) && $postcode != ''){
        $total_query = "SELECT count(*) as total_1_9 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 1 AND (voloc.vehicles) < 9 AND vol.postcode LIKE '%".$postcode."%' AND phone!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_1_9 = mysqli_fetch_assoc($total_reult)['total_1_9'];

        $total_query = "SELECT count(*) as total_10_19 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 10 AND (voloc.vehicles) < 19 AND vol.postcode LIKE '%".$postcode."%' AND phone!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_10_19 = mysqli_fetch_assoc($total_reult)['total_10_19'];

        $total_query = "SELECT count(*) as total_20_39 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 20 AND (voloc.vehicles) < 39 AND vol.postcode LIKE '%".$postcode."%' AND phone!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_20_39 = mysqli_fetch_assoc($total_reult)['total_20_39'];

        $total_query = "SELECT count(*) as total_40_99 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 40 AND (voloc.vehicles) < 99 AND vol.postcode LIKE '%".$postcode."%' AND phone!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_40_99 = mysqli_fetch_assoc($total_reult)['total_40_99'];

        $total_query = "SELECT count(*) as total_100 from company vol JOIN operating_centres voloc ON vol.licence_number = voloc.licence_number WHERE (voloc.vehicles) > 100 AND vol.postcode LIKE '%".$postcode."%' AND phone!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_100 = mysqli_fetch_assoc($total_reult)['total_100'];

    } else {

        $total_query = "SELECT count(*) as total_1_9 from operating_centres voloc WHERE (voloc.vehicles) > 1 AND (voloc.vehicles) < 9 AND phone!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_1_9 = mysqli_fetch_assoc($total_reult)['total_1_9'];

        $total_query = "SELECT count(*) as total_10_19 from operating_centres voloc WHERE (voloc.vehicles) > 10 AND (voloc.vehicles) < 19 AND phone!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_10_19 = mysqli_fetch_assoc($total_reult)['total_10_19'];

        $total_query = "SELECT count(*) as total_20_39 from operating_centres voloc WHERE (voloc.vehicles) > 20 AND (voloc.vehicles) < 39 AND phone!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_20_39 = mysqli_fetch_assoc($total_reult)['total_20_39'];

        $total_query = "SELECT count(*) as total_40_99 from operating_centres voloc WHERE (voloc.vehicles) > 40 AND (voloc.vehicles) < 99 AND phone!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_40_99 = mysqli_fetch_assoc($total_reult)['total_40_99'];

        $total_query = "SELECT count(*) as total_100 from operating_centres voloc WHERE (voloc.vehicles) > 100 AND phone!=''";
        $total_reult = mysqli_query($conn, $total_query);
        $total_100 = mysqli_fetch_assoc($total_reult)['total_100'];
    }
    
    return json_encode(array('total_1_9' => $total_1_9, 'total_10_19' => $total_10_19, 'total_20_39' => $total_20_39, 'total_40_99' => $total_40_99, 'total_100' => $total_100));
}

function send_data_to_api($enquiry){
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://admin2.fleetfocus.co.uk/api/lead-submit.php");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 
              http_build_query(array('lead_id' => 1, 'key' => 'dsiusd867906sdiyy89798798sfsfsdf98797777887sfsfsfsfsf', 'company' => $enquiry['company'], 'first_name' => $enquiry['first_name'], 'last_name' => $enquiry['last_name'], 'email' => $enquiry['email'], 'phone' => $enquiry['phone'], 'postcode' => $enquiry['postcode'], 'fleet_size' => $enquiry['fleet_size'], 'lead_notes' => '-', 'traffic_source' => 'OLDP')));
    // Receive server response ...
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $server_output = curl_exec($ch);
    return 1;
}
?>