//dom加载完成后执行的js
;$(function () {

    //ajax get请求
    $('.ajax-get').click(function () {
        var target;
        var that = this;
        var tishi = $(this).attr("data-tishi");
        if (tishi == '' || !tishi) {
            tishi = "确定删除此项记录？";
        }
        if ($(this).hasClass('confirm')) {
            layer.confirm(tishi, {icon: -1, title: '提示'}, function (index) {
                //do something
                layer.load(1);
                if ((target = $(that).attr('href')) || (target = $(that).attr('url'))) {
                    // updateAlert('执行中，请稍后。。。','alert-success');
                    $.get(target).success(function (data) {
                        if (data.code == 1) {
                            layui.layer.msg(data.msg, {
                                offset: '50%'
                                , icon: 1
                                , time: 1000
                            }, function () {
                                layer.closeAll();

                                if (data.url) {
                                    location.href = data.url;
                                } else {
                                    window.location.reload();
                                }
                            });

                        } else {
                            layui.layer.msg(data.msg, {
                                offset: '50%'
                                , icon: 2
                                , time: 1000
                            }, function () {
                                layer.closeAll();
                                if (data.url) {
                                    location.href = data.url;
                                } else {
                                    window.location.reload();
                                }
                            });

                        }
                    });

                }

            });
        } else {
            if ((target = $(that).attr('href')) || (target = $(that).attr('url'))) {
                layer.load(1);
                $.get(target).success(function (data) {
                    if (data.code == 1) {
                        layui.layer.msg(data.msg, {
                            offset: '50%'
                            , icon: 1
                            , time: 1000
                        }, function () {
                            layer.closeAll();

                            if (data.url) {
                                location.href = data.url;
                            } else {
                                window.location.reload();
                            }
                        });

                    } else {
                        layui.layer.msg(data.msg, {
                            offset: '50%'
                            , icon: 2
                            , time: 1000
                        }, function () {
                            layer.closeAll();
                            if (data.url) {
                                location.href = data.url;
                            } else {
                                window.location.reload();
                            }
                        });

                    }
                });

            }
        }

        return false;
    });

    //ajax post submit请求
    $('.ajax-post').click(function () {
        var target, query, form;
        var target_form = $(this).attr('target-form');
        var that = this;
        var nead_confirm=false;


            form = $('.'+target_form);
           if ( form.get(0).nodeName=='FORM' ){

                if($(this).attr('url') !== undefined){
                    target = $(this).attr('url');
                }else{
                    target = form.get(0).action;
                }
                query = form.serialize();
            }else if( form.get(0).nodeName=='INPUT' || form.get(0).nodeName=='SELECT' || form.get(0).nodeName=='TEXTAREA') {

                form.each(function(k,v){
                    if(v.type=='checkbox' && v.checked==true){
                        nead_confirm = true;
                    }
                })
               target = $(this).attr('url');
                query = form.serialize();
            }else{
               target = $(this).attr('url');
               query = form.find('input,select,textarea').serialize();
           }

        layer.load(1);
        $.post(target, query).success(function (data) {
            if (data.code == 1) {

                    layui.layer.msg(data.msg, {
                        offset: '50%'
                        , icon: 1
                        , time: 1000
                    }, function () {
                        layer.closeAll();

                        if (data.url) {
                            location.href = data.url;
                        } else {
                            window.location.reload();
                        }
                    });

            } else {

                layui.layer.msg(data.msg, {
                    offset: '50%'
                    , icon: 2
                    , time: 1000
                }, function () {
                    layer.closeAll();
                    if (data.url) {
                        location.href = data.url;
                    } else {
                        window.location.reload();
                    }
                });


            }
        });


        return false;
    });



});

/* 上传图片预览弹出层 */
