<?php
session_start();

require('../../includes/database.php');
include("../../log.php");

if (isset($_SESSION['accUsername']) && $_SESSION['accPassword'] != "") {
} else {
    header("Location:../../login/");
}
$accUsername = $_SESSION['accUsername'];

$accUsername = $_SESSION['accUsername'];
$accPassword = $_SESSION['accPassword'];

$query = "SELECT * FROM accounts	
			WHERE username = '$accUsername'
			&&   password = '$accPassword'";

$result = mysqli_query($con, $query) or die(mysqli_connect_error());
$rows = mysqli_num_rows($result);
if ($rows == 1) {
    while ($data = mysqli_fetch_assoc($result)) {

        extract($data);
        include('../components/head.php');

        if (isset($_SESSION['messageResponse']) != "") {
            $message_response = $_SESSION['messageResponse'];
        } else {
            $message_response = "";
        }

?>

        <body x-data class="is-header-blur is-sidebar-open" x-bind="$store.global.documentBody">
            <!-- App preloader-->
            <?php
            // include("../components/preloader.php");
            include("../components/sidebar.php");
            include("../components/sidebar-body.php");
            include("../components/nav.php");
            include("../components/mobile-search.php");
            include("../components/right-sidebar.php");
            ?>

            <!-- Main Content Wrapper -->
            <main class="main-content mail-app w-full px-[var(--margin-x)] pb-6">
                <div class="flex flex-col-reverse py-5 sm:flex-row sm:justify-between">
                    <div class="mt-2 flex items-center justify-between space-x-1">
                        <div class="flex items-center px-2.5">
                            <div x-data="usePopper({placement:'bottom-start',offset:4})" @click.outside="isShowPopper && (isShowPopper = false)" class="inline-flex">
                                <?php include("./send-message.php");
                                ?>
                            </div>
                        </div>
                        <div class="flex">
                            <button class="btn h-8 w-8 rounded-full p-0 hover:bg-slate-300/20 focus:bg-slate-300/20 active:bg-slate-300/25 dark:hover:bg-navy-300/20 dark:focus:bg-navy-300/20 dark:active:bg-navy-300/25">
                                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
                                </svg>
                            </button>
                        </div>
                    </div>
                    <div class="flex justify-between">
                        <div class="text-lg font-medium text-slate-700 dark:text-navy-100 sm:hidden">
                            Messages
                        </div>
                        <div class="flex items-center space-x-1">
                            <div class="flex items-center space-x-2">
                                <span>
                                    <?php echo $notification_counts ?>
                                </span>
                                <div class="flex">
                                    <div class="spinner h-7 w-7 animate-spin rounded-full border-[3px] border-slate-500 border-r-transparent dark:border-navy-300 dark:border-r-transparent"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <?php
                if ($message_response != "") {
                ?>
                    <div class="space-y-4 mt-3">
                        <div x-data="{isShow:true}" :class="!isShow && 'opacity-0 transition-opacity duration-100'" class="alert flex items-center justify-between overflow-hidden rounded-lg border border-success text-success">
                            <div class="flex">
                                <div class="px-4 py-3 sm:px-5 text-center"><?php echo $message_response ?></div>
                            </div>
                            <div class="px-2">
                                <button @click="isShow = false; setTimeout(()=>$root.remove(0),300)" class="btn h-7 w-7 rounded-full p-0 font-medium text-success border-success">
                                    <i class="fa fa-close"></i>
                                </button>
                            </div>
                        </div>
                    <?php } ?>
                    <div class="card">
                        <?php
                        require('../../includes/database.php');
                        $query = "SELECT * FROM notifications WHERE username='$username' ORDER BY date DESC";
                        $result = mysqli_query($con, $query) or die(mysqli_connect_error());
                        while ($noti = mysqli_fetch_assoc($result)) {
                            $input = $noti['date'];
                            $date = strtotime($input);
                            include("./message-preview.php");
                        } ?>
                    </div>
                    <div class="mt-4 flex flex-col space-y-1 px-5 text-xs sm:flex-row sm:justify-between sm:space-y-0">
                        <a href="#" class="hover:text-slate-700 hover:underline dark:hover:text-navy-100">All messages from <?php echo $siteNameShort ?> will appear here </a>
                        <p>Last activity: 3 minute ago</p>
                    </div>
            </main>

            <div class="fixed right-3 bottom-3 rounded-full bg-white dark:bg-navy-700">
                <button @click="showModal = true" class="btn h-14 w-14 rounded-full bg-warning p-0 font-medium text-white hover:bg-warning-focus focus:bg-warning-focus active:bg-warning-focus/90 sm:hidden">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                        <path stroke-linecap="round" stroke-linejoin="round" d="M15.232 5.232l3.536 3.536m-2.036-5.036a2.5 2.5 0 113.536 3.536L6.5 21.036H3v-3.572L16.732 3.732z" />
                    </svg>
                </button>
            </div>
            </div>

            <div id="message-preview"></div>
            <div id="send-new-message">
                <script>
                    window.addEventListener("DOMContentLoaded", () => Alpine.start());
                </script>
        </body>

        </html>


<?php
    }
}
?>