/**
 * Created by helly on 2019/2/28.
 */
document.write("<script language=javascript src='"+HY.siteUrl+"/static/common/js/layer/layer.js'></script>");

//公共类
var libs = {
    reloadPage: function(win) {
        var location = win.location;
        location.href = location.pathname + location.search;
    },
    redirect: function (url) {
        location.href = url;
    },
    back:function (e) {
        history.back();
    }
};

var HYLayer={
    alert: function (content, reFunction, btnText, title) {
        btnText = btnText || 'OK';
        title = title || 'MessageBox';
        layer.alert(content, { closeBtn: 1, btn: [btnText], title: title, resize: false })
    },
    showLoading: function (time, style) {
        time = time || 0;
        style = style || 0;
        return layer.load(style, { shade: [0.6, '#ccc'], time: time });
    },
    closeLoading: function () {
        layer.closeAll('loading');
    },
    //弹出提示 icon类型 0:带！的提示 1:√ 2:x 3:? 4:锁 5:难过脸 6:微笑脸
    showTips: function (content, icon, time, callback) {
        icon = icon || 0;
        time = time || 3000;
        layer.msg(content, {
            icon: icon,
            time: time,
            resize: false,
            shade: 0.5,
            shadeClose: true
        }, function () {
            //do something
            if (callback) {
                callback();
            }
        });
    },

    //JS confirm
    confirm: function (content, sureFunction, title, sureText, closeText, closeFunction) {
        title = title || "";
        sureText = sureText || "Sure";
        closeText = closeText || "Cancel";

        layer.confirm(
            content, { title: title, icon: 3, btn: [sureText, closeText], resize: false },
            function (index) {//确定回调
                if (sureFunction) {
                    sureFunction();
                }
                layer.close(index);
            }, function (index) {//cancel回调
                if (closeFunction) {
                    closeFunction();
                }
                layer.close(index);
            });
    },

    openDialog: function (content, title, width, height) {
        title = title || "";
        width = width || 500;
        height = height || 360;

        var offset = ((window.screen.height - height) * 0.3) + 'px';

        return layer.open({
            title: title
            , content: content
            , type: 1
            , offset: offset
            , shade: 0.5
            , resize: false
            , area: [width + 'px', height + 'px']
        });
    },

    openIframe: function (url, title, width, height, refresh) {
        title = title || "Dialog";
        width = width || 500;
        height = height || 400;
        refresh = refresh || 1;
        var offset = ((window.screen.height - height) * 0.3) + 'px';

        return layer.open({
            title: title
            , content: url
            , type: 2
            , offset: offset
            , shade: 0.5 // 遮罩层透明度
            , resize: false
            , area: [width + 'px', height + 'px'],
            end: function () {
                if(refresh==1){
                    location.reload();
                }
            }
        });
    }
};

/**
 * formsubmit
 * @param subBtn
 */
function formSubmit(subBtn){
    var btn = subBtn,
        text = btn.text(),
        form = btn.parents('form.ajaxForm');
    form.ajaxSubmit({
        url: btn.data('action') ? btn.data('action') : form.attr('action'),
        dataType: 'json',
        beforeSubmit: function () {
            btn.text(text + '..').prop('disabled', true);
            HYLayer.showLoading();
        },
        success: function (data) {
            btn.removeClass('disabled').text(text.replace('..', '')).parent().find('span').remove();
            HYLayer.closeLoading();
            if(data.status == undefined){
                HYLayer.alert(data.info);
            }
            if (data.status == 1) {
                if(data.isclose){//if iframe
                    var index = parent.layer.getFrameIndex(window.name);
                    setTimeout(function(){parent.layer.close(index)}, 1000);
                }else{
                    if(data.url){
                        HYLayer.showTips(data.info,1,3000,function () {
                            location.href = data.url;
                        })
                    }else{
                        HYLayer.showTips(data.info,1,3000,function () {
                            libs.reloadPage(window);
                        })
                    }
                }
            } else if (data.status == 0) {
                if(data.url){
                    HYLayer.showTips(data.info,2,2000,function () {
                        location.href = data.url;
                    })
                }else{
                    HYLayer.showTips(data.info,2);
                }
                btn.removeProp('disabled').removeAttr('disabled');
            }
        }
    });
}

/**
 * AJAX POST
 * @param postArr
 * @param url
 */
function ajaxPost(postArr,url,optObj) {
    $.ajax({
        type:"POST",
        url:url,
        contentType: "application/json; charset=utf-8",
        dataType:"json",
        data:JSON.stringify(postArr),//你要提交是json字符串
        beforeSend: function () {
            if(optObj){
                $("#"+optObj).attr({ disabled: "disabled" });
            }
            HYLayer.showLoading();
        },
        success:function (data) {
            if(data.status == undefined){
                HYLayer.showTips(data.info,2);
            }
            if (data.status == 1) {
                if(data.url && data.info){
                    HYLayer.showTips(data.info,1,3000,function () {
                        top.location.href = data.url;
                    })
                }
                if(data.info!==undefined && data.url==undefined){
                    HYLayer.showTips(data.info,1,3000,function () {
                        libs.reloadPage(window);
                    })
                }
                if(data.info==undefined && data.url!==undefined){
                    top.location.href = data.url;
                }
                if(data.info==undefined && data.url==undefined){
                    libs.reloadPage(window);
                }
            } else if (data.status == 0) {
                HYLayer.showTips(data.info,2);
            }
        },
        complete: function () {
            if(optObj){
                $("#"+optObj).removeAttr("disabled");
            }
            HYLayer.closeLoading();
        },
        error: function (data) {
            HYLayer.showTips(data.responseText,2);
        }
    })
}

/**
 * AJAX Request
 * @param postArr
 * @param url
 */
function ajaxRequest(postArr,url,async) {
    var status,returnData;
    async = async || false;
    $.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        async: async,
        data: postArr,
        beforeSend: function () {
            HYLayer.showLoading();
        },
        success: function (data) {
            if (data.status == 1) {
                status = true;
                returnData = data;
            } else {
                if (data.url) {
                    location.href = data.url;
                } else {
                    HYLayer.showTips(data.info, 2);
                }
            }
        },
        complete: function () {
            HYLayer.closeLoading();
        }
    });
    if (status)return returnData;
}

/**
 * ajaxlink
 * @param url
 */
function ajaxLink(url) {
    $.ajax({
        type: "GET",
        url: url,
        dataType: "json",
        beforeSend: function () {
            HYLayer.showLoading();
        },
        success: function (data) {
            if(data.status==1){
                if(data.info){
                    HYLayer.showTips(data.info,1,2000,function () {
                        location.href = data.url;
                    })
                }else{
                    libs.reloadPage(window);
                }
            }else{
                HYLayer.showTips(data.info, 2);
            }
        },
        complete: function () {
            HYLayer.closeLoading();
        }
    });
}


/**
 * chosen ajax select
 * @param sel
 * @param selName
 * @param url
 */
function selectLocation(sel,selName,url){
    jQuery("#"+selName+" option").each(function(){
        jQuery(this).remove();
    });
    jQuery("<option value=0>Please select</option>").appendTo(jQuery("#"+selName));
    if(jQuery("#"+sel).val()==0){
        return;
    }
    $("#"+selName).chosen("destroy");
    jQuery.getJSON(url,{pid:jQuery("#"+sel).val()},
        function(data){
            $("#telcode").val('+'+data.telcode)
            $("#telcodeshow").html('+'+data.telcode)
            if(data.clist){
                $.each(data.clist,function(idx,item){
                    $("<option value='"+item.id+"'>"+item.cname+"</option>").appendTo("#"+selName);
                });
                $("#"+selName).chosen();
            }else{
                $("#"+selName).chosen();
                jQuery("<option value='0'>Please select</option>").appendTo(jQuery("#"+selName));
            }
        }
    );
}


/**
 * 公共的JQUERY操作
 */
;(function () {
    $("#topNote").on('click',function () {
        $(this).parent().parent().hide();
    });

    //ajaxLink操作
    $(".ajaxLink").on('click',function (e) {
        e.preventDefault();
        var url=$(this).attr('data-url');
        var _msg=$(this).attr('data-msg');
        var _status=false;
        if(_msg){
            HYLayer.confirm(_msg,function () {
                _status=true;
                ajaxLink(url)
            });
        }else{
            ajaxLink(url)
        }
    });
    
    $("#currencySet>ul>li>a").on('click',function () {
        var _cid=$(this).data('id');
        ajaxLink(HY.siteUrl+'/do/changCurrency?id='+_cid);
    })

})(jQuery);