<?php
session_start();
require_once '../include/Constants.php';
require_once '../include/DatabaseConn.php';
require_once '../models/Investment.php';
require_once '../models/Admin.php';

$admin = Admin::getCurrentAdmin();

if (is_null($admin)) {
    $url = $_SERVER['REQUEST_URI'];
    header("location: ./login?" . RETURN_URL . "=" . $url);
    die();
}
$db = new DatabaseConn();
$link = $db->connect();

if (isset($_POST['group'])) {
    $group = filter_input(INPUT_POST, 'group');
    if (is_null($group) || is_bool($group) || empty($group) || strcmp($group, NONE) == 0) {
        $error = "Wallet address is not valid";
    } else {
        if (!isset($error)) {
            mysqli_query($link, "insert into igroup(`name`) VALUES('$group')");
            if (mysqli_affected_rows($link) > 0) {
                $success = "Investment Group has been added";
            } else {
                $error = "Duplicate entry";
            }
        }
    }
} else if (isset($_POST['delete'])) {
    $ids = filter_input(INPUT_POST, 'delete');
    $ids = json_decode($ids);
    foreach ($ids as $id) {
        Investment::deleteInvestmentGroup($id, $link);
    }
    $success = "Operation was successful";
} else if (isset($_POST['walletEditName'])) {
    $id = filter_input(INPUT_POST, 'walletEditName');
    $name = filter_input(INPUT_POST, 'groupEdit');
    mysqli_query($link, "update igroup set `name` = '$name' WHERE id = $id");
    if (mysqli_affected_rows($link) > 0) {
        $success = "Operation was successful";
    } else {
        $error = "Duplicate Entry";
    }
}

$groups = Investment::getInvesmentGroup($link);
?>
<!DOCTYPE html>

<html>
    <head>
        <?php require_once './partials/head.php'; ?>
        <link href="../css/datatables.css" rel="stylesheet" type="text/css"/>
        <link href="../css/theme-checkbox-radio.css" rel="stylesheet" type="text/css"/>
        <link href="../css/dt-global_style.css" rel="stylesheet" type="text/css"/>
        <link href="../css/custom_dt_custom.css" rel="stylesheet" type="text/css"/>
        <link href="../css/snackbar.min.css" rel="stylesheet" type="text/css"/>
        <style>
            .settings-profile input[type="submit"] {
                background: #007bff;
                color: #fff;
                font-weight: 600;
                padding: 10px 25px;
                height: inherit;
                font-size: 16px;
                margin-top: 10px;
                border: 1px solid #e0e3eb;
                border-radius: 5px;
            }
        </style>
        <link href="../css/form.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div id="app">
            <?php require_once './partials/top-header.php'; ?>
            <!--  BEGIN MAIN CONTAINER  -->
            <div class="main-container" id="container">
                <div class="overlay"></div>
                <div class="search-overlay"></div>
                <?php require_once './partials/side-bar.php'; ?>
                <!--  BEGIN CONTENT AREA  -->
                <div id="content" class="main-content">
                    <div class="layout-px-spacing">
                        <div class="row layout-top-spacing layout-spacing">
                            <div class="col-lg-12">
                                <div class="statbox widget box box-shadow">
                                    <div class="widget-content widget-content-area">
                                        <div class="table-responsive mb-4 style-1">
                                            <table id="style-1" class="table style-1 table-hover non-hover">
                                                <thead>
                                                    <tr>
                                                        <th>Name</th>
                                                        <th>Actions</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php
                                                    if (!is_null($groups)) {
                                                        foreach ($groups as $group) {
                                                            ?>
                                                            <tr data-wallet="<?php echo "{$group->getId()}"; ?>">
                                                                <td class="wallet-name"><?php echo "{$group->getName()}"; ?></td>
                                                                <td class="">
                                                                    <div class="dropdown custom-dropdown">
                                                                        <a class="dropdown-toggle" href="#" role="button" id="dropdownMenuLink1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                                                                            <svg
                                                                                xmlns="http://www.w3.org/2000/svg"
                                                                                width="24"
                                                                                height="24"
                                                                                viewBox="0 0 24 24"
                                                                                fill="none"
                                                                                stroke="currentColor"
                                                                                stroke-width="2"
                                                                                stroke-linecap="round"
                                                                                stroke-linejoin="round"
                                                                                class="feather feather-more-horizontal"
                                                                                >
                                                                            <circle cx="12" cy="12" r="1"></circle>
                                                                            <circle cx="19" cy="12" r="1"></circle>
                                                                            <circle cx="5" cy="12" r="1"></circle>
                                                                            </svg>
                                                                        </a>

                                                                        <div class="dropdown-menu" aria-labelledby="dropdownMenuLink1">
                                                                            <a class="dropdown-item edit-wallet cursor" data-id="<?php echo $group->getId(); ?>" data-name="<?php echo "{$group->getName()}"; ?>">Edit</a>
                                                                            <a class="dropdown-item cursor" onclick="delete_({emails: '<?php echo "{$group->getId()}"; ?>'})">Delete</a>
                                                                        </div>

                                                                    </div>
                                                                </td>
                                                            </tr>
                                                            <?php
                                                        }
                                                    }
                                                    ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>

                                </div>
                                <div class="card settings-profile" style="margin-top: 50px">
                                    <div class="card-body">
                                        <h5 class="card-title">New Investment Group</h5>
                                        <form method="post" action="group">
                                            <div class="form-row">
                                                <div class="col-md-6">
                                                    <label for="rewritePassword">Add Investment Group</label>
                                                    <input id="rewritePassword" name="group" type="text" class="form-control" placeholder="Enter name">
                                                </div>
                                                <div class="col-md-12">
                                                    <input type="submit" value="Save">
                                                </div>
                                            </div>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                    <?php require_once './partials/footer.php'; ?>
                </div>
                <!--  END CONTENT AREA  -->
            </div>
            <!-- END MAIN CONTAINER -->
        </div>
        <div class="modal fade login-modal" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="loginModalLabel"  aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header" id="loginModalLabel">
                        <h4 class="modal-title">Edit Investment Group</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                            <svg
                                aria-hidden="true"
                                xmlns="http://www.w3.org/2000/svg"
                                width="24"
                                height="24"
                                viewBox="0 0 24 24"
                                fill="none"
                                stroke="currentColor"
                                stroke-width="2"
                                stroke-linecap="round"
                                stroke-linejoin="round"
                                class="feather feather-x"
                                >
                            <line x1="18" y1="6" x2="6" y2="18"></line>
                            <line x1="6" y1="6" x2="18" y2="18"></line>
                            </svg>
                        </button>
                    </div>
                    <div class="modal-body">
                        <form class="mt-0" id="frmWallet" method="POST" action="group">
                            <div class="form-group">
                                <input type="text" class="form-control mb-2" id="groupEdit" name="groupEdit" value="" />
                                <input type="hidden" class="form-control mb-2" id="walletEditName" name="walletEditName" value="" />
                            </div>
                            <button type="submit" class="btn btn-primary mt-2 mb-2 btn-block">Update</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <?php require_once './partials/scripts.php'; ?>
        <script src="../js/datatables.js" type="text/javascript"></script>
        <script src="../js/snackbar.min.js" type="text/javascript"></script>
        <script src="https://cdn.jsdelivr.net/npm/select2@4.0.13/dist/js/select2.min.js"></script>
        <script>
                                                                                $('#list').select2({
                                                                                    placeholder: "Make a Selection"
                                                                                });
                                                                                $('.edit-wallet').click(function () {
                                                                                    $('#loginModal').modal();
                                                                                    $('#groupEdit').val($(this).attr('data-name'));
                                                                                    $('#walletEditName').val($(this).attr('data-id'));
                                                                                    return;
                                                                                });
                                                                                function delete_(emails) {
                                                                                    var form = document.createElement("form");
                                                                                    form.setAttribute('method', "post");
                                                                                    form.setAttribute('action', "group");
                                                                                    var i = document.createElement("input"); //input element, text
                                                                                    i.setAttribute('type', "hidden");
                                                                                    i.setAttribute('name', "delete");
                                                                                    i.setAttribute('value', JSON.stringify(emails));
                                                                                    form.appendChild(i);
                                                                                    document.getElementsByTagName('body')[0].appendChild(form);
                                                                                    $(form).trigger("submit");
                                                                                }
        </script>
        <?php if (isset($error)) { ?>
            <script>Snackbar.show({
                    text: '<?php echo "$error"; ?>',
                    pos: 'top-right'
                });</script>
        <?php } else if (isset($success)) { ?>
            <script> Snackbar.show({
                    text: '<?php echo "$success"; ?>',
                    pos: 'top-right'
                });</script>
        <?php } ?>

    </body>
</html>
