/**
 * Created by A.J on 2017/8/29.
 */
layui.use(['form', 'element', 'layer', 'layedit'], function() {
    var element = layui.element,
        form = layui.form,
        $ = layui.jquery,
        layer = layui.layer,
        layedit = layui.layedit;
    var index = layedit.build('pinglunk', {
        tool: ['strong', 'italic', 'underline', 'del', '|', 'left', 'center', 'right', 'link', 'unlink', 'face'],
        height: 200
    });
    var text = '',
        title = '';
    var active = {
        offset: function(othis) {
            var type = othis.data('type');
            layer.open({
                type: 1,
                offset: type,
                id: 'LAY_demo' + type,
                title: title,
                content: '<div style="padding: 20px 40px 20px 0px;">' + text + '</div>',
                btn: '',
                btnAlign: 'c',
                shade: 0,
                yes: function() {
                    layer.closeAll()
                }
            })
        }
    };
    $('.liuyan').on('click', function() {
        if ($("#catfish").length > 0) {
            text = $("#catfishliuyandiv").html();
            title = $("#liuyanbt").text();
            var othis = $(this),
                method = othis.data('method');
            active[method] ? active[method].call(this, othis) : ''
        }
    });
    form.on('submit(formliuyan)', function(data) {
        if ($("#catfish").length > 0) {
            $(".liuyanload").removeClass("layui-hide");
            $(".liuyanok").addClass("layui-hide");
            $.post($("#webroot").text() + "index/Index/liuyan", data.field, function(redata) {
                $(".liuyanload").addClass("layui-hide");
                if (redata == 'ok') {
                    $(".catfishliuyantishi").text('').addClass("layui-hide");
                    $(".liuyanok").removeClass("layui-hide")
                } else {
                    $(".catfishliuyantishi").text(redata).removeClass("layui-hide")
                }
            })
        }
        return false
    });
    form.on('submit(search)', function(data) {});
    if ($("#catfish").length <= 0) {
        $('.page_left_list').removeClass('page_left_list');
        $('.page_right_list').removeClass('page_right_list')
    }
    var zan = false;
    $("#zan").click(function() {
        if (zan == false && $("#catfish").length > 0) {
            $.post($("#webroot").text() + "index/Index/zan", {
                id: $(this).prev().val()
            }, function(data) {
                $("#zanshu").text(parseInt($("#zanshu").text()) + 1);
                zan = true
            })
        } else {
            layer.alert($("#zanyici").text())
        }
    });
    $("#shoucang").click(function() {
        if ($("#yishoucang").hasClass("layui-hide") && $("#catfish").length > 0) {
            $.post($("#webroot").text() + "index/Index/shoucang", {
                id: $(this).parent().parent().children("input:first").val()
            }, function(data) {
                $("#yishoucang").removeClass("layui-hide")
            })
        } else {
            layer.alert($("#yijingsc").text())
        }
    });
    form.on('submit(tijiaopl)', function(data) {
        if ($("#pinglun").children("span:eq(1)").hasClass("layui-hide") && $("#catfish").length > 0) {
            var str = layedit.getContent(index);
            if ($.trim(str) != '') {
                $("#pinglun").children("span:eq(0)").removeClass("layui-hide");
                $("#pinglun").children("span:eq(1)").addClass("layui-hide");
                $.post($("#webroot").text() + "index/Index/pinglun", {
                    id: $(this).prev().val(),
                    pinglun: str
                }, function(redata) {
                    if(redata == "ok"){
                        $("#pinglun").children("span:eq(0)").addClass("layui-hide");
                        $("#pinglun").children("span:eq(1)").removeClass("layui-hide");
                        layer.alert("评论成功！");
                        location.reload();
                    }
                    else if(redata == "prohibit"){
                        $("#pinglun").children("span:eq(0)").addClass("layui-hide");
                        layer.alert("您的评论内容有违禁词，请调整后提交");
                    }
                    else if(redata == "wait"){
                        $("#pinglun").children("span:eq(0)").addClass("layui-hide");
                        $("#pinglun").children("span:eq(1)").removeClass("layui-hide");
                        layer.alert("您已经提交评论，您的评论要审核通过后显示");
                    }
                })
            } else {
                layer.alert($("#meipinglun").text())
            }
        } else {
            layer.alert($("#yipinglun").text())
        }
        return false
    });
    $(".huifupinglun").on('click', function(){
        if ($("#catfish").length > 0){
            var obj = $(this).parent().parent().parent().next();
            if(obj.hasClass('layui-hide')){
                obj.removeClass('layui-hide');
            }
            else{
                obj.addClass('layui-hide');
            }
        }
    });
    form.on('submit(huifupl)', function(data) {
        if ($(this).children("span:eq(1)").hasClass("layui-hide") && $("#catfish").length > 0) {
            var obj = $(this);
            var str = $(this).parent().prev().val();
            if ($.trim(str) != '') {
                obj.children("span:eq(0)").removeClass("layui-hide");
                obj.children("span:eq(1)").addClass("layui-hide");
                $.post($("#webroot").text() + "index/Index/pinglun", {
                    id: $(this).prev().prev().val(),
                    pid: $(this).prev().val(),
                    pinglun: str
                }, function(redata) {
                    if(redata == "ok"){
                        obj.children("span:eq(0)").addClass("layui-hide");
                        obj.children("span:eq(1)").removeClass("layui-hide");
                        layer.alert("回复成功！");
                        location.reload();
                    }
                    else if(redata == "prohibit"){
                        obj.children("span:eq(0)").addClass("layui-hide");
                        layer.alert("您的回复内容有违禁词，请调整后提交");
                    }
                    else if(redata == "wait"){
                        obj.children("span:eq(0)").addClass("layui-hide");
                        obj.children("span:eq(1)").removeClass("layui-hide");
                        layer.alert("您已经提交回复，您的回复内容要审核通过后显示");
                    }
                })
            } else {
                layer.alert($("#meipinglun").text())
            }
        } else {
            layer.alert($("#yipinglun").text())
        }
        return false
    });
});