/**
 * [OElove] (C)2010-2099 oelove.com Inc.biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2020-11-17 by BO $
*/
$(function () {
    function Dynamic () {
        this.page = 1;
        this.zan();
        this.loadComment();
        this.moreComment();
        this.submitComment();
        this.publish();
        this.verify();
    }
    Dynamic.prototype = {
        zan: function () {
            var _this = this;
            $(document).on("click", "[f='zan_btn']", function () {
                var _ele = $(this);
                var dyid = _ele.attr("data-dyid");
                $.ajax({
                    type: "POST",
                    url: _ROOT_PATH + "index.php?c=dynamic&a=zans",
                    data: {
                        id: dyid
                    },
                    dataType: "json",
                    success: function (data) {
                        if (data.response == 1) {
                            var num = _ele.find("span").html();
                            if (_ele.hasClass("color_red")) {
                                _ele.find("span").html(parseInt(num) - 1);
                                _ele.removeClass("color_red");
                            } else {
                                _ele.find("span").html(parseInt(num) + 1);
                                _ele.addClass("color_red");
                            }
                        } else {
                            if (data.result) {
                                OEUI.message.msg({text: data.result});
                            } else {
                                OEUI.message.msg({text: '网络繁忙，请稍后再试'});
                            }
                        }
                    },
                    error: function () {
                        OEUI.message.msg({text: '网络繁忙，请稍后再试'});
                    }
                });
            })
        },
        loadComment: function () {
            var _this = this;
            var dyid = $("#dyid").val();
            $.ajax({
                type: "GET",
                url: _ROOT_PATH + "index.php?c=dynamic&a=comm",
                cache: false,
                data: {
                    dyid: dyid, page: _this.page, datatype: 'json'
                },
                dataType: "json",
                success: function(data) {
                    var nextpage = data.nextpage; //下一页
                    var result = data.result;
                    _this.page = nextpage; //重新赋值
                    if (result.length > 0) {
                        $("#list").append(result);
                        if (nextpage > 0) { //还有下一页
                            $("#comment_btn_more").show();
                        }
                        else { //没有下一页
                            $("#comment_btn_more").hide();
                        }
                        $("#comment_btn_loading").hide();
                    }
                },
                error: function() {

                }
            });
        },
        moreComment: function () {
            var _this = this;
            $('#comment_btn_more').on('click', function () {
                $('#comment_btn_loading').show();
                _this.loadComment();
            });
        },
        submitComment: function () {
            var is_http = false;
            $('#submit_comment_btn').on('click', function () {
                var el = this;
                var content = $('#comm_content').val();
                var verify = $('#verify').val();
                if (content == '') {
                    OEUI.message.msg({text: '请输入评论内容'});
                    return;
                }
                if (verify == '') {
                    OEUI.message.msg({text: '请输入验证码'});
                    return;
                }
                var dyid = $('#dyid').val();
                if (is_http) return;
                is_http = true;

                var timer, loading;
                timer = setTimeout(function () {
                    loading = OUEI.loading({
                        el: el
                    });
                    loading.show();
                }, 200);

                $.ajax({
                    type: "POST",
                    url: _ROOT_PATH + "index.php?c=dynamic",
                    cache: false,
                    data: {
                        a: "savecomm", dyid: dyid, content: content, checkcode: verify
                    },
                    dataType: "json",
                    success: function (data) {
                        is_http = false;
                        loading && loading.remove();
                        clearTimeout(timer);
                        timer = null;

                        var response = data.response;
                        var result = data.result;
                        if (response == '1') {
                            $("#comm_content").val("");
                            $("#verify").val("");
                            if (result.length>0) {
                                OEUI.message.msg({text: "评论成功"});
                                setTimeout(function () {
                                    location.reload();
                                }, 800);
                            }
                        } else {
                              if (result) {
                                  OEUI.message.msg({text: result});
                              } else {
                                  OEUI.message.msg({text: "网络繁忙，请稍后再试"});
                              }
                        }
                    },
                    error: function () {
                        is_http = false;
                        loading && loading.remove();
                        clearTimeout(timer);
                        timer = null;

                        OEUI.message.msg({text: "网络繁忙，请稍后再试"});
                    }
                });
            });
        },
        publish: function () {
            $('[f="publish"]').on('click', function () {
                OEUI.modal.dialog({
                    type: "iframe",
                    title: "发布动态",
                    width: 650,
                    height: 400,
                    content: _ROOT_PATH+"index.php?c=dynamic&a=add"
                });
            });
        },
        verify: function () {
            $('#verify_img').on('click', function () {
                $(this).attr('src', _ROOT_PATH +'source/include/validcode.php?type=number&width=65&height=30&num=5&r='+ parseInt(Math.random()*100));
            });
        }
    }
    new Dynamic();
});