//显示修改项
function showSel(e) {
    $(e).parents('.nsetOne').hide().next('.nsetTwo').show();
}

//隐藏修改项
function hideSel(e,flag) {
    $(e).parents('.nsetTwo').hide().prev('.nsetOne').show();
    $(e).parents('form')[0].reset();
    removeErrAll(e);
}

//设置头像操作
function setTx(e) {
    //$(e).parents('.nsetOne').hide().next('.nsetTwo').show();
    $('.sr-only').trigger('click');
}

//图片保存操作
function saveCanvas(e) {
    var result = $('#imgCropper').cropper('getCroppedCanvas', {width:80,height:80});  //获取canvas
    var img = result.toDataURL('image/jpeg');  //canvas转图片
    var url = $(e).parents('form').attr('action');
    layer.load(2);
    var ajaxTimeoutTest = $.ajax({
        url: url,  //请求的URL
        timeout: 10000, //超时时间设置，单位毫秒
        type: 'post',  //请求方式，get或post
        data: {
            base64String: img
        },  //请求所传参数，json格式
        success: function (result) { //请求成功的回调函数
            layer.closeAll();
            if (result.result == 1) {
                $('.avatar-face .img-circle').attr('src', result.fileUrl);   //更换图片
                hideSel(e);
            } else {
                layer.alert(result.msg, {
                    skin: 'nprs2',
                    title: false,
                    shade: 0,
                    shift: 3,
                    btn: false,
                    time: 0
                });
            }
        },
        error: function () {
            layer.closeAll();
            layer.alert('请求失败!', {
                skin: 'nprs2',
                title: false,
                shade: 0,
                shift: 3,
                btn: false,
                time: 0
            });
        },
        complete: function (XMLHttpRequest, status) { //请求完成后最终执行参数
            if (status == 'timeout') {//超时,status还有success,error等值的情况
                ajaxTimeoutTest.abort();
                layer.closeAll();
                layer.alert('网络不通畅,请重试!', {
                    skin: 'nprs2',
                    title: false,
                    shade: 0,
                    shift: 3,
                    btn: false,
                    time: 0
                });
            }
        }
    });
}

function removeTx(e) {
    var url = $(e).parents('form').attr('action');
    layer.load(2);
    var ajaxTimeoutTest = $.ajax({
        url: url,  //请求的URL
        timeout: 10000, //超时时间设置，单位毫秒
        type: 'post',  //请求方式，get或post
        data: {
            base64String: ''
        },  //请求所传参数，json格式
        success: function (result) { //请求成功的回调函数
            layer.closeAll();
            console.log(result);
            if (result.result == 1) {
                $('.avatar-face .img-circle').attr('src', result.fileUrl);
            }
        },
        error: function () {
            layer.closeAll();
            layer.alert('请求失败!', {
                skin: 'nprs2',
                title: false,
                shade: 0,
                shift: 3,
                btn: false,
                time: 0
            });
        },
        complete: function (XMLHttpRequest, status) { //请求完成后最终执行参数
            if (status == 'timeout') {//超时,status还有success,error等值的情况
                ajaxTimeoutTest.abort();
                layer.closeAll();
                layer.alert('网络不通畅,请重试!', {
                    skin: 'nprs2',
                    title: false,
                    shade: 0,
                    shift: 3,
                    btn: false,
                    time: 0
                });
            }
        }
    });
}

function yz(e, setName) {
    removeErr(e);
    var val = $('[setname="' + setName + '"]').val();
    var $this = $('[setname="' + setName + '"]');
    var flag = 1;
    if (setName == 'setCommon') {
        if (val == '') {
            addErr($this, '请输入内容');
            flag = 0;
        }
    } else if (setName == 'password') {
        if (val == '') {
            addErr($this, '请输入内容');
            flag = 0;
        } else if (val.length < 6) {
            addErr($this, '密码长度至少6位!');
            flag = 0;
        } else if ($('[setname="password3"]').val().length < 6) {
            addErr($('[setname="password3"]'), '密码长度至少6位!');
            flag = 0;
        } else if ($('[setname="password2"]').val().length < 6) {
            addErr($('[setname="password2"]'), '密码长度至少6位!');
            flag = 0;
        } else if (val != $('[setname="password2"]').val()) {
            addErr($('[setname="password2"]'), '两次密码不相同!');
            flag = 0;
        }
    } else if (setName == 'phoneNumber') {
        if (val == '') {
            addErr($this, '请输入内容');
            flag = 0;
        } else if (!inputctr.public.ismobile(Number(val))) {
            addErr($this, '手机号码有误!');
            flag = 0;
        }
    }
    return flag;
}

function postInfo(e, setName) {
    if (yz(e, setName) == 1) {
        $('[setname="' + setName + '"]').val();
        var url = $(e).parents('form').attr('action');
        layer.load(2);
        var ajaxTimeoutTest = $.ajax({
            url: url,  //请求的URL
            timeout: 10000, //超时时间设置，单位毫秒
            type: 'post',  //请求方式，get或post
            data: $(e).parents('form').serialize(),  //请求所传参数，json格式
            success: function (result) { //请求成功的回调函数
                layer.closeAll();
                if (result.result == 1) {
                    overRun(e, setName);
                } else if (result.result == 0) {
                    layer.alert(result.msg, {
                        skin: 'nprs2',
                        title: false,
                        shade: 0,
                        shift: 3,
                        btn: false,
                        time: 3000
                    });
                }
            },
            error: function () {
                layer.closeAll();
                layer.alert('请求失败!', {
                    skin: 'nprs2',
                    title: false,
                    shade: 0,
                    shift: 3,
                    btn: false,
                    time: 2000
                });
            },
            complete: function (XMLHttpRequest, status) { //请求完成后最终执行参数
                if (status == 'timeout') {//超时,status还有success,error等值的情况
                    ajaxTimeoutTest.abort();
                    layer.closeAll();
                    layer.alert('网络不通畅,请重试!', {
                        skin: 'nprs2',
                        title: false,
                        shade: 0,
                        shift: 3,
                        btn: false,
                        time: 2000
                    });
                }
            }
        });
    }
}

function addErr(e, info) {
    var dom = '<p class="setErr">' + info + '</p>';
    $(e).after(dom).addClass('setBorderSty');
}

function removeErr(e) {
    $(e).removeClass('setBorderSty').parents('.nset_ctr').find('.setErr').remove();
}

function removeErrAll(e) {
    $(e).parents('.nset_ctr').find('.setErr').remove();
    $(e).parents('.nset_ctr').find('input').removeClass('setBorderSty');
}

var yzm = null;
function getVerification(e,url) {
    var val = $('[setname="phoneNumber"]').val();
    removeErr('[setname="phoneNumber"]')
    if (val == '') {
        addErr('[setname="phoneNumber"]', '请输入内容');
    } else if (!inputctr.public.isdecimals(Number(val))) {
        addErr('[setname="phoneNumber"]', '手机号码有误!');
    } else {
        timeer(e);
        var ajaxTimeoutTest = $.ajax({
            url: url,  //请求的URL
            timeout: 10000, //超时时间设置，单位毫秒
            type: 'post',  //请求方式，get或post
            data: {
                mobile: val
            },  //请求所传参数，json格式
            success: function (result) { //请求成功的回调函数
                layer.closeAll();
                if (result.result == 1) {
                    yzm = result.code;
                } else {
                    layer.alert(result.msg, {
                        skin: 'nprs2',
                        title: false,
                        shade: 0,
                        shift: 3,
                        btn: false,
                        time: 2000
                    });
                }
            },
            error: function () {
                layer.closeAll();
                layer.alert('请求失败!', {
                    skin: 'nprs2',
                    title: false,
                    shade: 0,
                    shift: 3,
                    btn: false,
                    time: 2000
                });
            },
            complete: function (XMLHttpRequest, status) { //请求完成后最终执行参数
                if (status == 'timeout') {//超时,status还有success,error等值的情况
                    ajaxTimeoutTest.abort();
                    layer.closeAll();
                    layer.alert('网络不通畅,请重试!', {
                        skin: 'nprs2',
                        title: false,
                        shade: 0,
                        shift: 3,
                        btn: false,
                        time: 2000
                    });
                }
            }
        });
    }
}

function timeer(e) {
    var time = 60;
    $(e).prop({
        disabled: true
    });
    var t = setInterval(function () {
        time--;
        $(e).html(time + "秒");
        if (time == 0) {
            clearInterval(t);
            $(e).html("重新获取");
            $(e).prop({
                disabled: false
            });
        }
    }, 1000)
}

function overRun(e,setName) {
    if (setName == 'setCommon') {
        var sObj = $(e).parents('form').find('.nsetOne');
        sObj.find('.RealName').text($('[name="RealName"]').val());
        sObj.find('.Post').text($('[name="Post"]').val());
        layer.alert('修改成功!', {
            skin: 'nprs1',
            title: false,
            shade: 0,
            shift: 3,
            btn: false,
            time: 2000
        });
        $(e).next('button').trigger('click');
    } else if (setName == 'password') {
        layer.alert('设置成功!', {
            skin: 'nprs1',
            title: false,
            shade: 0,
            shift: 3,
            btn: false,
            time: 2000
        });
        $(e).next('button').trigger('click');
    } else if (setName == 'phoneNumber') {
        if ($('.setYzm').val() == yzm) {
            layer.alert('设置成功!', {
                skin: 'nprs1',
                title: false,
                shade: 0,
                shift: 3,
                btn: false,
                time: 2000
            });
            var sObj = $(e).parents('form').find('.nsetOne');
            sObj.find('.nset_p2 b').text($('[name="PhoneNumber"]').val())
            $(e).next('button').trigger('click');
        }
    }
}


$(function () {
    //获取页面地址
    var URL = window.URL || window.webkitURL;
    var $image = $('#imgCropper');
    var uploadedImageURL;
    //裁图插件初始化选项
    var options = {
        aspectRatio: 1,
        preview: '.imgPreview'
    };

    //cropper操作
    $image.on({
        ready: function (e) {
            //console.log(e.type);
        },
        cropstart: function (e) {
            //console.log(e.type, e.action);
        },
        cropmove: function (e) {
            //console.log(e.type, e.action);
        },
        cropend: function (e) {
            //console.log(e.type, e.action);
        },
        crop: function (e) {
            //console.log(e.type, e.x, e.y, e.width, e.height, e.rotate, e.scaleX, e.scaleY);
        },
        zoom: function (e) {
            //console.log(e.type, e.ratio);
        }
    }).cropper(options);

    //上传input DOM
    var $inputImage = $('#inputImage');


    if (URL) {
        $inputImage.change(function () {
            var files = this.files;
            var file;

            if (!$image.data('cropper')) {
                return;
            }

            if (files && files.length) {
                file = files[0];

                if (/^image\/\w+$/.test(file.type)) {
                    if (uploadedImageURL) {
                        URL.revokeObjectURL(uploadedImageURL);
                    }

                    uploadedImageURL = URL.createObjectURL(file);
                    $image.cropper('destroy').attr('src', uploadedImageURL).cropper(options);
                    $(this).parents('.nsetOne').hide().next('.nsetTwo').show();
                    $inputImage.val('');
                } else {
                    layer.alert('请选择一张图片!', {
                        skin: 'nprs2',
                        title: false,
                        shade: 0,
                        shift: 3,
                        btn: false,
                        time: 3000
                    });
                }
            }
        });
    } else {
        $inputImage.prop('disabled', true).parent().addClass('disabled');
    }

    $('[setname]').on('keyup blur', function () {
        removeErr(this);
        var setName = $(this).attr('setname');
        var val = $(this).val();
        if (setName == 'setCommon') {
            if (val == '') {
                addErr(this, '请输入内容');
            }
        } else if (setName == 'password' || setName == 'password3') {
            if (val == '') {
                addErr(this, '请输入内容');
            } else if (val.length < 6) {
                addErr(this, '密码长度至少6位!');
            }
        } else if (setName == 'password2') {
            if (val == '') {
                addErr(this, '请输入内容');
            } else if ($('[setname="password"]').eq(0).val().length < 6) {
                addErr(this, '密码长度至少6位!');
            } else if (val != $('[setname="password"]').eq(0).val()) {
                addErr(this, '两次密码不相同!');
            }
        } else if (setName == 'phoneNumber') {
            if (val == '') {
                addErr(this, '请输入内容');
            } else if (!inputctr.public.ismobile(Number(val))) {
                addErr(this, '手机号码有误!');
            }
        }
    });

})

