<?php
    include_once "db.php";

    if (isset($_GET['small'])) {
        $id = mysqli_escape_string($mysqli, $_GET['small']);
        $sql = "SELECT * FROM document WHERE id = '$id'";
        $insert = $mysqli->query($sql);
        while ($row = mysqli_fetch_array($insert)) {
            echo substr($row['content'], 0, 400) . "...";
            exit;
        }
    }

?>

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

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


<body>

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

    <main>

        <!--Main layout-->
        <div class="card-block container">

            <div style="padding-bottom: 20px">
                <a style="cursor: default">Juhendid</a> <i class="fa fa-arrow-right"></i> ...
            </div>

            <div class="box" style="padding-bottom: 5px">

                <?php

                $sql = "SELECT * FROM document WHERE id IN (SELECT documentId FROM favourite WHERE userId = '$authorId') ORDER BY title";
                $insert = $mysqli->query($sql);
                $rowCount = $insert->num_rows;

                ?>

                <div>
                    <h4 class="h2-responsive" id="favTableTitle" style="cursor: pointer">Lemmikud (<?=$rowCount;?>) <i class="fa fa-angle-up arrow"></i></h4>
                </div>

                <table class="table sortable" id="favTable">
                    <thead>
                        <tr>
                            <th class="sorttable_nosort" style="width: 1%"></th>
                            <th style="cursor: pointer; padding-left: 30px; width: 25%">Pealkiri</th>
                            <th style="cursor: pointer; padding-left: 30px; width: 17%">Osakond</th>
                            <th style="cursor: pointer; padding-left: 20px; width: 10%">Muudetud</th>
                            <th class="sorttable_nosort" style="padding-left: 30px">Lühisisu</th>
                        </tr>
                    </thead>
                    <tbody>

                        <?php
                        if ($rowCount > 0){
                            while ($row = mysqli_fetch_array($insert)){
                                ?>
                                <tr>
                                    <td><i class="fa fa-heart favourite" title="Ei meeldi" id="<?= $row['id']; ?>" style="cursor: pointer"></i></td>
                                    <td><a href="document.php?id=<?= $row['id']; ?>"><?php echo $row['title'] ?></a></td>
                                    <td><a href=files.php?department=<?= str_replace(" ", "%20", $row['department']); ?>><?= $row['department']; ?></a></td>
                                    <td><?php echo date('d.m.Y', strtotime($row['edited'])); ?></td>
                                    <td><div style="display: none; word-wrap: break-word; width: 400px; font-family: 'Times New Roman'; font-size: 14px; padding-left: 5px"></div><button class="btn btn-primary btn-sm smallContent" data-id="<?= $row['id']; ?>" type="button">Eelvaade</button></td>
                                </tr>
                                <?php
                            }
                        }
                        else{
                            ?>
                            <div style="padding-bottom: 10px">Mitte ühtegi lemmikut pole veel valitud.</div>
                            <?php
                        }
                        ?>

                    </tbody>

                </table>
            </div>

            <div style="height: 20px"></div>

            <div class="box" style="padding-bottom: 5px">

                <?php

                $sql = "SELECT * FROM document WHERE authorId = '$authorId' ORDER BY title";
                $insert = $mysqli->query($sql);
                $rowCount2 = $insert->num_rows;

                ?>

                <div>
                    <h4 class="h2-responsive" id="myTableTitle" style="cursor: pointer">Loodud (<?= $rowCount2; ?>) <i class="fa fa-angle-down arrow2"></i></h4>
                </div>

                <table class="table sortable" id="myTable" style="display: none">
                    <thead>
                    <tr>
                        <th style="cursor: pointer; padding-left: 30px; width: 25%">Pealkiri</th>
                        <th style="width: 2%; padding-left: 0px">Staatus</th>
                        <th style="cursor: pointer; padding-left: 30px; width: 17%">Osakond</th>
                        <th style="cursor: pointer; padding-left: 20px; width: 10%">Muudetud</th>
                        <th class="sorttable_nosort" style="padding-left: 18px">Lühisisu</th>
                    </tr>
                    </thead>
                    <tbody>

                    <?php

                    if ($rowCount2 > 0){
                        while ($row = mysqli_fetch_array($insert)){
                            ?>
                            <tr>
                                <td><a href="document.php?id=<?php echo $row['id']; ?>"><?php echo $row['title'] ?></a></td>
                                <td>
                                    <?php
                                    if ($row['state'] == 1){?>
                                        <i class="fa fa-check" title="Avalik"></i>
                                        <?php
                                    }
                                    else if ($row['state'] == 2){?>
                                        <i class="fa fa-hourglass-half" title="Kontrollimisel"></i>
                                        <?php
                                    }
                                    else{?>
                                        <i class="fa fa-lock" title="Arhiivis"></i>
                                        <?php
                                    }
                                    ?>
                                </td>
                                <td><a href=files.php?department=<?= str_replace(" ", "%20", $row['department']); ?>><?= $row['department']; ?></a></td>
                                <td><?php echo date('d.m.Y', strtotime($row['edited'])); ?></td>
                                <td style="padding-top: 5px; padding-left: 0px"><div style="display: none; word-wrap: break-word; width: 600px; font-family: 'Times New Roman'; font-size: 14px; padding-left: 5px"></div><button class="btn btn-primary btn-sm smallContent" data-id="<?= $row['id']; ?>" type="button">Eelvaade</button></td>
                            </tr>
                            <?php
                        }
                    }
                    else{
                        ?>
                        <div style="padding-bottom: 10px">Mitte ühtegi dokumenti pole Teie poolt loodud.</div>
                        <?php
                    }
                    ?>

                    </tbody>

                </table>
            </div>

            <div style="    height: 75px"></div>

        </div>

    </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 src="js/sorttable.js"></script>
    <script>

        $('.favourite').click(function () {
            var documentId = $(this).attr('id');
            var ourHeart = $(this);
            $.post(
                'userFavourites.php',
                {
                    ifNew: 0,
                    userId: <?=$authorId; ?>,
                    documentId: documentId
                },
                function (v) {
                    ourHeart.toggleClass('fa-heart-o').toggleClass('fa-heart');
                    setTimeout (function () {
                        ourHeart.parents("tr").hide();
                    }, 100);


                }
            );
        });

        $('.smallContent').click(function() {
            var button1 = $(this);
            var id = $(this).data('id');
            var div = $(this).prev('div');
            $.get(
                'files.php',
                {small: id},
                function (vastus) {
                    div.html(vastus);
                    div.toggle();
                    if (button1.text() == 'Eelvaade'){
                        button1.text('Peida');
                    }else{
                        button1.text('Eelvaade');
                    }
                }
            );
        });

        $('#favTableTitle').click(function () {
            $('#favTable').toggle();
            $('.arrow').toggleClass('fa-angle-down').toggleClass('fa-angle-up');
        });

        $('#myTableTitle').click(function () {
            $('#myTable').toggle();
            $('.arrow2').toggleClass('fa-angle-down').toggleClass('fa-angle-up');
        });
    </script>

</body>
</html>
