//# sourceURL=common.js
(function($){
    "use strict";
    
    $.alert=function (msg,title,cb,opts) {
        if($('div.am-modal-alert')[0]){
            $('div.am-modal-alert').remove();
        }
        var dialog=$('<div class="am-modal am-modal-alert" tabindex="-1"></div>');
        var div=$('<div class="am-modal-dialog"></div>');
        if(title){
            $(div).append('<div class="am-modal-hd">'+title+'</div>');
        }
        $(div).append('<div class="am-modal-bd">'+msg+'</div>');
        var footer=$('<div class="am-modal-footer"></div>');
        var btn=$('<span class="am-modal-btn">确定</span>');
        if($.isFunction(cb)){
            $(btn).bind('click',function(){
                cb();
            });
        }
        $(footer).append(btn);
        $(div).append(footer);
        $(dialog).append($(div));
        $(dialog).appendTo($(window.document.body));
        var opts=opts||{};
        $.extend(opts,{
            closeViaDimmer:true,
            dimmer:true
        });
        $(dialog).modal(opts);
    };
    $.confirm=function (msg,title,okFn,cancelFn) {
        if($('div.am-modal-confirm')[0]){
            $('div.am-modal-confirm').remove();
        }
        var dialog=$('<div class="am-modal am-modal-confirm" tabindex="-1"></div>');
        var div=$('<div class="am-modal-dialog" style="width: 30rem!important;"></div>');
        if(title){
            $(div).append('<div class="am-modal-hd" style="padding: 1rem 0.8rem 0.2rem 0.8rem !important;">'+title+'</div>');
        }
        $(div).append('<div class="am-modal-bd" style="padding: 0.5rem 0.8rem !important;">'+msg+'</div>');
        var footer=$('<div class="am-modal-footer"><span class="am-modal-btn" data-am-modal-cancel>取消</span><span class="am-modal-btn" data-am-modal-confirm>确定</span></div>');
        $(div).append(footer);
        $(dialog).append($(div));
        $(dialog).appendTo($(window.document.body));
        $(dialog).modal({
            relatedTarget: this,
            onConfirm: function(options) {
                if($.isFunction(okFn)){
                    okFn(options);
                }
            },
            closeOnConfirm: true,
            onCancel: function(){
                if($.isFunction(cancelFn)){
                    cancelFn();
                }
            }
        });
    };
    $.fn.message=function (msg,type,cb) {
        var level=['','am-alert-success','am-alert-warning','am-alert-danger','am-alert-secondary'];
        if($('div.am-alert')[0]){
            $('div.am-alert').remove();
        }
        var div=$('<div class="am-alert '+level[type]+'" data-am-alert></div>');
        $(div).append('<button type="button" class="am-close">&times;</button>');
        $(div).append('<p>'+msg+'</p>');
        $(div).appendTo($(this));

    };
    $.jAjax=function (opts) {
        if(!opts && !$.isPlainObject(opts)){
            return;
        }
        var conf={headers:{
            'request-type':'ajax'
        }};

        $.extend(conf,opts);
        conf.success=function (data,textStatus, jqXHR) {
            if(data.message && data.type){
                // $.alert(data.message);
                layer.open({
                    content: data.message
                    // ,style: 'color:#ffffff !important;'
                    ,skin: 'msg'
                    ,time: 5 //2秒后自动关闭
                });
            }else if($.isFunction(opts.success)){
                opts.success(data,textStatus, jqXHR);
            }
        }
        $.ajax(conf);
    }
    $.fmtMoney=function(num){
        num=num.toFixed(2);
        num = num.toString().replace(/\$|,/g,'');
        if(isNaN(num)) num = "0";
        var sign = (num == (num = Math.abs(num)));
        num = Math.floor(num*100+0.50000000001);
        var cents = num%100;
        num = Math.floor(num/100).toString();
        if(cents<10) cents = "0" + cents;
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
            num = num.substring(0,num.length-(4*i+3))+','+
                num.substring(num.length-(4*i+3));
        return (((sign)?'':'-') + num + '.' + cents);
    }

    $.loadingToast=function(id){
        function toast(id){
            id=id?id:'_loadingToast';
            this.show=function(msg,timeout){
                msg=msg?msg:'数据加载中';
                $('#'+id).remove();
                var $loadingToast=$('<div id="'+id+'" style="display:none;">' +
                '<div class="weui-mask_transparent"></div>' +
                '<div class="weui-toast">' +
                '<i class="weui-loading weui-icon_toast"></i>' +
                '<p class="weui-toast__content" style="color: white;">'+msg+'</p>' +
                '</div>' +
                '</div>');
                $('body').append($loadingToast);

                if ($loadingToast.css('display') != 'none') return;
                $loadingToast.fadeIn(100);
                if(timeout>-1){
                    setTimeout(function () {
                        $loadingToast.fadeOut(100);
                    }, timeout);
                }
            };
            this.close=function () {
                $('#'+id).fadeOut(100);
            }
        }
        return new toast(id);
    }

    $.back=function (nowUrl,backUrl) {
        if (window.history && window.history.pushState){//监听返回键
            $(window).on('popstate', function() {
                var hashLocation = location.hash;
                var hashSplit = hashLocation.split("#!/");
                var hashName = hashSplit[1];
                if (hashName !== '') {
                    var hash = window.location.hash;
                    if (hash === '') {
                        window.location.href=backUrl;
                    }
                }
            });
            window.history.pushState('forward', null, nowUrl);
        }
    }

    $.submitFrm=function (url,params,method) {
        if(!url){
            return;
        }
        method=method?'POST':method;
        var frm=$('<form action="'+url+'" method="'+method+'"></form>');
        var nparams=params||{};
        $.each(nparams,function (key,value) {
            var ipt=$('<input type="hidden" name="'+key+'" value="'+value+'">');
            ipt.appendTo(frm);
        })
        frm.submit();
    }
})(jQuery);