<!DOCTYPE html>
<html>
<?php
//error_reporting(E_ALL);
//ini_set("display_errors", 1);
    require '../commande_v2/lib/phpmailer/src/PHPMailer.php';
    require '../commande_v2/lib/phpmailer/src/SMTP.php';
    require '../commande_v2/lib/phpmailer/src/Exception.php';

    Class ClassElig    {



        public function __construct(  ) {

            $this->EtapeParam = [
                ['nom'=>'Zero !'    ,'titre'=>''],
                ['nom'=>'adresse'   ,'titre'=>'Mon test d’éligibilité'],
                ['nom'=>'resultat'  ,'titre'=>'Mon test d’éligibilité'],
                ['nom'=>'solution'  ,'titre'=>'Mon test d’éligibilité'],

            ];
        }

        public function CnxDBaseSQL() {
            if ($_SERVER['REMOTE_ADDR'] == "127.0.0.1") {
                $connexion = mysqli_connect("127.0.0.1", "root", "") or die ("could not connect to mysql");
                mysqli_select_db($connexion, "airmob") or die ("no database");
            } else {
//                $connexion = mysqli_connect("airmobeuagweb.mysql.db", "airmobeuagweb", "5345fhjthk875HJzzs112") or die ("could not connect to mysql");
                $connexion = mysqli_connect("airmobeuagwwwnew.mysql.db", "airmobeuagwwwnew", "8iH9MEjfVySL5q7YWkywKA") or die ("could not connect to mysql");
                mysqli_select_db($connexion, "airmobeuagwwwnew") or die ("no database");

            }
            return $connexion;
        }

    // Affiche le titre et les etapes
        public function AfficheSuiviEtape() {
            $Etapecourante = $_SESSION['AIRELIG']['etape'];
            include ("./form/step.php");
            echo "<div class='Taille14' id='' style='text-align:center; color:#245ba5;'>".$this->EtapeParam[$Etapecourante]['titre']."</div>";
            echo "<br>";

        }

    //  Affiche etape
        public function AfficheFormEtape() {
            $vers = $_SESSION['AIRELIG']['version'] ;
            $Etapecourante = $_SESSION['AIRELIG']['etape'];
            $NomForm = $this->EtapeParam[$Etapecourante]['nom'];
            if($vers) include ("./form/".$NomForm."_v".$vers.".php");
            else include ("./form/".$NomForm.".php");
        }

    // Version 2
        public function PostFormV2()        {

            $Etapecourante = $_SESSION['AIRELIG']['etape'];
            $Type          = $_SESSION['AIRELIG']['type'];
            if( $_REQUEST['btn_suivant']){
                foreach ($_REQUEST as $NomChamp => $Val) {
                    list($none,$prod,$cde) = explode ('_',$NomChamp);
                    // Conserve les choix de l utilisateur
                    if ($none=="form") {
                        $_SESSION['AIRELIG']['Verif']['Adresse'][$prod] = $Val;
                    }
                    //

                }

            // NOTE : Fonctionnement Eligible NEW FTTH / FTTO (LEONIX, FULLSAVE, ORANGE)
                if($Etapecourante==2) {
                $eligible ="non";
                 // Cherche le code INSEE du CP
                    $QF    = mysqli_query($this->CnxDBaseSQL() , "SELECT * FROM insee_cp WHERE code_postal LIKE '%". $_SESSION['AIRELIG']['Verif']['Adresse']['cp']."%'");
                    $ColCp = mysqli_fetch_array($QF, MYSQLI_ASSOC);

                 // FTTH (particulier) - Cherche si l INSEE est ELIGIBLE
                    $QF  = mysqli_query($this->CnxDBaseSQL() , "SELECT * FROM insee_egibilite WHERE insee='". $ColCp['insee']."'");
                    $ColInsee = mysqli_fetch_array($QF, MYSQLI_ASSOC);
                    if  ($ColInsee['insee']) {
                        $eligible ="oui";
                        $_SESSION['AIRELIG']['FTTH']   = "100";
                    }

                // FTTO - FULLSAVE + LEONIX
                // ordre de priorité de propostision : 1/ LEONIX - 2/ FULLSAVE - 3/ ORANGE
                    $QW    = mysqli_query($this->CnxDBaseSQL(), "SELECT * FROM  ftto_elig WHERE insee='". $ColCp['insee']."' ");
                    while ($ColFULLLEO = mysqli_fetch_array( $QW,MYSQLI_ASSOC )) {

                        if($ColFULLLEO['type']=="leonix") {
                            $eligible ="oui";
                            $_SESSION['AIRELIG']['FTTO']['id']   = "101";
                            $_SESSION['AIRELIG']['FTTO']['nom']  = "leonix";
                        } else {
                            if($ColFULLLEO['type']=="fullsave") {
                                $eligible ="oui";
                                $_SESSION['AIRELIG']['FTTO']['id']   = "102";
                                $_SESSION['AIRELIG']['FTTO']['nom']  = "fullsave";
                            }
                        }
                    }

                // FTTO -ORANGE
                    if( ! $_SESSION['AIRELIG']['FTTO'] ) {
                        $QF   = mysqli_query($this->CnxDBaseSQL(), "SELECT * FROM ftto_orange WHERE insee='" . $ColCp['insee'] . "'");
                        $Col2 = mysqli_fetch_array($QF, MYSQLI_ASSOC);

                        if ($Col2['insee']) {
                            $eligible                            = "oui";
                            if( $Col2['zone']=="O1" ) $Art ="103";
                            if( $Col2['zone']=="O2" ) $Art ="104";
                            if( $Col2['zone']=="O3" ) $Art ="105";
                            $_SESSION['AIRELIG']['FTTO']['id']   = $Art;
                            $_SESSION['AIRELIG']['FTTO']['nom']  = "orange";
                            $_SESSION['AIRELIG']['FTTO']['zone']  = $Col2['zone'];
                        }
                    }

                    //

                    $_SESSION['AIRELIG']['eligible'] = $eligible;
                }


                if($Etapecourante>=2) {

                    $Relig = ['eligible'=>$eligible, 'FTTH'=> $_SESSION['AIRELIG']['FTTH'],'FTTO'=> $_SESSION['AIRELIG']['FTTO']];
                    $Relig = json_encode($Relig);
                    $QF  = mysqli_query($this->CnxDBaseSQL() , "SELECT * FROM eligibilite WHERE idunique='". $_SESSION['AIRELIG']['idunique']."'");
                    $Col = mysqli_fetch_array($QF, MYSQLI_ASSOC);
                    $form = json_encode( $_SESSION['AIRELIG']['Verif']['Adresse'] );

                    if($Col) {
                        $UPDATE = "UPDATE eligibilite SET form='$form',eligible='".  $Relig."' WHERE idunique='". $_SESSION['AIRELIG']['idunique']."'";
                        $QUERY = mysqli_query($this->CnxDBaseSQL(), $UPDATE);
                    } else {
                        $Q_INSERT = "INSERT INTO eligibilite (idunique, creation,form,eligible ) VALUES ('" . $_SESSION['AIRELIG']['idunique'] . "','" . date('Y-m-d H:i:s') . "','$form','$Relig') ";
                        $QUERY 	  = mysqli_query ($this->CnxDBaseSQL() , $Q_INSERT);

                    }
                //  Si formulaire de rappel renseigné
                    if($Etapecourante=="3") {

                        $QF  = mysqli_query($this->CnxDBaseSQL() , "SELECT * FROM eligibilite WHERE idunique='". $_SESSION['AIRELIG']['idunique']."'");
                        $Col = mysqli_fetch_array($QF, MYSQLI_ASSOC);
                        $form = json_encode( $_SESSION['AIRELIG']['Verif']['Adresse'] );
                        //$this->MySendMail();
                        $this->MySendMailPHPMailer();
                    }

                    // if( $eligible=="non") unset($_SESSION['AIRELIG']);
                }

            }



        }

        public function PostForm()        {
            $Etapecourante = $_SESSION['AIRELIG']['etape'];
            $Type          = $_SESSION['AIRELIG']['type'];
            if( $_REQUEST['btn_suivant']){
                foreach ($_REQUEST as $NomChamp => $Val) {
                    list($none,$prod,$cde) = explode ('_',$NomChamp);
                // Conserve les choix de l utilisateur
                    if ($none=="form") {
                        $_SESSION['AIRELIG']['Verif']['Adresse'][$prod] = $Val;
                    }
                //

                }
            // NOTE : Fonctionnement Eligible
                if($Etapecourante==2) {
                // Cherche le code INSEE du CP
                    $QF  = mysqli_query($this->CnxDBaseSQL() , "SELECT * FROM insee_cp WHERE code_postal LIKE '%". $_SESSION['AIRELIG']['Verif']['Adresse']['cp']."%'");
                    $ColCp = mysqli_fetch_array($QF, MYSQLI_ASSOC);
                // Cherche si l INSEE est ELIGIBLE
                    $QF  = mysqli_query($this->CnxDBaseSQL() , "SELECT * FROM insee_egibilite WHERE insee='". $ColCp['insee']."'");
                    $ColInsee = mysqli_fetch_array($QF, MYSQLI_ASSOC);
                // ELIGIBLE [OK]
                    if  ($ColInsee['insee']) $eligible ="oui";
                    else                     $eligible ="non";
                    $_SESSION['AIRELIG']['eligible'] = $eligible;

                // Array
/*
                    echo "<pre>"; print_r($ColCp); echo "</pre>";
                    echo "<pre>"; print_r($ColInsee); echo "</pre>";*/
                }

                if($Etapecourante>=2) {
                    $QF  = mysqli_query($this->CnxDBaseSQL() , "SELECT * FROM eligibilite WHERE idunique='". $_SESSION['AIRELIG']['idunique']."'");
                    $Col = mysqli_fetch_array($QF, MYSQLI_ASSOC);
                    $form = json_encode( $_SESSION['AIRELIG']['Verif']['Adresse'] );

                    if($Col) {
                        $UPDATE = "UPDATE eligibilite SET form='$form',eligible='".  $_SESSION['AIRELIG']['eligible']."' WHERE idunique='". $_SESSION['AIRELIG']['idunique']."'";
                        $QUERY = mysqli_query($this->CnxDBaseSQL(), $UPDATE);
                    } else {
                        $Q_INSERT = "INSERT INTO eligibilite (idunique, creation,form,eligible ) VALUES ('" . $_SESSION['AIRELIG']['idunique'] . "','" . date('Y-m-d H:i:s') . "','$form','$eligible') ";
                        $QUERY 	  = mysqli_query ($this->CnxDBaseSQL() , $Q_INSERT);

                    }
                   if($Etapecourante=="3") {
                       echo "<h1>=>33</h1>";
                       $QF  = mysqli_query($this->CnxDBaseSQL() , "SELECT * FROM eligibilite WHERE idunique='". $_SESSION['AIRELIG']['idunique']."'");
                       $Col = mysqli_fetch_array($QF, MYSQLI_ASSOC);
                       $form = json_encode( $_SESSION['AIRELIG']['Verif']['Adresse'] );
                       $MyIP = getenv ("REMOTE_ADDR");
                       if($MyIP!='127.0.0.1'){
                          $this->MySendMail();

                       }


                   }

                   // if( $eligible=="non") unset($_SESSION['AIRELIG']);
                }

            }



        }

     //  NOTE : Envoi PHPMailer (avant utilsition de MySendMail)
        function MySendMailPHPMailer () {

            $mail = new PHPMailer\PHPMailer\PHPMailer();

            $mail->isSMTP();
            $mail->Host = "in-v3.mailjet.com";
            $mail->SMTPAuth = true;
            $mail->Username = "91b3f6b2dd7870d31a16fd0611f5b35a";
            $mail->Password = "c328d8d6bea5c27a4b2f72df0fe84676";
            $mail->SMTPSecure = "tls";
            $mail->Port = 587;
        
        // Pas le choix pour STRIPE
            $mail->From = "service-client@airmob.net";
            $mail->FromName = "AIRMOB";

        //  Envoi l info à AIRMOB
            $mail->addAddress('contact@airmob.net');

            $MsgBody = [];
            $array = $_SESSION['AIRELIG']['Verif']['Adresse'] ;

            $MsgBody[] = "Nom prénom : ".$array['nomprenom']."<br>Adresse : ".$array['no']. " ".$array['adr']." / ".$array['cp']." ".$array['ville']."<br>";
            $MsgBody[] = "Tél : ".$array['telephone']."<br>";
            $MsgBody[] = "Mail : ".$array['email']."<br>";
            $MsgBody[] = "Réponse : ".$_SESSION['AIRELIG']['eligible']."<br>";

            $mail->isHTML(true);
            $body  = join('',$MsgBody);
            $mail->Subject  = utf8_decode("Demande d'éligibilité");
            $mail->Body     = utf8_decode($body);
            $mail->AltBody  = utf8_decode($body);

            if(!$mail->send())            {
                $success ="";
            }
            else
            {
                //echo "Message has been sent successfully";
                $success ="OK";
            }


            if(!$success) $options['SendMail'] = "KO"; else $options['SendMail']  = "OK";

        }

        public function MySendMail ( ) {
            echo "<h1>=>11</h1>";
           // Array
            $array = $_SESSION['AIRELIG']['Verif']['Adresse'] ;

            $body = "Nom prénom : ".$array['nomprenom']."<br>Adresse : ".$array['no']. " ".$array['adr']." / ".$array['cp']." ".$array['ville']."<br>";
            $body .= "Tél : ".$array['telephone']."<br>";
            $body .= "Mail : ".$array['email']."<br>";
            $body .= "Réponse : ".$_SESSION['AIRELIG']['eligible']."<br>";

            //  Indiquer le elements pour analyser l err
            //echo "<pre>"; print_r($this->ActionCfg); echo "</pre>";
            //echo "Envoi un mail ! $etat";
            $to  = 'contact@airmob.net'; // notez la virgule
            $subject = utf8_decode("Demande d'éligibilité");
            $message = utf8_decode($body);
            $headers[] = 'MIME-Version: 1.0';
            $headers[] = 'Content-type: text/html; charset=iso-8859-1';
            $headers[] = 'To: AIRMOB <contact@airmob.net>';
            $headers[] = 'From: AIRMOB <contact@airmob.net>';
            $MyIP = getenv ("REMOTE_ADDR");
            if($MyIP=='127.0.0.1'){
                echo "127 N envoi pas de mail !";

            } else {
                $success = mail($to, $subject, $message, implode("\r\n", $headers));
            }

            //echo "FIN => ".$success;

        }

    // Etape version Mobile
        public function StepMobile() {
            $NoEtape   = $_SESSION['AIRELIG']['etape'];

            ${'active'.$NoEtape}='is-active' ;
            ${'bold'.$NoEtape}='bold; border-bottom:2px solid #235BA5' ;

            if(!$bold1 && !$bold2 && !$bold3 && !$bold4)  $bold5="bold; color :#000;";

            echo "
        <div id=\"container\" style=\"border:0px;\">
        <div id=\"content\" style=\"width:650px\">
            <br /><br />
            <ul class=\"list-unstyled multi-steps\" >
                <li class=\"$active1 \">Mon adresse</li>
                <li class=\"$active2\">Mes résultats</li>
                <li class=\"$active3\">Mes solutions</li>
            </ul>
            </div>
            </div>
        ";

        }

    //  Declenche la ligne etape
        public function Step() {
            $NoEtape   = $_SESSION['AIRELIG']['etape'];
            ${'active'.$NoEtape}='is-active' ;
            echo "
            <div  lass='container-fluid col-md-12'>
                <br /><br />
                <ul class=\"list-unstyled multi-steps\" >
                    <li class=\"$active1 \">Mon adresse</li>
                    <li class=\"$active2\">Mes résultats</li>
                    <li class=\"$active3\">Mes solutions</li>
                </ul>
            </div>";
        }
    }
?>