(function (w) {
    var Common = function(){
    }

    // 定时刷新列表时间
    Common.prototype.setIntervalTimes = 3 * 60 * 1000;

    /**
     * 加载层
     */
    Common.prototype.loading = function(){
        $("#loading-tips").addClass("showPrompt");
    }

    /**
     * 去除加载层
     */
    Common.prototype.closeload = function(){
        $("#loading-tips").removeClass("showPrompt");
    }

    /**
     * 初始化表格事件
     */
    Common.prototype.initTabEvent = function(tabId){
        // dataTable 默认错误提示是alert，这里做处理
        $.fn.dataTable.ext.errMode = function( settings, tn, msg){
            //打印msg，和tn来判断，进了这个方法都是ajax走了error才会到这里来
            console.log(msg);
        }
        var $tab = $("#"+tabId);
        // 当Ajax请求完成时触发
        $tab.on('xhr.dt', function ( e, settings, json, xhr ){
            // json 为空 可能是登陆超时，刷新页面
            if(json == null){
                console.error("请求错误");
                // window.location.reload (true);
            }
        } );

        var dom;// 一个节点对象
        // 选中行保存行节点对象
        $tab.find("tbody").on( 'click', 'tr', function () {
            if(dom){
                //上一次选中行颜色恢复
                $(dom).css("background-color", "");
            }
            //选中行变色
            $( this ).css("background-color", "#B0BED9");
            dom = this;
        });
        var $tablebody = $('#'+tabId+' tbody');
        //单击选择该数据返回ID
        $tablebody.on( 'click', 'tr', function () {
            if(!$(this)[0].hasAttribute("clicheckedid")){
                $tablebody.find("[clicheckedid]").removeAttr("clicheckedid");
                var data = $tab.DataTable().row(this).data();
                var id = "";
                if(data) id = data.id;
                $(this).attr("clicheckedid", id);
            }
        } );
    }

    /**
     * 获取表格选中行的id
     */
    Common.prototype.getTabCheckedTrId = function(tabId){
        var $tab = $("#"+tabId);
        var dom = $('#'+tabId+' tbody').find("[clicheckedid]");
        var id = "";
        if(dom) id = dom.attr("clicheckedid");
        return id;
    }

    /**
     * 表格刷新
     */
    Common.prototype.tabRefresh = function(tabId){
        var $tab = $("#"+tabId);
        $tab.dataTable().api().ajax.reload( null, false ); // 刷新表格数据，分页信息不会重置
    }

    /**
     * 加载委托方下拉框控件数据
     */
    Common.prototype.initWtfdata = function(id){
        $.ajax({
            url: "ht/findWtfAll",
            type: "post",
            contentType:"application/json;charset=utf-8",
            // async: false,// 同步请求 防止编辑时延迟
            data: JSON.stringify({}),
            success: function(data){
                if (data.success) {
                    var list = data.data;
                    if(list && list.length>0){
                        var html="<option value=''>请选择委托方</option>";
                        $.each(list,function (i,obj) {
                            if(obj.name != ""){
                                html+="<option value='"+obj.id+"'";
                                html+=">"+obj.yjwtf+"</option>";
                            }
                        })
                        $(id).html(html);
                    }
                }
            }
        });
    }

    /**
     * 加载分公司下拉控件数据
     */
    Common.prototype.initFgsdata = function(id, qxzShow, isasyncs){
        if(isasyncs === undefined || isasyncs) isasyncs = true;
        $.ajax({
            url: "grab/findFgsAll",
            type: "post",
            contentType:"application/json;charset=utf-8",
            async: isasyncs,// 同步请求 防止编辑时延迟
            data: JSON.stringify({}),
            success: function(data){
                if (data.success) {
                    var list = data.data;
                    if(list && list.length>0){
                        var html = "";
                        if(qxzShow === undefined || qxzShow) {
                            html+= "<option value=''>请选择分公司</option>";
                        }
                        $.each(list,function (i,obj) {
                            if(obj.name != ""){
                                html+="<option value='"+obj.id+"'";
                                html+=">"+obj.fgsMc+"</option>";
                            }
                        })
                        $(id).html(html);
                    }
                }
            }
        });
    }

    /**
     * 加载分公司下拉控件数据
     */
    Common.prototype.initFgsbyUserFgsidData = function(id, qxzShow, isasyncs){
        if(isasyncs === undefined || isasyncs) isasyncs = true;
        $.ajax({
            url: "grab/findFgsAllByUserFgsIds",
            type: "post",
            contentType:"application/json;charset=utf-8",
            async: isasyncs,// 同步请求 防止编辑时延迟
            data: JSON.stringify({}),
            success: function(data){
                if (data.success) {
                    var list = data.data;
                    if(list && list.length>0){
                        var html = "";
                        if(qxzShow === undefined || qxzShow) {
                            html+= "<option value=''>请选择分公司</option>";
                        }
                        $.each(list,function (i,obj) {
                            if(obj.name != ""){
                                html+="<option value='"+obj.id+"'";
                                html+=">"+obj.fgsMc+"</option>";
                            }
                        })
                        $(id).html(html);
                    }
                }
            }
        });
    }

    /**
     * 加载字典下拉框控件数据
     */
    Common.prototype.initDictdata = function(id, type, async){
        if(async === undefined) async = true;
        $.ajax({
            url: "dict/findAll",
            type: "post",
            contentType:"application/json;charset=utf-8",
            async: async,// 同步请求 防止编辑时延迟
            data: JSON.stringify({type: type}),
            success: function(data){
                if (data.success) {
                    var list = data.data;
                    if(list && list.length>0){
                        var html="<option value=''>请选择类别</option>";
                        $.each(list,function (i,obj) {
                            if(obj.name != ""){
                                html+="<option value='"+obj.value+"'";
                                html+=">"+obj.name+"</option>";
                            }
                        })
                        $(id).html(html);
                    }
                }
            }
        });
    }

    /**
     * 初始化员工下拉框数据
     */
    Common.prototype.initUserSelect = function(id, async) {
        if(async === undefined) async = true;
        $.ajax({
            url: "user/findAll",
            type: "post",
            contentType:"application/json;charset=utf-8",
            async: async,// 同步请求 防止编辑时延迟
            data: JSON.stringify({}),
            success: function(data){
                if (data.success) {
                    var list = data.data;
                    if(list && list.length>0){
                        var html="<option value=''>请选择业务员</option>";
                        $.each(list,function (i,obj) {
                            if(obj.name != ""){
                                html+="<option value='"+obj.id+"'";
                                html+=">"+obj.trueName+"</option>";
                            }
                        });
                        $(id).html(html);
                    }
                }
            }
        });
    }

    /**
     * 初始化业务员下拉框数据
     */
    Common.prototype.initYwyUserSelect = function(id, async) {
        if(async === undefined) async = true;
        $.ajax({
            url: "ywypz/findAll",
            type: "post",
            contentType:"application/json;charset=utf-8",
            async: async,// 同步请求 防止编辑时延迟
            data: JSON.stringify({}),
            success: function(data){
                if (data.success) {
                    var list = data.data;
                    if(list && list.length>0){
                        var html="<option value=''>请选择业务员</option>";
                        $.each(list,function (i,obj) {
                            if(obj.name != ""){
                                html+="<option value='"+obj.id+"'";
                                html+=">"+obj.trueName+"</option>";
                            }
                        });
                        $(id).html(html);
                    }
                }
            }
        });
    }

    /**
     * 按车架号查询车型信息
     */
    Common.prototype.queryvin = function(modalId, cjh) {
        if(!cjh || cjh == "" || cjh.length < 17 || cjh.length > 17){
            Common.prototype.alert("车架号错误", "车架号必须是17位！", 2);
            return;
        }
        Common.prototype.loading();//加载层
        $.ajax({
            url: "bj/queryvin",
            type: "post",
            contentType:"application/json;charset=utf-8",
            data: JSON.stringify({cjh: cjh}),
            success: function(data){
                Common.prototype.closeload();//去除加载层
                if (data.success) {
                    var obj = data.data;
                    if(obj.chexing != "") {
                        Common.prototype.loadCheXingData(modalId, obj.csh, obj.pinpai, obj.chexi, obj.chexing);
                    }else{
                        Common.prototype.alert("错误信息", "查无资料！", 2);
                    }
                }else{
                    Common.prototype.alert("错误信息", data.msg, 2);
                }
            }
        });
    }

    /**
     * 加载车型下拉数据
     */
    Common.prototype.loadCheXingData = function(modalId, csh, pinpai, chexi, chexing) {
        if(csh != "")
        $('#'+ modalId +" select[name='csh']").html("<option value='"+csh+"' selected>"+csh+"</option>");
        if(pinpai != "")
        $('#'+ modalId +" select[name='pinpai']").html("<option value='"+pinpai+"' selected>"+pinpai+"</option>");
        if(chexi != "")
        $('#'+ modalId +" select[name='chexi']").html("<option value='"+chexi+"' selected>"+chexi+"</option>");
        // $('#'+ modalId +" select[name='chexing']").html("<option value='"+chexing+"' selected>"+chexing+"</option>");
        $('#'+ modalId +" input[name='chexing']").val(chexing);
    }

    /**
     * 模态框 关闭时  里面的 清除内容 (解决模态框只请求一次服务器页面，内容不好刷新)
     */
    Common.prototype.modalHideDelHtml = function(modalId) {
        $('#'+modalId).on('hide.bs.modal', function () {
            $(this).removeData("bs.modal");
            //清除html
            $(this).find("div.modal-content").html("");
        })
    }

    /**
     * 初始化省市区下拉框数据
     */
    Common.prototype.initCitySelect = function(id, pid, async) {
        if(pid === undefined) pid = "1";
        if(async === undefined) async = true;
        $.ajax({
            url: "common/findCity",
            type: "post",
            contentType:"application/json;charset=utf-8",
            async: async,// 同步请求 防止编辑时延迟
            data: JSON.stringify({pid: pid}),
            success: function(data){
                if (data.success) {
                    var list = data.data;
                    if(list && list.length>0){
                        var html="<option value=''>请选择</option>";
                        $.each(list,function (i,obj) {
                            if(obj.name != ""){
                                html+="<option data-id='"+obj.id+"' value='"+obj.name+"'";
                                html+=">"+obj.name+"</option>";
                            }
                        });
                        $(id).html(html);
                    }
                }
            }
        });
    }

    /**
     * 提示框
     */
    Common.prototype.alert = function(title, text, type){
        text = text || "";
        var typeStr = "info";
        if(type == 1){
            typeStr = "success";
        }else if(type == 2){
            typeStr = "error";
        }
        swal({title: title, text: text, type: typeStr, confirmButtonColor: "#1ab394"});
    }


    /*  按枚举数据下拉框加载公共方法 *****************************************************************************************************/
    Common.prototype.loadSelectDataByEnum = function(id, data, defaultkey, defaultvalue){
        defaultkey = defaultkey || "";
        defaultvalue = defaultvalue || "";
        if(id && data){
            var html = "";
            if(defaultkey != ""){
                html+= "<option value='"+defaultvalue+"'>"+defaultkey+"</option>";
            }
            for(var k in data){
                html+="<option value='" + k + "'";
                html+=">" + data[k] + "</option>";
            }
            $(id).html(html);
        }
    }

    /*  枚举数据 *****************************************************************************************************/
    var ENUM = {};
    // 车辆类型
    ENUM.cllx = {
        "1": "事故车",
        "2": "二手车",
        "3": "新车",
        "4": "配件",
        "5": "商用车",
        "6": "其他"
    };
    // 变速箱类型
    ENUM.bsxlx = {
        "1": "自动",
        "2": "手动",
        "3": "手动一体"
    };
    // 燃油类型
    ENUM.rylx = {
        "1": "汽油",
        "2": "柴油",
        "3": "电动",
        "4": "油电混动"
    };
    // 使用性质
    ENUM.syxz = {
        "1": "非营运",
        "2": "租赁",
        "3": "租赁非营运",
        "4": "营运",
        "5": "预约出租",
        "6": "特种车辆",
        "7": "教练"
    };
    // 所有人性质
    ENUM.syrxz = {
        "1": "私户",
        "2": "公户"
    };
    // 进口方式
    ENUM.jkfs = {
        "1": "进口",
        "2": "平行进口",
        "3": "中规"
    };
    // 事故原因
    ENUM.sgyy = {
        "1": "碰撞",
        "2": "水淹",
        "3": "火烧",
        "4": "其他"
    };

    // 发拍类型
    ENUM.fptype = {
        pttj: 0,
        dx: 1
    };


    w.Common = new Common();
    w.Common.ENUM = ENUM;
})(window);