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 deleteEnterprise(id) {
    $.post("/supplier/getDestinationInfo.do","id="+id,function(data) {
        var data = eval("(" + data + ")");
        if(data.flag == 1){
             var datas=data.data;
             if(datas == ""){
                 alert("未选择合同");
                 return;
             }else{
                 $("#destination").combobox({
                     editable : false,
                     url :"/supplier/geAssignDestination.do?id="+id,//下拉选项框中数据 获取url
                     valueField : "id",//相当于 option 中的 value 发送到后台的
                     textField : "name",//option中间的内容 显示给用户看的
                     multiple:true,
                     //参数，可根据自己的需要来修改或者不要
                     formatter:function(row){
                         var opts=$(this).combobox('options');
                         return '<input type="checkbox" class="combobox-checkbox">' + row[opts.textField]
                     },
                     onSelect: function (row) { //选中一个选项时调用
                         var opts = $(this).combobox('options');
                         var el = opts.finder.getEl(this, row[opts.valueField]);
                         el.find('input.combobox-checkbox')._propAttr('checked', true);
                     },
                     onUnselect: function (row) {//不选中一个选项时调用
                         var opts = $(this).combobox('options');
                         var el = opts.finder.getEl(this, row[opts.valueField]);
                         el.find('input.combobox-checkbox')._propAttr('checked', false);
                     }
                 });
                 $("#userIDs").val(id);
                 $("#consumes_dialog").show();
                 $("#names").val("");
                 var queryParams = {};
                 var urls="/supplier/getDestination.do?id="+id;
                 queryDestinationList(urls,queryParams);
             }
        }
    })
}

function loadData2(obj) {
    var key, value, tagName, type, arr;
    for (x in obj) {
        key = x;
        value = obj[x];

        $("#addedit_dialog").find($("[name='" + key + "'],[name='" + key + "[]']")).each(function () {
            tagName = $(this)[0].tagName;
            type = $(this).attr('type');
            if (tagName == 'INPUT') {
                if (type == 'radio') {
                    $(this).attr('checked', $(this).val() == value);
                } else if (type == 'checkbox') {
                    arr = value.split(',');
                    //alert(arr);
                    for (var i = 0; i < arr.length; i++) {
                        //alert($(this).val()+'&&'+arr[i]+i);
                        if ($(this).val() == arr[i]) {
                            //alert(1);
                            $(this).prop('checked', true);
                            break;
                        }
                    }
                } else {
                    $(this).val(value);
                }
            } else if (tagName == 'SELECT' || tagName == 'TEXTAREA') {
                $(this).val(value);
            }

        });
    }
}
function showEnterprisePanel(id){
    $("#cc").combobox({
        editable : false,
        url :"/supplier/geAssignContract.do?id="+id,//下拉选项框中数据 获取url
        valueField : "id",//相当于 option 中的 value 发送到后台的
        textField : "name",//option中间的内容 显示给用户看的
        multiple:true,
        //参数，可根据自己的需要来修改或者不要
        formatter:function(row){
            var opts=$(this).combobox('options');
            return '<input type="checkbox" class="combobox-checkbox">' + row[opts.textField]
        },
        onSelect: function (row) { //选中一个选项时调用
            var opts = $(this).combobox('options');
            var el = opts.finder.getEl(this, row[opts.valueField]);
            el.find('input.combobox-checkbox')._propAttr('checked', true);
        },
        onUnselect: function (row) {//不选中一个选项时调用
            var opts = $(this).combobox('options');
            var el = opts.finder.getEl(this, row[opts.valueField]);
            el.find('input.combobox-checkbox')._propAttr('checked', false);
        }
    });
    $("#userID").val(id);
    $("#consume_dialog").show();
    $("#name").val("");
    var queryParams = {};
    var url="/supplier/getContractInfo.do?id="+id;
    queryCouponRechargehistoryList(url,queryParams);
}
function insertEnterprise(id){
    $("#enterprise").combobox({
        editable : false,
        url :"/supplier/geAssignEnterprise.do?id="+id,//下拉选项框中数据 获取url
        valueField : "id",//相当于 option 中的 value 发送到后台的
        textField : "name",//option中间的内容 显示给用户看的
        //这里的id是上面的combobox的id,因为我要在点击一个按钮的之后再动态的加载出来，所以我把它单独的抽取出来了。如果需要一开始就加载数据加载方式为：
        multiple:true,
        //参数，可根据自己的需要来修改或者不要
        formatter:function(row){
                var opts=$(this).combobox('options');
                return '<input type="checkbox" class="combobox-checkbox">' + row[opts.textField]
            },
            onLoadSuccess:function(){
                var opts = $(this).combobox('options');
                var target = this;
                var values = $(target).combobox('getValues');//获取选中的值的values
                $.map(values, function (value) {
                    var el = opts.finder.getEl(target, value);
                    el.find('input.combobox-checkbox')._propAttr('checked', true);
                })
            },
            onSelect: function (row) { //选中一个选项时调用
                var opts = $(this).combobox('options');
                //获取选中的值的values
                // var name=$("#enterprise").val();
                // alert(name);
                //当点击全选时，则勾中所有的选项
                // if(name="1127"){
                //     var a= $("#enterprise").combobox('getData');
                //     for(var i=0;i<a.length;i++){
                //         var el = opts.finder.getEl(this, a[i][opts.valueField]);
                //         el.find('input.combobox-checkbox')._propAttr('checked', true);
                //     }
                // }
                //设置选中值所对应的复选框为选中状态
                var el = opts.finder.getEl(this, row[opts.valueField]);
                el.find('input.combobox-checkbox')._propAttr('checked', true);
            },
            onUnselect: function (row) {//不选中一个选项时调用
                var opts = $(this).combobox('options');
                //获取选中的值的values
                // $("#enterprise").val($(this).combobox('getValues'));
                // 当取消全选勾中时，则取消所有的勾选
                // if($(this).combobox('getValues')=="全选"){
                //     var a= $("#"+id).combobox('getData');
                //     for(var i=0;i<a.length;i++){
                //         var el = opts.finder.getEl(this, a[i][opts.valueField]);
                //         el.find('input.combobox-checkbox')._propAttr('checked', false);
                //     }
                // }
                var el = opts.finder.getEl(this, row[opts.valueField]);
                el.find('input.combobox-checkbox')._propAttr('checked', false);
            }
});
    $("#user").val(id);
    $("#wrap_dialog").show();
    $("#enterpriseName").val("");
    var url="/supplier/getEnterpriseInfo.do?id="+id;
    var queryParams = {};
    querySupplierList(url,queryParams);
}
//服务商信息显示记录
function querySupplierList(urls, queryParams) {
    // 默认查询第一页 query查询时保存当前条件
    var TempPageNumber = 1, pageSize = 5;
    try {
        TempPageNumber = $('#datatable4').datagrid('getPager').pagination("options").pageNumber;
        pageSize = $('#datatable4').datagrid('getPager').pagination("options").pageSize;
    } catch (e) {

    }
    $('#datatable4')
        .datagrid(
            {
                title: "信息列表",
                iconCls: 'icon-search',
                pageNumber: TempPageNumber,
                pageSize: pageSize,
                pageList: [5, 7],
                nowrap: true,
                striped: true,
                border: true,
                noheader: true,
                url: urls,
                loadMsg: '数据加载中......',
                queryParams: queryParams,
                remoteSort: false,
                fitColumns: true,
                singleSelect: false,
                columns: [[
                    {
                        field:'check',checkbox:true
                    }, {
                        field: 'CODE',
                        title: '服务商编码',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }, {
                        field: 'NAME',
                        title: '服务商名称',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }
                    , {
                        field: 'LOGINNAME',
                        title: '登入名',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }, {
                        field: 'CREATETIME',
                        title: '创建时间',
                        sortable: 'false',
                        align: 'center',
                        width: 10,
                        formatter: function (value, row, index) {
                            return new Date(value).Format("yyyy-MM-dd hh:mm:ss");
                        }
                    }

                ]],
                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) {
                }

            });
    $('#datatable4').datagrid('getPager').pagination({
        displayMsg: '当前显示从 {from} 到 {to} 共{total}条记录',
        onBeforeRefresh: function (pageNumber, pageSize) {
            $(this).pagination('loading');
            $(this).pagination('loaded');
        }
    });
}
//合同信息显示记录
function queryCouponRechargehistoryList(urls, queryParams) {
    // 默认查询第一页 query查询时保存当前条件
    var TempPageNumber = 1, pageSize = 5;
    try {
        TempPageNumber = $('#datatable2').datagrid('getPager').pagination("options").pageNumber;
        pageSize = $('#datatable2').datagrid('getPager').pagination("options").pageSize;
    } catch (e) {

    }
    $('#datatable2')
        .datagrid(
            {
                title: "信息列表",
                iconCls: 'icon-search',
                pageNumber: TempPageNumber,
                pageSize: pageSize,
                pageList: [5, 7],
                nowrap: true,
                striped: true,
                border: true,
                noheader: true,
                url: urls,
                loadMsg: '数据加载中......',
                queryParams: queryParams,
                remoteSort: false,
                fitColumns: true,
                singleSelect: false,
                columns: [[
                    {
                        field:'check',checkbox:true
                    }, {
                        field: 'CODE',
                        title: '合同编码',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }, {
                        field: 'NAME',
                        title: '合同名称',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }
                    , {
                        field: 'REMARK',
                        title: '备注',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }
                    , {
                        field: 'APPCODE',
                        title: 'app编号',
                        sortable: 'true',
                        align: 'center',
                        width: 10,
                        // formatter: function (value, row, index) {
                        //     var str = new Date(value.time).Format("yyyy-MM-dd hh:mm:ss");
                        //     return str;
                        // }
                    }, {
                        field: 'CREATETIME',
                        title: '创建时间',
                        sortable: 'false',
                        align: 'center',
                        width: 10,
                        formatter: function (value, row, index) {
                            return new Date(value).Format("yyyy-MM-dd hh:mm:ss");
                        }
                    }

                ]],
                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) {
                }

            });
    $('#datatable2').datagrid('getPager').pagination({
        displayMsg: '当前显示从 {from} 到 {to} 共{total}条记录',
        onBeforeRefresh: function (pageNumber, pageSize) {
            $(this).pagination('loading');
            $(this).pagination('loaded');
        }
    });
}
//目的地显示记录
function queryDestinationList(urls, queryParams) {
    // 默认查询第一页 query查询时保存当前条件
    var TempPageNumber = 1, pageSize = 5;
    try {
        TempPageNumber = $('#datatable3').datagrid('getPager').pagination("options").pageNumber;
        pageSize = $('#datatable3').datagrid('getPager').pagination("options").pageSize;
    } catch (e) {

    }
    $('#datatable3')
        .datagrid(
            {
                title: "信息列表",
                iconCls: 'icon-search',
                pageNumber: TempPageNumber,
                pageSize: pageSize,
                pageList: [5,7],
                nowrap: true,
                striped: true,
                border: true,
                noheader: true,
                url: urls,
                loadMsg: '数据加载中......',
                queryParams: queryParams,
                remoteSort: false,
                fitColumns: true,
                singleSelect: false,
                columns: [[
                    {
                        field:'check',checkbox:true
                    }, {
                        field: 'NAME',
                        title: '名称',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }, {
                        field: 'ADDRESS',
                        title: '地址',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }
                    , {
                        field: 'BRAND',
                        title: '商标',
                        sortable: 'true',
                        align: 'center',
                        width: 10
                    }, {
                        field: 'CREATETIME',
                        title: '更新时间',
                        sortable: 'false',
                        align: 'center',
                        width: 10,
                        formatter: function (value, row, index) {
                            return new Date(value).Format("yyyy-MM-dd hh:mm:ss");
                        }
                    }

                ]],
                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) {
                }

            });
    $('#datatable3').datagrid('getPager').pagination({
        displayMsg: '当前显示从 {from} 到 {to} 共{total}条记录',
        onBeforeRefresh: function (pageNumber, pageSize) {
            $(this).pagination('loading');
            $(this).pagination('loaded');
        }
    });
}
function queryCouponList(urls, queryParams, paraUrls, state, isExist) {
    var pageNumber = 1, pageSize = 10;
    // 默认查询第一页 query查询时保存当前条件
    //var pageCount = 0;
    // var sum = parseInt(total);
    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: 'NAME',
                        title: '姓名',
                        sortable: 'false',
                        align: 'center',
                        width: 10
                    }, {
                        field: 'LOGINNAME',
                        title: '登录名',
                        sortable: 'false',
                        align: 'center',
                        width: 8
                    }, {
                        field: 'PHONE',
                        title: '手机号',
                        sortable: 'false',
                        align: 'center',
                        width: 10,
                    }
                    , {
                        field: 'EMAIL',
                        title: '电子邮箱',
                        sortable: 'false',
                        align: 'center',
                        width: 10
                    }

                    , {
                        field: 'ROLENAME',
                        title: '所属角色',
                        sortable: 'false',
                        align: 'center',
                        width: 10
                    }
                    , {
                        field: 'UPDATETIME',
                        title: '更新时间',
                        sortable: 'false',
                        align: 'center',
                        width: 10,
                        formatter: function (value, row, index) {
                            return new Date(value).Format("yyyy-MM-dd hh:mm:ss");
                        }
                    },{
                        field: 'operate', title: '操作', align: 'center', width: 15,
                        formatter: function (value, row, index) {
                            var str = '<a href="#" name="opera" class="easyui-linkbutton opera" onclick="showEnterprisePanel(' + row.ID + ');">选择合同</a>&nbsp;&nbsp;';
                            str = str + '<a href="#" name="opera" class="easyui-linkbutton opera" onclick="deleteEnterprise(' + row.ID + ');">指定目的地</a>&nbsp;&nbsp;';
                            str = str + '<a href="#" name="opera" class="easyui-linkbutton opera" onclick="insertEnterprise(' + row.ID + ');">指定供应商</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) {
                }
            })
    ;
    $('#datatable').datagrid('getPager').pagination({
        displayMsg: '',
    });

}

// 计算页面个数
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) {

    // jsRemoveItemFromSelect(objSelect, 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.code + '</td> ' +
        '<td><span>服务内容：</span>' + servicesName + '</td></tr>' +
        ' <tr><td><span>经销商代码：</span>' + result.contract_company_code + '</td>' +
        '<td><span>经销商名称：</span>' + specialOrganize + '</td></tr>' +
        ' <tr><td><span>车架号：</span>' + result.frame_code + '</td> ' +
        '<td><span>车&nbsp;&nbsp;型：</span>' + result.style + '</td> </tr>' +
        ' <tr><td><span>故障描述：</span>' + result.trouble_leve2 + '</td> </tr> ' +
        '<tr><td><span>事发地：</span>' + result.depart_address + '</td> </tr> ' +
        '<tr><td><span>目的地：</span>' + result.dest_address + '</td> <td></td> </tr> ';
    $(".PrintCase").html(s);


    // 救援详情
    var ss = '<tr> <th colspan="2"><span class="printList"></span>救援详情</th> </tr>' +
        '<tr><td>接案时间：</td><td>' + result.accept_time + '</td> </tr> ' +
        '<tr><td>到达时间：</td><td>' + result.arrive_time + '</td> </tr> ' +
        '<tr><td>完成时间：</td><td>' + result.finish_time + '</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.csignaturePath + ' "' + ' class=" signImg " />' +
        ' </th> <th> <div class=" signList "><span class=" printSignList ">' +
        '</span>车主/接车人签字</div> <img src=" ' + result.cksignaturePath + ' " class=" signImg " /> </th> </tr>';
    $(".sign").html(sss);
}

