<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
class database{
    private $db_host="localhost";
    private $db_user="ntlogix_root";
    private $db_pass="Ntl@2025";
    private $db_name="ntlogix_eci";
    private $conn=false;
    private $mysqli="";
    private $result=array();
    public function __construct()
    {
        if(!$this->conn)
        {
            $this->mysqli=new mysqli($this->db_host,$this->db_user,$this->db_pass,$this->db_name);
            $this->conn=true;
            if($this->mysqli->connect_error)
            {
                array_push($this->result,$this->mysqli->connect_error);
                return false;
            }
        }
        else
        {
            return true;
        }
    }
    public function __destruct()
    {
        if($this->conn){
            if($this->mysqli->close()){
                $this->conn=false;
                return true;
            }else{
                return false;
            }
        }
    }
    public function insert($table,$parms=array())
    {
       if($this->tableExists($table))
       {
        $table_columns=implode(',',array_keys($parms));
        $table_value=implode("','",$parms);
        $sql="INSERT INTO $table ($table_columns) VALUES ('$table_value')";
            if($this->mysqli->query($sql))
            {
                array_push($this->result,$this->mysqli->insert_id);
                return true;
            }
            else
            {
                array_push($this->result,$this->mysqli->error);
                return false;
            }
        }
        else
        {
            return false;
        }
       
    }
    public function delete($table,$where=null)
    {
        if($this->tableExists($table)){
            $sql="DELETE FROM $table";
            if($where!=null)
            {
                $sql.=" WHERE $where";
            }
            if($this->mysqli->query($sql))
            {
                array_push($this->result,$this->mysqli->affected_rows);
                return true;
            }
            else
            {
                array_push($this->result,$this->mysqli->error);
                return false;
            }
        }
    }
    public function update($table,$parms=array(),$where=null)
    {
        if($this->tableExists($table)){
            $args=array();
            foreach($parms as $key=>$value){
                $args[]="$key='$value'";
            }
            $sql="UPDATE $table SET ".implode(',',$args);
            if($where!=null){
                $sql.=" WHERE $where";
            }
            if($this->mysqli->query($sql)){
                array_push($this->result,$this->mysqli->affected_rows);
                return true;
            }
            else{
                array_push($this->result,$this->mysqli->error);
                return false;
            }
        }else{
            return false;
        }
    }
    public function pagination($table,$join=null,$where=null,$limit=null)
    {
        if($this->tableExists($table)){
            $cls="";
                if($limit!=null){
                $sql="SELECT COUNT(*) FROM $table";
                if($where!=null){
                    $sql.=" WHERE $where";
                }
                if($join!=null){
                    $sql.=" JOIN $join";
                }
                $query=$this->mysqli->query($sql);
                $total_record=$query->fetch_array();
                $total_record=$total_record[0];
                $total_page=ceil($total_record/$limit);
                $url=basename($_SERVER['PHP_SELF']);
                if(isset($_GET['page']))
                {
                    $page=$_GET['page'];
                }else{
                    $page=1;
                }
                $output="<ul class='pagination'>";
                if($page>1){
                    $output.="<li><a $cls href='$url?page=".($page-1)."'>Prev</a></li>";
                }
               if($total_record>$limit){
                for($i=1;$i<=$total_page;$i++){
                    if($i==$page){
                        $cls="class='active'";
                    }else{
                        $cls="";
                    }
                    $output.="<li><a $cls href='$url?page=$i'>$i</a></li>";
                }
               }
               if($total_page>$page){
                $output.="<li><a $cls href='$url?page=".($page+1)."'>Next</a></li>";
            }
                $output.="</ul>";
               return $output;

            }else{
                return false;
            }
        }else{
            return false;
        }
    }
    public function select($table,$rows="*",$join=null,$where=null,$order=null,$limit=null)
    {
        if($this->tableExists($table)){
            $sql="SELECT $rows FROM $table";
            if($join!=null){
                $sql.=" JOIN $join";
            }
            if($where!=null){
                $sql.=" WHERE $where";
            }
            if($order!=null){
                $sql.=" ORDER BY $order";
            }
            if($limit!=null){
                if(isset($_GET['page']))
                {
                    $page=$_GET['page'];
                }else{
                    $page=1;
                }
                $start=($page-1)*$limit;
                $sql.=" LIMIT $start,$limit";
            }
            $query=$this->mysqli->query($sql);
            if($query){
                $this->result=$query->fetch_all(MYSQLI_ASSOC);
                return true;
            }else{
                array_push($this->result,$this->mysqli->error);
                return false;
            }
        }else{
            return false;
        }
    }
    public function sql($sql)
    {
        $query=$this->mysqli->query($sql);
        if($query){
            $this->result=$query->fetch_all(MYSQLI_ASSOC);
            return true;
        }else{
            array_push($this->result,$this->mysqli->error);
            return false;
        }
    }
    public function getResult(){
        $val=$this->result;
        $this->result=array();
        return $val;
    }
    private function tableExists($table)
    {
        $sql="SHOW TABLES FROM $this->db_name LIKE '$table'";
        $tableInDb=$this->mysqli->query($sql);
        if($tableInDb->num_rows==1){
            return true;
        }
        else
        {
            array_push($this->result,$table."does not Exists in this database!");
            return false;
        }
    }
    public function Duplicate_Count($table,$where=null)
    {
        $this->select($table,'*',null,$where,null,null);                             
        $data=count($this->getResult());
        return $data;
    }
    public function Amount_Count($table,$where=null,$col)
    {
        $this->select($table,'*',null,$where,null,null);                             
        $data=$this->getResult();
        $c=0;
        foreach($data as list($col=>$cr))
        {  
            $c=$c+(int)$cr;
        } 
        return $c;
    }
    public function send_Mail($email,$msg,$subject)
    {                       
            require 'assets/sendMail/vendor/PHPMailer/src/Exception.php';
            require 'assets/sendMail/vendor/PHPMailer/src/PHPMailer.php';
            require 'assets/sendMail/vendor/PHPMailer/src/SMTP.php';  
            $id=null;
            $host=null;
            $pass=null;
            $port=null;          
            //Load composer's autoloader
            $this->select('email_config','*',null,'id=1',null,null);
            $result=$this->getResult();                              
            foreach($result as list("host_name"=>$h_name,"email_id"=>$email1,"password"=>$pass1,"port_no"=>$port_no))
            {

            $id=$email1;
            $pass=$pass1;
            $host=$h_name;
            $port=$port_no;
            }
            $mail = new PHPMailer(true);                            
            try {
                //Server settings
                $mail->isSMTP();                                     
                $mail->Host = $host;                      
                $mail->SMTPAuth = true;                             
                $mail->Username = $id;     
                $mail->Password = $pass;             
                $mail->SMTPOptions = array(
                    'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                    )
                );                         
                $mail->SMTPSecure = 'ssl';                           
                $mail->Port =$port;                                   
        
                //Send Email
                $mail->setFrom($id);        
                //Recipients
                $mail->addAddress($email);              
                $mail->addReplyTo($id);        
                //Content
                $mail->isHTML(true);                                  
                $mail->Subject = $subject;
                $mail->Body    = $msg;
                $mail->send();		
                 return 1;
            } catch (Exception $e) 
            {
             return 0;
            }
                    
    }

}
?>