<? include("../settings.php"); ?>
<? include("session.php"); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>SP Admin Panel</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Include all CSS/JS -->
        <!-- Bootstrap -->
    <link href="css/bootstrap.css" rel="stylesheet">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="js/jquery-2.1.1.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
    <!-- End of all CSS/JS -->
    <script src="ckeditor/ckeditor/ckeditor.js"></script>
    <script>
        
        function chkoptions(){
            if ($('input[name=sending_option]:checked').val()=="single"){
                $("#email_entry").prop('disabled', false);
                $("#role_id").prop('disabled', true);
            }else{
                $("#email_entry").prop('disabled', true);
                $("#role_id").prop('disabled', false);
            }
        }
    </script>

   
   </head>
<body>
<?
    if (isset($_REQUEST['confirm'])){
        
        $bind = array(":search1"=>$_REQUEST['email_id']);
        
        // Getting selected email
        $emails = $db->select("tbl_email", "email_id = :search1", $bind);
        $emailtemplate = $emails[0];
        $subject = $emailtemplate['email_subject'];
        $message = $emailtemplate['email_content'];
        
        // Getting registrants in selected category
        $i = 0;
        $bind = array(":search1"=>$_REQUEST['role_id']);
        $results = $db->select("tbl_registrant","role_id=:search1 AND email_sent=0 AND (registrant_paid='PAID' OR registrant_paid='Complimentary')", $bind);
        foreach ($results as $result){
			$date = date("d-m-Y");
            $subject = $emailtemplate['email_subject'];
            $message = $emailtemplate['email_content'];
            $registrant_sn = $result['registrant_sn'];
			$registrant_salutation = $result['registrant_salutation'];
			$registrant_fullname = $result['registrant_fullname'];
			$registrant_organisation = $result['registrant_organisation'];
			$registrant_papernumber = $result['registrant_papernumber'];
			$presentdate = $result['presentdate'];
			$presenttime = $result['presenttime'];
			$presentvenue = $result['presentvenue'];
			if ($result['registrant_cocktail_attend']==1)
				$registrant_cocktail_attend = "Yes";
			else
				$registrant_cocktail_attend = "No";
			$registrant_extra_cocktail = $result['registrant_extra_cocktail'];
			if ($result['registrant_dinner_attend']==1)
				$registrant_dinner_attend = "Yes";
			else
				$registrant_dinner_attend = "No";
			$registrant_extra_dinner = $result['registrant_extra_dinner'];
			$registrant_diet = $result['registrant_diet'];
			$email = $result['registrant_email'];

            $qrcode = "<img src='http://www.events-sp.com/SICC9/generateqr.php?num=".$registrant_sn."'>";
			
			$message = str_replace("@date@",$date,$message);
            $message = str_replace("@registrant_sn@",$registrant_sn,$message);
            $message = str_replace("@registrant_salutation@",$registrant_salutation,$message);
            $message = str_replace("@registrant_fullname@",$registrant_fullname,$message);
			$message = str_replace("@registrant_organisation@",$registrant_organisation,$message);
			$message = str_replace("@registrant_papernumber@",$registrant_papernumber,$message);
			$message = str_replace("@presentdate@",$presentdate,$message);
			$message = str_replace("@presenttime@",$presenttime,$message);
			$message = str_replace("@presentvenue@",$presentvenue,$message);
			$message = str_replace("@registrant_cocktail_attend@",$registrant_cocktail_attend,$message);
			$message = str_replace("@registrant_extra_cocktail@",$registrant_extra_cocktail,$message);
			$message = str_replace("@registrant_dinner_attend@",$registrant_dinner_attend,$message);
			$message = str_replace("@registrant_extra_dinner@",$registrant_extra_dinner,$message);
            $message = str_replace("@qr_code@",$qrcode,$message);

            $mail->IsHTML(true);
            $mail->SetFrom("sylvia@sicc9.org","SICC9 Conference Secretariat");
            $mail->ClearAllRecipients();
            $mail->AddAddress($email);
            $mail->Subject = $subject;
            $mail->Body = $message;
            $mail->Send(); // send message

            // Update Email Sent
            $bind = array(":search1"=>$result['registrant_id']);
            $update = array("email_sent"=>1);
            $db->update("tbl_registrant",$update,"registrant_id=:search1",$bind);
            $i++;
        }
        echo "<script>alert('$i Email(s) Sent');</script>";
    }
?>

<? include("nav_menu.php"); ?>
  
      <div class="well">
<div class="content" bgcolor="#FFFFD0">
<div class="col-12 feature">
<div class="panelCheck">
    <div class="panel-heading">
        <h3 class "panel-title"><strong>Send Emails</strong></h3>
    </div>
<div class="panel-body">
<form name="email" id="email" method='POST' action="" enctype="multipart/form-data" class="form-horizontal" role="form">
        <div class='form-group'>
            <label for='email_subject' class='col-lg-2 control-label'>Select an Email to send</label>
            <div class='col-lg-10 text-left'>
                <select class="form-control" name="email_id" ><option value="0">Please Select</option>
                    <?
                        $emails = $db->select("tbl_email");
                        foreach($emails as $email){
                            echo "<option value='".$email['email_id']."'>".$email['email_subject']."</option>";
                        }
                    ?>
                </select>
            </div>
        </div>
        <br/><br/>
        <div class='form-group'>
            <label for='registrant_organisation' class='col-lg-2 control-label'><input type="radio" name="sending_option" value="group" checked onchange="chkoptions();"> Receipient Group</label>
            <div class='col-lg-10 text-left'>
                <select class="form-control" id="role_id" name="role_id" ><option value="0">Please Select</option>
                    <?
                        $results = $db->select("tbl_role");
                        foreach ($results as $result){
                            echo "<option value='".$result['role_id']."'>".$result['role_name']."</option>";
                        }
                    ?>
                </select>
             </div>
        </div>
        <div class='form-group'>
            <label for='registrant_organisation' class='col-lg-2 control-label'><input type="radio" name="sending_option" value="single" onchange="chkoptions();" disabled> Receipient Email</label>
            <div class='col-lg-10 text-left'>
                <input type="text" class="form-control" id="email_entry" name="email_entry" >
             </div>
        </div>
    <center>
    <input type="submit" name="confirm" value="Send" class="btn btn-default">
    <input type="button" value="Close" class="btn btn-default" onclick="window.history.back();">
    </center>
</form>
</div>       

</div>
</div>
</div>
</div>
    
<script type="text/javascript">
    chkoptions();
</script>
    
    
</body>
</html>
