$(function() {
    // 菜单
    $(".header .nav li").mouseover(function (e) {
        $(".header .nav li").removeClass("on");
        $(this).addClass("on");
        $(".header .nav .on .subnav").show();
    });
    $(".header .nav li").mouseout(function(){
        $(".subnav").hide();
    });
    // 侧边类容管理
    $(".cguanli").click(function(){
        $(".cguanli").removeClass("curr");
        $(this).addClass("curr");
        $(".cguanli ul").hide(100);
        $(".curr ul").show(100);
    });
    layui.use('layer', function(){
    var layer = layui.layer;
        // 弹出发布内容
        $('.k_add').on('click', function(){
            var data_url = $(this).attr('data_url');
            var title = $(this).attr('title');
            layer.open({
                type: 2,
                title: title,
                shadeClose: true,
                shade: 0.8,
                area: ['1080px', '90%'],
                content: data_url
            }); 
        });
        // 弹出置顶内容
        $('.k_settop').on('click', function(){
            var data_url = $(this).attr('data_url');
            var title = $(this).attr('title');
            layer.confirm(title, {
                title: '置顶',
                skin: 'layui-layer-rim',
                btn: ['确定','取消']
            }, function(){
                $.ajax({
                    url: data_url,
                    type: "GET",
                    dataType: "json",
                    success: function(data) {
                        loading = layer.load(3);
                        if (data.code == 1) {
                            layer.close(loading);
                            layer.msg(data.msg, {
                                icon: 1,
                                time: 1000
                            },function() {
                                location.reload();
                            });
                        } else {
                            layer.close(loading);
                            layer.msg(data.msg, {
                                icon: 2,
                                anim: 6,
                                time: 1000
                            });
                        }
                    }
                });
            }, function(){
                layer.msg('将失去曝光率');
            });
        });
        // 弹出触发内容
        $('.k_t').on('click', function(){
            var data_url = $(this).attr('data_url');
            var title = $(this).attr('title');
            layer.confirm(title, {
                title: '提示',
                skin: 'layui-layer-rim',
                btn: ['确定','取消']
            }, function(){
                $.ajax({
                    url: data_url,
                    type: "POST",
                    dataType: "json",
                    success: function(data) {
                        loading = layer.load(3);
                        if (data.code == 1) {
                            layer.close(loading);
                            layer.msg(data.msg, {
                                icon: 1,
                                time: 1000
                            },function() {
                                location.reload();
                            });
                        } else {
                            layer.close(loading);
                            layer.msg(data.msg, {
                                icon: 2,
                                anim: 6,
                                time: 1000
                            });
                        }
                    }
                });
            });
        });
        // 弹出发布商家
        $('.k_adds').on('click', function(){
            var data_url = $(this).attr('data_url');
            var title = $(this).attr('title');
            layer.open({
                type: 2,
                title: title,
                shadeClose: true,
                shade: 0.8,
                area: ['1080px', '90%'],
                content: data_url
            }); 
        });
        //弹出触发链接弹出
        $('.del_t').on('click', function(){
            var name = $(this).attr('title');
            var url = $(this).attr('data_url');
            layer.confirm(name,
            function(index) {
                loading = layer.load(2, {
                    shade: [0.2, '#000']
                });
                $.ajax({
                    url: url,
                    type: "GET",
                    dataType: "json",
                    success: function(data) {
                        loading = layer.load(3);
                        if (data.code == 1) {
                            layer.close(loading);
                            layer.msg(data.msg, {
                                icon: 1,
                                time: 1000
                            },function() {
                                location.reload();
                            });
                        } else {
                            layer.close(loading);
                            layer.msg(data.msg, {
                                icon: 2,
                                anim: 6,
                                time: 1000
                            });
                        }
                    }
                });
            });
        });
    }); 
    // 签到
    $(".k_qiandao").click(function(){
        var data_url = $(this).attr('data_url');
        $.ajax({
            url: data_url,
            type: "GET",
            dataType: "json",
            success: function(data) {
                loading = layer.load(3);
                if (data.code == 1) {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 1,
                        time: 1000
                    },function() {
                        location.reload();
                    });
                } else {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 2,
                        anim: 6,
                        time: 1000
                    });
                }
            }
        });
    });   

    layui.use(['form', 'upload', 'jquery'],
    function() {
        var form = layui.form,
        upload = layui.upload,
        jq = layui.jquery;
        // 自定义金额充值
        form.on('submit(cz)',
        function(data) {
            loading = layer.load(2, {
                shade: [0.2, '#000']
            });
            var param = data.field;
            jq.post('/index.php/index/alipay/paysubmit.html', param,
              function(data) {
                if (data.code == 1) {
                    layer.close(loading);
                    layer.msg(data.msg, {icon: 1, time: 1000}, function(){
                       $('.main').append(data.url);
                     });
                } else {
                    layer.close(loading);
                    layer.msg(data.msg, {icon: 2, anim: 2, time: 1000});
                }
              });
            return false;
        });
        // 通用表单提交
        form.on('submit(cms)',
        function(data) {
            loading = layer.load(2, {
                shade: [0.2, '#000']
            });
            var urls = jq('.form').attr('url');
            var t_url = jq('.form').attr('t_url');
            var param = data.field;
            jq.post(urls, param,
            function(data) {
                if (data.code == 1) {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 1,
                        time: 1000
                    },
                    function() {
                        location.href = t_url;
                    });
                } else {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 2,
                        anim: 6,
                        time: 1000
                    });
                }
            });
            return false;
        });
        //上传图片1
        upload.render({
            elem: '.upimg',
            url: "/index.php/admin/up/up.html",
            accept: 'file',
            //size: 100,
            exts: 'jpg|png|gif|bmp|jpeg',
            done: function(res) {
                if (res.code == '1') {
                    var up = jq('.upimg').attr('up');
                    jq('input[id=' + up + ']').val(res.path);
                    return layer.msg(res.msg);
                } else {
                    return layer.msg(res.msg);
                }
            }
        });
        //上传图片2
        upload.render({
            elem: '.upimg1',
            url: "/index.php/admin/up/up.html",
            accept: 'file',
            //size: 100,
            exts: 'jpg|png|gif|bmp|jpeg',
            done: function(res) {
                if (res.code == '1') {
                    var up = jq('.upimg1').attr('up');
                    jq('input[id=' + up + ']').val(res.path);
                    return layer.msg(res.msg);
                } else {
                    return layer.msg(res.msg);
                }
            }
        });
        // 触发无弹窗
        jq('.k_fno').click(function() {
            var data_url = jq(this).attr('data_url');
            loading = layer.load(2, {
                shade: [0.2, '#000']
            });
            jq.post(data_url,
            function(data) {
                if (data.code == 1) {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 1,
                        time: 1000
                    },
                    function() {
                        location.reload();
                    });
                } else {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 2,
                        anim: 6,
                        time: 1000
                    });
                }
            });
        });
        // 发送验证码
        jq('#phonecode').click(function() {
            var obj = $(this);
            var options = {
                seconds: 60,
                type: 'REG'
            };
            var name = $("#phone").val();　
            var t_url = $(this).attr('t_url');
            if (name == "") {　　　
                layer.msg('手机号码必填');　　　　
                return false;　　
            } 
            loading = layer.load(0, {
                shade: [0.2, '#000']
            });
            jq.getJSON(t_url+'?phone=' + name,
            function(data) {
                if (data.code == 1) {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 1,
                        time: 1000
                    },
                    function() {
                        countDown(60, obj);
                    });
                } else {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 2,
                        anim: 6,
                        time: 1000
                    });
                }
            });
        });
        // 弹出提示
        jq('.k_tishi').click(function() {
            var name = $(this).attr('name');
            layer.confirm(name, {
              btn: ['了解']
            }, function(){
              layer.msg('感谢关注', {icon: 1});
            });
        });
    });
    // 固定快捷充值
    $(".k_cz").click(function(){
        var data_url = $(this).attr('data_url');
        $.ajax({
            url: data_url,
            type: "GET",
            dataType: "json",
            success: function(data) {
                loading = layer.load(3);
                if (data.code == 1) {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 1,
                        time: 1000
                    },function() {
                        $('.main').append(data.url);
                    });
                } else {
                    layer.close(loading);
                    layer.msg(data.msg, {
                        icon: 2,
                        anim: 6,
                        time: 1000
                    });
                }
            }
        });
    });  
    // 发送验证码
    function countDown(seconds, obj) {
        if (seconds > 1) {
            seconds--;
            $(obj).attr('disabled', true).html(seconds + "秒"); //禁用按钮
            setTimeout(function() {
                countDown(seconds, obj);
            },
            1000);
        } else {
            $(obj).attr('disabled', false).html("获取验证码"); //启用按钮
        }
    } 
    //弹出发送信息
    $(".k_f").click(function() {
        var data_url = $(this).attr('data_url');
        var title = $(this).attr('title');
        layer.prompt({
            title: title,
            formType: 2
        },
        function(text, index) {
            $.ajax({
                url: data_url+"?config=" + text,
                type: "GET",
                dataType: "json",
                success: function(data) {
                    loading = layer.load(3);
                    if (data.code == 1) {
                        layer.close(loading);
                        layer.msg(data.msg, {
                            icon: 1,
                            time: 1000
                        },
                        function() {
                            location.reload();
                        });
                    } else {
                        layer.close(loading);
                        layer.msg(data.msg, {
                            icon: 2,
                            anim: 6,
                            time: 1000
                        });
                    }
                }
            });
        });
    });
});
