<?php
require_once "common/front_header.php";

if (isset($_POST['btnWork'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];


    if ($name != '' && $email != '' && $message != '') {

        $response = $db->addWorkWithUs($name, $email, $message);

        if ($response["error"] == false) {
            echo "<script type='text/javascript'>
                swal({
                                title: \"Success!\",
                                text: \"Application Submitted Successfully! We will get back to you\",
                                icon: \"success\",
                                button: \"Thank you!\",
                            });
               </script>";

        } else
            echo "<script type='text/javascript'>
                swal({
                                title: \"Sorry!\",
                                text: \"Error submitting application!\",
                                icon: \"warning\",
                                button: \"Please try again!\",
                            });
            </script>";


        require_once 'libs/mailer/class.phpmailer.php';

        $mail = new PHPMailer();
        $mail->IsSMTP();
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = "ssl";
        $mail->Host = "creat.co.ke";
        $mail->Port = 465;
        $mail->Username = "uncommon@creat.co.ke";
        $mail->Password = "O6+VLDzLe+jv";
        $mail->SMTPKeepAlive = true;
        $mail->SMTPDebug = 0;
        $mail->SetFrom(EMAIL, WEB_NAME);
        $mail->AddReplyTo(EMAIL, WEB_NAME);

        $message1 = "Dear " . $email . ",<br/><br/>

                        Thank you for contacting Woman Kenya.<br/><br/>

                        We will get back to you.
                        <br/><br/>

                        Regards,
                        <br/><br/>
                        Woman Kenya Team";


        $message2 = "Dear " . EMAIL . ",<br/><br/>

                        You have a message from.<br/><br/>
                        <br/><br/>
                        
                        Name:" . $name . ".<br/><br/>
                        Email:  " . $email . ".<br/><br/>
                        Email:  <strong>" . $message . ".</strong><br/><br/>
                        <br/><br/>
                          
                        Regards,
                        <br/><br/>
                        Woman Kenya Team";


        $subject = "WOMAN KENYA";

        try {
            $mail->addAddress($email);
            $mail->MsgHTML($message1);
            $mail->Subject = $subject;
            $mail->Send();

            $mail->ClearAllRecipients();

            $mail->addAddress(EMAIL);
            $mail->MsgHTML($message2);
            $mail->Subject = $subject;
            $mail->Send();

        } catch (Exception $e) {
            $response["error"] = true;
            $response["message"] = $e->getMessage();
        }

    }
}

?>

<div class="container">
    <div class="row">
        <div class="col-12 col-md-8">
            <div class="row">

                <style>
                    @media (min-width: 1200px) {
                        #image {
                            height: 325px !important;
                            width: 680px !important;
                        }
                    }

                    .show_more_main {
                        margin: 15px 25px;
                    }

                    .show_more {
                        background-color: #ef926d;
                        border: 1px solid;
                        border-color: #d3d3d3;
                        color: #ffffff;
                        font-size: 16px;
                        outline: 0;
                    }

                    .show_more {
                        cursor: pointer;
                        display: block;
                        padding: 10px 0;
                        text-align: center;
                        font-weight: bold;
                    }

                    .loding {
                        background-color: #e9e9e9;
                        border: 1px solid;
                        border-color: #c6c6c6;
                        color: #333;
                        font-size: 12px;
                        display: block;
                        text-align: center;
                        padding: 10px 0;
                        outline: 0;
                        font-weight: bold;
                    }

                    .loding_txt {
                        background-image: url(loading.gif);
                        background-position: left;
                        background-repeat: no-repeat;
                        border: 0;
                        display: inline-block;
                        height: 16px;
                        padding-left: 20px;
                    }
                </style>

                <div class="container">
                    <div class="postList">
                        <?php
                        $dbHost     = "localhost";
                        $dbUsername = "root";
                        $dbPassword = "";
                        $dbName     = "womankenya";

                        // Create database connection
                        $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

                        // Check connection
                        if ($db->connect_error) {
                            die("Connection failed: " . $db->connect_error);
                        }

                        // Get records from the database
                        $query = $db->query("SELECT * FROM tbl_articles WHERE status = 'published' ORDER BY id DESC LIMIT 3");

                        if ($query->num_rows > 0) {
                            while ($article = $query->fetch_assoc()) {
                                $postID = $article['id'];
                                ?>
                                <div class="col-12 mt-5 ">
                                    <div class="card p-4 border-0">
                                        <a href="article.php?id=<?php echo $article['id']; ?>"
                                           style="text-decoration: none; font-family: 'Roboto', sans-serif!important; color: #000;">
                                            <div class="titles py-4"><?php echo $article['title']; ?></div>
                                        </a>


                                        <style>

                                            .entry-header .entry-meta {
                                                border-top: 1px solid #ddd;
                                            }

                                            .entry-meta {
                                                font-size: 10px;
                                                letter-spacing: 2px;
                                                text-transform: uppercase;
                                                font-family: 'Roboto', sans-serif!important;
                                                color: #6f6b6b;
                                                text-align: center;
                                            }
                                        </style>


                                        <div class="entry-meta" style="margin-bottom: 10px!important;">
			<span class="date-bar-white-bg">



				<span class="entry-cat">By <?php echo $article['author']; ?></a></span>

									<span class="entry-date updated"> | <time datetime="2019-03" itemprop="datePublished">

									     <?php $timeStamp = date("F d, Y", strtotime($article['created_at']));
                                         echo $timeStamp; ?>

									   </time>
									   </span>
			</span>
                                        </div>

                                        <a href="article.php?id=<?php echo $article['id']; ?>">
                                            <img src="assets/img/post_pic/<?php echo $article['image']; ?>"
                                                 class="card-img-top" style="width: 100%!important; height: 100% !important;"
                                                 id="image"
                                                 alt="...">
                                        </a>
                                        <div class="card-body">
                                            <a href="article.php?id=<?php echo $article['id']; ?>"
                                               style="text-decoration: none; font-family: 'Roboto', sans-serif!important; color: #6f6b6b; font-size: 14px">
                                                <p class="card-text">
                                                    <?php
                                                    echo $article['summary']; ?>
                                                </p>
                                            </a>
                                            <a href="article.php?id=<?php echo $article['id']; ?>" class="btn btn-dark mt-3">Read
                                                More</a>
                                        </div>
                                    </div>
                                </div>

                            <?php } ?>
                            <div class="show_more_main" id="show_more_main<?php echo $postID; ?>">
                                <span id="<?php echo $postID; ?>" class="show_more"
                                      title="Load more posts" style="font-family: 'Roboto', sans-serif!important; color: #fff;">Show more</span>
                                <span class="loding" style="display: none; font-family: 'Roboto', sans-serif!important; color: #514f4f;"><span
                                            class="loding_txt">Loading...</span></span>
                            </div>
                        <?php } ?>
                    </div>
                </div>

            </div>

            <script type="text/javascript">
                $(document).ready(function () {
                    $(document).on('click', '.show_more', function () {
                        var ID = $(this).attr('id');
                        $('.show_more').hide();
                        $('.loding').show();
                        $.ajax({
                            type: 'POST',
                            url: 'ajax_more.php',
                            data: 'id=' + ID,
                            success: function (html) {
                                $('#show_more_main' + ID).remove();
                                $('.postList').append(html);
                            }
                        });
                    });
                });
            </script>
        </div>
        <div class="col-12 col-md-4 mt-5">
            <?php
            include_once "common/front_sidebar.php";
            ?>
        </div>
    </div>
</div>
<?php
require_once "common/front_footer.php";
?>
