<?php

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

class Proyectoexpo
{
	private $pdo;
	public function __construct()
	{
		try
		{
			require("variables.php");
			$this->pdo = new PDO('mysql:host='.$hostname.';dbname='.$database, $username, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES  \'UTF8\''));
			$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		}
		catch(Exception $e)
		{
			die($e->getMessage()); 
		}
	}
	public function guardarpyp(pyp $data)
	{
		try 
		{
		$sql = "INSERT INTO registro_nom (registro_nom,registro_ocupa,registro_otroocupa,registro_mail,registro_tel,registro_empresa,registro_puesto,registro_entere,id_pago,nombre_pago,identidad,qr) 
		        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

		$res=$this->pdo->prepare($sql)
		     ->execute(
			array(
				$data->__GET('nombre'), 
				$data->__GET('ocupacion'), 
				$data->__GET('otroocupa'), 
				$data->__GET('email'),
				$data->__GET('telefono'),
				$data->__GET('empresa'),
				$data->__GET('puestoemp'),
				$data->__GET('entero'),
				$data->__GET('idtrans'),
				$data->__GET('nametrans'),
				$data->__GET('identidad'),
				$data->__GET('qr')
				)
			);
		} catch (Exception $e) 
		{
			die($e->getMessage());
		}
		return $res;
	}
	public function enviocor(pyp $data)
	{
		// date_default_timezone_set('America/Mexico_City');
		// $date = date('Y-m-d H:i:s');
		require 'vendor/autoload.php';
		require("variables.php");
		$mail = new PHPMailer(true);
		try 
		{
			$identidad=$data->__GET('identidad');
			$nombre=$data->__GET('nombre');
			$email=$data->__GET('email');
			$image_data=$data->__GET('qr');
			$data = substr($image_data, strpos($image_data, ","));
			$filename= $identidad .".png";
			$encoding = "base64"; 
			$type = "image/png";

			$mail->SMTPDebug = 0;                      // Enable verbose debug output

			$mail->Debugoutput = 'html';
		    
		    $mail->isSMTP();                                            // Send using SMTP
		    $mail->Host       = $host;                    // Set the SMTP server to send through
		    $mail->SMTPAuth   = true;                                   // Enable SMTP authentication
		    $mail->Username   = $usernamee;                     // SMTP username
		    $mail->Password   = $contra;                               // SMTP password
		    $mail->SMTPSecure = 'tls';         // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` also accepted
		    $mail->Port       = $port;                                    // TCP port to connect to

		    $mail->setFrom('no-reply@medical-expo.com.mx', 'Medical Expo');
		    //$mail->addAddress($emai,$nombre);     // Add a recipient
		    // $mail->addAd dress('ellen@example.com');               // Name is optional
		    // $mail->addReplyTo('info@example.com', 'Information');
		    $mail->addCC('programacion@medimarcas.com', 'Monzon');
		    $mail->addBCC('web@surtimedik.com');
		    $mail->isHTML(true);                               // Set email format to HTML
		    $mail->CharSet = 'UTF-8';
		    $mail->AddStringAttachment(base64_decode($data), $filename, $encoding, $type);
		    $mail->AddEmbeddedImage('../img/correo/registro-visita.png', 'logoinen');
		    $mail->AddEmbeddedImage('../registro/QRS/'.$identidad.'.png', 'qr');
		    $mail->Subject = 'Registro Asistencia MedicalExpo';
		    $mail->Body    = '<div style="display:block;margin: auto;width: 600px;font-family: arial;">
					<img src="cid:logoinen" alt="gracias">
						<div style="padding-left: 30px;text-align: right;">
							<b style="color: #2F7DB3; font-size: 30px;padding:5px">'.$nombre.'</b>
						</div>
						<div style="text-align: center;width:600px;padding-top: 5px">
							<p style="color:black">la informacion del QR es:</p>
					  		<b style="color:#2F7DB3;font-size: 30px">'.$identidad.'</b>
					  		<img style="display: block;margin: auto;" src="cid:qr" alt="qr">
					  		 <p style="padding-top:30px;">Si requieres factura da da click <a href="http://localhost/proyectos/medicalexpo/fact">aqui</a> y ten a la mano tu Codigo de identidad</p>
						</div>
				</div>';
		    // $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
		    $mail->send();
		    // echo 'mensaje enviado con exito';
			
		} 

		catch (Exception $e) {
		    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
		}
	}
    public function selectcfdi()
	{
		try
		{
			$result = array();

			$stm = $this->pdo->prepare("SELECT cfdi_codigo, cfdi_descripcion  FROM cfdi");
			$stm->execute();

			foreach($stm->fetchAll(PDO::FETCH_OBJ) as $r)
			{
				$rowcfdi = new pyp();

				$rowcfdi->__SET('cfdi_codigo', $r->cfdi_codigo);
				$rowcfdi->__SET('cfdi_descripcion', $r->cfdi_descripcion);
				$result[] = $rowcfdi;
			}

			return $result;
		}
		catch(Exception $e)
		{
			die($e->getMessage());
		}
	}
	public function selectcocupacion()
	{
		try
		{
			$result = array();

			$stm = $this->pdo->prepare("SELECT id, descripcion  FROM ocupacion");
			$stm->execute();

			foreach($stm->fetchAll(PDO::FETCH_OBJ) as $r)
			{
				$rowocupa = new pyp();

				$rowocupa->__SET('id', $r->id);
				$rowocupa->__SET('descripcion', $r->descripcion);
				$result[] = $rowocupa;
			}

			return $result;
		}
		catch(Exception $e)
		{
			die($e->getMessage());
		}
	}
	public function selectgiro()
	{
		try
		{
			$result = array();

			$stm = $this->pdo->prepare("SELECT id, descripcion  FROM giro");
			$stm->execute();

			foreach($stm->fetchAll(PDO::FETCH_OBJ) as $r)
			{
				$rowgiro = new pyp();

				$rowgiro->__SET('id', $r->id);
				$rowgiro->__SET('descripcion', $r->descripcion);
				$result[] = $rowgiro;
			}

			return $result;
		}
		catch(Exception $e)
		{
			die($e->getMessage());
		}
	}
	public function guardarsponsor(pyp $data)
	{
		// print_r($data);
		try 
		{
		$sql = "INSERT INTO sponsor (logo,nombre,empresa,estado,municipio,telefono,correo,profesion,puesto,nombretitu,banco,cuenta,claveinter,urlcodigo,qrsponsor,giro_id,tiposponsor) 
		        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

		$res=$this->pdo->prepare($sql)
		     ->execute(
			array(
				$data->__GET('logo'), 
				$data->__GET('nombre'), 
				$data->__GET('empresa'), 
				$data->__GET('estado'),
				$data->__GET('municipio'),
				$data->__GET('telefono'),
				$data->__GET('email'),
				$data->__GET('profesion'),
				$data->__GET('puesto'),
				$data->__GET('nombretitu'),
				$data->__GET('banco'),
				$data->__GET('cuenta'),
				$data->__GET('claveint'),
				$data->__GET('urlcodigo'),
				$data->__GET('qrsponsor'),
				$data->__GET('giro_id'),
				$data->__GET('tiposponsor')
				)
			);
		} 
		catch (Exception $e) 
		{
			die($e->getMessage());
		}
		return $res;
	}
	public function guardarcontacto(pyp $data)
	{
		try 
		{
		$sql = "INSERT INTO contacto (empresa,puesto,nombre,apellidos,telefono,correo,mensaje) 
		        VALUES (?, ?, ?, ?, ?, ?, ?)";

		$res=$this->pdo->prepare($sql)
		     ->execute(
			array(
				$data->__GET('empresa'), 
				$data->__GET('puesto'), 
				$data->__GET('nombre'), 
				$data->__GET('apellidos'),
				$data->__GET('telefono'),
				$data->__GET('correo'),
				$data->__GET('mensaje')
				)
			);
		} 
		catch (Exception $e) 
		{
			die($e->getMessage());
		}
		return $res;
	}
	public function obtenersponsor($id)
	{
		// echo "id:" . $id;
		try 
		{
			$stm = $this->pdo->prepare("SELECT * FROM sponsor WHERE urlcodigo = ? ");
			          

			$stm->execute(array($id));
			$r = $stm->fetch(PDO::FETCH_OBJ);

			$usuo = new pyp();

				$usuo->__SET('id', $r->id);
				$usuo->__SET('nombre', $r->nombre);
				$usuo->__SET('urlcodigo', $r->urlcodigo);
				$usuo->__SET('logo', $r->logo);
				// print_r($usuo);
			return $usuo;
		} catch (Exception $e) 
		{
			die($e->getMessage());
		}
	}
	public function saveinvitspon(pyp $data)
	{
		try 
		{
		$sql = "INSERT INTO invitesponsor (nombre,ocupacion,otraocupa,correo,tel,empresa,cargo,comoentere,id_pago,nombrepago,identidad,qr,sponsor_id) 
		        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

		$res=$this->pdo->prepare($sql)
		     ->execute(
			array(
				$data->__GET('nombre'), 
				$data->__GET('ocupacion'), 
				$data->__GET('otraocupa'), 
				$data->__GET('correo'),
				$data->__GET('tel'),
				$data->__GET('empresa'),
				$data->__GET('cargo'),
				$data->__GET('comoentere'),
				$data->__GET('idtrans'),
				$data->__GET('nombrepago'),
				$data->__GET('identidad'),
				$data->__GET('qr'),
				$data->__GET('sponsor_id')
				)
			);
		} catch (Exception $e) 
		{
			die($e->getMessage());
		}
		return $res;
	}
	// 	public function obtenersponsor2($id)
	// {
	// 	try 
	// 	{
	// 		$stm = $this->pdo->prepare("SELECT * FROM sponsor WHERE urlcodigo = ? ");
			          

	// 		$stm->execute(array($id));
	// 		$r = $stm->fetch(PDO::FETCH_OBJ);

	// 		$usuo = new pyp();

	// 			$usuo->__SET('id', $r->id);
	// 			$usuo->__SET('nombre', $r->nombre);
	// 			$usuo->__SET('urlcodigo', $r->urlcodigo);
	// 			$usuo->__SET('logo', $r->logo);
	// 			// print_r($usuo);
	// 		return $usuo;
			
	// 	} catch (Exception $e) 
	// 	{
	// 		die($e->getMessage());
	// 	}	
	// }

	function disconnect () 
	{
	   global $pdo, $stmt;
	   $stmt->closeCursor();
	   $stmt = null;
		$pdo = null;
	}
}
 ?>
