<?php
    include "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>
        <div class="card-block container" style="padding-bottom: 60px">
            <table class="table sortable">
                <thead>
                    <tr>
                        <?php
                        if (!isset($_GET['check'])){?>
                            <th class="sorttable_nosort" style="width: 2%"></th>
                            <?php
                        }
                        ?>
                        <th style="cursor: pointer; padding-left: 30px; width: 25%">Pealkiri</th>
                        <?php
                        if (isset($_GET['check'])){?>
                            <th style="cursor: pointer; padding-left: 30px; width: 17%">Osakond</th>
                            <?php
                        }
                        if (!empty($_GET['check']) && $_GET['check'] == 'old'){?>
                            <th style="cursor: pointer; width: 10%">Arhiveeritud</th>
                            <?php
                        }
                        else{?>
                            <th style="cursor: pointer; width: 10%">Muudetud</th>
                            <?php
                        }
                        ?>
                        <th class="sorttable_nosort">Lühisisu</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        if (!empty($_GET['search'])){
                            $search = mysqli_escape_string($mysqli, $_GET['search']);
                            $searchUpper = ucfirst($search);
                            if ($right == 2){
                                $sql = "SELECT id, title, edited, ROUND ( ( LENGTH(content) - LENGTH(REPLACE(REPLACE (content, '$search', ''), '$searchUpper', '')) ) / LENGTH('$search') ) AS count FROM document WHERE state = 1 AND content LIKE '%". $search ."%' ORDER BY count DESC";
                            }
                            else{
                                $sql = "SELECT id, title, edited, ROUND ( ( LENGTH(content) - LENGTH(REPLACE(REPLACE (content, '$search', ''), '$searchUpper', '')) ) / LENGTH('$search') ) AS count FROM document WHERE state = 1 AND content LIKE '%". $search ."%' AND readableFor LIKE '%". $authorDM ."%' ORDER BY count DESC";
                            }
                            ?>
                                <div style="padding-bottom: 20px">
                                    <a href="index.php">Juhendid</a> <i class="fa fa-arrow-right"></i> <a style="cursor: default">Otsing</a>
                                </div>
                            <?php
                        }
                        else if(!empty($_GET['department'])){
                            $department = mysqli_escape_string($mysqli, $_GET['department']);
                            if ($right == 2){
                                $sql = "SELECT * FROM document WHERE state = 1 AND department='$department' ORDER BY title";
                            }else{
                                $sql = "SELECT * FROM document WHERE state = 1 AND department='$department' AND readableFor LIKE '%". $authorDM ."%' ORDER BY title";
                            }
                            ?>
                                <div style="padding-bottom: 20px">
                                    <a href="index.php">Juhendid</a> <i class="fa fa-arrow-right"></i> <a style="cursor: default"><?= $department; ?></a>
                                </div>
                            <?php
                        }
                        else if(!empty($_GET['check'])){
                            if ($_GET['check'] == 'new'){
                                $sql = "SELECT * FROM document WHERE state = 2 ORDER BY title";
                                $catalog = 'Uued';
                            }
                            else if ($_GET['check'] == 'old'){
                                $sql = "SELECT * FROM document WHERE state = 3 ORDER BY title";
                                $catalog = 'Arhiiv';
                            }
                            ?>
                            <div style="padding-bottom: 20px">
                                <a href="index.php">Juhendid</a> <i class="fa fa-arrow-right"></i> <a style="cursor: default"><?= $catalog; ?></a>
                            </div>
                            <?php
                        }

                        $insert = $mysqli->query($sql);

                        if (($insert->num_rows) > 0){
                            while ($row = mysqli_fetch_array($insert)){
                                ?>
                                <tr>
                                    <?php
                                    if (!isset($_GET['check'])) {
                                        ?>
                                        <td>
                                            <?php
                                            $id = $row['id'];
                                            $sql = "SELECT * FROM favourite WHERE userId = $authorId AND documentId = '$id'";
                                            $action = $mysqli->query($sql);
                                            if (($action->num_rows) > 0) {
                                                ?>
                                                <i class="fa fa-heart favourite" title="Ei meeldi"
                                                   id="<?= $row['id']; ?>" style="cursor: pointer"></i>
                                                <?php
                                            } else {
                                                ?>
                                                <i class="fa fa-heart-o favourite" title="Meeldib"
                                                   id="<?= $row['id']; ?>" style="cursor: pointer"></i>
                                                <?php
                                            }
                                            ?>
                                        </td>
                                        <?php
                                    }
                                    ?>
                                    <td><a href="document.php?id=<?php echo $row['id']; ?>"><?php echo $row['title'] ?></a></td>
                                    <?php
                                    if (isset($_GET['check'])){?>
                                        <td><a href=files.php?department=<?= str_replace(" ", "%20", $row['department']); ?>><?= $row['department']; ?></a></td>
                                        <?php
                                    }
                                    ?>
                                    <td><?php echo date('d.m.Y', strtotime($row['edited'])); ?></td>
                                    <td style="padding-top: 5px; padding-left: 0px; width: 50%"><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
                            }
                        }
                        ?>
                </tbody>
            </table>
        </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>
        
        $('.smallContent').click(function() {
            var button1 = $(this);
            var id = $(this).data('id');
            var div = $(this).prev('div');
            $.get(
                'files.php',
                {small: id},
                function (v) {
                    div.html(v);
                    div.toggle();
                    if (button1.text() == 'Eelvaade'){
                        button1.text('Peida');
                    }else{
                        button1.text('Eelvaade');
                    }
                }
            );
        });

        $('.favourite').click(function () {
            var documentId = $(this).attr('id');
            var ourHeart = $(this);
            if (ourHeart.hasClass('fa-heart-o')){
                $.post(
                    'userFavourites.php',
                    {
                        ifNew: 1,
                        userId: <?=$authorId; ?>,
                        documentId: documentId
                    },
                    function (v) {
                        ourHeart.toggleClass('fa-heart-o').toggleClass('fa-heart');
                        ourHeart.attr('title', 'Ei meeldi');

                    }
                );
            }
            else if (ourHeart.hasClass('fa-heart')){
                $.post(
                    'userFavourites.php',
                    {
                        ifNew: 0,
                        userId: <?=$authorId; ?>,
                        documentId: documentId
                    },
                    function (v) {
                        ourHeart.toggleClass('fa-heart-o').toggleClass('fa-heart');
                        ourHeart.attr('title', 'Meeldib');
                    }
                );
            }
        });
    </script>

</body>
</html>