<?php
	
	ob_start();
	require 'connexionBD.php';

	$db = new Connect($_GET['mySchool']);
	$dc = $db->connect_db($_GET['mySchool']);
	
	$fonction="membre";
	
	if($_GET['categorie']=="adherent"){
    	$req = 'SELECT adherent.nom as nom, idEcole, sexe, qrCode, numAdherent, adherent.postnom as postnom, adherent.prenom as prenom, dateNaissance FROM adherent WHERE adherent.idAdherent="'.$_GET['idAdherent'].'" AND validite="OUI"';
    	$q=$dc->prepare($req);
    	$q->execute();
    	
    	$reqx = 'SELECT fonction.nom as fonction FROM adherentfonction, fonction WHERE adherentfonction.idFonction=fonction.idFonction AND adherentfonction.idAdherent="'.$_GET['idAdherent'].'" AND adherentfonction.validite="OUI" AND fonction.validite="OUI"';
    	$qx=$dc->prepare($reqx);
    	$qx->execute();
    	while($a=$qx->fetch()){
    	    $fonction=$a['fonction'];
    	}
    	
    	$queryp = 'UPDATE adherent SET qrCode="OUI" WHERE idAdherent ="'.$_GET['idAdherent'].'"';
		$qp=$dc->prepare($queryp);
		$qp->execute();
	}
	
	elseif($_GET['categorie']=="conjoint"){
    	$req = 'SELECT conjoint.nom as nom, idEcole, sexe, qrCode, numConjoint as numAdherent, conjoint.postnom as postnom, conjoint.prenom as prenom, dateNaissance FROM conjoint WHERE conjoint.idConjoint="'.$_GET['idConjoint'].'" AND validite="OUI"';
    	$q=$dc->prepare($req);
    	$q->execute();
    	
    	$reqx = 'SELECT fonction.nom as fonction FROM fonctionconjoint, fonction WHERE fonctionconjoint.idFonction=fonction.idFonction AND fonctionconjoint.idConjoint="'.$_GET['idConjoint'].'" AND fonctionconjoint.validite="OUI" AND fonction.validite="OUI"';
    	$qx=$dc->prepare($reqx);
    	$qx->execute();
    	while($a=$qx->fetch()){
    	    $fonction=$a['fonction'];
    	}
    	
    	$queryp = 'UPDATE conjoint SET qrCode="OUI" WHERE idConjoint ="'.$_GET['idConjoint'].'"';
		$qp=$dc->prepare($queryp);
		$qp->execute();
	}
	
	elseif($_GET['categorie']=="enfant"){
    	$req = 'SELECT enfant.nom as nom, idEcole, sexe, qrCode, numEnfant as numAdherent, enfant.postnom as postnom, enfant.prenom as prenom, dateNaissance FROM enfant WHERE enfant.idEnfant="'.$_GET['idEnfant'].'" AND validite="OUI"';
    	$q=$dc->prepare($req);
    	$q->execute();
    	
    	$queryp = 'UPDATE enfant SET qrCode="OUI" WHERE idEnfant="'.$_GET['idEnfant'].'"';
		$qp=$dc->prepare($queryp);
		$qp->execute();
	}
	
	while($a=$q->fetch()){
		$noms=mb_strtoupper($a['nom']." ".$a['postnom']." ".$a['prenom']);
		$nom=mb_strtoupper($a['nom']);
		$postnom=mb_strtoupper($a['postnom']);
		$prenom=mb_strtoupper($a['prenom']);
		$sexe=mb_strtoupper($a['sexe']);
		$dateNaissance=$a['dateNaissance'];
		$numAdherent=$a['numAdherent'];
		$dt = DateTime::createFromFormat('Y-m-d', $dateNaissance);$date1 = $dt->format('d/m/Y');
		
		$reqx = 'SELECT denomination FROM ecole WHERE idEcole="'.$a['idEcole'].'" AND validite="OUI"';
		$qx=$dc->prepare($reqx);
    	$qx->execute();
    	
    	while($ax=$qx->fetch()){
    	    $nomEglise = mb_strtoupper($ax['denomination']);
    	}
	}
	
	
	
	
	$deb = "LE FRERE";
	if($sexe=="F"){ $deb="LA SOEUR"; }
	
	//echo "Generating QR Code";
	include('phpqrcode/qrlib.php');
    // how to save PNG codes to server
    $tempDir = "qrcodes/";
    /*$codeContents = "
    NOM DE L'EGLISE : ".$nomEglise.",
    NOM : ".$nom.",
    POSTNOM : ".$postnom.",
    PRENOM : ".$prenom.",
    SEXE : ".$sexe."
    
    ";*/
    
    $codeContents = $prenom." ".$nom." ".$postnom." EST CERTAINEMENT MEMBRE DE L'EGLISE ".$nomEglise.". Cliquer sur le lien suivant https://kelasionline.com/ekleziasoft/ pour accéder à votre compte.";
    
    // we need to generate filename somehow, 
    // with md5 or with database ID used to obtains $codeContents...
    $fileName = '005_file_'.md5($codeContents).'.png';
    $pngAbsoluteFilePath = $tempDir.$fileName;
    $urlRelativeFilePath = $tempDir.$fileName;
    
    // generating
    if (!file_exists($pngAbsoluteFilePath)) {
        QRcode::png($codeContents, $pngAbsoluteFilePath);
        //echo 'File generated!';
        //echo '<hr />';
    } else {
        //echo 'File already generated! We can use this cached file to speed up site on common codes!';
        //echo '<hr />';
    }
    
    //echo 'Server PNG File: '.$pngAbsoluteFilePath;
    //echo '<hr />';
    
    // displaying
    //echo '<img src="'.$urlRelativeFilePath.'" />';
	
	
	
	

	/*
	$req = 'SELECT adherent.nom as nom, adherent.postnom as postnom, adherent.prenom as prenom, adherent.sexe as sexe, adherent.dateNaissance as dateNaissance, adherent.telephone as telephone, ville.nom as nomVille, province.nom as nomProvince, avenue.nom as nomAvenue, quartier.nom as nomQuartier, commune.nom as nomCommune, numdomicile.nom as numDomicile, adherent.numAdherent as numAdherent, adherent.photo as photo, adherent.dateCreation as dateCreation FROM ville, adherent, province, commune, quartier, avenue, numdomicile WHERE adherent.idVille=ville.idVille AND ville.validite="OUI" AND ville.idProvince=province.idProvince AND adherent.idCommune=commune.idCommune AND adherent.idQuartier=quartier.idQuartier AND adherent.idAvenue=avenue.idAvenue AND adherent.idAdherent="'.$_GET['idAdherent'].'" AND adherent.idNumDomicile=numdomicile.idNumDomicile AND adherent.validite="OUI" AND province.validite="OUI" AND commune.validite="OUI" AND avenue.validite="OUI" AND quartier.validite="OUI" AND numdomicile.validite="OUI"';
// 	echo $req; die;
	$q=$dc->prepare($req);
	$q->execute();
	
	while($a=$q->fetch()){
		$nom=$a['nom'];
		$postnom=$a['postnom'];
		$prenom=$a['prenom'];
		$sexe=$a['sexe'];
		$dateNaissance=$a['dateNaissance'];
		$telephone=$a['telephone'];
		$dateCreation=$a['dateCreation'];
		$numAdherent=$a['numAdherent'];
		$photo=$a['photo'];
		$nomProvince=$a['nomProvince'];
		$nomAvenue=$a['nomAvenue'];
		$nomQuartier=$a['nomQuartier'];
		$nomCommune=$a['nomCommune'];
		$numDomicile=$a['numDomicile'];
		$nomVille=$a['nomVille'];
	}
	
	$adresse = $numDomicileArchi.",".$nomAvenue.",".$nomQuartier.",".$nomCommune;
	$nomComplet = $nom." ".$postnom." ".$prenom;
	
	$cheminPhoto="";
    if(trim($photo)<>""){
	    $dirname="../../img/".$_GET['mySchool']."/adherent/".$photo;
        $dir = opendir($dirname);
        while($file = readdir($dir)) {
            if($file != '.' && $file != '..' && !is_dir($dirname.$file)){
                $cheminPhoto=$dirname.'/'.$file;
                $i++;
            }
        }
        closedir($dir);
	}*/
			
?>

<!DOCTYPE html>
<html lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="" />
<meta name="author" content="http://templateq.com" />
</head>
<style type="text/css">
<!--
table
{
    width:  100%;
    border:solid 1px;
    border-collapse: collapse;
}
th
{
    width:  100%;
    text-align:;
    border:solid 1px;
    padding:2px;
    font-size:12px;
}
td
{
    width:  100%;
    text-align:;
    border:solid 1px;
    padding:2px;
    font-size:12px;
}
-->
</style>

<body>
	
	<!--
	<div style="position:absolute;left:-18px;margin-top:-17px">
	    <img src="img/CARTE DE MEMBRE_Alliance_RECTO.jpg" style="width:94.9%; vertical-align:middle; z-index:-1">
	</div>
	-->
	<!--
	<?php //$dt = DateTime::createFromFormat('Y-m-d', $dateNaissance);$date1 = $dt->format('d/m/Y'); ?>
	<?php //$dt = DateTime::createFromFormat('Y-m-d', $dateCreation);$date2 = $dt->format('d/m/Y'); ?>
	<div style="position:absolute; left:76px; top:15px; padding:0px; font-size:7px; z-index:10000"> <?php echo strtoupper($numAdherent); ?> </div>
	<div style="position:absolute; left:50px; top:41px; padding:0px; font-size:7px; z-index:10000"> <?php echo strtoupper($nomComplet); ?> </div>
	<div style="position:absolute; left:46px; top:70px; padding:0px; font-size:7px; z-index:10000"> <?php echo strtoupper($sexe); ?> </div>
	<div style="position:absolute; left:102px; top:70px; padding:0px; font-size:7px; z-index:10000"> <?php echo strtoupper($nomProvince); ?> </div>
	<div style="position:absolute; left:67px; top:99px; padding:0px; font-size:7px; z-index:10000"> <?php echo strtoupper($telephone); ?> </div>
	<div style="position:absolute; left:56px; top:124px; padding:0px; font-size:7px; z-index:10000"> <?php echo strtoupper($numDomicile); ?>, AV. <?php echo strtoupper($nomAvenue); ?> </div>
	<div style="position:absolute; left:22px; top:133px; padding:0px; font-size:7px; z-index:10000"> Q. <?php echo strtoupper($nomQuartier); ?>, C. <?php echo strtoupper($nomCommune); ?> </div>
	<div style="position:absolute; left:92px; top:157px; padding:0px; font-size:7px; z-index:10000"> <?php echo strtoupper($date1); ?> </div>
	<div style="position:absolute; left:220px; top:152px; padding:0px; font-size:6px; z-index:10000"> <?php echo ucfirst($nomVille); ?>, le <?php echo strtoupper($date2); ?> </div>
	<div style="position:absolute; left:204px; top:64px; padding:0px; font-size:6px; z-index:10000"> <img src="<?php echo $cheminPhoto; ?>" width="74"> </div>
    <br/><br/><br/> -->
    
    <?php if($fonction=="membre"){ ?>
    
    <div style="position:absolute;left:-18px;margin-top:-18px">
	    <img src="img/Carte_Verso_PA_OK.jpg" style="width:94.9%; vertical-align:middle; z-index:-1">
	</div>
	<!-- <img src="img/Carte de membre_verso_pierre angulaire_by_Tomajer.jpg" style="width:84%; vertical-align:middle; z-index:1"> -->
	<div style="position:absolute; left:-5px; top:132; padding:0px; z-index:10000"> <img src="../../img/QRCodeWhite.PNG" width="47"> </div>
	<div style="position:absolute; left:-5px; top:130; padding:0px; z-index:10000"> <img src="<?php echo $urlRelativeFilePath; ?>" width="47"> </div>
	<!-- <div style="position:absolute; left:13px; top:124; padding:0px; z-index:10000; font-size:3px; color:#808080;"> <?php echo $numAdherent; ?> </div> -->
	
	<?php } ?>
	
	
	
    <?php if($fonction<>"membre"){ ?>
    
    <div style="position:absolute;left:-18px;margin-top:-18px">
	    <img src="img/Carte_Serviteur_Verso_PA_OK.jpg" style="width:94.9%; vertical-align:middle; z-index:-1">
	</div>
	<!-- <img src="img/Carte de membre_verso_pierre angulaire_by_Tomajer.jpg" style="width:84%; vertical-align:middle; z-index:1"> -->
	<div style="position:absolute; left:-5px; top:132; padding:0px; z-index:10000"> <img src="../../img/QRCodeWhite.PNG" width="47"> </div>
	<div style="position:absolute; left:-5px; top:130; padding:0px; z-index:10000"> <img src="<?php echo $urlRelativeFilePath; ?>" width="47"> </div>
	<!-- <div style="position:absolute; left:13px; top:124; padding:0px; z-index:10000; font-size:3px; color:#808080;"> <?php echo $numAdherent; ?> </div> -->
	
	<?php } ?>
	
	

</body>
</html>


<?php 
   
	$content = ob_get_clean();
    require_once 'html2pdf.class.php';
    try
    {
        $html2pdf = new HTML2PDF("L", "A7", "fr");
        $html2pdf->setDefaultFont("Arial");
        $html2pdf->writeHTML($content);
        $html2pdf->Output("votre_pdf.pdf");
    }
    catch(HTML2PDF_exception $e) {
        echo $e;
        exit;
    }
	
?>

