// 对Date的扩展，将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子： 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}


// 代金券消费
function queryOrderCheckList(urls, queryParams) {
    // 默认查询第一页 query查询时保存当前条件
    var TempPageNumber = 1, pageSize = 10;
    try {
        TempPageNumber = $('#datatable2').datagrid('getPager').pagination("options").pageNumber;
        pageSize = $('#datatable2').datagrid('getPager').pagination("options").pageSize;
    } catch (e) {

    }
    $('#datatable')
        .datagrid(
            {
                title: "对账列表",
                iconCls: 'icon-search',
                pageNumber: TempPageNumber,
                pageSize: pageSize,
                pageList: [10, 50],
                nowrap: true,
                striped: true,
                border: true,
                noheader: true,
                url: urls,
                loadMsg: '数据加载中......',
                queryParams: queryParams,
                remoteSort: false,
                fitColumns: true,
                singleSelect: false,
                columns: [[

                    {
                        field: 'ORDER_CODE',
                        title: '案件编号',
                        sortable: 'true',
                        align: 'left',
                        width: 40
                    }, {
                        field: 'VIN_NO',
                        title: 'VIN码',
                        sortable: 'true',
                        align: 'left',
                        width: 25
                    }, {
                        field: 'ADDRESS',
                        title: '案件事发地',
                        sortable: 'true',
                        align: 'left',
                        width: 60
                    },
                    {
                        field: 'SERVICE_NAME',
                        title: '服务类型',
                        sortable: 'true',
                        align: 'left',
                        width: 20
                    },
                    {
                        field: 'CREATE_TIME',
                        title: '开案时间',
                        sortable: 'true',
                        align: 'left',
                        width: 30,
                        formatter:  function (value, row, index) {
                            return new Date(value).Format("yyyy-MM-dd hh:mm")
                        }
                    },{
                        field: 'RECUESTATE',
                        title: '完成情况',
                        sortable: 'true',
                        align: 'left',
                        width: 20,
                        formatter: function (value, row, index) {
                            switch (value) {
                                case  '51':
                                    return "取消服务";
                                case  '53':
                                    return "救援失败";
                                case  '55':
                                    return "救援成功";
                            }
                        }
                    },

                    {
                        field: 'MONEY',
                        title: '救援服务费',
                        sortable: 'true',
                        align: 'center',
                        width: 15,
                        formatter: function (value, row, index) {
                            var ALL_FEE1 = parseInt(row.MONEY) + parseInt(row.MONEY);
                            return '800';
                        }
                    },
                    {
                        field: 'ALL_FEE1',
                        title: '经销商核对',
                        sortable: 'true',
                        align: 'center',
                        width: 15,
                        formatter: function (value, row, index) {
                            var ALL_FEE1 = parseInt(row.TOTAL_FEE1) + parseInt(row.OTHER_FEE1);
                            return ALL_FEE1 || '-';
                        }
                    },
                    {
                        field: 'ACCOUNTSTATE',
                        title: '结算状态',
                        sortable: 'true',
                        align: 'left',
                        width: 25,
                        formatter: function (value, row, index) {
                            switch (value) {
                                case  '2':
                                    return "未填写";
                                case  '3':
                                    return "等待审核";
                                case  '4':
                                    return "对账完成";
                                case  '6':
                                    return "开票完成";
                                default :
                                    return "对账中";
                            }
                        }
                    },
                    {
                        field: 'operation',
                        title: '操作',
                        sortable: 'true',
                        align: 'left',
                        width: 20,
                        formatter: function (value, row, index) {
                            var str = '<a href="#" name="opera" class="easyui-linkbutton opera" onclick=showDetail(' +index+ ')>核对</a>';
                            return str;
                        }
                    }
                ]],
                pagination: true,
                rownumbers: false,
                onLoadSuccess: function (data) {
                }

                ,
                toolbar: [],
                onCheck: function (rowIndex, rowData) {
                }

                ,
                onCheckAll: function (rows) {
                }
                ,
                onUncheck: function (rowIndex, rowData) {
                }
                ,
                onLoadSuccess: function (data) {
                }
                ,
                onUncheckAll: function (rows) {
                }

            }
        )
    ;
}

function showDetail(index) {
    var order = $('#datatable').datagrid('getRows')[index]
    $('#i-checkStateValue').val(order.CHECK_STATE);
    $('#i-checkID').val(order.CHECK_ID);
    $('#i-orderID').val(order.ORDER_ID);
    $('#uid').val(order.ID);
    if(order.ACCOUNTSTATE == 2 || order.ACCOUNTSTATE == 3) {
        $("#acceptBtn").show();
    }else{
        $("#acceptBtn").hide();
    }


    $("#checkInfoEdit_windowContent").show();
    console.log(order);
    $("#i-orderCode").html(order.ORDER_CODE);
    $("#i-orderState").html(function () {
        switch (order.RECUESTATE) {
            case  '51':
                return "取消服务";
            case  '53':
                return "救援失败";
            case  '55':
                return "救援成功";
        }
    });
    $("#i-checkState").html(function () {
        switch (order.ACCOUNTSTATE) {
            case  '2':
                return "等待填写";
            case  '3':
                return "等待审核";
            case  '4':
                return "对账完成";
            case  '6':
                return "开票完成";
            default :
                return "未填写";
        }
    });
    $("#i-serviceName").html(order.SERVICE_NAME);
    $("#i-address").html(order.ADDRESS);
    if (order.DESTINATION != null && order.DESTINATION != '') {
        $('.i-destAddressTR').show();
        $("#i-destAddress").html(order.DESTINATION);
    } else {
        $('.i-destAddressTR').hide();
    }
    $("#i-companyName").html(order.NAME);
    $("#i-frameCode").html(order.VIN_NO);
    $("#i-totalFee").html(800);
    $("#i-isSuccessServer").val("1");
    $("#i-isSuccessServer").change(function(){
        var select=$("#i-isSuccessServer").val();
        if(select == "0"){
            $("#i-totalFee1").html(parseInt(600));
        }
        if(select == "1"){
            $("#i-totalFee1").html(parseInt(800));
        }
        if(select == "2"){
            $("#i-totalFee1").html(parseInt(300));
        }
    });
    $("#i-totalFee").html(function () {
        if (order.FLOW_ID == "1") {
            $("#i-isSuccessServerTD").show();
            $(".i-refMileageTR").hide();
            $("#i-isSuccessServer").val(order.IS_SUCCESS_SERVER);
            if (order.MONEY >0) {
                $('#i-otherFee').val(order.MONEY);
                return (parseInt(order.MONEY) + parseInt(order.MONEY));
            } else {
                if (order.IS_SUCCESS_SERVER == 1) {
                    return 800;
                } else {
                    return 600;
                }
            }
        } else if (order.FLOW_ID == "2") {
            $(".i-refMileageTR").show();
            $("#i-isSuccessServerTD").hide();
            if (order.MONEY>0 || order.MONEY > 0) {
                return parseInt(order.MONEY) + parseInt(order.MONEY);
            } else {
                var REF_MILEAGE2 = order.MONEY;
                if (!REF_MILEAGE2) REF_MILEAGE2 = 0;
                else REF_MILEAGE2 = parseInt(REF_MILEAGE2 / 1000)
                if (REF_MILEAGE2 > 15) {
                    REF_MILEAGE2 = REF_MILEAGE2 - 15
                }
                $("#i-refMileage").html(REF_MILEAGE2);
                return 150 + REF_MILEAGE2 * 6;
            }
        }
    });

    var totalFee = $("#i-totalFee").html();
    if (order.MONEY) {
        $('#i-totalFee1').html(parseInt(800))
    } else {
        $('#i-totalFee1').html(totalFee)
    }

    var mile = !order.MILE ? 0 : parseInt(order.MILE);
    if (order.MILE > 15000 && (!order.MILE > 0)) {
        mile = parseInt((MILE - 15000) / 1000);
    }
    $('#i-mileage1').val(mile);

}
