<?php
include("../classes/manager.class.php");
$m = new Manager();
$output = '';
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('includes/inc_head.php'); ?>
    <link href="<?= PROJECT_URL ?>js/plugins/jquery.nestable/nestable.css" type="text/css" rel="stylesheet"
          media="screen,projection">
</head>
<body>
<!-- Start Page Loading -->
<div id="loader-wrapper">
    <div id="loader"></div>
    <div class="loader-section section-left"></div>
    <div class="loader-section section-right"></div>
</div>
<!-- End Page Loading -->
<div id="container">
    <?php include('includes/inc_top.php'); ?>
    <div class="row">
        <div class="col s12 m12">
            <div class="">
                <div class="">
                    <h3>Menus Setup</h3>
                </div>
                <hr>
                <?php
                if (isset($_GET['edit'])) {
                    $id = $_GET['id'];
                if (empty($_POST)) {
                    $qry = Menu::GetMenu($id);
                    $name = $qry->name;
                    $url = $qry->url;
                    $status = $qry->status;
                }
                else if (!empty($_POST)) {
                    extract($_REQUEST);
                    $data['name'] = trim($name);
                    $data['url'] = $url;
                    $data['status'] = $status;

                    $errors = array();
                    if ((strlen($name) == 0))
                        array_push($errors, "Please enter navigation label");
                    if ((strlen($url) == 0))
                        array_push($errors, "Please enter URL");
                if (count($errors) == 0) {
                    $q = Menu::UpdateMenu($id, $data);
                    $output = ''; ?>
                    <script>window.location.href = '?sucess'</script>
                <?php
                } else {
                    //Prepare errors for output
                    foreach ($errors as $val) {
                        $output .= "<div class='alert alert-warning fade in'>
                            <button type='button' class='close' data-dismiss='alert'>&times;</button>
                            <strong>Attention! </strong>$val</div>";
                    }
                }
                }
                ?>
                    <form role="form" method="post"
                          action="">
                        <div class="col s12 form-group">
                            <div class="row">
                                <?php echo $output; ?>
                            </div>
                            <div class="col s8">
                                <div class="row">
                                    <input type="hidden" name="id" value="<?= $id ?>">
                                    <span>(*) Mark Fields are mandatory</span><br/><br/>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" name="name" id="name" value="<?= $name ?>">
                                        <label for="name" class="active">Navigation Label *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" name="url" id="url" value="<?= $url ?>">
                                        <label for="url" class="active">URL *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <select class="form-control" name="status" id="status">
                                            <option value="1" <?php if ($status == '1') {
                                                echo "selected='selected'";
                                            } ?>>Active
                                            </option>
                                            <option value="0" <?php if ($status == '0') {
                                                echo "selected='selected'";
                                            } ?>>Inactive
                                            </option>
                                        </select>
                                        <label for="status" class="control-label">Status</label><br>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <button type="submit"
                                                style="margin: auto;"
                                                class="btn waves-effect waves-light">
                                            Save
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                <?php
                }
                else {
                if (!empty($_POST)) {
                extract($_REQUEST);
                //print_r($_REQUEST['nestable-output']);
                $obj = json_decode($_REQUEST['nestable-output']);
                $i = 0;
                if ($obj) {
                    foreach ($obj AS $row) {
                        //echo "<br>".$row->id;
                        //echo "0";
                        $i++;
                        //echo "".$i;
                        $data['parent_id'] = "0";
                        $data['menu_order'] = $i;
                        Menu::UpdateMenu($row->id, $data);
                        if (isset($row->children)) {
                            foreach ($row->children AS $rows) {
                                //echo "<br>".$rows->id;
                                //echo " ".$row->id;
                                $i++;
                                //echo "".$i;
                                $data['parent_id'] = $row->id;
                                $data['menu_order'] = $i;
                                Menu::UpdateMenu($rows->id, $data);
                                if (isset($rows->children)) {
                                    foreach ($rows->children AS $rowss) {
                                        //echo "<br>".$rowss->id;
                                        //echo " ".$rows->id;
                                        $i++;
                                        //echo "".$i;
                                        $data['parent_id'] = $rows->id;
                                        $data['menu_order'] = $i;
                                        Menu::UpdateMenu($rowss->id, $data);
                                    }
                                }
                            }
                        }
                    }
                } ?>
                    <script>window.location.href = '?sucess'</script>
                <?php
                }
                ?>
                <?php if (isset($_GET['sucess'])) { ?>
                    <div id='card-alert' class='card green lighten-5'>
                        <div class='card-content green-text'>
                            <p>SUCCESS : <strong>Attention! </strong>Submitted!</p>
                        </div>
                        <button type='button' class='close green-text' data-dismiss='alert' aria-label='Close'>
                            <span aria-hidden='true'>x</span>
                        </button>
                    </div>
                <?php } ?>
                <?php if (isset($_GET['error'])) { ?>
                    <div id='card-alert' class='card orange lighten-5'>
                        <div class='card-content orange-text'>
                            <p>WARNING : <strong>Attention! </strong>Some error occurred!</p>
                        </div>
                        <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                            <span aria-hidden='true'>x</span>
                        </button>
                    </div>
                <?php } ?>
                    <div class="row">
                        <div class="col l8">
                            <div class="dd" id="nestable">
                                <ol class="dd-list">
                                    <?php
                                    $filter = " WHERE parent_id = '0' AND status <> '0' ORDER BY menu_order";
                                    $menulist = Menu::GetMenuList($filter);
                                    if ($menulist) {
                                        foreach ($menulist as $menu) {
                                            echo '<li class="dd-item dd3-item" data-id="' . $menu->menu_id . '"><div class="dd-handle dd3-handle"></div><div class="dd3-content">' . $menu->name . '<span><a href="?edit&id=' . $menu->menu_id . '"><i class="mdi-image-edit right"></i></a></span></div>';

                                            $filter = " WHERE parent_id = '" . $menu->menu_id . "' AND status <> '0' ORDER BY menu_order";
                                            $submenulist = Menu::GetMenuList($filter);
                                            if ($submenulist) {
                                                echo '<ol class="dd-list">';
                                            }
                                            if ($submenulist) {
                                                foreach ($submenulist as $menu) {
                                                    echo '<li class="dd-item dd3-item" data-id="' . $menu->menu_id . '"><div class="dd-handle dd3-handle"></div><div class="dd3-content">' . $menu->name . '<span><a href="?edit&id=' . $menu->menu_id . '"><i class="mdi-image-edit right"></i></a></span></div>';
                                                    $filter = " WHERE parent_id = '" . $menu->menu_id . "' AND status <> '0' ORDER BY menu_order";
                                                    $innersubmenulist = Menu::GetMenuList($filter);
                                                    if ($innersubmenulist) {
                                                        echo '<ol class="dd-list">';
                                                    }
                                                    if ($innersubmenulist) {
                                                        foreach ($innersubmenulist as $menu) {
                                                            echo '<li class="dd-item dd3-item" data-id="' . $menu->menu_id . '"><div class="dd-handle dd3-handle"></div><div class="dd3-content">' . $menu->name . '<span><a href="?edit&id=' . $menu->menu_id . '"><i class="fa fa-thumb-tack" style="float: right;"></i></a></span></div></li>';
                                                        }
                                                    }
                                                    if ($innersubmenulist) {
                                                        echo '</ol>';
                                                    }
                                                    echo '</li>';
                                                }
                                            }
                                            if ($submenulist) {
                                                echo '</ol>';
                                            }
                                            echo '</li>';
                                        }
                                    }
                                    ?>
                                </ol>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col l8">
                            <form role="form" method="post" action="">
                                <input type="hidden" id="nestable-output"
                                       name="nestable-output" value=""/>
                                <br>
                                <button type="submit" class="btn waves-effect waves-light">Save</button>
                            </form>
                        </div>
                    </div>

                <?php } ?>
            </div>
        </div>
    </div>
    <?php if (!isset($_GET['edit'])) { ?>
        <div class="row">
            <div class="col l12">
                <h4 class="panel-heading">
                    In active Menus
                </h4>

                <div class="row">
                    <div class="col l8">
                        <div class="dd" id="nestable2">
                            <ol class="dd-list">
                                <?php
                                $filter = " WHERE parent_id = '0' AND status = '0' ORDER BY menu_order";
                                $menulist = Menu::GetMenuList($filter);
                                if ($menulist) {
                                    foreach ($menulist as $menu) {
                                        echo '<li class="dd-item dd3-item" data-id="' . $menu->menu_id . '"><div class="dd-handle dd3-handle"></div><div class="dd3-content">' . $menu->name . '<span><a href="?edit&id=' . $menu->menu_id . '"><i class="fa fa-thumb-tack" style="float: right;"></i></a></span></div>';

                                        $filter = " WHERE parent_id = '" . $menu->menu_id . "' AND status = '0' ORDER BY menu_order";
                                        $submenulist = Menu::GetMenuList($filter);
                                        if ($submenulist) {
                                            echo '<ol class="dd-list">';
                                        }
                                        if ($submenulist) {
                                            foreach ($submenulist as $menu) {
                                                echo '<li class="dd-item dd3-item" data-id="' . $menu->menu_id . '"><div class="dd-handle dd3-handle"></div><div class="dd3-content">' . $menu->name . '<span><a href="?edit&id=' . $menu->menu_id . '"><i class="fa fa-thumb-tack" style="float: right;"></i></a></span></div>';
                                                $filter = " WHERE parent_id = '" . $menu->menu_id . "' AND status = '0' ORDER BY menu_order";
                                                $innersubmenulist = Menu::GetMenuList($filter);
                                                if ($innersubmenulist) {
                                                    echo '<ol class="dd-list">';
                                                }
                                                if ($innersubmenulist) {
                                                    foreach ($innersubmenulist as $menu) {
                                                        echo '<li class="dd-item dd3-item" data-id="' . $menu->menu_id . '"><div class="dd-handle dd3-handle"></div><div class="dd3-content">' . $menu->name . '<span><a href="?edit&id=' . $menu->menu_id . '"><i class="fa fa-thumb-tack" style="float: right;"></i></a></span></div></li>';
                                                    }
                                                }
                                                if ($innersubmenulist) {
                                                    echo '</ol>';
                                                }
                                                echo '</li>';
                                            }
                                        }
                                        if ($submenulist) {
                                            echo '</ol>';
                                        }
                                        echo '</li>';
                                    }
                                }
                                ?>
                            </ol>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?php } ?>
</div>
</div>
<?php include('includes/inc_footer.php'); ?>
<?php include('includes/inc_js.php'); ?>
<script type="text/javascript" src="<?= PROJECT_URL ?>js/plugins/jquery.nestable/jquery.nestable.js"></script>
<script type="text/javascript">
    $(document).ready(function () {

        var updateOutput = function (e) {
            var list = e.length ? e : $(e.target),
                output = list.data('output');
            if (window.JSON) {
                output.val(window.JSON.stringify(list.nestable('serialize')));//, null, 2));
            } else {
                output.val('JSON browser support required for this demo.');
            }
        };

        // activate Nestable for list 1
        $('#nestable').nestable({
            group: 1
        })
            .on('change', updateOutput);

        // activate Nestable for list 2
        $('#nestable2').nestable({
            group: 1
        })
            .on('change', updateOutput);

        // output initial serialised data
        updateOutput($('#nestable').data('output', $('#nestable-output')));
        updateOutput($('#nestable2').data('output', $('#nestable2-output')));

        $('#nestable-menu').on('click', function (e) {
            var target = $(e.target),
                action = target.data('action');
            if (action === 'expand-all') {
                $('.dd').nestable('expandAll');
            }
            if (action === 'collapse-all') {
                $('.dd').nestable('collapseAll');
            }
        });

        $('#nestable3').nestable();

    });
</script>
</body>
</html>