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

    开通时间筛选：<input type="text" class="input-sm" placeholder="开始日期"  id="startDate"/>
    <input type="text" class="input-sm" placeholder="结束日期"  id="endDate"/>
    &nbsp;&nbsp;&nbsp;
    关键字:<input id="keywords" placeholder="输入关键字" class="input-medium search-query" type="text" />

    <button id="searchd" type="button" class="btn" >搜索</button>&nbsp;&nbsp;
    <button onclick="exportExcel();" type="button" class="btn" >导出</button>
    <br>
    <br>
</div>

<div class="row">
    <div class="col-xs-12">
        <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 -->

<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 startDate = $("#startDate").val();
                var endDate = $("#endDate").val();
                var keywords = $("#keywords").val();
                myGrid.setGridParam({postData:{startDate:startDate ,endDate:endDate,
                        keywords:keywords,page:1},
                    url : '${contextPath}/fs/deposit/getLawyerSinOrder',page:1}).trigger('reloadGrid');
            });


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

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


            myGrid=	jQuery(grid_selector).jqGrid({
                subGrid : false,
                url : "${contextPath}/fs/deposit/getLawyerSinOrder",
                datatype : "json",
                mtype:'POST',//设置http方式为post以解决乱码问题c
                colNames : ['id','开通时间','过期时间','支付订单号', '支付金额','律师姓名','律师电话'],
                colModel : [{
                    name : 'id',
                    index : 'id',
                    width : 10,
                    search : false,
                    editable : false,
                    hidden : true
                    //sorttype : 'date'
                },{
                    name : 'createDate',
                    index : 'createDate',
                    width : 200,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'delayTime',
                    index : 'delayTime',
                    width : 200,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'tradeNo',
                    index : 'tradeNo',
                    width : 300,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'price',
                    index : 'price',
                    width : 200,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'lawyerName',
                    index : 'lawyerName',
                    width : 200,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'phone',
                    index : 'phone',
                    width : 200,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                }],
                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 optionFormatter(cellvalue, options, cell) {
                console.log(cell);
                var template = "&nbsp;<button data-toggle='modal' onclick='passOrCancelDeposit(\""+cell.lawyerId+"\",\""+1+"\")' class='btn btn-minier btn-yellow'><i class='ace-icon fa fa-hdd-o '></i>通过</button>";
                template += "&nbsp;&nbsp;&nbsp;<button data-toggle='modal' onclick='passOrCancelDeposit(\""+cell.lawyerId+"\",\""+0+"\")' class='btn btn-minier btn-info'><i class='ace-icon fa fa-hdd-o '></i>取消</button>";
                return template;
            }

            function enableTooltips(table) {
                $('.navtable .ui-pg-button').tooltip({
                    container : 'body'
                });
                $(table).find('.ui-pg-div').tooltip({
                    container : 'body'
                });
            };

        });
    });

    function exportExcel() {
        var keys = [], ii = 0, rows = "";
        var grid_selector = "#grid-table";
        var ids = $(grid_selector).getDataIDs(); // Get All IDs
        var row = $(grid_selector).getRowData(ids[0]); // Get First row to get the labels
        var label = $(grid_selector).jqGrid('getGridParam','colNames');
        for(var k in label){
            rows = rows + label[k] + "\t"; // output each Column as tab delimited
        }
        for (var k in row) {
            keys[ii++] = k; // capture col names
        }
        rows = rows + "\n"; // Output header with end of line
        var tIds = {"id":true,"createDate":true,"delayTime":true,"tradeNo":true,
            "price":true,"lawyerName":false,"phone":true };
        for (var i = 0; i < ids.length; i++) {
            row = $(grid_selector).getRowData(ids[i]); // get each row
            for (var j = 0; j < keys.length; j++){
                if(tIds[keys[j]]){
                    rows = rows + '="' + row[keys[j]] + '"\t'; // output each Row as tab delimited
                }else{
                    rows = rows + row[keys[j]] + '\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}/fs/report/getExcel?fileName=lawyerSinOrder' 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();
    }

</script>