/**
 * [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 DynamicList() {
        this.condition = {
            s_age1: $("#s_age1").val(),
            s_age2: $("#s_age2").val(),
            s_gender: $("[name='s_gender'][checked='checked']").val(),
            s_dist1: $("#s_dist1").val(),
            s_dist2: $("#s_dist2").val(),
            s_dist3: $("#s_dist3").val(),
            s_marry: $("#s_marry").val(),
            s_height1: $("#s_height1").val(),
            s_height2: $("#s_height2").val(),
            s_education: $("#s_edu").val(),
            s_salary: $("#s_salary").val(),
            page: 1
        };
        this.doZan();
        this.filterBtn();
        this.applyFriend();
        this.releaseDynamic();
        this.myDynamic();
        this.del();
    }
    DynamicList.prototype = {
        //建立url链接
        buildUrl: function () {
            var url = "/index.php?c=dynamic&a=list";
            for (let key in this.condition) {
                if (key == "s_gender") {
                    this.condition[key] = $("[name='s_gender'][checked='checked']").val();
                } else if (key == "page") {
                    continue;
                }
                else {
                    this.condition[key] = $("#"+key).val();
                }
                if (this.condition[key]) {
                    url += "&" + key + "=" + this.condition[key];
                }
            }
            return url;
        },
        //搜索按钮
        filterBtn: function () {
            var _this = this;
            $("#filter_btn").on("click", function () {
                var url = _this.buildUrl();
                location.href = url;
            })
        },
        //点赞动态
        doZan: function () {
            var _this = this;
            $(document).on("click", "[f='zan_btn']", function () {
                if (_LOGIN_STATUS == 0) {
                    location.href = _ROOT_PATH + "index.php?c=passport&a=login";
                }
                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 {

                        }

                    }
                })
            })
        },
        //加关注
        applyFriend: function () {
            var _this = this;
            $(document).on("click", "[f='follow']", function () {
                if (_LOGIN_STATUS == 0) {
                    location.href = _ROOT_PATH + "index.php?c=passport&a=login"
                    return ;
                }
                var target = $(this);
                var el = this;
                var id = target.attr('data-id');
                var follow = target.attr('data-follow');
                var url = '';
                if (follow == '0') {
                    url = _ROOT_PATH + 'index.php?c=home&a=addlisten';
                } else {
                    url = _ROOT_PATH + 'index.php?c=home&a=dellisten';
                }

                var loading, timer;
                timer = setTimeout(function () {
                    loading = OEUI.loading({
                        el: el,
                        color: '#666'
                    });
                    loading.show();
                }, 200);

                $.ajax({
                    type: 'POST',
                    url: url,
                    data: {
                        id:id
                    },
                    success: function (json) {
                        clearTimeout(timer);
                        timer = null;
                        loading &&　loading.remove();
                        var res = JSON.parse(json);
                        if (res.response == '1') {
                            if (follow == '0') {
                                target.attr('data-follow', '1').addClass('bo_eb color_9').removeClass('color_main bo_main').html('已关注');
                            } else {
                                target.attr('data-follow', '0').removeClass('bo_eb color_9').addClass('color_main bo_main').html('加关注');
                            }
                        } else {
                            if (res.result) {
                                OEUI.message.msg({text: res.result});
                            } else {
                                OEUI.message.msg({text: '网络繁忙，请稍后再试'});
                            }
                        }
                    },
                    error: function () {
                        clearTimeout(timer);
                        timer = null;
                        loading &&　loading.remove();
                        OEUI.message.msg({text: '网络繁忙，请稍后再试'});
                    }
                });
            })
        },
        //发布动态
        releaseDynamic: function () {
            $("#release_btn").on("click", function () {
                if (_LOGIN_STATUS == 0) {
                    location.href = _ROOT_PATH + "index.php?c=passport&a=login"
                    return ;
                }
                OEUI.modal.dialog({
                    type: "iframe",
                    title: "发布动态",
                    width: 650,
                    height: 400,
                    content: _ROOT_PATH+"index.php?c=dynamic&a=add"
                });
            });
        },
        //我的动态
        myDynamic: function () {
            $(document).on("click", "[f='my_dynamic']", function () {
                if (_LOGIN_STATUS == 0) {
                    location.href = _ROOT_PATH + "index.php?c=passport&a=login"
                    return;
                }
                location.href = _ROOT_PATH + "index.php?c=dynamic&a=my"
            })
        },
        //删除
        del: function () {
            var is_http = false;
            $('[f="del"]').on('click', function () {
                var id = $(this).attr('data-dyid');
                if (is_http) return;
                is_http = true;
                var el = this;
                OEUI.modal.info({
                    title: '',
                    text: '是否该条删除动态？',
                    confirm: function () {
                        var timer, loading;
                        timer = setTimeout(function() {
                            loading = OEUI.loading({
                                el: el,
                                color: '#999'
                            });
                            loading.show();
                        }, 200);
                        $.ajax({
                            type: "POST",
                            url: _ROOT_PATH+"index.php?c=dynamic",
                            data: {
                                a:"del", id:id
                            },
                            success: function(json) {
                                is_http = false;
                                loading && loading.remove();
                                clearTimeout(timer);
                                timer = null;
                                var res = JSON.parse(json);
                                if (res.response == '1') {
                                    OEUI.message.msg({text: '删除成功'});
                                    setTimeout(function(){
                                        window.location.reload();
                                    }, 500);
                                }
                                else {
                                    if (res.result) {
                                        OEUI.message.msg({text: res.result});
                                    }
                                    else {
                                        OEUI.message.msg({text: '删除失败'});
                                    }
                                }
                            },
                            error: function() {
                                is_http = false;
                                loading && loading.remove();
                                clearTimeout(timer);
                                timer = null;
                                OEUI.message.msg({text: '系统繁忙，请稍后再试'});
                            }
                        });
                    },
                    cancel: function () {
                        is_http = false;
                    }
                });
            });
        }
    };
    new DynamicList();
    



    /*-------------------- 搜索 begin -------------------*/
	//筛选 类型
	$(document).on("click", "[f='but_ser_type']", function(){
		$_id = $(this).attr("data-id");
		$("#s_type").val($_id);
		$("[f='but_ser_type']").removeClass("current");
		$(this).addClass("current");
		$("#dynamic_form").submit();
	});

    //发表评论 弹出iframe 8.05.05
    $(document).on("click", "[f='pop2_dynamic_addcomm']", function(){
        $id = $(this).attr("data-id");
        $width = $(this).attr("data-width");
        if (typeof($width) == "undefined") {
            $width = 500;
        }
        $height = $(this).attr("data-height");
        if (typeof($height) == "undefined") {
            $height = 300;
        }
        layer.open({
            title:'发表评论',
            type: 2,
            area: [''+$width+'px', ''+$height+'px'],
            shadeClose: true,
            scrollbar: false,
            content: _ROOT_PATH+"index.php?c=dynamic&a=addcomm&dyid="+$id+"&r="+get_rndnum(8)
        });
    });
    
    /*-------------------- 搜索 end -------------------*/
    
    /*----------------- 分割线 add begin ------------------*/
    
    //弹出发布页 iframe 8.05.05
    $(document).on("click", "[f='pop2_dynamic_add']", function(){
        $width = $(this).attr("data-width");
        if (typeof($width) == "undefined") {
            $width = 650;
        }
        $height = $(this).attr("data-height");
        if (typeof($height) == "undefined") {
            $height = 400;
        }
        layer.open({
            title:'发表动态',
            type: 2,
            area: [''+$width+'px', ''+$height+'px'],
            shadeClose: true,
            scrollbar: false,
            content: _ROOT_PATH+"index.php?c=dynamic&a=add&r="+get_rndnum(8)
        });
    });
    
	//切换表情
	$(document).on("click", "[f='tab_em']", function(){
		if ($("#msg_face_box").css("display") == "block") {
			$("#msg_face_box").hide();
		}
		else {
			$("#msg_face_box").show();
		}
	});
	//选择表情
	$(document).on("click", "[f='selectem']", function(){
		$_id = $(this).attr("data-id");
		$tag_em = "{emf_"+$_id+"}";
		var myField = document.getElementById("dynamic_content");
		if (document.selection) {
			//myField.focus();
			sel = document.selection.createRange();
			sel.text = $tag_em;
			//myField.focus();
		}
		else if (myField.selectionStart || myField.selectionStart == '0') {
			if (myField.value == "写点什么吧...") {
				myField.value = "";
			}
			var startPos = myField.selectionStart;
			var endPos = myField.selectionEnd;
			var cursorPos = endPos;
			myField.value = myField.value.substring(0, startPos)
						  + $tag_em
						  + myField.value.substring(endPos, myField.value.length);
			cursorPos += $tag_em.length;
			//myField.focus();
			myField.selectionStart = cursorPos;
			myField.selectionEnd = cursorPos;
		}
		else {
			if (myField.value == "写点什么吧...") {
				myField.value = "";
			}
			myField.value += $tag_em;
			//myField.focus();
		}
		$("#msg_face_box").hide();
	});

    
	//上传封面 8.05.07
	$(document).on("change", "[f='upload_dynamic_img']", function(){
		$_file_id = $(this).attr("id");
		$_module = $(this).attr("data-mod");
		if (typeof($_module) == "undefined") {
			$_module = "dynamic";
		}
		$_thumb = $(this).attr("data-thumb");
		if (typeof($_thumb) == "undefined") {
			$_thumb = "";
		}
		$_forbidcom = $(this).attr("data-forbidcom");
		if (typeof($_forbidcom) == "undefined") {
			$_forbidcom = "";
		}
		$_forbidwater = $(this).attr("data-forbidwater");
		if (typeof($_forbidwater) == "undefined") {
			$_forbidwater = "";
		}
		$_file_url = _ROOT_PATH+"index.php?m=user&c=index&a=upload&module="+$_module+"&thumbfiles="+$_thumb+"&forbidwater="+$_forbidwater+"&forbidcom="+$_forbidcom;
		_dynamicUploadImg($_file_id, $_file_url);
	});
    
	
	//内容 JSON
	$(document).on("blur", "#dynamic_content", function(){
		if ($(this).val() == "写点什么吧...") {
			$(this).val("");
		}
	});
	$(document).on("click", "#dynamic_content", function(){
		if ($(this).val() == "写点什么吧...") {
			$(this).val("");
		}
	});
	
	//切换图片验证码 7.08.22
	$(document).on("click", "#img_code", function(){
		$(this).attr("src", _ROOT_PATH+"source/include/validcode.php?type=number&width=65&height=30&num=5&r="+get_rndnum(8));
	});
    
    /*----------------- 分割线 add end ------------------*/
    
    
    //删除动态...8.08.16
    $(document).on("click", "[f='but_dynamic_showhand']", function(){
        $obj_div = $(this).parent().find("div");
        if ($obj_div.css("display") == "none") {
            $obj_div.show();
        }
        else {
            $obj_div.hide();
        }
    });
    $(document).on("click", "[f='but_dynamic_del']", function(){
        $_id = $(this).attr("data-id");
        $_from = $(this).attr("data-from");
        
        $.ajax({
            type: "POST",
            url: _ROOT_PATH+"index.php?c=dynamic",
            cache: false,
            data: {
                a:"del", id:$_id, r:get_rndnum(8)
            },
            dataType: "json",
            success: function($data) {
                $json = eval($data);
                $response = $json.response;
                $result = $json.result;
                if ($response == '1') {
                    layer.msg("删除成功");
                    setTimeout(function(){
                        if ($_from == "detail") {
                            window.location.href = _ROOT_PATH+"index.php?c=dynamic";
                        }
                        else {
                            window.location.reload();
                        }
                    }, 500);
                }
                else {
                    if ($result.length > 0) {
                        layer.msg($result);
                    }
                    else {
                        layer.msg("删除失败");
                    }
                }
            },
            error: function() {
                layer.msg("系统繁忙，请稍后再试");
            }
        });
    });


    //点赞 2018.12.21
    $(document).on("click", '[f="but_dynamic_zan"]', function(){
        $id = $(this).attr("data-id");

        $obj_id = $(this).attr("id");
        $("#"+$obj_id).attr("f", "");

        $.ajax({
            type: "POST",
            url: _OE_FILE  + "?c=dynamic",
            cache: false,
            data: {
                id:$id, a:"zans", r:get_rndnum(8)
            },
            dataType: "json",
            success: function($data) {
                $json = eval($data);
                $response = $json.response;
                $result = $json.result;

                $zans = parseInt($("#zans_"+$id).html());
                $("#"+$obj_id).attr("f", "but_dynamic_zan");

                if ($response == "1") {
                    if ($("#"+$obj_id).hasClass("current")) {
                        $zans = parseInt($zans)-1;
                        $("#zans_"+$id).html(parseInt($zans));
                        $("#"+$obj_id).removeClass("current");
                        //$("#"+$obj_id).find("img").attr("src", _ROOT_PATH+"tpl/wbapp/images/svg/dys.svg");
                    }else{
                        $zans = parseInt($zans)+1;
                        $("#zans_"+$id).html(parseInt($zans));
                        $("#"+$obj_id).addClass("current");
                        //$("#"+$obj_id).find("img").attr("src", _ROOT_PATH+"tpl/wbapp/images/svg/dy.svg");
                        //微信消息提醒 2018.12.19
                        _sendWxDynamicTip($id, 1);
                    }
                }
                else {
                    if ($result.length > 0) {
                        layer.msg($result);
                    }
                    else {
                        layer.msg("操作失败");
                    }
                }
            },
            error: function() {
                layer.msg("系统繁忙，请稍后再试");
                $("#"+$obj_id).attr("f", "but_dynamic_zan");
            }
        });
    });
    
});

//ajax加载评论分页
function _getCommPageList($page) {
	if (typeof($page) == "undefined") {
		$page = 1;
	}
	$obj_box = $("#comm_json_data");
	$.ajax({
		type: "GET",
		url:  _ROOT_PATH+"index.php?c=dynamic&a=comm&datatype=json&dyid="+$("#dyid").val(),
		cache: false,
		data: {page:$page, r:get_rndnum(8)},
		dataType: "json",
		success: function($data) {
			$json = eval($data);
			$prepage = $json.prepage; //上一页
			$nextpage = $json.nextpage; //下一页
			$result = $json.result;
			if ($result.length > 0) {
				if ($page == "1") {
					$obj_box.html($result);
				}
				else {
					$obj_box.append($result);
				}
				if ($nextpage > 0) { //还有下一页
					$("[f='but_get_commpage']").attr("data-page", $nextpage);
					$("[f='but_get_commpage']").show();
					$("[f='but_get_commpage']").find("label").hide();
				}
				else { 
					$("[f='but_get_commpage']").hide();
				}
			}
		},
		error: function() {
		
		}
	});
}

//上传图片 ajax提交 支持IE
function _dynamicUploadImg($file, $url) {
	if (_dynamicCheckImgType($file) == true) {
        $imgnum = parseInt($("#dynamic_imgnum").val());
        if ($imgnum > 9) {
            layer.msg("最多能上传9张照片");
            return false;
        }
        $obj = $("#label_"+$imgnum+"_"+$file);
        $obj.show();
        
        $("#label_"+$file).find("img").hide();
        $("#label_"+$file).show();
        
		$.ajaxFileUpload({
			"url": $url,
			"secureuri": false,
			"fileElementId": $file,
			"dataType": "json",
			"data": {
				uploadpart: $file
			},
			async: this.async,
			beforeSend: function ($xmlobj) {},
			complete: function ($data) {},
			success: function ($data) {
				$json = eval($data);
				$response = $json.response;
				$result = $json.result;
				$imgthumb = $json.thumbimg;
				$imgurl = $json.uploadimg;
				$attid = $json.attid;
				if (typeof($attid) == "undefined") {
					$attid = "";
				}
				if ($response == "1") {
					if ($("#"+$file+"_thumb"+$imgnum).length > 0) {
						$("#"+$file+"_thumb"+$imgnum).val($imgthumb);
					}
					if ($("#"+$file+"_url"+$imgnum).length > 0) {
						$("#"+$file+"_url"+$imgnum).val($imgurl);
					}
					if ($("#"+$file+"_preview"+$imgnum).length > 0) {
						$img = _ROOT_PATH+$imgurl+"?r="+get_rndnum(8);
						$("#"+$file+"_preview"+$imgnum).attr("src", $img).show();
					}
                    $("#dynamic_imgnum").val($imgnum+1);
					_oeloveFileSynYun($attid); //同步云存储

				}
				else {
					if ($result.length > 0 ) {
						layer.msg($result);
					}
					else {
						layer.msg("上传失败，请检查图片。");
					}
                    $("#label_"+$file).hide();
				}

			},
			error: function ($data, e, b) {
				layer.msg("上传失败，请检查网络...");
                $("#label_"+$file).hide();
			}
		});

	}
}

//判断图片格式
function _dynamicCheckImgType(filepart) {
	$res = true;
	$oe_file_obj = $("#"+filepart);
	$oe_file_val = $oe_file_obj.val();
	$oe_file_size = 0;
	if ($oe_file_val.length == 0) {
		layer.msg("请选择要上传的图片");
		$res = false;
	}
	if(!/.(gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG)$/.test($oe_file_val)){ 
		layer.msg("图片格式不正确");
		$res = false;
	}
	return $res;
}



//发送微信浏览提醒...
function _sendWxDynamicTip($id, $tiptype) {
    if (typeof($tiptype) == 'undefined') {
        $tiptype = 0;
    }
    $.ajax({
        type: "POST",
        url: _OE_FILE +"?c=dynamic",
        cache: false,
        data: {
            a:"sendwxtip", id:$id, tiptype:$tiptype, r:get_rndnum(8)
        },
        dataType: "json",
        success: function(data) {
            $json = eval(data);
            $response = $json.response;
            $result = $json.result;
            if ($response == '1') {

            }
            else {
            }
        },
        error: function() {

        }
    });
}


$(function(){
    //点击查看热心人二维码 9.07.26
    $(document).on("click", "[f='but_show_union']", function(){
        $id = $(this).attr("data-id");
        $qr_url = _ROOT_PATH+"index.php?c=scan&a=unqr&id="+$id+"&size=4";
        
        $("#un_qr").attr("src", $qr_url);
        $("#un_qr_box").show();
    });
    $(document).on("click", "[f='but_close_union']", function(){
        $("#un_qr").attr("src", "");
        $("#un_qr_box").hide();
    });
});


