/**
 * [OEUI] (C)2010-2099 oelove.com Inc.biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * $ Last update 2020/11/17 by CL $
*/
$(function(){

	function OESafety(){
        this.obj_id = '';
        this._this_dialog = null;
        
        this.clickCredit();
        this.clickAdd();
    }
    
    
    OESafety.prototype={
        
        //诚信查询
        clickCredit:function(){
            _this = this ;
            
            //查询类型
            $(document).on("click", "[f='but_credit_type']", function(){
                var type = $(this).attr("data-type");
                $("[f='but_credit_type']").removeClass("current");
                $(this).addClass("current");
                
                var text_t = "";
                if (type == "mobile") {
                    text_t = "请输入查询手机号";
                }
                else if (type == "qq") {
                    text_t = "请输入查询QQ号";
                }
                else if (type == "email") {
                    text_t = "请输入查询邮箱";
                }
                else if (type == "weixin") {
                    text_t = "请输入查询微信";
                }
                else if (type == "idcard") {
                    text_t = "请输入查询身份证号";
                }
                else {
                    text_t = "请输入查询关键词";
                }
                $("#credit_key").val("");
                $("#credit_type").val(type);
                $("#credit_result").hide();
            });
            
            //查询
            $(document).on('click','[f="but_submit_rpzy"]',function(){
                _this.obj_id = $(this).attr('id');
                
                var rpzy_flag = $(this).attr("data-flag");

                if (rpzy_flag == "0") {
                    OEUI.message.msg({
                        text: '网站暂未开启该功能，请联系客服',
                    });
                    return false;
                }
                
                $("#credit_result").hide();
                var keyword = $("#credit_key").val();
                var type = $("#credit_type").val();
                if (keyword == '') {
                    OEUI.message.msg({
                        text: '请输入要查询的信息',
                    });
                    return false;
                }
                
                if (!$("#"+_this.obj_id).hasClass("forbid_submit")) {
                    $("#"+_this.obj_id).addClass("forbid_submit");
                    var loading = OEUI.loading({
                        el: this,
                        width: 30,
                        height: 30
                    });
                    loading.show();
                
                    $.ajax({
                        type: "POST",
                        url: _ROOT_PATH + "index.php?c=rpzy",
                        cache: false,
                        data: {
                            a:"query", type:type, value:keyword, datatype:"json"
                        },
                        dataType: "json",
                        success: function(data) {
                            var response = data.response;
                            var result = data.result;
                            $("#key_tips").html("“"+keyword+"”");
                            $("#credit_img").removeClass("current");
                            if (response == "1") { //没有举报记录
                                $("#credit_img").removeClass("current");
                                $("#check_tips").html("记录良好，没有被举报过");
                            }
                            else if (response == "2") { //有举报记录
                                $("#credit_img").addClass("current");
                                $("#check_tips").html("有举报记录，可能存在风险");
                            }
                            else { //查询失败
                                $("#credit_img").addClass("current");
                                $("#check_tips").html("信息查询失败");
                            }
                            $("#credit_result").show();
                            
                            $("#"+_this.obj_id).removeClass("forbid_submit");
                            loading.remove();
                        },
                        error: function() {
                            OEUI.message.msg({
                                text: '系统繁忙，请稍后再试"'
                            });
                            
                            $("#"+_this.obj_id).removeClass("forbid_submit");
                            loading.remove();
                        }
                    });
                
                }
            });
        },
        
        
        //发表
        clickAdd:function() {
            var _this = this;
            
            //弹出发布页 
            $(document).on("click", "[f='pop_safety_addto']", function(){
                var width = $(this).attr("data-width");
                if (typeof(width) == "undefined") {
                    width = 500;
                }
                var height = $(this).attr("data-height");
                if (typeof(height) == "undefined") {
                    height = 400;
                }
                _this._this_dialog = OEUI.modal.dialog({
                    type: 'iframe',
                    title: '发布防骗文章',
                    width: width,
                    height: height,
                    content: _ROOT_PATH+"index.php?c=safety&a=add"
                });
            });
            
            //提交发表
            $(document).on('click','[f="but_submit_safetyadd"]',function(){
                _this.obj_id = $(this).attr('id');
                
                var title = $("#safety_title").val();
                var content = $("#safety_content").val();

                if (title == '') {
                    OEUI.message.msg({
                        text: '请填写标题',
                    });
                    return false;
                }
                if (content == '') {
                    OEUI.message.msg({
                        text: '请填写内容',
                    });
                    return false;
                }
                
                if (!$("#"+_this.obj_id).hasClass("forbid_submit")) {
                    $("#"+_this.obj_id).addClass("forbid_submit");
                    
                    $.ajax({
                        type: "POST",
                        url: _ROOT_PATH + "index.php?c=safety",
                        cache: false,
                        data: {
                            a:"saveadd", title:title, content:content
                        },
                        dataType: "json",
                        success: function(data) {
                            var response = data.response;
                            var result = data.result;
                            if (response == "1") {
                                OEUI.message.msg({
                                    text: '文章发表成功，请等待审核！'
                                });
                                setTimeout(function(){
                                    window.top.location.reload();
                                }, 500); 
                            }
                            else {
                                if (result == '' || result == null) {
                                    result = '发表失败，请检查';
                                }
                                OEUI.message.msg({
                                    text: result
                                });
                            }
                            
                            $("#"+_this.obj_id).removeClass("forbid_submit");
                            
                        },
                        error: function() {
                            OEUI.message.msg({
                                text: '系统繁忙，请稍后再试'
                            });
                            
                            $("#"+_this.obj_id).removeClass("forbid_submit");

                        }
                    });
                
                }
            })
        }
    }

    window.Safety = new OESafety();
});

