<?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['beneficiary-message']) != "") {
            $message = $_SESSION['beneficiary-message'];
        } else {
            $message = "";
        }

        if (isset($_SESSION['beneficiary_info']) != "") {
            $beneficiary_info = $_SESSION['beneficiary_info'];
        } else {
            $beneficiary_info = "";
        }
?>

        <body x-data class="is-header-blur" 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 w-full px-[var(--margin-x)] pb-8">
                <div class="flex items-center justify-between py-5 lg:py-6">
                    <div class="flex items-center space-x-1">
                        <h2 class="text-xl font-medium text-slate-700 line-clamp-1 dark:text-navy-50 lg:text-2xl">
                            My Beneficiaries
                        </h2>
                        <div x-data="usePopper({placement:'bottom-start',offset:4})" @click.outside="isShowPopper && (isShowPopper = false)" class="inline-flex">
                            <button x-ref="popperRef" @click="isShowPopper = !isShowPopper" 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">
                                <i class="fas fa-chevron-down"></i>
                        </div>
                    </div>
                    <?php include("./add-beneficiary.php"); ?>
                </div>
                <!-- <p>For Easy and quick transfer initiation</p> -->
                <?php
                if ($beneficiary_info != "") {
                ?>
                    <div x-data="{isShow:true}" :class="!isShow && 'opacity-0 transition-opacity duration-300'" class="mt-3 mb-2 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"><b><?php echo $beneficiary_info ?></b></div>
                        </div>
                        <div class="px-2">
                            <button @click="isShow = false; setTimeout(()=>$root.remove(),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 } ?>
                <?php
                if ($message != "") {
                ?>
                    <div class="mb-2 mt-2 space-y-4">
                        <div x-data="{isShow:true}" :class="!isShow && 'opacity-0 transition-opacity duration-300'" class="alert flex items-center justify-between overflow-hidden rounded-lg border border-error text-error">
                            <div class="flex">
                                <div class="px-4 py-3 sm:px-5 text-center">Incorrect Pin</div>
                            </div>
                            <div class="px-2">
                                <button @click="isShow = false; setTimeout(()=>$root.remove(),300)" class="btn h-7 w-7 rounded-full p-0 font-medium text-error border-error">
                                    <i class="fa fa-close"></i>
                                </button>
                            </div>
                        </div>
                    <?php } ?>

                    <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 sm:gap-5 lg:grid-cols-3 lg:gap-6 xl:grid-cols-4">

                        <?php
                        require('../../includes/database.php');
                        $query = "SELECT * FROM beneficiaries WHERE email='$email'";
                        $result = mysqli_query($con, $query) or die(mysqli_connect_error());
                        while ($beneficiary = mysqli_fetch_assoc($result)) {
                        ?>
                            <div class="card">
                                <div class="p-2 text-right">
                                    <div x-data="usePopper({placement:'bottom-end',offset:4})" @click.outside="isShowPopper && (isShowPopper = false)" class="inline-flex">
                                        <button x-ref="popperRef" @click="isShowPopper = !isShowPopper" 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" stroke-width="2">
                                                <path stroke-linecap="round" stroke-linejoin="round" d="M5 12h.01M12 12h.01M19 12h.01M6 12a1 1 0 11-2 0 1 1 0 012 0zm7 0a1 1 0 11-2 0 1 1 0 012 0zm7 0a1 1 0 11-2 0 1 1 0 012 0z" />
                                            </svg>
                                        </button>

                                        <div x-ref="popperRoot" class="popper-root" :class="isShowPopper && 'show'">
                                            <div class="popper-box rounded-md border border-slate-150 bg-white py-1.5 font-inter dark:border-navy-500 dark:bg-navy-700">
                                                <ul>
                                                    <li>
                                                        <form action="../../bank-manager.php?action=delete-beneficiary" method="post">
                                                            <input type="hidden" name="email" value="<?php echo $email ?>">
                                                            <input type="hidden" name="id" value="<?php echo $beneficiary['id']; ?>">
                                                            <button type="submit" class="flex h-8 items-center px-3 font-medium tracking-wide outline-none transition-all hover:bg-slate-100 hover:text-slate-800 focus:bg-slate-100 focus:text-slate-800 dark:hover:bg-navy-600 dark:hover:text-navy-100 dark:focus:bg-navy-600 dark:focus:text-navy-100">
                                                                Remove
                                                            </button>
                                                        </form>
                                                    </li>
                                                </ul>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="flex grow flex-col items-center px-4 pb-5 sm:px-5">
                                        <h3 class="pt-3 text-lg font-medium text-slate-700 dark:text-navy-100">
                                            <?php echo $beneficiary['name']; ?>
                                        </h3>
                                        <p class="text-xs+"><?php echo $beneficiary['bank']; ?></p>
                                        <p class="text-xs+"><?php echo $beneficiary['number']; ?></p>
                                        <div class="mt-6 grid w-full grid-cols-1 gap-2">
                                            <button class="btn space-x-2 bg-slate-150 px-0 font-medium text-slate-800 hover:bg-slate-200 focus:bg-slate-200 active:bg-slate-200/80 dark:bg-navy-500 dark:text-navy-50 dark:hover:bg-navy-450 dark:focus:bg-navy-450 dark:active:bg-navy-450/90">
                                                <i class="fa fa-send"></i>
                                                <span> Send Funds </span>
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        <?php } ?>
                    </div>


                    <div id="x-teleport-target">
                    </div>

                    <script>
                        window.addEventListener("DOMContentLoaded", () => Alpine.start());
                    </script>
        </body>


        </html>


<?php
    }
}
?>