<?php
    include "db.php";
    if (!empty($_POST)){
        //set up data
        $title = $_POST['title'];
        $department = $_POST['department'];
        $readable = $_POST['readable'];
        $content = $_POST['content'];

        //insert data
        $sql = "INSERT INTO document (title, edited, authorId, department, readableFor, content, ifEdit, state) VALUES ('$title', NOW(), '$authorId', '$department', '$readable', '$content', 0, 2)";
        $insert = $mysqli->query($sql);
    }
?>


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

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

<body>

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

    <main>
        <div class="card-block container">

            <div class="text-center">
                <h3><i class="fa fa"></i>Loo uus dokument</h3>
                <hr class="mt-2 mb-2">
            </div>

            <form method="post" action="" enctype="multipart/form-data">
                <div class="md-form">
                    <input type="text" id="title" class="form-control" name="title" maxlength="50">
                    <label for="title" >Pealkiri</label>
                </div>

                <div style="font-size: 13px; color: #757575">Vali, millise osakonna kausta see fail salvestatakse.</div>

                <select class="mdb-select" id="department" name="department" style="padding-top: 0px">
                        <option value="1" disabled selected> Osakond</option>
                        <option value="Avalik"> Avalik</option>
                        <option value="Billing ja IT"> Billing ja IT</option>
                        <option value="Erilahendused"> Erilahendused</option>
                        <option value="Finants"> Finants</option>
                        <option value="Jaemüük"> Jaemüük</option>
                        <option value="Juhtkond"> Juhtkond</option>
                        <option value="Klienditeenindus"> Klienditeenindus</option>
                        <option value="Müük ja turundus"> Müük ja turundus</option>
                        <option value="Personal"> Personal</option>
                        <option value="Tehnika"> Tehnika</option>
                        <option value="Tootearendus"> Tootearendus</option>
                        <option value="Võrguhooldus"> Võrguhooldus</option>
                        <option value="Äriklientuur"> Äriklientuur</option>
                </select>

                <div style="font-size: 13px; color: #757575">Vali, millised osakonnad seda dokumenti näha saavad.</div>

                <select class="mdb-select" id="departmentList" name="departmentList" multiple style="">
                    <option value="" disabled selected> Kasutusõigused</option>
                    <option value="Billing ja IT"> Billing ja IT</option>
                    <option value="Erilahendused"> Erilahendused</option>
                    <option value="Finants"> Finants</option>
                    <option value="Jaemüük"> Jaemüük</option>
                    <option value="Juhtkond"> Juhtkond</option>
                    <option value="Klienditeenindus"> Klienditeenindus</option>
                    <option value="Müük ja turundus"> Müük ja turundus</option>
                    <option value="Personal"> Personal</option>
                    <option value="Tehnika"> Tehnika</option>
                    <option value="Tootearendus"> Tootearendus</option>
                    <option value="Võrguhooldus"> Võrguhooldus</option>
                    <option value="Äriklientuur"> Äriklientuur</option>
                </select>

                <div class="md-form">
                    <textarea class="ckeditor" id="editor" name="editor"></textarea>
                </div>

                <div class="text-center" style="padding-bottom: 60px">
                    <button class="btn btn-primary" type="button" name="submit" id="submit">Saada kontrollimsele</button>
                </div>
            </form>
        </div>

    </main>

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

    <!-- SCRIPTS -->

    <script src="ckeditor/ckeditor.js"></script>
    <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 src="sweetalert-master/dist/sweetalert.min.js"></script>
    <script>
        $(document).ready(function() {
            $('.mdb-select').material_select();
        });

        $('#submit').click(function (e) {
            swal({
                        title: "Kas oled kindel, et soovid dokumendi kontrollimisele saata?",
                    type: "info",
                    confirmButtonColor: "#0040aa",
                    confirmButtonText: "Jah",
                    showCancelButton: true,
                    cancelButtonText: "Ei",
                    closeOnConfirm: false
                },
                function(isConfirm){
                    if (isConfirm && document.getElementById('title').value.length != 0 && document.getElementById('department').value.length != 0 && JSON.stringify($('#departmentList').val().length) != 0 && CKEDITOR.instances['editor'].getData().length > 0) {
                        $.post(
                            'upload.php',
                            {
                                title: document.getElementById('title').value,
                                department: document.getElementById('department').value,
                                readable: JSON.stringify($('#departmentList').val()),
                                content: CKEDITOR.instances['editor'].getData()
                            },
                            function (v) {
                                toastr["success"]("Dokument kontrollimisele saadetud!");
                                setTimeout(function () {
                                    window.location.href = "index.php";
                                }, 500);
                            }
                        );
                    }
                    else {
                        toastr["error"]("Palun täida kõik lahtrid!");
                        swal.close();
                    }

                }
            );
        });


    </script>
    
    <style>
        .dropdown-content li>span {
            line-height: 0.8rem;
            padding: 0.4rem;
            font-size: 1rem;
        }
    </style>


</body>
</html>