<%@ 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>
    筛选日期：<input type="text" class="input-sm" placeholder="开始日期"  id="startDate"/>
    <input type="text" class="input-sm" placeholder="结束日期"  id="endDate"/>
    关键字:<input id="keywords" placeholder="输入关键字" class="input-medium search-query" type="text" />
    &nbsp;审核状态：<select id="IsReason">
    <option selected value="">全部</option>
    <option value="3">审核中</option>
    <option value="1">未通过</option>
    <option value="2">已通过</option>
</select>
    &nbsp;代理方式：<select id="agentType">
    <option selected value="">全部</option>
    <option value="0">固定风险</option>
    <option value="1">半风险</option>
    <option value="2">全风险</option>
</select>
    &nbsp;<button id="searchd" type="button" class="btn" >搜索</button>
    <button type="button" class="btn btn-success btn-sm" onclick='exportOrder()'>
        导出excel表
    </button>
    <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-shenHe-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;">
                    <input hidden = hidden id="shenHe-adsBiddId">
                    <input hidden = hidden id="shenHe-adsLawsuitId">
                    <input hidden = hidden id="shenHe-agent">
                    <label class="control-label col-sm-5">审核结果：</label>
                    <div class="col-sm-8 input-group">
                        <select class="col-sm-7 input-group" id="shenHe-isReason" onchange="updateHidden()">
                            <option value="3" selected>请选择</option>
                            <option value="1">未通过</option>
                            <option value="2">已通过</option>
                        </select>
                    </div>
                    <div class="col-sm-8 input-group"  id="shenHe-hidden">

                    </div>
                </div>
            </div>
            <div class="modal-footer no-margin-top">
                <div class="text-center">
                    <button id="mod-shenHe-success" type="button" class="btn btn-sm btn-primary"
                            onclick="toShenHe();">
                        确定
                    </button>
                    <button class="btn btn-sm" data-dismiss="modal">
                        取消
                    </button>
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>

<div id="modal-pic-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-pic-tip" class="red clearfix"></div>
                <div class="form-group" style="margin-left: 30px;">
                    <div id="pic"></div>
                </div>
            </div>
            <div class="modal-footer no-margin-top">
                <div class="text-center">
                    <button class="btn btn-sm" data-dismiss="modal">
                        取消
                    </button>
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>


<div id="modal-refund-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 class="form-group"  >
                    <label class="col-sm-3 control-label">用户名:</label>
                    <div class="col-sm-8 input-group">
                        <span id="edit-name" class="form-control"></span>
                    </div>
                </div>
                <div class="form-group"  >
                    <label class="col-sm-3 control-label">性别:</label>
                    <div class="col-sm-8 input-group">
                        <span  id="edit-sex"  class="form-control"></span>
                    </div>
                </div>
                <div class="form-group"  >
                    <label class="col-sm-3 control-label">手机号码:</label>
                    <div class="col-sm-8 input-group">
                        <sapn id="edit-phone" class="form-control"></sapn>
                    </div>
                </div>
                <div class="form-group"  >
                    <label class="col-sm-3 control-label">案件地区:</label>
                    <div class="col-sm-3 input-group">
                        <span id="edit-aera" class="form-control"></span>
                    </div>
                </div>
                <div class="form-group"  >
                    <label class="col-sm-3 control-label">案件类型:</label>
                    <div class="col-sm-3 input-group">
                        <span id='edit-caseType' class="form-control"></span>
                    </div>
                </div>
                <div class="form-group"  >
                    <label class="col-sm-3 control-label">是否涉及财产:</label>
                    <div class="col-sm-8 input-group">
                        <span id="edit-property" class="form-control"></span>
                    </div>
                </div>
                <div class="form-group"  >
                    <label class="col-sm-3 control-label">标的额:</label>
                    <div class="col-sm-8 input-group">
                        <span id="edit-targetAmount" class="form-control"></span>
                    </div>
                </div>
                <div class="form-group"  >
                    <label class="col-sm-3 control-label">代理方式:</label>
                    <div class="col-sm-8 input-group">
                        <span id="edit-agentType" class="form-control"></span>
                    </div>
                </div>
                <div class="form-group"  >
                    <label class="col-sm-3 control-label">案件描述:</label>
                    <div class="col-sm-8 input-group">
                        <span id="edit-txt" ></span>
                    </div>
                </div>
                <div class="form-group"  >
                    <label class="col-sm-3 control-label">案件来源:</label>
                    <div class="col-sm-8 input-group">
                        <span id="edit-orderSource" class="form-control"></span>
                    </div>
                </div>
            </div>
            <div class="modal-footer no-margin-top">
                <div class="text-center">
                    <div class="modal-footer no-margin-top">
                        <div class="text-center" id="edit-button">
                            <button class="btn btn-sm" data-dismiss="modal">
                                取消
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>

<div id="modal-fee-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-fee-tip" class="red clearfix"></div>
                <input id="fee-id" hidden = hidden>
                <div class="form-group"  >
                    <label class="col-sm-3 control-label">律师费:</label>
                    <div class="col-sm-8 input-group">
                        <input id="fee-fee" class="form-control">
                    </div>
                </div>
            </div>
            <div class="modal-footer no-margin-top">
                <div class="text-center">
                    <button class="btn btn-sm" onclick="updateFee()" >
                        确定
                    </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 keyword = $("#keywords").val();
                var IsReason = $("#IsReason").val();
                var agentType = $("#agentType").val();
                var startDate = $("#startDate").val();
                var endDate = $("#endDate").val();
                myGrid.setGridParam({postData:{keyword:keyword,IsReason:IsReason,agentType:agentType,
                        startDate:startDate,endDate:endDate,type:1,page:1},
                    url : '${contextPath}/ads/lawsuit/getAdsBiddEntrust',page:1}).trigger('reloadGrid');
            });

            $('#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"
            });

            $('#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}/ads/lawsuit/getAdsBiddEntrust?type=1",
                datatype : "json",
                mtype:'POST',//设置http方式为post以解决乱码问题
                colNames : ['adsBiddId','案件订单号','提交日期','审核状态','律师','律师电话', '当事人','当事人电话',
                    '代理方式','律师费','平台佣金','当事人跟单人员','律师跟单人员','操作'],
                colModel : [{
                    name : 'adsBiddId',
                    index : 'adsBiddId',
                    width : 150,
                    search : false,
                    editable : false,
                    hidden : true
                    //sorttype : 'date'
                },{
                    name : 'adsLawsuitId',
                    index : 'adsLawsuitId',
                    width : 120,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'creatDate',
                    index : 'creatDate',
                    width : 120,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'isReason',
                    index : 'isReason',
                    width : 120,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'lawyerName',
                    index : 'lawyerName',
                    width : 120,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'lawyerPhone',
                    index : 'lawyerPhone',
                    width : 120,
                    search : false,
                    editable : false,
                    //sorttype : 'date'
                },{
                    name : 'userName',
                    index : 'userName',
                    width : 120,
                    search : false,
                    editable : false,
                    // sorttype : 'date'
                },{
                    name : 'userPhone',
                    index : 'userPhone',
                    width : 120,
                    search : false,
                    editable : false,
                    sorttype : 'date'
                },{
                    name : 'agentType',
                    index : 'agentType',
                    width : 120,
                    search : false,
                    editable : false,
                    sorttype : 'date'
                },{
                    name : 'cost',
                    index : 'cost',
                    width : 100,
                    search : false,
                    editable : false,
                    sorttype : 'date'
                },{
                    name : 'referralFee',
                    index : 'referralFee',
                    width : 100,
                    search : false,
                    editable : false,
                    sorttype : 'date'
                },{
                    name : 'XCXService',
                    index : 'XCXService',
                    width : 100,
                    search : false,
                    editable : false
                },{
                    name : 'APPService',
                    index : 'APPService',
                    width : 100,
                    search : false,
                    editable : false
                },{
                    name : '',
                    index : '',
                    width : 250,
                    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='showImg(\""+cell.adsBiddId+"\")' class='btn btn-minier btn-yellow'>查看协议</button>";
                if (cell.isReason === "审核中"){
                    template += "&nbsp;&nbsp;<button data-toggle='modal'  onclick='shenHe(\""+cell.adsBiddId+"\",\""+cell.adsLawsuitId+"\",\""+cell.agentType+"\")' class='btn btn-minier btn-info'>审  核</button>";
                }
                template += "&nbsp;&nbsp;<button data-toggle='modal'  onclick='showAdsLaw(\""+cell.adsLawsuitId+"\")' class='btn btn-minier btn-default'>案件详情</button>";
                if (cell.agentType === "固定收费"){
                    template += "&nbsp;&nbsp;<button data-toggle='modal'  onclick='preUpdateFee(\""+cell.adsLawsuitId+"\")' class='btn btn-minier btn-danger'>更改律师费</button>";
                }
                return template;
            }


        });
    });

    function showImg(adsBiddId) {
        $.ajax({
            dataType : "json",
            url : "${contextPath}/ads/lawsuit/getPic",
            type : "post",
            data : {
                'adsBiddId' : adsBiddId
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                var all = "";
                for (var i = 0 ; i < ret.length;i++){
                    all += "<a style='padding: 8px;' href='"+ret[i].url+"' target='newPic'>" +
                        "<img src='"+ret[i].url+"' width='180px' height='320px'/>" +
                        "<a/>"
                }
                $("#pic").html(all);
                console.log(ret);
            }
        });
        $("#modal-pic-body").modal("toggle");
    }
    function shenHe(adsBiddId,adsLawsuitId,agentType) {
        $("#shenHe-isReason").val("3");
        $("#shenHe-reason").val("");
        $("#shenHe-adsBiddId").val(adsBiddId);
        $("#shenHe-adsLawsuitId").val(adsLawsuitId);
        $("#shenHe-agent").val(agentType);
        updateHidden();
        $("#modal-shenHe-body").modal("toggle");
    }
    function toShenHe() {
        var adsBiddId = $("#shenHe-adsBiddId").val();
        var adsLawsuitId = $("#shenHe-adsLawsuitId").val();
        var isReason = $("#shenHe-isReason").val();
        var reason = $("#shenHe-reason").val();
        var agentType = $("#shenHe-agent").val();
        $.ajax({
            dataType : "json",
            url : "${contextPath}/ads/lawsuit/shenHe",
            type : "post",
            data : {
                'adsBiddId' : adsBiddId,
                'isReason' : isReason,
                'reason' : reason,
                'type' : "1",
                'agentType' : agentType,
                'adsLawsuitId' : adsLawsuitId,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                $("#modal-shenHe-body").modal("toggle");
                jQuery("#grid-table").trigger("reloadGrid");
            }
        });
    }

    function exportOrder(){
        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="案件订单号\t提交日期\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 = 1; j < keys.length; j++){
                if (j !== 13) {
                    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=协议审核Excel' 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 updateHidden() {
        var a = $("#shenHe-isReason").val();
        if (a === "1"){
            $("#shenHe-hidden").html('<label class="control-label col-sm-5">不通过原因：</label>\n' +
                '                        <div class="col-sm-8 input-group">\n' +
                '                            <textarea class="control-label col-sm-12" style="height:100px"  id="shenHe-reason"></textarea>\n' +
                '                        </div>');
        }else{
            $("#shenHe-hidden").html('');
        }
    }

    function showAdsLaw(adsLawsuitId) {
        $("#edit-name").html("");
        $("#edit-sex").html("");
        $("#edit-phone").html("");
        $("#edit-aera").html("");
        $("#edit-caseType").html("");
        $("#edit-property").html("");
        $("#edit-targetAmount").html("");
        $("#edit-agentType").html("");
        $("#edit-txt").html("");
        $("#edit-orderSource").html("");
        $.ajax({
            dataType : "json",
            url : "${contextPath}/ads/vip/getLawsuitById",
            type : "post",
            data : {
                'id' : adsLawsuitId,
                'type' : "1",
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                $("#edit-name").html(ret.name);
                var se = "";
                if (ret.sex === "0"){
                    se = "男";
                }else{
                    se = "女";
                }
                $("#edit-sex").html(se);
                $("#edit-phone").html(ret.phone);
                $("#edit-aera").html(ret.province + " " + ret.city);
                $("#edit-caseType").html(ret.caseType);
                $("#edit-property").html(ret.property);
                $("#edit-targetAmount").html(ret.targetAmount);
                var ag = "";
                if (ret.agentType === "0"){
                    ag = "固定收费";
                }else if (ret.agentType === "1"){
                    ag = "全风险代理";
                }else{
                    ag = "半风险代理";
                }
                $("#edit-agentType").html(ag);
                $("#edit-txt").html(ret.txt);
                $("#edit-orderSource").html(ret.orderSource);
                $("#modal-refund-body").modal("toggle");
            }
        });
    }

    function preUpdateFee(adsLawsuitId) {
        $.ajax({
            dataType : "json",
            url : "${contextPath}/ads/vip/getLawsuitById",
            type : "post",
            data : {
                'id' : adsLawsuitId,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                $("#fee-id").val(adsLawsuitId);
                $("#fee-fee").val(ret.cost);
                $("#modal-fee-body").modal("toggle");
            }
        });
    }

    function updateFee() {
        var adsLawsuitId = $("#fee-id").val();
        var cost = $("#fee-fee").val();
        $.ajax({
            dataType : "json",
            url : "${contextPath}/ads/lawsuit/updateCost",
            type : "post",
            data : {
                'id' : adsLawsuitId,
                'cost' : cost,
            },
            complete : function(ret) {
                var ret = eval("(" + ret.responseText + ")");
                console.log(ret);
                alert(ret.message);
                $("#modal-fee-body").modal("toggle");
                jQuery("#grid-table").trigger("reloadGrid");
            }
        });
    }


</script>