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 queryCouponList(urls, queryParams,rescueState) {
    var pageNumber = 1, pageSize = 10;
    try {
        pageNumber = $('#datatable').datagrid('getPager').pagination("options").pageNumber;
        pageSize = $('#datatable').datagrid('getPager').pagination("options").pageSize;
    } catch (e) {
        pageNumber = 1, pageSize = 10;
    }
    $('#datatable')
        .datagrid(
            {
                title: "信息列表",
                iconCls: 'icon-search',
                pageNumber: pageNumber,
                pageSize: pageSize,
                pageList: [5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
                nowrap: true,
                striped: true,
                border: true,
                noheader: true,
                url: urls,
                loadMsg: '数据加载中......',
                queryParams: queryParams,
                remoteSort: false,
                fitColumns: true,
                singleSelect: false,
                // frozenColumns: [[{
                //     field: 'orderkey',
                //     checkbox: true
                // }]],
                columns: [[

                    {
                        field: 'ORDER_CODE',
                        title: '案件编号',
                        sortable: 'false',
                        align: 'center',
                        width: 10,
                        formatter: function (value, row, index) {
                            if (rescueState == "2") {
                                var str = value;

                                str = '<a class="aColor"><div style="color: rgb(66, 139, 202)"' +
                                    '  onclick="completionOfRescue(\'' + row.ORDER_CODE + '\',\'' + row.NAME + '\',' +
                                    '\'' + row.SERVICE_NAME + '\');">' + value + '</div></a>';

                                return str;
                            }else {
                                return value;
                            }
                        }
                    }, {
                        field: 'CREATE_TIME',
                        title: '案件创建时间',
                        sortable: 'false',
                        align: 'center',
                        width: 10,
                        formatter: function (value, row, index) {
                            return new Date(value).Format("yyyy-MM-dd hh:mm:ss");
                        }
                    }
                    , {
                        field: 'VIN_NO',
                        title: 'VIN码',
                        sortable: 'false',
                        align: 'center',
                        width: 10
                    }
                    , {
                        field: 'PLATE_NUMBER',
                        title: '车牌号',
                        sortable: 'false',
                        align: 'center',
                        width: 10
                    }
                    , {
                        field: 'NAME',
                        title: '服务商名称',
                        sortable: 'false',
                        align: 'center',
                        width: 15
                    }
                    , {
                        field: 'CODE',
                        title: '服务商代码',
                        sortable: 'false',
                        align: 'center',
                        width: 10
                    }
                    , {
                        field: 'SERVICE_NAME',
                        title: '服务类型',
                        sortable: 'false',
                        align: 'center',
                        width: 10
                    }
                    , {
                        field: 'TASK_STATUS',
                        title: '服务状态',
                        sortable: 'false',
                        align: 'center',
                        width: 10,
                        formatter: function(value,row,index){
                            if(value == ''){
                                var value = row.STATUS;
                                if (value == 11){
                                    return "订单创建,等待完善信息";
                                }else if (value == 13){
                                    return "改为预约订单";
                                }else if (value == 15){
                                    return "订单需要授权";
                                }else if (value == 19){
                                    return "等待坐席处理";
                                }else if (value == 17){
                                    return "订单待付款";
                                }else if (value == 21){
                                    return "调度服务商,等待回应";
                                }else if (value == 23){
                                    return "等待系统自动调度";
                                }else if (value == 25){
                                    return "自动调度中,等待回应";
                                }else if (value == 28){
                                    return "待派车";
                                }else if (value == 31){
                                    return "救援中";
                                }else if (value == 51){
                                    return "取消服务";
                                }else if (value == 53){
                                    return "救援失败,订单结束";
                                }else if (value == 55){
                                    return "救援完成";
                                }
                            }else if (value == 10000){
                                return "调度成功，等待出发";
                            }else if (value == 11001){
                                return "救援车发车，正在赶往现场";
                            }else if (value == 12001){
                                return "救援车到达，核对救援资格";
                            }else if (value == 13001){
                                return "救援资格核对成功，开始验车";
                            }else if (value == 14001){
                                return "验车完成，开始救援作业";
                            }else if (value == 15001){
                                return "验车完成，拖车作业中";
                            }else if (value == 16001){
                                return "作业完成，正在拖往目的地";
                            }else if (value == 17001){
                                return "拖车到达目的地，生成结算单";
                            }else if (value == 18001){
                                return "确认结算单";
                            }else if (value == 19001){
                                return "任务完成";
                            }else if (value == 11002){
                                return "服务人员已经出发";
                            }else if (value == 12002){
                                return "服务人员到达，核对信息";
                            }else if (value == 13002){
                                return "信息核对完成，收取材料";
                            }else if (value == 15002){
                                return "材料收取完毕，前往验车点";
                            }else if (value == 17002){
                                return "到达验车点，开始验车";
                            }else if (value == 11003){
                                return "服务人员前往验车点";
                            }else if (value == 12003){
                                return "到达验车点，取回材料";
                            }else if (value == 16002){
                                return "作业完成，生成结算单";
                            }else if (value == 19002){
                                return "任务取消";
                            }else if (value == 19003){
                                return "任务放弃";
                            }else if (value == 14002){
                                return "验车完成，开始困境救援";
                            }
                        }
                    }
                ]],
                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) {
                }
            })
    ;
    $('#datatable').datagrid('getPager').pagination({
        displayMsg: '',
        // onBeforeRefresh: function (pageNumber, pageSize) { 当前显示从 {from} 到 {to}
        //     $(this).pagination('loading');
        //     $(this).pagination('loaded');
        // },
        // onSelectPage:function (pageNumber, pageSize) {
        //    // $('#datatable').panel('refresh', urls+'?pageNumber='+pageNumber+"&pageSize="+pageSize);
        //     var params={};
        //     params["pageNumber"]=pageNumber;
        //     params["pageSize"]=pageSize;
        //     var result = ajaxgetresultpost(urls,params );
        //     $('#datatable').datagrid('loadData', result.userOrderViewVOList);
        // }
    });

}

// 计算页面个数
function getPageCount(total, pageSize) {
    if (total % pageSize == 0) {
        return total / pageSize;
    } else {
        return parseInt(total / pageSize) + 1;
    }

}


function toCompairTime(a, b) {
    var arr = a.split("-");
    var starttime = new Date(arr[0], arr[1], arr[2]);
    var starttimes = starttime.getTime();
    var arrs = b.split("-");
    var lktime = new Date(arrs[0], arrs[1], arrs[2]);
    var lktimes = lktime.getTime();

    if (starttimes >= lktimes) {

        return false;
    }
    else
        return true;
}

// 2.向select选项中 加入一个Item
function jsAddItemToSelect(objSelect, listfirst, idList) {
//判断是否存在
    if (listfirst != null && listfirst.length > 0) {
        objSelect.options.add(new Option('请选择', 0));
        for (var i = 0; i < listfirst.length; i++) {
            var objItemValue = idList[i];
            //  alert(objItemValue);
            var objItemText = listfirst[i];
            //alert(objItemText);
            var varItem = new Option(objItemText, objItemValue);
            objSelect.options.add(varItem);
        }
    }

    //alert("成功加入");
}

//
// function jsRemoveItemFromSelect(objSelect, listfirst) {
// //判断是否存在
//     if (listfirst != null && listfirst.length > 0) {
//         for (var i = 0; i < listfirst.length; i++) {
//             var objItemValue = listfirst[i];
//           //  if (!jsSelectIsExitItem(objSelect, objItemValue)) {
//                 for (var i = 0; i < objSelect.options.length; i++) {
//                     if (objSelect.options[i].value == objItemValue) {
//                         objSelect.options.remove(i);
//                         break;
//                     }
//         //        }
//             }
//         }
//     }
// }


function jsRemoveItemFromSelect(objSelect) {
//判断是否存在
    var optionList = objSelect.options
    for (var i = 0; i < optionList.length; i++) {
        alert(optionList.length);
        optionList.remove(i);
    }
}
// 1.判断select选项中 是否存在Value="paraValue"的Item
function jsSelectIsExitItem(objSelect, objItemValue) {
    var isExit = false;
    for (var i = 0; i < objSelect.options.length; i++) {
        if (objSelect.options[i].value == objItemValue) {
            isExit = true;
            break;
        }
    }
    return isExit;
}

// 求援完成确定
function completionOfRescue(code, special_organize, servicesname) {
    var path = $("#paths").val();
    $(".order_print").show();
    var param = {};
    param["code"] = code;
    var result = ajaxgetresultpost(path + '/casesInfo/completionOfRescue.do', param);
    var specialOrganize = special_organize == "undefined" || special_organize == null ? '' : special_organize;
    var servicesName = servicesname == "undefined" || servicesname == null ? '' : servicesname;
    var s = ' <tr> <th colspan="3"><span class="printList"></span>案件信息</th> </tr>' +
        ' <tr> <td><span>案件编号：</span>' + result.ORDER_CODE + '</td> ' +
        '<td><span>服务内容：</span>' + result.SERVICE_TIME + '</td></tr>' +
        ' <tr><td><span>经销商代码：</span>' + result.CODE + '</td>' +
        '<td><span>经销商名称：</span>' + result.NAME + '</td></tr>' +
        ' <tr><td><span>车架号：</span>' + result.VIN_NO + '</td> ' +
        '<td><span>车&nbsp;&nbsp;型：</span>' + result.MODEL + '</td> </tr>' +
        ' <tr><td><span>故障描述：</span>' + result.MAL_REMARK + '</td> </tr> ' +
        '<tr><td><span>事发地：</span>' + result.START_ADDRESS + '</td> </tr> ' +
        '<tr><td><span>目的地：</span>' + result.DESTINATION_ADDRESS + '</td> <td></td> </tr> ';
    $(".PrintCase").html(s);
    var createTime=new Date(result.CREATE_TIME).Format("yyyy-MM-dd hh:mm:ss");
    var arriveTime=new Date(result.ARRIVE_TIME).Format("yyyy-MM-dd hh:mm:ss");
    var finishTime=new Date(result.FINISH_TIME).Format("yyyy-MM-dd hh:mm:ss");
    // 救援详情
    var ss = '<tr> <th colspan="2"><span class="printList"></span>救援详情</th> </tr>' +
        '<tr><td>接案时间：</td><td>' + createTime + '</td> </tr> ' +
        '<tr><td>到达时间：</td><td>' + arriveTime + '</td> </tr> ' +
        '<tr><td>完成时间：</td><td>' + finishTime + '</td> </tr> '
    $(".saveDetail").html(ss);
    // 签名
    //alert(result.csignaturePath,result.cksignaturePath);
    var sss = ' <tr> <th> <div class=" signList "><span class=" printSignList ">' +
        '</span>车主签字</div> <img src="' + result.URL + ' "' + ' class=" signImg " />' +
        ' </th> <th> <div class=" signList "><span class=" printSignList ">' +
        '</span>车主/接车人签字</div> <img src="' + result.URLS + ' " class=" signImg " /> </th> </tr>';
    $(".sign").html(sss);
}

