/**
 * [OElove] (C)2010-2099 oelove.com biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 *  Last update 2020-11-10 by CL
*/
$(function(){
    function Set() {
        this.switchBtn();
        this.pwd();
        this.bank();
        this.weixin();
        this.bindLogin();
        this.cancelBind();
        this.rz();
        this.switchMarry();
    }
    Set.prototype = {
        //开关按钮
        switchBtn: function () {
            $(".oeui_switch").on("change", function () {
                var _mod = $(this).attr("data-mod");
                if ($(this).hasClass("checked")) {
                    var val = 1;
                }
                else {
                    var val = 0;
                }
                //隐藏手机号码 8.12.07
                if (_mod == "hidemobile") {
                    if ($(this).hasClass("checked")) {
                        val = 0;
                    }
                    else {
                        val = 1;
                    }
                }

                $.ajax({
                    type: "POST",
                    url: _OE_FILE + "&c=set",
                    cache: false,
                    data: {
                        a:"saveset", mod:_mod, value:val, datatype:"json", r:get_rndnum(8)
                    },
                    dataType: "json",
                    success: function(data) {
                        var json = eval(data);
                        var response = json.response;
                        var result = json.result;
                        if (response == "1") {
                            OEUI.message.msg({text: "设置成功"})
                        }
                    },
                    error: function() {
                    }
                });
            });
        },
        //密码相关
        pwd: function () {
            var _this = this;
            //打开密码窗
            $(document).on("click", "#pwd_page_btn", function(){
                var width = $(this).attr("data-width");
                if (typeof(width) == "undefined") {
                    width = 550;
                }
                var height = $(this).attr("data-height");
                if (typeof(height) == "undefined") {
                    height = 340;
                }

                var _url = _OE_FILE + "&c=set&a=password";
                window.pwdDialog = OEUI.modal.dialog({
                    type: "iframe",
                    title: "修改密码",
                    width: width,
                    height: height,
                    content: _url
                });
            });
            //修改密码
            
            $(document).on("click", "[f='edit_password']", function () {
                var oldpassword = $("#oldpassword").val();
                var newpassword = $("#newpassword").val();
                var confirmpassword = $("#confirmpassword").val();
                if (oldpassword.length == 0) {
                    OEUI.message.msg({text: "请输入旧密码"});
                    return false;
                }
                if (newpassword.length == 0) {
                    OEUI.message.msg({text: "请输入新密码"});
                    return false;
                }
                if (confirmpassword.length == 0) {
                    OEUI.message.msg({text: "请输入确认密码"});
                    return false;
                }
                if (confirmpassword != newpassword) {
                    OEUI.message.msg({text: "确认密码不正确"});
                    return false;
                }
                $.ajax({
                    type: "POST",
                    url: _OE_FILE + "&c=set",
                    cache: false,
                    data: {
                        a:"savepassword", oldpassword:oldpassword, newpassword:newpassword,
                        confirmpassword:confirmpassword, datatype:"json", r:get_rndnum(8)
                    },
                    dataType: "json",
                    success: function(data) {
                        var json = eval(data);
                        var response = json.response;
                        var result = json.result;
                        if (response == "1") {
                            window.parent.OEUI.message.msg({text: "修改成功"})
                            window.parent.pwdDialog && window.parent.pwdDialog.remove();
                        }
                        else {
                            if (result.length > 0) {
                                OEUI.message.msg({text: result})
                            }
                            else {
                                OEUI.message.msg({text: "修改失败"})
                            }
                        }
                    },
                    error: function() {
                        OEUI.message.msg({text: "系统繁忙，请稍后再试"});
                    }
                });
            });
        },
        //收款相关
        bank: function () {
            //打开收款资料页
            var _this = this;
            $(document).on("click", "#open_bank_btn", function(){
                var width = $(this).attr("data-width");
                if (typeof(width) == "undefined") {
                    width = 600;
                }
                var height = $(this).attr("data-height");
                if (typeof(height) == "undefined") {
                    height = 570;
                }

                var _url = _OE_FILE + "&c=set&a=bank";
                _this.bankPage = OEUI.modal.dialog({
                    type: "iframe",
                    title: "修改收款资料",
                    width: width,
                    height: height,
                    content: _url
                });
            });
            //选择收款方式
            $(document).on("", "", function () {
                var val = $(this).attr(data-val);
                if (val == null) {
                    val = "";
                }
                if (val == "bank") {
                    $(".bank").show();
                    $(".alipay").hide();
                }
                else if (val == "alipay") {
                    $(".bank").hide();
                    $(".alipay").show();
                }
                $("#banktype").val(val);
                return false;
            });
            //修改收款资料
            $(document).on("click", "#submit_bank_btn", function () {
                var alipaycontact = $("#alipaycontact").val();
                var alipayaccount = $("#alipayaccount").val();

                var bankname = $("#bankname").val();
                var banksub = $("#banksub").val();
                var bankcontact = $("#bankcontact").val();
                var bankaccount = $("#bankaccount").val();

                var mobile_code = $("#bank_mobile_code").val();

                if (alipayaccount.length == 0 && bankaccount.length == 0) {
                    OEUI.message.msg({text: "支付宝和银行卡至少填写一种"})
                    return false;
                }
                if (alipayaccount.length > 0) {
                    if (alipaycontact.length == 0) {
                        OEUI.message.msg({text: "支付宝收款人姓名不能为空"})
                        return false;
                    }
                }
                if (bankaccount.length > 0) {
                    if (bankcontact.length == 0) {
                        OEUI.message.msg({text: "银行卡开户人姓名不能为空"})
                        return false;
                    }
                }
                if (alipaycontact.length > 0 && bankcontact.length > 0) {
                    if (alipaycontact != bankcontact) {
                        OEUI.message.msg({text: "支付宝姓名和银行卡开户人姓名不一致"})
                        return false;
                    }
                }

                if (mobile_code.length == 0) {
                    OEUI.message.msg({text: "请填写手机验证码"})
                    return false;
                }

                $.ajax({
                    type: "POST",
                    url: _OE_FILE + "&c=set",
                    cache: false,
                    data: {
                        a:"savebank", alipaycontact:alipaycontact, alipayaccount:alipayaccount,
                        bankname:bankname, banksub:banksub, bankcontact:bankcontact, bankaccount:bankaccount,
                        mobilecode:mobile_code, datatype:"json", r:get_rndnum(8)
                    },
                    dataType: "json",
                    success: function(data) {
                        var json = eval(data);
                        var response = json.response;
                        var result = json.result;
                        if (response == "1") {
                            OEUI.message.msg({text: "修改成功"})
                            setTimeout(function(){
                                window._this.bankPage.remove();
                            }, 500);
                        }
                        else {
                            if (result.length > 0) {
                                OEUI.message.msg({text: result})
                            }
                            else {
                                OEUI.message.msg({text: "修改失败"})
                            }
                        }
                    },
                    error: function() {
                        OEUI.message.msg({text: "系统繁忙，请稍后再试"})
                    }
                });
            })
        },
        //微信相关
        weixin: function () {
            //弹出微信绑定登录框
            $(document).on("click", "#bindwx_btn", function(){
                var width = $(this).attr("data-width");
                if (typeof(width) == "undefined") {
                    width = 350;
                }
                var height = $(this).attr("data-height");
                if (typeof(height) == "undefined") {
                    height = 380;
                }
                OEUI.modal.dialog({
                    type: "iframe",
                    title:'扫码绑定',
                    width: width,
                    height: height,
                    content: _ROOT_PATH+"index.php?c=passport&a=wxcode&r="+get_rndnum(8)
                });
            });
        },
        //绑定登录
        bindLogin: function () {
            $(document).on("click", "[f='bind_login_btn']", function(){
                var type = $(this).attr("data-type");
                var login_url = _ROOT_PATH+"index.php?c=passport&a=auth&mod="+type+"&from=set";
                window.location.href = login_url;
            });
        },
        //解除绑定
        cancelBind: function () {
            $(document).on("click", "[f='but_cancel_bind']", function(){
                var type = $(this).attr("data-type");

                OEUI.info.info({
                    title: '温馨提示',
                    text: '确定要解除登录吗？',
                    confirmText: "确定",
                    cancelText: "取消",
                    confirm: function () {
                        //点击确定
                        $.ajax({
                            type: "POST",
                            url: _OE_FILE + "&c=set",
                            cache: false,
                            data: {
                                a:"cancelbind", bindtype:type, datatype:"json", r:get_rndnum(8)
                            },
                            dataType: "json",
                            success: function(data) {
                                var json = eval(data);
                                var response = json.response;
                                var result = json.result;
                                if (response == "1") {
                                    OEUI.message.msg({text: "解除成功"})
                                    setTimeout(function(){
                                        window.location.reload();
                                    }, 500);
                                }
                                else {
                                    if (result.length > 0) {
                                        OEUI.message.msg({text: result})
                                    }
                                    else {
                                        OEUI.message.msg({text: "解除失败"})
                                    }
                                }
                            },
                            error: function() {
                                OEUI.message.msg({text: "系统繁忙，请稍后再试"})
                            }
                        });
                    },
                    cancel: function () {

                    }
                });
            });
        },
        //认证
        rz: function () {
            $(document).on("click", '[f="rz_btn"]', function(){
                var type = $(this).attr("data-type");
                OEUI.modal.info({
                    title: "温馨提示",
                    text: "确定去实名认证吗？",
                    confirmText: "确定",
                    cancelText: "取消",
                    confirm: function () {
                        window.location.href = _OE_FILE +'&c=rz&type='+type;
                    },
                    cancel: function () {

                    }
                });
            });
        },
        //切换婚况
        switchMarry: function () {
            $('#switch_marry').on('change', function () {
                var flag = $("#switch_flag").val();
              
                $.ajax({
                    type: "POST",
                    url: _OE_FILE,
                    data:{
                        c:'set', a:'setstatus', flag:flag
                    },
                    success: function(json) {
                        var res = JSON.parse(json);
                        
                        if (res.response == '1') {
                            OEUI.message.msg({text: '修改成功'});
                            setTimeout(function(){
                                window.location.reload();
                            }, 800);
                        } else if ($response == '2') {
                            OEUI.modal.info({
                                title: '温馨提示',
                                text: '当前状态不可修改，请联系网站客服处理',
                            });
                        } else {
                            if (res.result.length == 0 || res.result == '') {
                                res.result = '修改失败，请检查！';
                            }
                            OEUI.message.msg(res.result);
                        }
                    },
                    error: function() {
                        OEUI.message.msg('网络繁忙，请稍后再试！');
                    }
                });
            });
        }
    };
    new Set();
});