<%@ 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" />

<div style="background: rgba(1,1,2,0.05)">
    <br>
    关键字:<input id="keywords" placeholder="输入关键字" class="input-medium search-query" type="text" />
    &nbsp;平 台:
    <select id='platform'>
        <option selected value="" >所有账号</option>
    </select>
    &nbsp;<button id="searchd" type="button" class="btn" >搜索</button>
    &nbsp;<button id="addAcc" type="button" onclick="ppreAdd(1)" class="btn" >添加账号</button>
    <br>
    <br>
</div>

<div class="row" >
    <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 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;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">账号：</label>
                    <div class="col-sm-8 input-group">
                        <input class="control-label col-sm-5" type="text" id="add-account">
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">账号名称：</label>
                    <div class="col-sm-8 input-group">
                        <input class="control-label col-sm-5" type="text" id="add-accountName">
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">平台：</label>
                    <div class="col-sm-8 input-group">
                        <select id="add-platfrom" class="col-sm-5">
                            <option>请选择</option>
                        </select>
                    </div>
                </div>
            </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="addAccount();">
                        确定
                    </button>
                    <button class="btn btn-sm" data-dismiss="modal">
                        取消
                    </button>
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>

<div id="modal-edit-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-edit-tip" class="red clearfix"></div>
                <input style="display: none" class="control-label" type="text" id="accountId">
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">账号：</label>
                    <div class="col-sm-8 input-group">
                        <input class="control-label col-sm-5" type="text" id="edit-account">
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">账号名称：</label>
                    <div class="col-sm-8 input-group">
                        <input class="control-label col-sm-5" type="text" id="edit-accountName">
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">平台：</label>
                    <div class="col-sm-8 input-group">
                        <select id="edit-platfrom" class="col-sm-5">
                            <option>请选择</option>
                        </select>
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">管理员：</label>
                    <div class="col-sm-8 input-group">
                        <select id="edit-sysUser" class="col-sm-5">
                            <option>请选择</option>
                        </select>
                    </div>
                </div>
                <div class="form-group" style="margin-left: 30px;margin-bottom: 20px;">
                    <label class="control-label col-sm-3">合作律师：</label>
                    <div class="col-sm-8 input-group">
                        <input class="control-label col-sm-5" type="text" id="edit-coop">
                    </div>
                </div>
            </div>
            <div class="modal-footer no-margin-top">
                <div class="text-center">
                    <button id="mod-edit-success" type="button" class="btn btn-sm btn-primary"
                            onclick="editAccount();">
                        确定
                    </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}/xmt/acc/getXmtAccount",
                datatype : "json",
                mtype:'POST',//设置http方式为post以解决乱码问题
                colNames : ['id','账号','账号名称','创建时间','平台', '管理员','合作律师','操作'],
                colModel : [{
                    name : 'id',
                    index : 'id',
                    width : 150,
                    search : false,
                    editable : false,
                    hidden : true,
                    //sorttype : 'date'
                },{
                    name : 'account',
                    index : 'account',
                    width : 150,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'accountName',
                    index : 'accountName',
                    width : 200,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'createDate',
                    index : 'createDate',
                    width : 200,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'platfromName',
                    index : 'platfromName',
                    width : 150,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'sysUserName',
                    index : 'sysUserName',
                    width : 150,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'lawyerName',
                    index : 'lawyerName',
                    width : 150,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : '',
                    index : '',
                    width : 300,
                    editable : false,
                    search : false,
                    sortable : false,
                    fixed:true,
                    formatter : optionFormatter
                }],
                sortname : "createDate",
                sortorder : "desc",
                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) {
                console.log(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 ppreAdd(type) {
        $("#add-accountName").val("");
        $("#add-account").val("");
        $("#add-platfrom").val("");
        $("#modal-add-body").modal("toggle");
        preAddAccount(type);
    }
    function preAddAccount(type) {
        var sec = document.getElementById('add-platfrom');
        var sss = document.getElementById('platform');
        var ccc = document.getElementById('edit-platfrom');
        sec.innerHTML = "<option value='' selected>请选择</option>";
        sss.innerHTML = "<option value='' selected>全部账号</option>";
        ccc.innerHTML = "<option value='' selected>请选择</option>";
        $.getJSON("${contextPath}/xmt/acc/getPlatform",function(result){
            $.each(result, function(i, v){
                console.log(v);
                if (type === 1){
                    sec.innerHTML += "<option value='" + v.xmtKey + "'>" + v.xmtVal + "</option>";
                }else if(type === 2){
                    sss.innerHTML += "<option value='" + v.xmtKey + "'>" + v.xmtVal + "</option>";
                }else{
                    ccc.innerHTML += "<option value='" + v.xmtKey + "'>" + v.xmtVal + "</option>";
                }
            });
        });
    }



    preAddAccount(2);

    function addAccount() {
        var accountName = $("#add-accountName").val();
        var account = $("#add-account").val();
        var platfromId = $("#add-platfrom").val();
        $.ajax({
            dataType : "json",
            url : "${contextPath}/xmt/acc/addAccount",
            type : "post",
            data : {
                'account' : account,
                'accountName' : accountName,
                'platfromId' : platfromId,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                $("#modal-add-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}/xmt/acc/deleteAccount",
            type : "post",
            data : {
                'id' : id,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                jQuery("#grid-table").trigger("reloadGrid");
                getAllSysuser();
            }
        });
    }
    function getAllSysuser() {
        var sec = document.getElementById('edit-sysUser');
        sec.innerHTML = "<option value='' selected>请选择</option>";
        $.getJSON("${contextPath}/xmt/acc/getAllSysuser",function(result){
            $.each(result, function(i, v){
                sec.innerHTML += "<option value='" + v[0] + "'>" + v[1] + "</option>";
            });
        });
    }

    preAddAccount(3);
    getAllSysuser();
    function showEdit(id) {
        preAddAccount(3);
       /* setTimeout(function () {*/
            $.ajax({
                dataType : "json",
                url : "${contextPath}/xmt/acc/getAccById",
                type : "post",
                data : {
                    'id' : id,
                },
                complete : function(ret) {
                    console.log(ret);
                    var ret = eval("(" + ret.responseText + ")");
                    // console.log(ret);
                    $("#edit-accountName").val(ret.accountName);
                    $("#edit-account").val(ret.account);
                    $("#accountId").val(ret.id);
                    $("#edit-platfrom").val(ret.platfromName);
                    $("#edit-sysUser").val(ret.sysUserName);
                    $("#edit-coop").val(ret.lawyerName);
                    $("#modal-edit-body").modal("toggle");
                    // jQuery("#grid-table").trigger("reloadGrid");
                }
            });
      /*  }, 50);*/
    }

    function editAccount() {
        var id = $("#accountId").val();
        var accountName = $("#edit-accountName").val();
        var account = $("#edit-account").val();
        var platfromId = $("#edit-platfrom").val();
        var userId = $("#edit-sysUser").val();
        var cooperateLawyer =$("#edit-coop").val();
        if (platfromId === null){
            alert("平台不能为空！！");
        }
        $.ajax({
            dataType : "json",
            url : "${contextPath}/xmt/acc/editAccount",
            type : "post",
            data : {
                'id' : id,
                'accountName' : accountName,
                'account' : account,
                'platfromId' : platfromId,
                'userId' : userId,
                'cooperateLawyer' : cooperateLawyer,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                $("#modal-edit-body").modal("toggle");
                jQuery("#grid-table").trigger("reloadGrid");
                getAllSysuser();
            }
        });
    }

    $(document).ready(function($) {
        $.fn.typeahead.Constructor.prototype.blur = function() {
            var that = this;
            setTimeout(function () { that.hide() }, 250);
        };
        $('#edit-coop').typeahead({
            source: function(query, process) {
                var parameter = {keyword: query};
                $.post('${contextPath}/xmt/acc/getCoopLawyer', parameter, function (data) {
                    var ret = eval("(" + data + ")");
                    process(ret);
                });
            }
        });
    });


</script>
