<?php
include("../classes/manager.class.php");
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
$m = new Manager();
$output = '';
$name = '';
$password = '';
$email = '';
$contactno = '';
$type = '';
$role = '';
$status = '';
$id = '';
$MenuMainSelected = '';
$selectedMainMenu['menu_id'] = '';
if (!isset($_SESSION['sess_userid'])) {
    header("Location: " . PROJECT_ADMIN_URL);
    exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <?php include('includes/inc_head.php'); ?>

</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="">
                <h3>User Setup</h3>
            </div>
            <hr>
            <?php if ($_SESSION['sess_role'] == 'VEC') {
                if (isset($_GET['view'])) { ?>
                    <a class="btn waves-effect waves-light" style="float:right;margin-left:5px;" href="?add">Add <i
                            class="mdi-content-add-box"></i></a>
                    <div class="clearfix"></div>
                <?php }
            } ?>
            <br/>
            <?php
            if (isset($_GET['add'])) {
            if (isset($_POST) && !empty($_POST)) {
                extract($_REQUEST);
                $data['username'] = $name1 = trim($name);
                $data['email'] = $email;
                $data['password'] = $password2 = trim($password);
                $data['type'] = $type;
                $data['role'] = $role;
                $data['status'] = $status;
                $data['contactno'] = $contactno;
                $usercheck = User_setup::CheckUserSetupByUsername($name, 0);

                $errors = array();
                // Validate the input

                if ((strlen($name1) == 0) || (strlen($name1) > 20))
                    array_push($errors, "Please enter  name");
                $space = strrpos($name1, " ");
                if ($space > 0)
                    array_push($errors, "Spaces not allowed");

                if ((strlen($password2) == 0) || (strlen($password2) > 20))
                    array_push($errors, "Please enter password");
                $spacep = strrpos($password2, " ");

                if ($spacep > 0)
                    array_push($errors, "Spaces not allowed ");
                if ((!filter_var($email, FILTER_VALIDATE_EMAIL)) || ((strlen($email) > 50)))
                    array_push($errors, "Invalid email format");
                if ((strlen($type) == 0))
                    array_push($errors, "Please select type");
                if ((strlen($role) == 0))
                    array_push($errors, "Please select role");
                if ($usercheck)
                    array_push($errors, "username already exist");
            if (count($errors) == 0) {
                $q = User_setup::AddUserSetup($data);
            if ($q) {
                $menuArray = $_POST['menu'];
                $final_menu = implode(",", $menuArray);
                $dataMenu['menu_id'] = $final_menu;
                $dataMenu['us_id'] = $q;
                $MenuAdd = Menu::AddMenuInUser($dataMenu);
                $output = '';
                //header("Location: ?view&sucess"); ?>
                <script>window.location.href = '?view&sucess'</script>
            <?php
            echo "<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>";
            } else {
                $output = '';
                echo "<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>";
            }
            } else {
                //Prepare errors for output
                foreach ($errors as $val) {
                    $output .= "<div id='card-alert' class='card orange lighten-5'>
                      <div class='card-content orange-text'>
                        <p>WARNING : <strong>Attention! </strong>$val</p>
                      </div>
                      <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
                }
            }
            }
            ?>
                <div class="row">
                    <form action="" method="post" id="user_setup_form" novalidate="novalidate">
                        <div class="col s12 form-group">
                            <?php echo $output; ?>
                            <div class="col s8">
                                <div class="row">
                                    <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" id="name" maxlength="20"
                                               name="name"
                                               value='<?php echo $name; ?>'/>
                                        <label for="name" class="control-label">User Name *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="password" id="password"
                                               name="password"
                                               maxlength="20"
                                               value='<?php echo $password; ?>'/>
                                        <label for="password" class="control-label">Password *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="email" name="email"
                                               maxlength="50"
                                               value='<?php echo $email; ?>'/>
                                        <label for="email" class="control-label">Email *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <input class="form-control" type="text" id="contactno" name="contactno"
                                               maxlength="11"
                                               pattern="^[0]{1}[3]{1}\d{9}" placeholder="03xxxxxxxx"
                                               value='<?php echo $contactno; ?>'/>
                                        <label for="contactno" class="control-label">Contact No *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <select class="form-control" id="getFname" name="type">
                                            <option></option>

                                            <?php if ($_SESSION['sess_username'] == 'admin') { ?>
                                                <option
                                                    value="<?= __ADMIN__ ?>" <?php if ($type == __ADMIN__) {
                                                    echo 'selected';
                                                } ?>>Administrator
                                                </option>
                                            <?php } ?>
                                            <option value="<?= __PROFILE__ ?>" <?php if ($type == __PROFILE__) {
                                                echo 'selected';
                                            } ?> >Profile
                                            </option>
                                        </select>
                                        <label for="getFname" class="control-label">Type *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <div id="admin"></div>
                                        <select name="role" class="form-control">
                                            <option></option>
                                            <option value="V" <?php if ($role == 'V') {
                                                echo 'selected';
                                            } ?>>View
                                            </option>
                                            <option value="VE" <?php if ($role == 'VE') {
                                                echo 'selected';
                                            } ?>>View & Edit
                                            </option>
                                            <option value="VEC" <?php if ($role == 'VEC') {
                                                echo 'selected';
                                            } ?>>View, Edit & Create
                                            </option>
                                        </select>
                                        <label for="role" class="control-label" id="role">Role *</label>
                                    </div>
                                </div>
                                <div class="row margin">
                                    <div id="status" class="input-field col s12">
                                        <select class="form-control" name="status">
                                            <option value="active" <?php if ($status == 'active') {
                                                echo "selected='selected'";
                                            } ?>>Active
                                            </option>
                                            <option value="inactive" <?php if ($status == 'inactive') {
                                                echo "selected='selected'";
                                            } ?>>Inactive
                                            </option>
                                        </select>
                                        <label for="status" class="control-label">Status</label>
                                    </div>
                                    <br/>
                                </div>
                                <div class="row margin">
                                    <div class="input-field col s12">
                                        <div id="Rmenu_rights"><h3>Menu Rights</h3>
                                            <hr>
                                            <div class="menu_right"
                                                 style=" height: 200px; width:100%;  overflow-x: hidden;   float: left;  margin-bottom: 25px;">
                                                <table class="table table-striped">
                                                    <thead>
                                                    <tr>
                                                        <th>Levels</th>
                                                        <th>Option/Screen</th>
                                                        <th></th>
                                                    </tr>
                                                    </thead>
                                                    <tbody>
                                                    <?php
                                                    $filter = " WHERE parent_id = '0' AND status <> '0' ORDER BY menu_order";
                                                    $Parentmenu = Menu::GetMenuList($filter);
                                                    if ($Parentmenu) {
                                                        foreach ($Parentmenu as $row_menu) {
                                                            $menuId = $row_menu->menu_id;
                                                            $selectedMainMenu = Menu::GetSelectedMenu($id);
                                                            $MenuMainSelected = $selectedMainMenu['menu_id'];
                                                            ?>
                                                            <tr>
                                                            <td>1</td>
                                                            <td>
                                                                <div class="menu_checkbox"><span
                                                                        for="<?php echo $row_menu->name; ?>"><?php echo $row_menu->name; ?></span>
                                                                </div>
                                                            </td>
                                                            <td>
                                                                <input type="checkbox"
                                                                       value='<?php echo $row_menu->menu_id; ?>'
                                                                       name="menu[]"
                                                                       id="<?php echo $row_menu->name; ?>" <?php
                                                                $selectedMenu = Menu::GetSelectedMenu($id);
                                                                $MenuSelected = $selectedMenu['menu_id'];
                                                                $MenuSelected;
                                                                $MenuSelected = explode(",", $MenuSelected);
                                                                $count = count($MenuSelected);
                                                                ?>
                                                                    <?php
                                                                    for ($i = 0; $i < $count; $i++) { ?>
                                                                        <?php if ($MenuSelected[$i] == $row_menu->menu_id) {
                                                                            echo 'checked';
                                                                        } ?>
                                                                    <?php } ?>
                                                                    />
                                                                <label for="<?php echo $row_menu->name; ?>"></label>
                                                            </td>
                                                            <?php
                                                            //$menuName = $row_menu->name;
                                                            $filters = " WHERE parent_id = '$menuId' AND status <> '0' ORDER BY menu_order";
                                                            $menu = Menu::GetMenuList($filters);
                                                            if ($menu) {
                                                                foreach ($menu as $row_menus) {
                                                                    $sub_menuId = $row_menus->menu_id;
                                                                    ?>
                                                                    </tr>
                                                                    <tr>
                                                                    <td>2</td>
                                                                    <td>
                                                                        <div class="menu_checkbox"
                                                                             style="margin-left: 25px;">
                                                                            <span><?php echo $row_menus->name; ?></span>
                                                                        </div>
                                                                    </td>
                                                                    <td>
                                                                        <input type="checkbox"
                                                                               value='<?php echo $row_menus->menu_id; ?>'
                                                                               name="menu[]"
                                                                               id="<?php echo $row_menus->name; ?>"<?php
                                                                        $selectedMenu = Menu::GetSelectedMenu($id);
                                                                        $MenuSelected = $selectedMenu['menu_id'];
                                                                        $MenuSelected;
                                                                        $MenuSelected = explode(",", $MenuSelected);
                                                                        $count = count($MenuSelected);
                                                                        for ($i = 0; $i < $count; $i++) {
                                                                            if ($MenuSelected[$i] == $row_menus->menu_id) {
                                                                                echo 'checked';
                                                                            }
                                                                        }

                                                                        ?> />
                                                                        <label
                                                                            for="<?php echo $row_menus->name; ?>"></label>
                                                                    </td>
                                                                    <?php
                                                                    //$menuName = $row_menu->name;
                                                                    $filters = " WHERE parent_id = '$sub_menuId' AND status <> '0' ORDER BY menu_order";
                                                                    $menus = Menu::GetMenuList($filters);
                                                                    if ($menus) {
                                                                        foreach ($menus as $row_menuss) {
                                                                            ?>
                                                                            </tr>
                                                                            <tr>
                                                                                <td>3</td>
                                                                                <td>
                                                                                    <div class="menu_checkbox"
                                                                                         style="margin-left: 50px;">
                                                                                        <span><?php echo $row_menuss->name; ?></span>
                                                                                    </div>
                                                                                </td>
                                                                                <td>
                                                                                    <input type="checkbox"
                                                                                           value='<?php echo $row_menuss->menu_id; ?>'
                                                                                           name="menu[]"
                                                                                           id="<?php echo $row_menus->name; ?>"<?php
                                                                                    $selectedMenu = Menu::GetSelectedMenu($id);
                                                                                    $MenuSelected = $selectedMenu->menu_id;
                                                                                    $MenuSelected;
                                                                                    $MenuSelected = explode(",", $MenuSelected);
                                                                                    $count = count($MenuSelected);
                                                                                    for ($i = 0; $i < $count; $i++) {
                                                                                        if ($MenuSelected[$i] == $row_menuss->menu_id) {
                                                                                            echo 'checked';
                                                                                        }
                                                                                    }

                                                                                    ?> />
                                                                                    <label
                                                                                        for="<?php echo $row_menuss->name; ?>"></label>
                                                                                </td>
                                                                            </tr>
                                                                            <?php
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                    ?>
                                                    </tbody>
                                                </table>
                                                <hr>
                                            </div>
                                        </div>
                                        <button id="btnsubmit" type="submit" class="btn waves-effect waves-light">Submit
                                        </button>
                                        <button type="button" class="btn waves-effect waves-light"
                                                onclick="location.href='user_setup.php?view'">Cancel
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <?php
            } // add close

            if (isset($_GET['view'])) {

                ?>
                <?php if (isset($_GET['sucess'])) { ?>
                    <div id='card-alert' class='card green lighten-5'>
                        <div class='card-content green-text'>
                            <p>Successfully updated!</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>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="table-datatables">
                    <table id="data-table-simple" class="responsive-table display">
                        <thead>
                        <tr>
                            <th>Action</th>
                            <th>Username</th>
                            <th>Email</th>
                            <th>Contact</th>
                            <th>Type</th>
                            <th>Role</th>
                            <th>Status</th>
                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th>Action</th>
                            <th>Username</th>
                            <th>Email</th>
                            <th>Contact</th>
                            <th>Type</th>
                            <th>Role</th>
                            <th>Status</th>
                        </tr>
                        </tfoot>
                        <tbody>
                        <?php
                        if ($_SESSION['sess_username'] != 'admin') {
                            $filter = "WHERE us.username <> 'admin' and us.type <> 'administrator' ";
                        }
                        $qry = User_setup::GetUserSetupList();
                        if ($qry) {
                            foreach ($qry as $qery) { ?>
                                <tr>
                                    <td>
                                        <?php
                                        if ($_SESSION['sess_role'] == 'VEC' || $_SESSION['sess_role'] == 'VE') {
                                            ?>
                                            <a href='?edit&id=<?php echo $qery->us_id; ?>&v' title="View">
                                                    <span
                                                        style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                                        class="mini-stat-icon pink"><i
                                                            class="mdi-action-description"></i></span>
                                            </a>
                                            <a href="?edit&id=<?php echo $qery->us_id; ?>" title="Edit">
                                                    <span
                                                        style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                                        class="mini-stat-icon orange"><i
                                                            class="mdi-image-edit"></i></span>
                                            </a>
                                        <?php } else { ?>
                                            <a href="?edit&id=<?php echo $qery->us_id; ?>&v" title="View">
                                                    <span
                                                        style="width: 20px; height: 20px; font-size: 12px; line-height: 20px;"
                                                        class="mini-stat-icon pink"><i
                                                            class="mdi-action-description"></i></span>
                                            </a>
                                        <?php } ?>
                                    </td>
                                    <td><?php echo $qery->username; ?></td>
                                    <td><?php echo $qery->email; ?></td>
                                    <td><?php echo $qery->contactno; ?></td>
                                    <td><?php echo ucwords($qery->type); ?></td>
                                    <td><?php if ($qery->role == 'V') {
                                            echo "View";
                                        } elseif ($qery->role == 'VE') {
                                            echo "View Edit";
                                        } else {
                                            echo "View Edit Create";
                                        } ?></td>
                                    <td><?= ($qery->status == 'active') ? ucwords($qery->status) : "Inactive"; ?></td>
                                </tr>
                                <?php
                            }
                        } ?>
                        </tbody>
                    </table>
                </div>
                <?php

            } // close view


            if (isset($_GET['edit']) && !empty($_POST)) {
                extract($_REQUEST);
                $id = $_GET['id'];
                $data['username'] = $name1 = trim($name);
                $data['email'] = $email;
                $data['contactno'] = $contactno;
                $data['password'] = $password2 = trim($password);
                $data['type'] = $type;
                $data['role'] = $role;
                $data['status'] = $status;

                $usercheck = User_setup::CheckUserSetupByUsername($name, $id);
                $errors = array();
                // Validate the input
                if ((strlen($name1) == 0) || (strlen($name1) > 20))
                    array_push($errors, "Please enter  name");
                $space = strrpos($name1, " ");
                if ($space > 0)
                    array_push($errors, "Spaces not allowed");

                if ((strlen($password2) == 0) || (strlen($password2) > 20))
                    array_push($errors, "Please enter  password");
                $spacep = strrpos($password2, " ");

                if ($spacep > 0)
                    array_push($errors, "Spaces not allowed ");

                if ((!filter_var($email, FILTER_VALIDATE_EMAIL)) || ((strlen($email) > 50)))
                    array_push($errors, "Invalid email format");
                if ((strlen($type) == 0))
                    array_push($errors, "Please select type");
                if ((strlen($role) == 0))
                    array_push($errors, "Please select role");

                if ($usercheck)
                    array_push($errors, "username already exist");

                if (count($errors) == 0) {
                    $q = User_setup::UpdateUserSetup($id, $data);
                    if ($q) {
                        $menuArray = $_POST['menu'];
                        $final_menu = implode(",", $menuArray);

                        $dataMenu['menu_id'] = $final_menu;
                        $dataMenu['us_id'] = $id;

                        $Del_MenuSetup = Menu::DeleteMenuSetup($id);

                        $MenuAdd = Menu::AddMenuInUser($dataMenu);

                        $output = '';
                        //header("Location: ?view&sucess"); ?>
                        <script>window.location.href = '?view&sucess'</script>
                        <?php
                        echo "<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>";
                    } else {
                        $output = '';
                        echo "<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>";
                    }
                } else {
                    //Prepare errors for output
                    foreach ($errors as $val) {
                        $output .= "<div id='card-alert' class='card orange lighten-5'>
                      <div class='card-content orange-text'>
                        <p>WARNING : <strong>Attention! </strong>$val</p>
                      </div>
                      <button type='button' class='close orange-text' data-dismiss='alert' aria-label='Close'>
                        <span aria-hidden='true'>x</span>
                      </button>
                    </div>";
                    }
                }
            }
            if (isset($_GET['edit'])) {
                $id = $_GET['id'];
                if (empty($_POST)) {
                    $qry = User_setup::GetUserSetupId($id);
                    $name = $qry->username;
                    $email = $qry->email;
                    $contactno = $qry->contactno;
                    $password = $qry->password;
                    $type = $qry->type;
                    $role = $qry->role;
                    $status = $qry->status;
                }
                if ($_REQUEST['id'] && isset($_REQUEST['v'])) {
                    ?>
                    <style>
                        h4 {
                            float: left;
                            margin: 0;
                            width: 30%;
                        }

                        p {
                            float: left;
                        }

                        .activity-icon {
                            margin: 0;
                        }
                    </style>
                    <div class="row">
                        <!--  The form that will be parsed by jQuery before submit  -->
                        <div class="col s12 form-group">
                            <div class="col s8">
                                <div class="row">
                                    <div>User Name:</div>
                                    <span><?php echo $name; ?></span>
                                </div>
                                <div class="row">
                                    <div>Password:</div>
                                    <span><?php echo $password; ?></span>
                                </div>
                                <div class="row">
                                    <div>Email:</div>
                                    <span><?php echo $email; ?></span>
                                </div>
                                <div class="row">
                                    <div>Contact No:</div>
                                    <span><?php echo $contactno; ?></span>
                                </div>
                                <div class="row">
                                    <div>Type:</div>
                            <span>
                                <?php if ($type == __ADMIN__) {
                                    echo 'Administrator';
                                } else if ($type == __PROFILE__) {
                                    echo 'Profile';
                                }
                                ?>
                            </span>
                                </div>
                                <div class="row">
                                    <div id="admin"></div>
                                    <div>Role:</div>
                            <span>
                                <?php if ($role == 'V') {
                                    echo "View";
                                } else if ($role == 'VE') {
                                    echo "View & Edit";
                                } else if ($role == 'VEC') {
                                    echo "View, Edit & Create";
                                } ?>
                            </span>
                                </div>
                                <div class="row">
                                    <div>Status</div>
                            <span>
                                <?php
                                if ($status == 'active') {
                                    echo "Active";
                                } else if ($status == 'inactive') {
                                    echo "Inactive";
                                } ?>
                            </span>
                                </div>
                                <div class="row">
                                    <div id="Rmenu_rights"><h3>Menu Rights</h3>
                                        <hr>
                                        <div class="menu_right">
                                            <table class="table table-bordered">
                                                <thead>
                                                <tr>
                                                    <th>Levels</th>
                                                    <th>Option/Screen</th>
                                                    <th></th>
                                                </tr>
                                                </thead>
                                                <tbody>
                                                <?php
                                                $filter = " WHERE parent_id = '0' AND status <> '0' ORDER BY menu_order";
                                                $Parentmenu = Menu::GetMenuList($filter);
                                                if ($Parentmenu) {
                                                    foreach ($Parentmenu as $row_menu) {
                                                        $menuId = $row_menu->menu_id;
                                                        $selectedMainMenu = Menu::GetSelectedMenu($id);
                                                        $MenuMainSelected = $selectedMainMenu->menu_id;
                                                        ?>

                                                        <tr>
                                                        <td>1</td>
                                                        <td>
                                                            <?php echo $row_menu->name; ?>
                                                        </td>

                                                        <td>
                                                            <?php
                                                            $selectedMenu = Menu::GetSelectedMenu($id);
                                                            $MenuSelected = $selectedMenu->menu_id;
                                                            $MenuSelected;
                                                            $MenuSelected = explode(",", $MenuSelected);
                                                            $count = count($MenuSelected);
                                                            $check = 0;
                                                            for ($i = 0; $i < $count; $i++) { ?>
                                                                <?php if ($MenuSelected[$i] == $row_menu->menu_id) {
                                                                    //echo $row_menu->name;
                                                                    echo "<div class='activity-icon terques'>
                                                    <i class='mdi-action-done'></i>
                                                </div>";
                                                                    $check = 1;
                                                                }
                                                            }
                                                            if ($check == 0) {
                                                                echo "<div class='activity-icon red'>
                                                    <i class='mdi-content-clear'></i>
                                                </div>";
                                                                $check = 0;
                                                            } ?>
                                                        </td>
                                                        <?php
                                                        //$menuName = $row_menu->name;
                                                        $filters = " WHERE parent_id = '$menuId' AND status <> '0' ORDER BY menu_order";
                                                        $menu = Menu::GetMenuList($filters);
                                                        if ($menu) {
                                                            foreach ($menu as $row_menus) {
                                                                $sub_menuId = $row_menus->menu_id;
                                                                ?>
                                                                </tr>
                                                                <tr>
                                                                <td>2</td>
                                                                <td>
                                                                    <?php echo $row_menus->name; ?>
                                                                </td>
                                                                <td>
                                                                    <?php
                                                                    $selectedMenu = Menu::GetSelectedMenu($id);
                                                                    $MenuSelected = $selectedMenu->menu_id;
                                                                    $MenuSelected;
                                                                    $MenuSelected = explode(",", $MenuSelected);
                                                                    $count = count($MenuSelected);
                                                                    $check = 0;
                                                                    for ($i = 0; $i < $count; $i++) {
                                                                        if ($MenuSelected[$i] == $row_menus->menu_id) {
                                                                            //echo $row_menus->name;
                                                                            echo "<div class='activity-icon terques'>
                                                    <i class='mdi-action-done'></i>
                                                </div>";
                                                                            $check = 1;
                                                                        }
                                                                    }
                                                                    if ($check == 0) {
                                                                        echo "<div class='activity-icon red'>
                                                    <i class='mdi-content-clear'></i>
                                                </div>";
                                                                        $check = 0;
                                                                    }
                                                                    ?>
                                                                </td>
                                                                <?php
                                                                //$menuName = $row_menu->name;
                                                                $filters = " WHERE parent_id = '$sub_menuId' AND status <> '0' ORDER BY menu_order";
                                                                $menus = Menu::GetMenuList($filters);
                                                                if ($menus) {
                                                                    foreach ($menus as $row_menuss) {
                                                                        ?>
                                                                        </tr>
                                                                        <tr>
                                                                            <td>3</td>
                                                                            <td>
                                                                                <?php echo $row_menuss->name; ?>
                                                                            </td>
                                                                            <td>
                                                                                <?php
                                                                                $selectedMenu = Menu::GetSelectedMenu($id);
                                                                                $MenuSelected = $selectedMenu->menu_id;
                                                                                $MenuSelected;
                                                                                $MenuSelected = explode(",", $MenuSelected);
                                                                                $count = count($MenuSelected);
                                                                                $check = 0;
                                                                                for ($i = 0; $i < $count; $i++) {
                                                                                    if ($MenuSelected[$i] == $row_menuss->menu_id) {
                                                                                        //echo $row_menus->name;
                                                                                        echo "<div class='activity-icon terques'>
                                                    <i class='mdi-action-done'></i>
                                                </div>";
                                                                                        $check = 1;
                                                                                    }
                                                                                }
                                                                                if ($check == 0) {
                                                                                    echo "<div class='activity-icon red'>
                                                    <i class='mdi-content-clear'></i>
                                                </div>";
                                                                                    $check = 0;
                                                                                }
                                                                                ?>
                                                                            </td>
                                                                        </tr>
                                                                        <?php
                                                                    }
                                                                }

                                                            }
                                                        }
                                                    }
                                                }
                                                ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                <?php } else {
                    ?>
                    <div class="row">
                        <!--  The form that will be parsed by jQuery before submit  -->
                        <form action="?edit&id=<?php echo $id ?>" method="post" id="user_setup_form"
                              novalidate="novalidate">
                            <div class="col s12 form-group">
                                <div class="col s8">
                                    <div class="row">
                                        <?php echo $output; ?>
                                    </div>
                                    <div class="row">
                                        <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" id="name" maxlength="20"
                                                   name="name"
                                                   value="<?php echo $name; ?>" <?php if ($name == 'admin') {
                                                echo "readonly";
                                            } ?> />
                                            <label for="name" class="control-label">User Name *</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="password" id="password" maxlength="20"
                                                   name="password"
                                                   value="<?php echo $password; ?>"/>
                                            <label for="password" class="control-label">Password</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="email" maxlength="50"
                                                   name="email"
                                                   value="<?php echo $email; ?>"/>
                                            <label for="email" class="control-label">Email</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <input class="form-control" type="text" id="contactno" maxlength="11"
                                                   pattern="^[0]{1}[3]{1}\d{9}" placeholder="03xxxxxxxx"
                                                   name="contactno"
                                                   value="<?php echo $contactno; ?>"/>
                                            <label for="contactno" class="control-label">Contact No</label>
                                        </div>
                                    </div>
                                    <div class="row margin">
                                        <div class="input-field col s12">
                                            <select class="form-control" id="getFname" name="type">
                                                <option></option>

                                                <?php if ($_SESSION['sess_username'] == 'admin') { ?>
                                                    <option
                                                        value="<?= __ADMIN__ ?>" <?php if ($type == __ADMIN__) {
                                                        echo 'selected';
                                                    } ?>>Administrator
                                                    </option>
                                                <?php } ?>
                                                <option value="<?= __PROFILE__ ?>" <?php if ($type == __PROFILE__) {
                                                    echo 'selected';
                                                } ?> >Profile
                                                </option>
                                            </select>
                                            <label for="getFname" class="control-label">Type </label>
                                        </div>
                                        <div class="row margin">
                                            <div id="admin"></div>
                                            <div id="role" class="input-field col s12">
                                                <select name="role" class="form-control">
                                                    <option></option>
                                                    <option value="V" <?php if ($role == 'V') {
                                                        echo "selected";
                                                    } ?>>View
                                                    </option>
                                                    <option value="VE" <?php if ($role == 'VE') {
                                                        echo "selected";
                                                    } ?>>View & Edit
                                                    </option>
                                                    <option value="VEC" <?php if ($role == 'VEC') {
                                                        echo "selected";
                                                    } ?>>View, Edit & Create
                                                    </option>
                                                </select>
                                                <label for="role" class="control-label">Role </label>
                                            </div>
                                        </div>
                                        <div class="row margin">
                                            <div id="status" class="input-field col s12">
                                                <select class="form-control" name="status" id="status">
                                                    <option value="active" <?php if ($status == 'active') {
                                                        echo "selected='selected'";
                                                    } ?>>Active
                                                    </option>
                                                    <option value="inactive" <?php if ($status == 'inactive') {
                                                        echo "selected='selected'";
                                                    } ?>>Inactive
                                                    </option>
                                                </select>
                                                <label for="status" class="control-label">Status</label><br>
                                            </div>
                                            <br/>
                                        </div>
                                        <div class="row margin">
                                            <div class="input-field col s12">
                                                <div id="Rmenu_rights"><h3>Menu Rights</h3>
                                                    <hr>
                                                    <div class="menu_right"
                                                         style=" height: 200px; width: 100%; overflow-x: hidden;   float: left;  margin-bottom: 25px;">
                                                        <table class="table table-striped">
                                                            <thead>
                                                            <tr>
                                                                <th>Levels</th>
                                                                <th>Option/Screen</th>
                                                                <th></th>
                                                            </tr>
                                                            </thead>
                                                            <tbody>
                                                            <?php
                                                            $filter = " WHERE parent_id = '0' AND status <> '0' ORDER BY menu_order";
                                                            $Parentmenu = Menu::GetMenuList($filter);
                                                            if ($Parentmenu) {
                                                                foreach ($Parentmenu as $row_menu) {
                                                                    $menuId = $row_menu->menu_id;
                                                                    $selectedMainMenu = Menu::GetSelectedMenu($id);
                                                                    $MenuMainSelected = $selectedMainMenu->menu_id;
                                                                    ?>

                                                                    <tr>
                                                                    <td>1</td>
                                                                    <td>
                                                                        <div class="menu_checkbox"><span
                                                                                for="<?php echo $row_menu->name; ?>"><?php echo $row_menu->name; ?></span>
                                                                    </td>

                                                                    <td><input type="checkbox"
                                                                               value='<?php echo $row_menu->menu_id; ?>'
                                                                               name="menu[]"
                                                                               id="<?php echo $row_menu->name; ?>" <?php
                                                                        $selectedMenu = Menu::GetSelectedMenu($id);
                                                                        $MenuSelected = $selectedMenu->menu_id;
                                                                        $MenuSelected;
                                                                        $MenuSelected = explode(",", $MenuSelected);
                                                                        $count = count($MenuSelected);
                                                                        ?>
                                                                            <?php
                                                                            for ($i = 0; $i < $count; $i++) { ?>


                                                                                <?php if ($MenuSelected[$i] == $row_menu->menu_id) {
                                                                                    echo 'checked';
                                                                                } ?>

                                                                            <?php } ?> />
                                                                        <label
                                                                            for="<?php echo $row_menu->name; ?>"></label>
                                                                    </td>

                                                                    <?php
                                                                    //$menuName = $row_menu->name;
                                                                    $filters = " WHERE parent_id = '$menuId' AND status <> '0' ORDER BY menu_order";
                                                                    $menu = Menu::GetMenuList($filters);
                                                                    if ($menu) {
                                                                        foreach ($menu as $row_menus) {
                                                                            $sub_menuId = $row_menus->menu_id;
                                                                            ?>

                                                                            </tr>
                                                                            <tr>
                                                                            <td>2</td>
                                                                            <td>
                                                                                <div class="menu_checkbox"
                                                                                     style="margin-left: 25px;">
                                                                                    <span
                                                                                        for="<?php echo $row_menus->name; ?>"><?php echo $row_menus->name; ?></span>
                                                                            </td>
                                                                            <td>
                                                                                <input type="checkbox"
                                                                                       value='<?php echo $row_menus->menu_id; ?>'
                                                                                       name="menu[]"
                                                                                       id="<?php echo $row_menus->name; ?>"<?php
                                                                                $selectedMenu = Menu::GetSelectedMenu($id);
                                                                                $MenuSelected = $selectedMenu->menu_id;
                                                                                $MenuSelected;
                                                                                $MenuSelected = explode(",", $MenuSelected);
                                                                                $count = count($MenuSelected);
                                                                                for ($i = 0; $i < $count; $i++) {
                                                                                    if ($MenuSelected[$i] == $row_menus->menu_id) {
                                                                                        echo 'checked';
                                                                                    }
                                                                                }

                                                                                ?> />
                                                                                <label
                                                                                    for="<?php echo $row_menus->name; ?>"></label>
                                                                            </td>
                                                                            <?php
                                                                            //$menuName = $row_menu->name;
                                                                            $filters = " WHERE parent_id = '$sub_menuId' AND status <> '0' ORDER BY menu_order";
                                                                            $menus = Menu::GetMenuList($filters);
                                                                            if ($menus) {
                                                                                foreach ($menus as $row_menuss) {
                                                                                    ?>

                                                                                    </tr>
                                                                                    <tr>
                                                                                        <td>3</td>
                                                                                        <td>
                                                                                            <div class="menu_checkbox"
                                                                                                 style="margin-left: 50px;">
                                                                                                <span
                                                                                                    for="<?php echo $row_menuss->name; ?>"><?php echo $row_menuss->name; ?></span>
                                                                                        </td>
                                                                                        <td>
                                                                                            <input type="checkbox"
                                                                                                   value='<?php echo $row_menuss->menu_id; ?>'
                                                                                                   name="menu[]"
                                                                                                   id="<?php echo $row_menuss->name; ?>"<?php
                                                                                            $selectedMenu = Menu::GetSelectedMenu($id);
                                                                                            $MenuSelected = $selectedMenu->menu_id;
                                                                                            $MenuSelected;
                                                                                            $MenuSelected = explode(",", $MenuSelected);
                                                                                            $count = count($MenuSelected);
                                                                                            for ($i = 0; $i < $count; $i++) {
                                                                                                if ($MenuSelected[$i] == $row_menuss->menu_id) {
                                                                                                    echo 'checked';
                                                                                                }
                                                                                            }

                                                                                            ?> />
                                                                                            <label
                                                                                                for="<?php echo $row_menuss->name; ?>"></label>
                                                                                        </td>

                                                                                    </tr>
                                                                                    <?php
                                                                                }
                                                                            }

                                                                        }
                                                                    }

                                                                }
                                                            }
                                                            ?>
                                                            </tbody>
                                                        </table>
                                                    </div>
                                                </div>
                                                <div class="clearfix"></div>
                                                <?php if ($_SESSION['sess_role'] == 'VE' || $_SESSION['sess_role'] == 'VEC') { ?>
                                                    <button id="btnsubmit" type="submit"
                                                            class="btn waves-effect waves-light">
                                                        Submit
                                                    </button>
                                                <?php } ?>
                                                <button type="button" class="btn waves-effect waves-light"
                                                        onclick="location.href='user_setup.php?view'">
                                                    Cancel
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                <?php } ?>
                <?php
            }
            ?>

        </div>
    </div>
</div>
</div>
</div>
<?php include('includes/inc_footer.php'); ?>

<?php include('includes/inc_js.php'); ?>
<script type="application/javascript">
    $(document).ready(function () {
        var fewSeconds = 2;
        $('#btnsubmit').click(function () {
            // Ajax request
            var btn = $(this);
            btn.hide();
            setTimeout(function () {
                btn.show();
            }, fewSeconds * 1000);
        });
    });
</script>
</body>
</html>