<script>
    var list = '';
</script>

<div>
    <?php
    $status = $this->session->userdata('status');
    $role_id = $this->session->userdata('role_id');
    ?>
    <div class="col-sm-10 col-sm-offset-3 main col-md-10 col-md-offset-2 main">

        <div class="col-sm-6 col-md-6 ">
            <a href="<?php echo base_url(); ?>super_admin/create/<?php echo $role_id ?>" class="btn btn-sm btn-success">Add <?php
                if ($user_role == 'All') {
                    echo '';
                } else {
                    echo $user_role;
                }
                ?> User
            </a>
            <input type="submit" value="Change Role" id="change_role_button" class="btn btn-sm btn-success"  data-toggle="modal" data-target="#changeRole" >
            <input type="submit" value="Change Status" id="change_status_button" class="btn btn-sm btn-success"  data-toggle="modal" data-target="#changeStatus" >
            <?php
            $short = $this->session->userdata('short');
            if ($short === 'ASC') {
                ?>
                <a href="<?php echo base_url(); ?>super_admin/shorted_users_list/<?php echo $user_role . '/' . $this->session->userdata('role_id') . '/' . 'DESC'; ?>" class="btn btn-sm btn-success" >Short user by name</a>
            <?php } else { ?>
                <a href="<?php echo base_url(); ?>super_admin/shorted_users_list/<?php echo $user_role . '/' . $this->session->userdata('role_id') . '/' . 'ASC'; ?>" class="btn btn-sm btn-success" >Short user by name</a>
            <?php } ?>

            <!--</div>-->
        </div>
        
        <div class="col-sm-6 col-md-6">
            <form name="control_form">
                <div class="col-sm-6">
                    <select class="form-control" for="user_role_id2" name="user_role_id2" id="user_role_id2" onchange="selectUser(this.value, user_status.value, 'change');">
                        <option value='0'> All</option>
                        <option value='1'> Super Admin </option>
                        <option value='2'> Account Officer</option>
                        <option value='4'> Organization</option>
                        <option value='3'> Resource Person</option>
                        <option value='5'> Student</option>
                    </select>
                </div>
                <div class="col-sm-6">
                    <select class="form-control" for="user_status" name="user_status" id="user_status" onchange="selectUser(user_role_id2.value, this.value, 'change');">
                        <!--<option value=''> Select Status</option>-->
                        <option value='all'> All</option>
                        <option value='0'> Inactive</option>
                        <option value='1'> Active</option>
                        <option value='2'> Archive</option>
                    </select>
                </div>
            </form>
        </div>

        
        <!-- Select User By Role and Status -->

        <script>


            function selectUser(roleID, userStatus, objID) {
                if (roleID === '') {
                    serverPage = '<?php echo base_url(); ?>super_admin/select_user_list/' + '' + '/' + userStatus + '/' + 0;
                } else {
                    serverPage = '<?php echo base_url(); ?>super_admin/select_user_list/' + roleID + '/' + userStatus + '/' + 0;
                }

                xmlhttp.open("GET", serverPage);
                xmlhttp.onreadystatechange = function ()
                {
//                    alert(xmlhttp.readyState);
//                    alert(xmlhttp.status);
                    if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
                    {
                        document.getElementById(objID).innerHTML = xmlhttp.responseText;
//                        location.reload();
                    }
                };
                xmlhttp.send(null);
            }
        </script>

        <div class="container col-sm-8 col-sm-offset-2 col-md-offset-0 col-md-12" id="change">
            <hr>
            <div class="">
                <a href="<?php echo base_url(); ?>super_admin">Dashboard</a>->>
                <span><?php echo $user_role; ?> User</span>
            </div>
            <hr>
            <div class="span11 right">
                <div class="col-lg-6 col-md-4" >
                    <h4><?php echo $user_role; ?> User Information</h4> 
                </div>
                <div class="col-lg-2 col-md-2">
                </div>
                <div class="col-lg-4 col-md-4">
                    <a class="btn btn-sm btn-primary" href="<?php echo base_url(); ?>super_admin/export_user_list_csv">Export</a> 
                    <a class="btn btn-sm btn-primary" href="#" data-toggle="modal" data-target="#importFile">Import</a> 
                    <input type="submit" value="Print" id="change_role_button" class="btn btn-sm btn-primary  "  data-toggle="modal" data-target="" onclick="printPreview('user_list');"><!--#print-->
                    <a class="btn btn-sm btn-primary" href="#" onclick="sendMailToUser();">Email</a>
                    <a class="btn btn-sm btn-primary" href="#" onclick="sendMailToUser();">SMS</a>
                </div>
            </div>

            <!--User LIST-->

            <div class="table-responsive col-lg-12 col-md-10 col-sm-7" id="user_list">
                <form name="myform" action=""  method="post">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th><input type="checkbox" name="all_list" id="all_list" onchange="toggleAllUserSelection(document.myform.person_id)"/></th>
                                <!--<th>User ID</th>-->
                                <th>Profile Picture</th>
                                <th>User Name</th>
                                <th>User Email</th>
                                <th>Contact Number</th>
                                <th>Role</th>
                                <th>Status</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                        <script>

                            function toggleAllUserSelection(field) {
                                allList = document.getElementById("all_list").checked;
                                if (allList) {
                                    for (i = 0; i < field.length; i++) {
                                        field[i].checked = true;
                                        list += field[i].value + '.';
                                    }
//                                    document.getElementById('change_role_button').disabled = false;
//                                    document.getElementById('change_status_button').disabled = false;
                                } else {
                                    for (i = 0; i < field.length; i++)
                                        field[i].checked = false;
                                    list = '';
//                                    document.getElementById('change_role_button').disabled = true;
//                                    document.getElementById('change_status_button').disabled = true;
                                }
                            }

                            function setUserIdList(userID)
                            {
                                if ($("#person_id" + userID).is(":checked")) {
                                    list += userID + '.';
                                } else {
                                    list = list.replace(userID + ".", "");
                                }
                                if (list) {
//                                    document.getElementById('change_role_button').disabled = false;
//                                    document.getElementById('change_status_button').disabled = false;
                                } else {
//                                    document.getElementById('change_role_button').disabled = true;
//                                    document.getElementById('change_status_button').disabled = true;
                                }
//                                alert(userID);
                            }

                            function setInstantUserIdList(userID) {
                                list += userID + '.';
                            }
                        </script>
                        <?php
                        foreach ($users as $v_user) {
                            ?>
                            <tr>
                                <td>
                                    <input name="person_id" id="person_id<?php echo $v_user->person_id; ?>" type="checkbox"  value="<?php echo $v_user->person_id; ?>" onchange="setUserIdList(this.value);"/>
                                </td>

                                                        <!--<td><?php // echo $v_user->person_id       ?></td>-->
                                <td><img src="<?php echo base_url() . $v_user->person_image_thumb; ?>" width="50" height="50"> </td>
                                <td><?php echo $v_user->person_first_name . ' ' . $v_user->person_last_name; ?></td>
                                <td><?php echo $v_user->person_email ?></td>
                                <td><?php echo $v_user->person_contact_number ?></td>
                                <td>
                                    <?php
                                    if ($v_user->person_role == '1') {
                                        echo 'Admin';
                                    }
                                    if ($v_user->person_role == '2') {
                                        echo 'Account';
                                    }
                                    if ($v_user->person_role == '3') {
                                        echo 'Resource';
                                    }
                                    if ($v_user->person_role == '4') {
                                        echo 'Organization';
                                    }
                                    if ($v_user->person_role == '5') {
                                        echo 'Student';
                                    }
                                    ?><br>
                                    <?php if ($v_user->person_role != 5) { ?><a href="#" class="" data-toggle="modal" data-target="#changeRole" onclick="setInstantUserIdList(<?php echo $v_user->person_id; ?>);">Change Role</a><?php } ?>
                                </td>

                                <!--
                                    Action: Active, Inactive, Archive, Edit
                                -->

                                <td><?php
                                    if ($v_user->person_status == '1') {
                                        echo '<p style="color:limegreen;">Active</p>';
                                        ?>
                                        <a href="<?php echo base_url(); ?>super_admin/make_user_inactive/<?php echo $v_user->person_id; ?>"><i class="fa fa-user-times" title="Inactive"></i></a>
                                        <?php
                                    } else if ($v_user->person_status == '0') {
                                        echo '<p style="color:red;">Inactive</p>';
                                        ?>
                                        <a href="<?php echo base_url(); ?>super_admin/make_user_active/<?php echo $v_user->person_id; ?>">
                                            <i class="fa fa-user-plus" title="Active"></i></a>
                                        <?php
                                    } else if ($v_user->person_status == '2') {
                                        echo '<p style="color:blue;">Archived';
                                        ?>
                                        <a href="<?php echo base_url(); ?>super_admin/make_user_active/<?php echo $v_user->person_id; ?>"><i class="fa fa-user-plus" title="Active"></i> </a>
                                    <?php } ?>
                                </td>

                            <script>
                                function changeUsersRole(userRoleId)
                                {
                                    //                userRoleId = document.getElementById("person_role_modal_input").value;
                                    serverPage = '<?php echo base_url(); ?>super_admin/change_users_role/' + list + '/' + userRoleId;
                                    xmlhttp.open("GET", serverPage);
                                    xmlhttp.onreadystatechange = function ()
                                    {
                                        if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
                                        {
                                            document.getElementById('res').innerHTML = xmlhttp.responseText;
                                            location.reload();
                                        }
                                    };
                                    xmlhttp.send(null);
                                }
                            </script>

                            <td style="width: 200px;">
                                <a href="<?php echo base_url(); ?>super_admin/edit_user_information/<?php echo $v_user->person_id; ?>"><div class="span1"><i class="fa fa-edit">Edit</i></div></a>
                                <a href="<?php echo base_url(); ?>super_admin/view_detail/<?php echo $this->uri->segment(3) . '/' . $v_user->person_id; ?>"><div class="span1"><i class="fa fa-edit">View detail</i></div></a>
                                <?php if ($v_user->person_status != 2) { ?><a href="<?php echo base_url(); ?>super_admin/delete_user/<?php echo $v_user->person_id; ?>" onclick="return checkAction('delete');"><i class="fa fa-remove">Delete</i></a><br><?php } ?>
                                <?php if ($v_user->person_role == 5) { ?><a href="<?php echo base_url() ?>super_admin_student/assign_course/<?php echo $v_user->person_id; ?>"><i class="fa fa-remove">Assign Course</i></a><br><?php } ?>
                            </td>
                            </tr>
                        <?php } ?>
                        </tbody>
                    </table>
                    <br>
                </form>

                

            </div>

        </div>  
    </div> 

    <!-- Modal Start For Change Role and Status-->
    <!--
    

    <?php include 'admin_modal_view/modal_change_role.php'; ?>
    <?php include 'admin_modal_view/modal_change_status.php'; ?>
    <?php include 'admin_modal_view/modal_for_print.php'; ?>
    
    <!--

Modal:: 
IMPORT FILE FORM

    -->



    <div class="modal fade" id="importFile" tabindex="-1" role="dialog" aria-labelledby="importFile" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">Add User</h4>
                </div>
                <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
                    <p>
                        <?php
                        $message = $this->session->userdata('message');
                        $this->session->unset_userdata('message');
                        if ($message) {
                            echo $message;
                        }
                        ?>
                    </p>
                    <div class="table-responsive col-sm-12">
                        <?php // echo validation_errors(); ?>
                        <form action="<?php echo base_url(); ?>super_admin/import_user_csv_file" method="post" name="import_csv" enctype="multipart/form-data" >
                            <div class="form-group col-sm-8" >
                                <label for="user_info_file" style="float:left;">Select CSV File</label><span class="required"> * <?php // echo form_error('user_image');                                                              ?></span><br><br>
                                <input style="float:left;" type='file' onchange="readURL(this, 'file_0');" name="user_info_file" value="<?php // echo set_value('user_image');                                                              ?>"/>
                                <div id="file" style="float:left;width: 50px; height: 50px;" ></div>
                            </div>
                            <div class="checkbox col-sm-8" >
                                <button type="submit" class="btn btn-default">Submit</button>
                                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="modal-footer">
                    <!--<button type="button" class="btn btn-primary">Save changes</button>-->
                </div>
            </div>
        </div>
    </div>
</div>


<SCRIPT LANGUAGE="JavaScript">

    document.forms['control_form'].elements['user_role_id2'].value = '<?php echo $role_id; ?>';
    document.forms['control_form'].elements['user_status'].value = '<?php echo $status; ?>';

    function checkAction(action) {
        message = 'do you want to ' + action + '!!';
        chk = confirm(message);
        if (chk) {
            return true;
        } else {
            return false;
        }
    }



    function changeUsersStatus()
    {
        userStatus = document.getElementById("person_status").value;
        serverPage = '<?php echo base_url(); ?>super_admin/change_users_status/' + list + '/' + userStatus;
        xmlhttp.open("GET", serverPage);
        xmlhttp.onreadystatechange = function ()
        {
            if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
            {
                document.getElementById('res').innerHTML = xmlhttp.responseText;
                location.reload();
            }
        };
        xmlhttp.send(null);
    }

    function setPerPage(perPage) {

        serverPage = '<?php echo base_url(); ?>super_admin/set_per_page/' + perPage;
        xmlhttp.open("GET", serverPage);
        xmlhttp.onreadystatechange = function ()
        {
            if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
            {
                location.reload();
            }
        };
        xmlhttp.send(null);
    }

    function sendMailToUser() {
        if (userList === '') {
            userList = '';
            window.location = '<?php echo base_url(); ?>super_admin/send_mail_to_user/' + userList;
        } else {
            window.location = '<?php echo base_url(); ?>super_admin/send_mail_to_user/' + userList;
        }
    }


    function printPreview(divName) {
        serverPage = '<?php echo base_url(); ?>super_admin/make_print_data/' + list;
        xmlhttp.open("GET", serverPage);
        xmlhttp.onreadystatechange = function ()
        {
            if (xmlhttp.readyState === 4 && xmlhttp.status === 200)
            {
                window = document.getElementById('change').innerHTML = xmlhttp.responseText;
                window.print();
            }
        };
        xmlhttp.send(null);
    }


    function printDiv(divName) {
        window.print();
    }
</script>