<?php
/**
 * userMail.php -- Send User a Mail
 */

include '../inc/config.inc.php'; // Configuration

ob_start();
session_start();

//date_default_timezone_set('Africa/Lagos');

error_reporting(E_ALL);
ini_set('display_errors', 1);

if ( !isset($_SESSION['admin']) || $_SESSION["loggedin"] !== true || trim($_SESSION['admin']) == '' ){
	//header('Location: ' . $BASE_URL . 'wp-admin/');
    header('Location: ' . $BASE_URL . 'signin');
	exit;
}

if ( isset($_GET['id']) ){
    $user_id = intval($_GET['id']);
    // Users
    $users = $db_con->prepare("SELECT * FROM users WHERE id='$user_id'");
    $users->execute();
    $user = $users->fetch(PDO::FETCH_ASSOC);

?>

<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Mail User - Alert Signals Nation</title>
    <!-- Favicon -->

    <link rel="shortcut icon" href="../../img/Signals-Alert-3.png" />
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <!-- Typography CSS -->
    <link rel="stylesheet" href="css/typography.css">
    <!-- Style CSS -->
    <link rel="stylesheet" href="css/style.css">
    <!-- Responsive CSS -->
    <link rel="stylesheet" href="css/responsive.css">

    <script src="../plugins/sweetalerts/promise-polyfill.js"></script>
    <link href="../plugins/sweetalerts/sweetalert.css" rel="stylesheet" type="text/css" />

    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,600,700" rel="stylesheet" />
    <style>
        /*------ Google Font Style ------ */
        body {font-family:'Roboto'!important;}
    </style>

</head>
<body class="sidebar-main-active right-column-fixed">
    <!-- loader Start -->
    <div id="loading">
        <div id="loading-center">
        </div>
    </div>
    <!-- loader END -->
    <!-- Wrapper Start -->
    <div class="wrapper">
        <!-- Sidebar  -->
        <?php include 'navbar.php' ?>
        <!-- Page Content  -->
        <div id="content-page" class="content-page">
            <div class="container-fluid">
                <div class="row">
                    <div class="col-sm-12 col-lg-12">
                        <div class="iq-card">
                            <div class="iq-card-header d-flex justify-content-between">
                                <div class="iq-header-title">
                                    <h4 class="card-title" style="font-family: 'Roboto'!important;">Mail</h4>
                                </div>
                            </div>
                            <div class="iq-card-body">
                                <div class="row">
                                    <div class="col-sm-9">
                                        <div class="tab-content mt-0" id="v-pills-tabContent">
                                            <div class="tab-pane fade show active" id="v-pills-home" role="tabpanel" aria-labelledby="v-pills-home-tab">
                                                <form action="" method="post">
                                                    <!--<input type="text" name="user_id" value="<?= $user['id']; ?>"><br>-->
                                                    <input type="hidden" name="user_email" value="<?= $user['email']; ?>">
                                                    <div class="form-group">
                                                        <label for="exampleInputText1">Subject</label>
                                                        <input type="text" class="form-control" name="user_subject" placeholder="Enter Subject">
                                                    </div>
                                                    <div class="form-group">
                                                        <label for="exampleFormControlTextarea1">Message</label>
                                                        <textarea class="form-control" name="user_message" rows="5"></textarea>
                                                    </div>
                                                    <button type="submit" name="sendmail" class="btn btn-primary">Submit</button>
                                                </form>
                                                <?php
                                                if ( isset($_POST['sendmail']) ){
                                                    $user_email = $_POST['user_email'];
                                                    $user_fullname = $user['fullname'];
                                                    $user_subject = $_POST['user_subject'];
                                                    $user_message = $_POST['user_message'];

                                                    if ( empty($user_subject) || empty($user_message) ){
                                                ?>
                                                        <script>
                                                            setTimeout(function(){
                                                                noticePop('All Fields Must Be Filled.', 'error');
                                                            }, 2000);
                                                        </script>
                                                <?php
                                                    } else {
                                                        //if ( SendMailToUser($supportEmail, $BASE_URL, $user_email, $user_fullname, $user_subject, $user_message) ){
                                                        SendMailToUser( $supportEmail, $BASE_URL, $user_email, $user_fullname, $user_subject, $user_message );
                                                ?>
                                                            <script>
                                                                setTimeout(function(){
                                                                    noticePop('Mail Has Been Sent.', 'success');
                                                                }, 2000);
                                                            </script>
                                                <?php
                                                        //} else {
                                                ?>
                                                            <!--<script>
                                                                setTimeout(function(){
                                                                    noticePop('Something Went Wrong. We couldn\'t Send the Mail.', 'error');
                                                                }, 2000);
                                                            </script>-->
                                                <?php
                                                        //}
                                                    }
                                                }
                                                ?>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- Wrapper END -->
    <!-- Footer -->
    <?php include 'footer.php' ?>
    <!-- color-customizer END -->
    <!-- Optional JavaScript -->
    <script src="../plugins/jquery-3.1.1.min.js"></script>
    <script src="../plugins/sweetalerts/sweetalert2.min.js"></script>
    <script src="https://unpkg.com/@popperjs/core@2/dist/umd/popper.js"></script>

    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="js/jquery.min.js"></script>
    <script src="js/popper.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <!-- Appear JavaScript -->
    <script src="js/jquery.appear.js"></script>
    <!-- Countdown JavaScript -->
    <script src="js/countdown.min.js"></script>
    <!-- Counterup JavaScript -->
    <script src="js/waypoints.min.js"></script>
    <script src="js/jquery.counterup.min.js"></script>
    <!-- Wow JavaScript -->
    <script src="js/wow.min.js"></script>
    <!-- Apexcharts JavaScript -->
    <script src="js/apexcharts.js"></script>
    <!-- Slick JavaScript -->
    <script src="js/slick.min.js"></script>
    <!-- Select2 JavaScript -->
    <script src="js/select2.min.js"></script>
    <!-- Owl Carousel JavaScript -->
    <script src="js/owl.carousel.min.js"></script>
    <!-- Magnific Popup JavaScript -->
    <script src="js/jquery.magnific-popup.min.js"></script>
    <!-- Smooth Scrollbar JavaScript -->
    <script src="js/smooth-scrollbar.js"></script>
    <!-- lottie JavaScript -->
    <script src="js/lottie.js"></script>
    <!-- am core JavaScript -->
    <script src="js/core.js"></script>
    <!-- am charts JavaScript -->
    <script src="js/charts.js"></script>
    <!-- am animated JavaScript -->
    <script src="js/animated.js"></script>
    <!-- am kelly JavaScript -->
    <script src="js/kelly.js"></script>
    <!-- am maps JavaScript -->
    <script src="js/maps.js"></script>
    <!-- am worldLow JavaScript -->
    <script src="js/worldLow.js"></script>
    <!-- Style Customizer -->
    <script src="js/style-customizer.js"></script>
    <!-- Chart Custom JavaScript -->
    <script src="js/chart-custom.js"></script>
    <!-- Custom JavaScript -->
    <script src="js/custom.js"></script>

    <script>
        function noticePop(value, type) {
            swal({
                html: '<span style="font-size:18px!important;">' + `${value}` + '</span>',
                type: `${type}`,
                timer: 4000,
                showConfirmButton: false,
                allowOutsideClick: false,
                allowEscapeKey: false,
            }).then(function() {
                window.location.href = 'userMail?id=<?= $user_id; ?>';
            });
        }
    </script>

</body>
</html>

<?php
}
?>