<?php
    include "db.php";

    if (!empty($_POST)){
        //set up data
        $title = utf8_decode($_POST['title']);
        $content = utf8_decode($_POST['content']);

        //insert data
        $sql = "INSERT INTO feedback (title, date, content) VALUES ('$title', NOW(), '$content')";
        $insert = $mysqli->query($sql);


        require 'PHPMailer/PHPMailerAutoload.php';

        $mail = new PHPMailer;

        $mail->isSMTP();                                      // Set mailer to use SMTP
        //$mail->Host = 'mailhost.ut.ee';
        $mail->Host = 'mail.neti.ee';                        // Specify main and backup SMTP servers //vaata et kaitsmist ikka ülikooli võrgus teed!!!
        $mail->SMTPAuth = false;                               // Enable SMTP authentication
        $mail->Port = 25;                                    // TCP port to connect to

        $mail->addAddress('sandervunk24@hotmail.com');     // Add a recipient
        $mail->isHTML(true);                                  // Set email format to HTML

        $mail->Subject = $title;
        $mail->Body    = $content;

        $mail->send();
    }

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <?php include ('head.html'); ?>
</head>

<body>

    <header>
        <?php include('header.php'); ?>
    </header>


    <main>
        <!--Naked Form-->
        <div class="card-block container">

            <form method="post" action="" enctype="multipart/form-data" onsubmit="alertFunction();">
                <!--Header-->
                <div class="text-center">
                    <h3><i class="fa fa-envelope"></i> Võta ühendust!</h3>
                    <p>Ootame konstruktiivset tagasisidet!</p>
                    <hr class="mt-2 mb-2">
                </div>

                <!--Body-->
                <div class="md-form">
                    <i class="fa fa-tag prefix"></i>
                    <input type="text" id="title" class="form-control" id='title' name="title">
                    <label for="form34">Pealkiri</label>
                </div>

                <div class="md-form">
                    <i class="fa fa-pencil prefix"></i>
                    <input type="text" id="content" class="form-control" id='content' name="content">
                    <label for="form34">Sisu</label>
                </div>

                <div class="text-center">
                    <button class="btn btn-primary" type="button" name="submit" id="submit">Saada</button>
                    <div class="call">
                        <br>
                        <p>Või hoopis helista!
                            <br>
                            <span><i class="fa fa-phone"> </i></span> + 372 56 208 555</p>
                    </div>
                </div>
            </form>

        </div>
        <!--Naked Form-->

    </main>

    <!--Footer-->
    <footer class="page-footer fixed-bottom" style="text-align: center">
        <?php include ('footer.html'); ?>
    </footer>
    <!--/.Footer-->


    <!-- SCRIPTS -->
    <script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="js/tether.min.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/mdb.min.js"></script>
    <script>
        $('#submit').click(function (e) {
            e.preventDefault();
            $.post(
                'feedback.php',
                {
                    title: document.getElementById('title').value,
                    content: document.getElementById('content').value
                },
                function (v) {
                    toastr["success"]("Tagasiside saadetud!");
                    $('#title').val('');
                    $('#content').val('');
                }
            )
        });

        $('#content').keyup(function (e) {
            if (e.keyCode == 13) {
                $('#submit').trigger("click");
            }
        });
    </script>


</body>
</html>