<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"></c:set>

<link rel="stylesheet" href="${contextPath}/static/assets/css/jquery-ui.css" />
<link rel="stylesheet" href="${contextPath}/static/assets/css/datepicker.css" />
<link rel="stylesheet" href="${contextPath}/static/assets/css/ui.jqgrid.css" />
<br>
<div style="background: rgba(1,1,2,0.05)">
    <button class="btn btn-large btn-info" onclick="preAddGroup()">新增小组</button>
</div>
<div class="row">
    <div >
        <div class="col-xs-6" >
            <table id="grid-table"></table>
            <div id="grid-pager"></div>
            <script type="text/javascript">
                var $path_base = "${contextPath}/static";//in Ace demo this will be used for editurl parameter
            </script>
            <!-- PAGE CONTENT ENDS -->
        </div><!-- /.col -->
    </div><!-- /.row -->
</div>

<div id="modal-add-body" class="modal fade" tabindex="-1" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header no-padding">
                <div class="table-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        <span class="white">&times;</span>
                    </button>
                    添加小组
                </div>
            </div>
            <div class="modal-body" style="overflow-y: scroll;">
                <div id="mod-add-tip" class="red clearfix"></div>
                <div class="form-group" style="margin-left: 30px;">
                    <label for="add-groupName" class="control-label col-sm-3">小组名称：</label>
                    <div class="col-sm-8 input-group">
                        <input type="text" class="control-label col-sm-7" id="add-groupName">
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;">
                    <label for="add-groupLeader" class="control-label col-sm-3">组长：</label>
                    <div class="col-sm-8 input-group">
                        <input type="text" class="control-label col-sm-7" id="add-groupLeader">
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;">
                    <label for="add-groupMember" class="control-label col-sm-3">组员：</label>
                    <div class="col-sm-8 input-group">
                        <input type="text" class="control-label col-sm-7" id="add-groupMember">
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;">
                    <label for="add-gdUser" class="control-label col-sm-3">小组跟单人员：</label>
                    <div class="col-sm-8 input-group">
                        <input type="text" class="control-label col-sm-7" id="add-gdUser">
                    </div>
                </div>
            </div>
            <div>
                <p><span style="color: rgba(0,0,0,0.44);font-size: 12px">(P.S.组长或组员中多人用“、”隔开，如：张三、李四)</span></p>
            </div>
            <div class="modal-footer no-margin-top">
                <div class="text-center">
                    <button id="mod-add-success" type="button" class="btn btn-sm btn-primary"
                            onclick="addGroup();">
                        确定
                    </button>
                    <button class="btn btn-sm" data-dismiss="modal">
                        取消
                    </button>
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>

<div id="modal-update-body" class="modal fade" tabindex="-1" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header no-padding">
                <div class="table-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        <span class="white">&times;</span>
                    </button>
                    编辑小组
                </div>
            </div>
            <div class="modal-body" style="overflow-y: scroll;">
                <div id="mod-update-tip" class="red clearfix"></div>
                <input id="update-id" hidden = "hidden">
                <div class="form-group" style="margin-left: 30px;">
                    <label for="update-groupName" class="control-label col-sm-3">小组名称：</label>
                    <div class="col-sm-8 input-group">
                        <input type="text" class="control-label col-sm-7" id="update-groupName">
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;">
                    <label for="update-groupLeader" class="control-label col-sm-3">组长：</label>
                    <div class="col-sm-8 input-group">
                        <input type="text" class="control-label col-sm-7" id="update-groupLeader">
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;">
                    <label for="update-groupMember" class="control-label col-sm-3">组员：</label>
                    <div class="col-sm-8 input-group">
                        <input type="text" class="control-label col-sm-7" id="update-groupMember">
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;">
                    <label for="update-gdUser" class="control-label col-sm-3">小组跟单人员：</label>
                    <div class="col-sm-8 input-group">
                        <input type="text" class="control-label col-sm-7" id="update-gdUser">
                    </div>
                </div>
            </div>
            <div>
                <p><span style="color: rgba(0,0,0,0.44);font-size: 12px">(P.S.组长或组员中多人用“、”隔开，如：张三、李四)</span></p>
            </div>
            <div class="modal-footer no-margin-top">
                <div class="text-center">
                    <button id="mod-update-success" type="button" class="btn btn-sm btn-primary"
                            onclick="updateGroup();">
                        确定
                    </button>
                    <button class="btn btn-sm" data-dismiss="modal">
                        取消
                    </button>
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>

<script type="text/javascript" >

    var myGrid = null;
    var scripts = [ null,  "${contextPath}/static/assets/js/date-time/bootstrap-datepicker.js", "${contextPath}/static/assets/js/date-time/locales/bootstrap-datepicker.zh-CN.js", "${contextPath}/static/assets/js/jqGrid/jquery.jqGrid.js", "${contextPath}/static/assets/js/jqGrid/i18n/grid.locale-cn.js", null ]

    $('.page-content-area').ace_ajax('loadScripts', scripts, function() {
        jQuery(function ($) {

            var grid_selector = "#grid-table";
            var pager_selector = "#grid-pager";

            /*搜索框  主要对Jquery grid进行刷新 带参数刷新    */
            $("#searchd").click(function(){
                var keywords = $("#keywords").val();
                var platform = $("#platform").val();
                myGrid.setGridParam({postData:{keywords:keywords,platform:platform,page:1},
                    url : '${contextPath}/xmt/acc/getXmtAccount',page:1}).trigger('reloadGrid');
            });


            $(document).keydown(function (event) {
                var key = window.event ? event.keyCode : event.which;
                if (key == 13) {
                    $("#searchd").click();
                }
            });


            myGrid=	jQuery(grid_selector).jqGrid({
                subGrid : false,
                url : "${contextPath}/ssdb/manager/getGroup",
                datatype : "json",
                mtype:'POST',//设置http方式为post以解决乱码问题
                colNames : ['id','组名','组长','组员','跟单人员','操作'],
                colModel : [{
                    name : 'id',
                    index : 'id',
                    width : 150,
                    search : false,
                    editable : false,
                    hidden : true,
                    //sorttype : 'date'
                },{
                    name : 'groupName',
                    index : 'groupName',
                    width : 150,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'groupLeader',
                    index : 'groupLeader',
                    width : 200,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'groupMember',
                    index : 'groupMember',
                    width : 200,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'gdUser',
                    index : 'gdUser',
                    width : 200,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : '',
                    index : '',
                    width : 300,
                    editable : false,
                    search : false,
                    sortable : false,
                    fixed:true,
                    formatter : optionFormatter
                }],
                sortname : "groupName",
                sortorder : "asc",
                viewrecords : true,
                rowNum : 10,
                rowList : [ 10, 20, 30 ],
                pager : pager_selector,
                altRows : true,
                height:"auto",//自动根据每页条数弹性调整表格高度
                loadComplete : function() {
                    var table = this;
                    setTimeout(function(){
                        /*styleCheckbox(table);
                        updateActionIcons(table);*/
                        updatePagerIcons(table);
                        enableTooltips(table);
                    }, 0);
                }
            });

            function updatePagerIcons(table) {
                var replacement = {
                    'ui-icon-seek-first' : 'ace-icon fa fa-angle-double-left bigger-140',
                    'ui-icon-seek-prev' : 'ace-icon fa fa-angle-left bigger-140',
                    'ui-icon-seek-next' : 'ace-icon fa fa-angle-right bigger-140',
                    'ui-icon-seek-end' : 'ace-icon fa fa-angle-double-right bigger-140'
                };
                $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function() {
                    var icon = $(this);
                    var $class = $.trim(icon.attr('class').replace('ui-icon', ''));

                    if ($class in replacement)
                        icon.attr('class', 'ui-icon ' + replacement[$class]);
                })
            }

            function enableTooltips(table) {
                $('.navtable .ui-pg-button').tooltip({
                    container : 'body'
                });
                $(table).find('.ui-pg-div').tooltip({
                    container : 'body'
                });
            };
            function optionFormatter(cellvalue, options, cell) {
                var template = "<button data-toggle='modal'  onclick='showEdit(\""+cell.id+"\")' class='btn btn-minier btn-yellow'>修改</button>";
                template += "&nbsp;&nbsp;<button data-toggle='modal'  onclick='preDelete(\""+cell.id+"\")' class='btn btn-minier btn-info'>删除</button>";
                return template;
            }


        });
    });

    function showEdit(id) {
        $("#update-id").val("");
        $("#update-groupName").val("");
        $("#update-groupLeader").val("");
        $("#update-groupMember").val("");
        $("#update-gdUser").val("");
        $.ajax({
            dataType : "json",
            url : "${contextPath}/ssdb/manager/getGroupById",
            type : "post",
            data : {
                'id' : id,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                $("#update-id").val(ret.id);
                $("#update-groupName").val(ret.groupName);
                $("#update-groupLeader").val(ret.groupLeader);
                $("#update-groupMember").val(ret.groupMember);
                $("#update-gdUser").val(ret.gdUser);
                $("#modal-update-body").modal("toggle");
            }
        });
    }

    function updateGroup() {
        var id = $("#update-id").val();
        var groupName = $("#update-groupName").val();
        var groupLeader = $("#update-groupLeader").val();
        var groupMember = $("#update-groupMember").val();
        var gdUser = $("#update-gdUser").val();
        $.ajax({
            dataType : "json",
            url : "${contextPath}/ssdb/manager/updateGroup",
            type : "post",
            data : {
                'id' : id,
                'groupName' : groupName,
                'groupLeader' : groupLeader,
                'groupMember' : groupMember,
                'gdUser' : gdUser,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                $("#modal-update-body").modal("toggle");
                jQuery("#grid-table").trigger("reloadGrid");
            }
        });
    }

    function preDelete(id) {
        var result = confirm('是否删除？');
        if(result){
            deleteAccount(id)
        }
    }

    function deleteAccount(id) {
        $.ajax({
            dataType : "json",
            url : "${contextPath}/ssdb/manager/deleteGroup",
            type : "post",
            data : {
                'id' : id,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                jQuery("#grid-table").trigger("reloadGrid");
            }
        });
    }

    function preAddGroup() {
        $("#modal-add-body").modal("toggle");
    }

    function addGroup() {
        var groupName = $("#add-groupName").val();
        var gdUser = $("#add-gdUser").val();
        var groupLeader = $("#add-groupLeader").val();
        var groupMember = $("#add-groupMember").val();
        $.ajax({
            dataType : "json",
            url : "${contextPath}/ssdb/manager/addGroup",
            type : "post",
            data : {
                'groupName' : groupName,
                'gdUser' : gdUser,
                'groupLeader' : groupLeader,
                'groupMember' : groupMember,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                $("#modal-add-body").modal("toggle");
                jQuery("#grid-table").trigger("reloadGrid");
            }
        });
    }



</script>
