<?php

    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\SMTP;
    use PHPMailer\PHPMailer\Exception;

	class Model{

		public static $host="localhost";
		public static $dbName="dannvtuc_portal"; //Database Name
		public static $username="dannvtuc_portal"; // Database Username
		public static $password="0YduFgqECKSQ"; //Database Password

        public static $dbh;
        
		public $emailUsername="app@dannvtu.com.ng"; //Support Email Address
		public $emailPassword="7kEY327GsGtTwRe"; //Support Email Password
        public $sitename;

        public function __construct(){
            $this->dbh = $this->connectDb();
            global $sitename;
			$this->sitename = $sitename;
        }

        public function connectDb(){
            $pdo = new PDO("mysql:host=".self::$host.";dbname=".self::$dbName,self::$username,self::$password);
	    	$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	    	return $pdo;
        }

		public function connect(){
			return $this->dbh;
		}

		public function sendMail($email,$subject,$message){
		    global $sitename;

            $mail = new PHPMailer(true);

	        try {

                $mail->isSMTP();                                            
                $mail->Host       = $_SERVER['SERVER_NAME'];                
                $mail->SMTPAuth   = true;                                   
                $mail->Username   = $this->emailUsername;                   
                $mail->Password   = $this->emailPassword;                   
                $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS;            
                $mail->Port       = 465;                                    

                $mail->setFrom($this->emailUsername, $sitename);
                $mail->addAddress($email);     

                $mail->isHTML(true);                                  
                $mail->Subject = $subject;
                $mail->Body    = $message;
                $mail->AltBody = strip_tags($message);

                $mail->send();
                return 0;
                return 'Message has been sent';
            } catch (Exception $e) {
                return 1;
                return "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
            }
	    }

        public function getConfigValue($list,$name){
			foreach($list AS $item){
				if($item->name == $name){return $item->value;}
			}
		}

		public function getApiConfiguration(){
			$dbh=self::connect();
			$sql = "SELECT * FROM apiconfigs";
            $query = $dbh->prepare($sql);
            $query->execute();
            $results=$query->fetchAll(PDO::FETCH_OBJ);
            return $results;
		}

		public function getSiteConfiguration(){
			$dbh=self::connect();
			$sql = "SELECT * FROM sitesettings WHERE sId=1";
            $query = $dbh->prepare($sql);
            $query->execute();
            $results=$query->fetch(PDO::FETCH_OBJ);
            return $results;
		}

         public function __destruct() {
        $this->dbh = null; 
    }

	}

?>