<%@ page import="com.fadu.app.util.SystemCache" %>
<%@ 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>
    省：<select id="province">
    <option selected value="" >全部</option>
    <% for(String s: SystemCache.provinceMap.keySet()){ %>
    <option value="<%=s %>" ><%=SystemCache.getProvinceName(s) %></option>
    <%} %>
</select>
    关键字:<input id="keywords" placeholder="输入关键字" class="input-medium search-query" type="text" />
    &nbsp;<button id="searchd" type="button" class="btn" >搜索</button>
<%--    &nbsp;<button id="addAcc" type="button" onclick="preAddAccount()" class="btn" >添加合作律师</button>--%>
    &nbsp;<button id="exportExcel" type="button" onclick="exportExcel()" class="btn" >导出数据</button>
    &nbsp;<button id="setLawyer" type="button" onclick="setLawyer()" 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>
                <input style="display: none" class="control-label" type="text" id="subTypeId">
                <div class="form-group" style="margin-left: 30px;">
                    <label class="control-label">律师电话：</label>
                    <input class="control-label" type="text" id="add-phone">
                </div>
                <div class="form-group" style="margin-left: 30px;">
                    <label class="control-label">律师微信：</label>
                    <input class="control-label" type="text" id="add-weChat">
                </div>
                <%--<div class="form-group" style="margin-left: 30px;">
                    <label class="control-label">职业年限：</label>
                    <input class="control-label" type="text" id="add-expYear">
                </div>--%>
                <div class="form-group" style="margin-left: 30px;">
                    <label class="control-label">合作时间：</label>
                    <input type="text" class="input-sm" placeholder="选择日期"  id="add-coopDate"/>
                </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="coopId">
                <div class="form-group" style="margin-left: 30px;">
                    <label class="control-label">律师微信：</label>
                    <input class="control-label" type="text" id="edit-weChat">
                </div>
                <%--<div class="form-group" style="margin-left: 30px;">
                    <label class="control-label">职业年限：</label>
                    <input class="control-label" type="text" id="edit-expYear">
                </div>--%>
                <div class="form-group" style="margin-left: 30px;">
                    <label class="control-label">合作时间：</label>
                    <input type="text" class="input-sm" placeholder="选择日期"  id="edit-coopDate"/>
                </div>
                <div class="form-group" style="margin-left: 30px;">
                    <label class="control-label">总粉丝数：</label>
                    <input class="control-label" type="text" id="edit-fansCount">
                </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="editCoop();">
                        确定
                    </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 province = $("#province").val();
                myGrid.setGridParam({postData:{keywords:keywords,province:province,page:1},
                    url : '${contextPath}/xmt/coop/getCoopLawyer',page:1}).trigger('reloadGrid');
            });

            $('#add-coopDate').datepicker({
                language: 'zh-CN',
                pickTime: false,
                todayBtn: true,
                autoclose: true,
                minView: '2',
                forceParse: false,
                format:"yyyy-mm-dd"
            });
            $('#edit-coopDate').datepicker({
                language: 'zh-CN',
                pickTime: false,
                todayBtn: true,
                autoclose: true,
                minView: '2',
                forceParse: false,
                format:"yyyy-mm-dd"
            });


            $(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/coop/getCoopLawyer",
                datatype : "json",
                mtype:'POST',//设置http方式为post以解决乱码问题
                colNames : ['id','isRed','律师姓名','律师手机号','微信','职业年限', '省','市',
                    '合作开始时间','总粉丝数','总播放量','操作'],
                colModel : [{
                    name : 'id',
                    index : 'id',
                    width : 150,
                    search : false,
                    editable : false,
                    hidden : true
                    //sorttype : 'date'
                },{
                    name : 'isRed',
                    index : 'isRed',
                    width : 150,
                    search : false,
                    editable : false,
                    hidden : true
                    //sorttype : 'date'
                },{
                    name : 'lawyerName',
                    index : 'lawyerName',
                    width : 120,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'phone',
                    index : 'phone',
                    width : 120,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'weChat',
                    index : 'weChat',
                    width : 100,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'expYear',
                    index : 'expYear',
                    width : 100,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'province',
                    index : 'province',
                    width : 100,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'city',
                    index : 'city',
                    width : 100,
                    search : false,
                    editable : false,
                    // sorttype : 'date'
                },{
                    name : 'coopDate',
                    index : 'coopDate',
                    width : 100,
                    search : false,
                    editable : false,
                    sorttype : 'date'
                },{
                    name : 'fansCount',
                    index : 'fansCount',
                    width : 100,
                    search : false,
                    editable : false,
                    sorttype : 'date'
                },{
                    name : 'bfCount',
                    index : 'bfCount',
                    width : 100,
                    search : false,
                    editable : false,
                    sorttype : 'date'
                },{
                    name : '',
                    index : '',
                    width : 350,
                    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-danger'>删除</button>";
                /*template += "&nbsp;&nbsp;<button data-toggle='modal'  onclick='makeCoop(\""+cell.id+"\")' class='btn btn-minier btn-yellow'>绑定账号</button>";*/
                /*if (cell.isRed === "是"){
                    template += "&nbsp;&nbsp;<button data-toggle='modal'  onclick='makeRed(\""+cell.id+"\",\""+0+"\")' class='btn btn-minier btn-grey'>取消网红律师</button>";
                }else{
                    template += "&nbsp;&nbsp;<button data-toggle='modal'  onclick='makeRed(\""+cell.id+"\",\""+1+"\")' class='btn btn-minier btn-info'>开通网红律师</button>";
                }*/
                return template;
            }


        });
    });

    function preAddAccount() {
        $("#add-phone").val("");
        $("#add-weChat").val("");
        $("#add-expYear").val("");
        $("#add-coopDate").val("");
        $("#modal-add-body").modal("toggle");
    }

    function addAccount() {
        var phone = $("#add-phone").val();
        var weChat = $("#add-weChat").val();
        var coopDate =$("#add-coopDate").val();
        $.ajax({
            dataType : "json",
            url : "${contextPath}/xmt/coop/addAccount",
            type : "post",
            data : {
                'phone' : phone,
                'weChat' : weChat,
                'coopDate' : coopDate,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                if (ret.succ === "true"){
                    $("#modal-add-body").modal("toggle");
                    jQuery("#grid-table").trigger("reloadGrid");
                }
            }
        });
    }

    function showEdit(id) {
        $("#coopId").val(id);
        $("#modal-edit-body").modal("toggle");
        $.ajax({
            dataType : "json",
            url : "${contextPath}/xmt/coop/getCoopById",
            type : "post",
            data : {
                'id' : id,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                $("#edit-weChat").val(ret.weChat);
                // $("#edit-expYear").val(ret.expYear);
                $("#edit-coopDate").val(ret.coopDate);
                $("#edit-fansCount").val(ret.fansCount);
            }
        });
    }

    function editCoop() {
        var id = $("#coopId").val();
        var weChat = $("#edit-weChat").val();
        // var expYear = $("#edit-expYear").val();
        var coopDate =$("#edit-coopDate").val();
        var fansCount =$("#edit-fansCount").val();
        $.ajax({
            dataType : "json",
            url : "${contextPath}/xmt/coop/editCoop",
            type : "post",
            data : {
                'id' : id,
                'weChat' : weChat,
                'coopDate' : coopDate,
                'fansCount' : fansCount,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                if (ret.succ === "true"){
                    $("#modal-edit-body").modal("toggle");
                    jQuery("#grid-table").trigger("reloadGrid");
                }
            }
        });
    }

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

    function deleteCoop(id) {
        $.ajax({
            dataType : "json",
            url : "${contextPath}/xmt/coop/deleteCoop",
            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 makeRed(id,type) {
        $.ajax({
            dataType : "json",
            url : "${contextPath}/xmt/coop/makeRed",
            type : "post",
            data : {
                'id' : id,
                'type' : type,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                jQuery("#grid-table").trigger("reloadGrid");
            }
        });
    }

    function exportExcel() {
        var keys = [], ii = 0, rows = "";
        var grid_selector = "#grid-table";
        var ids = $(grid_selector).getDataIDs(); // Get All IDs
        console.log("哈哈");
        console.log(ids);
        var row = $(grid_selector).getRowData(ids[0]); // Get First row to get the labels
        console.log("呵呵");
        console.log(row);
        for (var k in row) {
            keys[ii++] = k; // capture col names
            rows = rows + k + "\t"; // output each Column as tab delimited
        }
        rows="合作律师Id\t是否是网红律师\t律师姓名\t律师电话\t微信\t职业年限\t省\t市\t合作开始时间\t总粉丝数\t总播放量";
        console.log(typeof(rows));
        rows = rows + "\n"; // Output header with end of line
        for (i = 0; i < ids.length; i++) {
            row = $(grid_selector).getRowData(ids[i]); // get each row
            //var rowData = $('#gridTable').jqGrid('getRowData',rowId);
            for (j = 0; j < keys.length-1; j++){
                rows = rows + row[keys[j]].replace(/^\s*|\s*$/g,"") + "\t"; // output each Row as tab delimited
            }
            rows = rows + "\n"; // output each row with end of line
        }
        rows = rows + "\n"; // end of line at the end
        var form = "<form name='csvexportform' action='${contextPath}/sys/ssdbOrder/operateOrder?oper=coopLawyer' method='post'>";
        form = form + "<input type='hidden' name='csvBuffer' value='" + encodeURIComponent(rows) + "'>";
        form = form + "</form><script>document.csvexportform.submit();</sc" + "ript>";
        OpenWindow = window.open('', '');
        OpenWindow.document.write(form);
        OpenWindow.document.close();
    }

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


</script>