<?php
    ini_set('display_errors', '1');
    ini_set('display_startup_errors', '1');
    error_reporting(E_ALL);

    $BASE_URL='https://dev.srimaone.in/srimaticket/';
    session_start();    
    ob_start();
    require_once 'dompdf/autoload.inc.php';
    use Dompdf\Dompdf;
    use Dompdf\Options;
    
    include('QRCode/libs/phpqrcode/qrlib.php'); 
    
    require('config/Database.php');
    $db = Database::getInstance();
    $mysqli = $db->getConnection();
    if(isset($_SESSION['current_selected_seats'])){
        $message = '';
        $is_success = 0;
        $std_id = json_encode($_SESSION['stdid']);
        $sid = json_encode($_SESSION['sid']);        
        $reg_email = json_encode($_SESSION['reg_email']);

        $seats = $_SESSION['current_selected_seats'];
        $reg_mob=$_SESSION['session_mobile_number'];
        $qrcontentarr = array();
        if(count($seats) > 0){
            
            foreach($seats as $stype=>$seatnos){
                foreach($seatnos as $sno){
                    $sql = "INSERT INTO std_ticket (std_id, reg_mobile, sid, seat_type,seat_no) VALUES ('$std_id','$reg_mob','$sid','$stype','$sno')";
                    $mysqli->query($sql);
                }
            }
            
            
            $seat_query = "SELECT * FROM std_ticket WHERE reg_mobile=".$reg_mob; 
	        $seat_dt= $mysqli->query($seat_query);	
            if ($seat_dt->num_rows > 0) {
                while($row_seat = $seat_dt->fetch_assoc()){ 
                    $qrcontentarr['seat_no'][$row_seat["seat_type"]][]=$row_seat["seat_no"];
                }
            }    
            $qrcontentarr['mobile_no']=$reg_mob;
            $qrcontentarr['booking_name']=$_SESSION['login_name'];
            
            $tempDir = 'QRCode/codeimage/'; 
        	$codeContents = json_encode($qrcontentarr,true); 
        	QRcode::png($codeContents, $tempDir.''.$reg_mob.'.png', QR_ECLEVEL_L, 5);
        	
        	
            $_SESSION['current_selected_seats'] = []; 
            $_SESSION['current_select_seat_count'] = 0;
            
            $data = array('login_name' => $_SESSION['login_name'], 'email' => $_SESSION['reg_email'][0],'mob' => $reg_mob);
            $query = http_build_query($data);
            $options = array(
                'http' => array(
                    'header' => "Content-Type: application/x-www-form-urlencoded\r\n".
                                "Content-Length: ".strlen($query)."\r\n".
                                "User-Agent:MyAgent/1.0\r\n",
                    'method'  => "POST",
                    'content' => $query,
                ),
            );
            $stream = stream_context_create($options);
            
            
        
            $options = new Options();
            $options->set('isRemoteEnabled', TRUE);
            $dompdf = new Dompdf($options);
            $context = stream_context_create([ 
                'ssl' => [ 
                    'verify_peer' => FALSE, 
                    'verify_peer_name' => FALSE,
                    'allow_self_signed'=> TRUE 
                ] 
            ]);
            $dompdf->setHttpContext($context);
            
            $file_name = 'ticketpdf/'.$reg_mob. '.pdf';
            $html = file_get_contents($BASE_URL."pdfcontent.php", false, $stream);
            //$dompdf->loadHtml('<h1>Welcome to CodexWorld.com</h1>');
            //echo $html;exit;
            $dompdf->loadHtml($html);
            $dompdf->setPaper('A4', 'landscape');
            $dompdf->render();
            $file = $dompdf->output();
            file_put_contents($file_name, $file);
            
            
            
            
            include('Phpmailer_lib.php');
            $maillib = new phpmailer_lib();
            $mail=$maillib->load();
            $mail->SMTPDebug = false;                                       
            $mail->isSMTP();                                            
            $mail->Host       = 'smtp.gmail.com;';                    
            $mail->SMTPAuth   = true;                             
            $mail->Username   = 'admissions.smvthane@smgi.edu.in';                 
            $mail->Password   = 'Sai@02131944#';                        
            $mail->SMTPSecure = 'tls';                              
            $mail->Port       = 587;  
            
            $mail->setFrom('admissions.smvthane@smgi.edu.in', 'Sri Ma-Vidyalaya');           
            $mail->addAddress($_SESSION['reg_email'][0]);
            $mail->IsHTML(true);       //Sets message type to HTML    
            $mail->AddAttachment($file_name);                                  
            $mail->Subject = 'Sri Ma-Vidyalaya | Seats Confirmation !!';//Your Seat has been booked 
            
            $mailhtml = file_get_contents($BASE_URL."mailcontent.php", false, $stream);
            
            $mail->Body    = $mailhtml;
            
            if($mail->send()){                
                               
                header("Location: ".$BASE_URL."thank-you.php");
                die();
            }else{
                header("Location: ".$BASE_URL."selectseat.php");
                die();
            }
            // $message = 'Success';
            // $is_success = 1;
        }else{
            header("Location: ".$BASE_URL."dashboard.php");
            die();
            // $message = 'Something Went to wrong! Please contact administrator!';
            // $is_success = 0;
        }
        // $data['is_success'] = $is_success;
        // $data['message'] = $message;
        // echo json_encode($data,true);
    }else{

    }
    
?>