<?php $data = $this->data; ?>

<?php $pager = \Config\Services::pager(); ?>

<?= view("header.php",$data); ?>
    <?= view("userManage/aside.php",$data); ?>
    <div class="span9" id="content" style="width:100%;">
        <div style="width:100%;min-height:700px;">
            <div style="margin:0px 10px;margin-top:10px;padding:0px 10px;">
                <button class="btn btn-primary" href="#addUserWindow" data-toggle="modal">添加用户</button>
            </div>
            <div class="row-fluid" style="width:auto;margin:0px 10px;">
                <!-- block -->
                <div class="block">
                    <div class="block-content collapse in">
                        <div class="span12">
                            <table class="table table-striped">
                                <thead>
                                <tr>
                                    <th>#</th>
                                    <th>昵称</th>
                                    <th>用户名</th>
                                    <th>角色</th>
                                    <th>部门</th>
                                    <th>编辑</th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php for($i = 0;$i < count($data["users"]);$i++){ ?>
                                <tr user_id="<?= $data["users"][$i]["id"] ?>">
                                    <td><?= $i +1 ?></td>
                                    <td><?= $data["users"][$i]["nick"] ?></td>
                                    <td><?= $data["users"][$i]["username"] ?></td>
                                    <td role_id='<?= $data["users"][$i]["role"] ?>'><?php if($data["users"][$i]["role"] == 0){echo "超级管理员";}
                                    else if($data["users"][$i]["role"] == 1){echo "管理员";}
                                    else if($data["users"][$i]["role"] == 2){echo "普通用户";} ?></td>
                                    <td org_section_id='<?php if($data["users"][$i]["org_section_id"] != ""){echo $data["users"][$i]["org_section_id"];}
                                    else{ echo "0";} ?>'>
                                    <?= $data["users"][$i]["org_section_name"] ?></td>
                                    <td>
                                        <button class="btn btn-primary btn-mini" href="#editUserWindow" data-toggle="modal" onclick="showEditUserWindow(this)">编辑</button>
                                        <button class="btn btn-danger btn-mini" onclick="delUser(this)">删除</button>
                                    </td>
                                </tr>
                                <?php } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="pagination" style="text-align:right;padding-right:30px;">
                        <div style="display:inline;">
                            <select id="per_page" size="1" style="width:100px;margin:0px;float:right;height:30px;margin-left:5px;" onchange="perPageChange()">
                                <option value="10" <?php if($data["pageInfo"]["perPage"] == 15){ echo 'selected="selected"';}?>>10 条/页</option>
                                <option value="20"  <?php if($data["pageInfo"]["perPage"] == 20){ echo 'selected="selected"';}?>>20 条/页</option>
                                <option value="50" <?php if($data["pageInfo"]["perPage"] == 50){ echo 'selected="selected"';}?>>50 条/页</option>
                                <option value="100" <?php if($data["pageInfo"]["perPage"] == 100){ echo 'selected="selected"';}?>>100 条/页</option>
                            </select>
                        </div>
                        <?= $pager->makeLinks($data["pageInfo"]["page"], $data["pageInfo"]["perPage"],$data["pageInfo"]["total"]) ?>
                    </div>
                </div>
                <!-- /block -->           
            </div>
        </div>
    </div>

    <!-- 添加用户弹窗 -->
    <div id="addUserWindow" class="modal hide" style="width:500px;transform: translateX(-50%);margin:0px;left:50%;">
        <div class="modal-header">
            <button data-dismiss="modal" class="close" type="button">&times;</button>
            <h3>添加用户</h3>
        </div>
        <div class="modal-body">
            <table>
                <tr>
                    <td><label>用户名：</label></td>
                    <td><input class="input-xlarge focused" id="username_add" type="text" value="" style="display:inline;margin: 0px;width:300px;"></td>
                </tr>
                <tr>
                    <td><label>昵称：</label></td>
                    <td><input class="input-xlarge focused" id="nick_add" type="text" value="" style="display:inline;margin: 0px;width:300px;"></td>
                </tr>
                <tr>
                    <td><label>密码：</label></td>
                    <td><input class="input-xlarge focused" id="password_add" type="text" value="" style="display:inline;margin: 0px;width:300px;"></td>
                </tr>
                <tr>
                    <td><label>角色：</label></td>
                    <td>
                        <select id="role_add" style="display:inline;margin: 0px;width:314px;height:32px;">
                            <option value='0'>超级管理员</option>
                            <option value='1'>管理员</option>
                            <option value='2' selected="selected">普通用户</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td><label>部门：</label></td>
                    <td>
                        <select id="org_section_id_add" style="display:inline;margin: 0px;width:314px;height:32px;">
                            <option value="0">（空）</option>
                            <?php for($i = 0;$i < count($data["orgSections"]);$i++){ ?>
                            <option value='<?= $data["orgSections"][$i]["id"] ?>'><?= $data["orgSections"][$i]["org_section_name"] ?></option>
                            <?php } ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><button data-dismiss="modal" class="btn btn-primary" style="width:100%;" onclick="addUser()">添 加</button></td>
                </tr>
            </table>
        </div>
    </div>

    <!-- 编辑用户弹窗 -->
    <div id="editUserWindow" class="modal hide" style="width:500px;transform: translateX(-50%);margin:0px;left:50%;">
        <div class="modal-header">
            <button data-dismiss="modal" class="close" type="button">&times;</button>
            <h3>编辑用户</h3>
        </div>
        <div class="modal-body">
            <table>
                <tr>
                    <td><label>用户Id：</label></td>
                    <td><input class="input-xlarge focused" disabled id="user_id_edit" type="text" value="" style="display:inline;margin: 0px;width:300px;"></td>
                </tr>
                <tr>
                    <td><label>用户名：</label></td>
                    <td><input class="input-xlarge focused" id="username_edit" type="text" value="" style="display:inline;margin: 0px;width:300px;"></td>
                </tr>
                <tr>
                    <td><label>昵称：</label></td>
                    <td><input class="input-xlarge focused" id="nick_edit" type="text" value="" style="display:inline;margin: 0px;width:300px;"></td>
                </tr>
                <tr>
                    <td><label>密码：</label></td>
                    <td><input class="input-xlarge focused" id="password_edit" type="text" value="" style="display:inline;margin: 0px;width:300px;"></td>
                </tr>
                <tr>
                    <td><label>角色：</label></td>
                    <td>
                        <select id="role_edit" style="display:inline;margin: 0px;width:314px;height:32px;">
                            <option value='0'>超级管理员</option>
                            <option value='1'>管理员</option>
                            <option value='2' selected="selected">普通用户</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td><label>部门：</label></td>
                    <td>
                        <select id="org_section_id_edit" style="display:inline;margin: 0px;width:314px;height:32px;">
                        
                        </select>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><button data-dismiss="modal" class="btn btn-primary" style="width:100%;" onclick="updateUserInfo()">提 交</button></td>
                </tr>
            </table>
        </div>
    </div>
    <?= view("footer.php",$data); ?>

    <style>
.modal-body table tr td label,input,select {
    margin: 8px 0px;
}

.pagination {
    margin: 0px 0px;
}
</style>


<script>
/*****************************************************
*          删除用户
*****************************************************/
function delUser(e){
    var userId = $(e).parent().parent().attr("user_id");
    var host = window.location.host;
    var url = "http://" + host + "/userManageCtr/delUser"
    var postData = {};
    postData["userId"] = userId;
    var isSure = confirm("确定删除该用户吗?");
    if(isSure == true){
        $.ajax({
            url: url,
            data: JSON.stringify(postData),
            type: "DELETE",
            dataType: "json",
            contentType:"application/json",
            success: function(data) {
                if(data.status == "200"){
                    location.reload();
                }else{
                    
                }
            }
        });
    }
}

/*****************************************************
*          添加用户
*****************************************************/
function addUser(){
    var username = $("#username_add").val();
    var nick = $("#nick_add").val();
    var password = $("#password_add").val();
    var role = $("#role_add").val();
    var org_section_id = $("#org_section_id_add").val();
    password = md5(password);

    var host = window.location.host;
    var url = "http://" + host + "/userManageCtr/addUser"
    var postData = {};
    postData["username"] = username;
    postData["nick"] = nick;
    postData["password"] = password;
    postData["role"] = role;
    postData["org_section_id"] = org_section_id;

    $.ajax({
        url: url,
        data: JSON.stringify(postData),
        type: "DELETE",
        dataType: "json",
        contentType:"application/json",
        success: function(data) {
            if(data.status == "200"){
                location.reload();
            }else{
                
            }
        }
    });
}

/*****************************************************
*          展示编辑用户弹窗
*****************************************************/
function showEditUserWindow(e){
    var userId = $(e).parent().parent().attr("user_id");
    var nick = $($(e).parent().prevAll()[3]).text();
    var username = $($(e).parent().prevAll()[2]).text();
    var role_id = $($(e).parent().prevAll()[1]).attr("role_id");
    var org_section_id = $($(e).parent().prevAll()[0]).attr("org_section_id");
    $("#user_id_edit").val(userId);
    $("#username_edit").val(username);
    $("#nick_edit").val(nick);
    var role_content = "";
    if(role_id == "0"){
        role_content = role_content + "<option value='0' selected='selected'>超级管理员</option><option value='1'>管理员</option><option value='2'>普通用户</option>"
    }else if(role_id == "1"){
        role_content = role_content + "<option value='0'>超级管理员</option><option value='1' selected='selected'>管理员</option><option value='2'>普通用户</option>"
    }else if(role_id =="2"){
        role_content = role_content + "<option value='0'>超级管理员</option><option value='1'>管理员</option><option value='2' selected='selected'>普通用户</option>"
    }
    $("#role_edit").empty();
    $("#role_edit").append(role_content);

    var host = window.location.host;
    var url = "http://" + host + "/orgManageCtr/getAllOrgSections"
    postData = {};
    $.ajax({
        url: url,
        data: JSON.stringify(postData),
        type: "DELETE",
        dataType: "json",
        contentType:"application/json",
        success: function(data) {
            if(data.status == "200"){
                var items = data.data;
                var content = '';
                $("#org_section_id_edit").empty();
                var items = data.data;
                if(org_section_id == "0"){
                    content = '<option value="0">（空）</option>';
                }
                for(var i = 0 ;i < items.length ;i++){
                    content = content + '<option value="' + items[i]["id"];
                    if(items[i]["id"] == org_section_id){
                        content = content + '" selected="selected ';
                    }
                    content = content + '">' + items[i]["org_section_name"] + '</option>';
                }
                $("#org_section_id_edit").append(content);
            }
        }
    });
}

/*****************************************************
*          更新用户信息
*****************************************************/
function updateUserInfo(){
    var userId = $("#user_id_edit").val();
    var username = $("#username_edit").val();
    var nick = $("#nick_edit").val();
    var password = $("#password_edit").val();
    var role = $("#role_edit").val();
    var org_section_id = $("#org_section_id_edit").val();
    password = md5(password);

    var host = window.location.host;
    var url = "http://" + host + "/userManageCtr/editUser"
    var postData = {};
    postData["userId"] = userId;
    postData["username"] = username;
    postData["nick"] = nick;
    postData["password"] = password;
    postData["role"] = role;
    postData["org_section_id"] = org_section_id;

    $.ajax({
        url: url,
        data: JSON.stringify(postData),
        type: "DELETE",
        dataType: "json",
        contentType:"application/json",
        success: function(data) {
            if(data.status == "200"){
                location.reload();
            }else{
                
            }
        }
    });
}

/*****************************************************
*          改变页码后，刷新页面
*****************************************************/
function perPageChange(){
    var perPage = $("#per_page").val();
    var host = window.location.host;
    var oriParam = window.location.search;
    var paramStr = oriParam.substring(1);
    paramArr = paramStr.split("&");
    param = {};                            
    for(var i = 0;i < paramArr.length;i++){
        keyVal = paramArr[i].split("=");
        param[keyVal[0]] = keyVal[1];
    }
    var url = window.location.href.split("?")[0];
    delete param["perPage"];
    for(var key in param){
        if(url.indexOf("?") !== -1){
            url = url + "&" + key + "=" + param[key];
        }else{
            url = url + "?" +  key + "=" + param[key];
        }
    }
    if(url.indexOf("&") !== -1){
            url = url + "&perPage=" + perPage;
        }else{
            url = url + "?perPage=" + perPage;
        }
    window.location.href = url;
}
</script>



