<?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;
    
    
    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'];
        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);
                }
            }
            $_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       = 'mail.imageonline.co.in;';                    
            $mail->SMTPAuth   = true;                             
            $mail->Username   = 'jagdish@imageonline.co.in';                 
            $mail->Password   = 'Harad@12345';                        
            $mail->SMTPSecure = 'tls';                              
            $mail->Port       = 587;  
            
            $mail->setFrom('jagdish@imageonline.co.in', 'Name');           
            $mail->addAddress($_SESSION['reg_email'][0]);
            $mail->IsHTML(true);       //Sets message type to HTML    
            $mail->AddAttachment($file_name);                                  
            $mail->Subject = 'Seat Booked !!';//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."ticketbook.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{

    }
    
?>