<%@ 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"/>--%>
    <select id="keywordId">
        <option value="1">昨日</option>
        <option value="2" selected>今日</option>
        <option value="3">7天</option>
        <option value="4">30天</option>
    </select>
    <input type="text" class="input-sm" placeholder="开始日期"  id="startDate"/>
    <input type="text" class="input-sm" placeholder="结束日期"  id="endDate"/>&nbsp;
    <button id="searchd" type="button" class="btn" >筛选</button>&nbsp;&nbsp;&nbsp;
    <button id="exportExcel" type="button" onclick="exportExcel()" class="btn">导出数据</button>
<%--    <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进行刷新 带参数刷新    */

            var date = new Date();

            $("#searchd").click(function(){
                var keywordId = $("#keywordId").val();
                var startTime= $('#startDate').val();
                var endTime= $('#endDate').val();
                if((startTime==null|| startTime=='')&& (endTime==null|| endTime=='')){
                    var dateInfo=getDateInfo(keywordId);
                    startTime=dateInfo.startTime;
                    endTime=dateInfo.endTime;
                }else{
                    startTime+=' 00:00:00';
                    endTime+=' 23:59:59';
                }
                myGrid.setGridParam({postData:{'startTime':startTime,'endTime':endTime},
                    url : '${contextPath}/ads/lawsuit/getAdsCaseReleaseCount',page:1}).trigger('reloadGrid');
            });
            $("#keywordId").change(function () {
                $('#startDate').val('');
                $('#endDate').val('');
            })

            $(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}/ads/lawsuit/getAdsCaseReleaseCount",
                datatype : "json",
                postData:getDateInfo(2),
                mtype:'POST',//设置http方式为post以解决乱码问题c
                colNames : ['案件来源','提交数','发布数','发布率','固定收费数', '固定收费占比','半风险数',
                    '半风险占比','平均标的额','平均固定律师费','前期平均律师费','后期平均风险收费比例'],
                colModel : [{
                    name : 'name',
                    index : 'name',
                    width : 120,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'commitNum',
                    index : 'commitNum',
                    width : 80,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'releaseCount',
                    index : 'releaseCount',
                    width : 80,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'releaseRate',
                    index : 'releaseRate',
                    width : 80,
                    search : false,
                    editable : false,
                    formatter : percentageFormatter
                },{
                    name : 'fixedCost',
                    index : 'fixedCost',
                    width : 80,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'fixedCostRatio',
                    index : 'fixedCostRatio',
                    width : 100,
                    search : false,
                    editable : false,
                    formatter : percentageFormatter
                },{
                    name : 'halfRisk',
                    index : 'halfRisk',
                    width : 80,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'halfRiskRatio',
                    index : 'halfRiskRatio',
                    width : 100,
                    search : false,
                    editable : false,
                    formatter : percentageFormatter
                },{
                    name : 'avgQuota',
                    index : 'avgQuota',
                    width : 200,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'avgFixedCost',
                    index : 'avgFixedCost',
                    width : 200,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'earlyAvgCost',
                    index : 'earlyAvgCost',
                    width : 200,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'laterAvgCost',
                    index : 'laterAvgCost',
                    width : 200,
                    search : false,
                    editable : false,
                    formatter : percentageFormatter
                }],
                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 percentageFormatter(cellvalue, options, cell) {
                return cellvalue+"%";
            }

            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 = {"name":true,"commitNum":true,"releaseCount":true,'releaseRate':true,"fixedCost":true,
            "fixedCostRatio":true,"halfRisk":true,"halfRiskRatio":true,"avgQuota":true,"avgFixedCost":true,
            "earlyAvgCost":true,"laterAvgCost":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}/ads/lawsuit/getAdsCaseReleaseCountExcel?fileName=案件发布统计' 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  getDateInfo(keywordId) {
        var date=new Date();
        var date2=new Date();
        var result={"startTime":'',"endTime":''};
        if(keywordId==1){//昨天
            date.setDate(date.getDate() - 1);
            date2.setDate(date2.getDate() - 1);
        }else if(keywordId==2){//今天
            date.setDate(date.getDate());
            date2.setDate(date2.getDate());
        }else if(keywordId==3){
            date.setDate(date.getDate()-7);
            date2.setDate(date2.getDate());
        }else if(keywordId==4){
            date.setDate(date.getDate()-30);
            date2.setDate(date2.getDate());
        }
        var s =  date.getFullYear()+"-" + (date.getMonth()+1) + "-" + date.getDate();
        var s1 = date2.getFullYear()+"-" + (date2.getMonth()+1) + "-" + date2.getDate();
        result.startTime=s+" 00:00:00";
        result.endTime=s1+" 23:59:59";
        return result;
    }

</script>