<?php

require '../includes/DB/connection.php';



/**
 * 
 */
class CenterFunction
{
	


	public function all_leads()
	{
        $connect=new Connection();
        $con=$connect->connect();

		// Query to select all leads
        $query = "SELECT * FROM `lead` l ,`user` u , `branch` b where l.user_id=u.user_id AND l.branch_id= b.branch_id";

        // Execute the query
        $result = $con->query($query);

        // Check if the query was successful
        if (!$result) {
            die("Error in query: " . $con->error);
        }

        // Fetch the results as an associative array
        $leads = [];
        while ($row = $result->fetch_assoc()) {
            $leads[] = $row;
        }

        // Free result set
        $result->free();
        

        return $leads;
	}


    public function pending_leads()
    {
        $connect=new Connection();
        $con=$connect->connect();


		// Query to select all leads
        $query = "SELECT * FROM `lead` l ,`user` u , `branch` b where l.user_id=u.user_id AND l.branch_id= b.branch_id AND l.lead_status=0;        ";

        // Execute the query
        $result = $con->query($query);

        // Check if the query was successful
        if (!$result) {
            die("Error in query: " . $con->error);
        }

        // Fetch the results as an associative array
        $leads = [];
        while ($row = $result->fetch_assoc()) {
            $leads[] = $row;
        }

        // Free result set
        $result->free();
        

        return $leads;
    }


    public function pending_lead_count()
    {

        $connect=new Connection();
        $con=$connect->connect();


        // Query to select all leads
        $query = "SELECT * FROM `lead` l ,`user` u , `branch` b where l.user_id=u.user_id AND l.branch_id= b.branch_id AND l.lead_status=0;";

        // Execute the query
        $result = $con->query($query);
        $rowcount=mysqli_num_rows($result);

        // Check if the query was successful
        if (!$result) {
            die("Error in query: " . $con->error);
        }

        return $rowcount;

    }

    

	public function add_lead($lead_first_name, $lead_last_name, $lead_phone_1, $lead_phone_2 , $lead_email , $lead_address ,$lead_source, $lead_pref_lang ,$campus_id ,$branch_id,$user_id)
	{


        $connect=new Connection();
        $con=$connect->connect();

		// Query to select all leads
        $query = "INSERT INTO `lead` (`lead_id`, `lead_first_name`, `lead_last_name`, `lead_phone_1`, `lead_phone_2`, `lead_email`, `lead_address`, `lead_source`, `lead_pref_lang`, `lead_status`, `lead_created_date`, `lead_created_time`, `lead_updated_date`, `lead_updated_time`, `campus_id`, `branch_id`, `user_id`) VALUES ('', '$lead_first_name', '$lead_last_name	', '$lead_phone_1', '$lead_phone_2', '$lead_email', '$lead_address', '$lead_source', '$lead_pref_lang', '1', CURDATE(), CURTIME(),CURDATE(), CURTIME(), '$campus_id', '$branch_id', '$user_id');";

        // Execute the query
        $result = $con->query($query);

        // Check if the query was successful
        if (!$result) {
            die("Error in query: " . $con->error);
        }


        if($result)
        {
            $response="<p style='color:green'>Lead Added Successfully</p>";
            return $response ;
        }
        else 
        {
            $response="<p style='color:red'>Lead Added Failed</p>";
            return $response ;
        }
        // Fetch the results as an associative array

		
	}


    public function all_branch()
    {
        $connect=new Connection();
        $con=$connect->connect();

        $query = "SELECT * FROM `branch`";

        // Execute the query
        $result = $con->query($query);
        return $result;


    }


    public function all_campus()
    {
        $connect=new Connection();
        $con=$connect->connect();

        $query = "SELECT * FROM `campus`";

        // Execute the query
        $result = $con->query($query);
        return $result;


    }

}


// $cf=new CenterFunction();

// $rst= $cf->all_leads();

// print_r($rst);

?>