<!DOCTYPE html>
<html>
<head>
<meta name=viewport content="width=device-width, initial-scale=1, user-scalable=0">
<meta charset="UTF-8">
<title>친구 목록</title>
</head>
<body>
<script src="js/jquery-3.5.1.min.js"></script> 
<script src="js/core.js"></script>
<div class="page-hdr">친구 목록</div> 
<div class="page-body">
	<div class="page-mnu">
		<div class="menu" onclick="goHome()">홈</div> 
		<div class="menu" onclick="goList()">내친구</div>
	</div>
	<div id="list" class="section"></div>
	<div class="page-msg hide">등록된 친구가 없습니다.</div>
</div>

<script>
$(document).ready(function() {
	Page.init(start); 
	});
	var myId = null; 
	function start(uid) {
		myId = uid;
		AJAX.call("jsp/friendList.jsp", {email: uid}, function(data) {
		var list = JSON.parse(data.trim());
		show(list); 
	});
}
	
	function show(list) {
		var str = "", cnt = 0;
		for (var i=0; i<list.length; i++) {
			if (list[i].id == myId) continue;
			str += getFeedCode(list[i]);
			cnt++;
		}
		$("#list").append(str); 
		if (cnt == 0) {
			$(".page-msg").removeClass("hide"); 
			}
		}
		
	function getFeedCode(user) { 
		var str = "<div class='feed'>"; 
		str += "<div class='author'>"; 
		if (user.images == null) {
		str += "<div class='photo'></div>"; 
		}
		else {
			var imgurl = "images/" + user.images[0];
			str += "<div class='photo' style='background-image: url(\"" + imgurl + "\")'></div>";
		}
		str += "<div class='name'>" + user.name + "</div>";
		str += "<div class='btn-del' onclick=‘removeFriend(\"" + user.id + "\")'></div>";
		str += "</div>";
		str += "</div>";
		
		return str;
	}
	
	function removeFriend(uid) {
		var params = {id: myId, frid: uid}; 
		AJAX.call("jsp/friendRemove.jsp", params, function(data) {
		var code = data.trim(); 
		
		if (code == "ER”) {
			alert("친구 삭제 중 오류가 발생하였습니다."); }
		else {
			alert("친구 삭제가 완료되었습니다."); 
			window.location.reload();
		} 
		});
	}
	
	function goHome() { 
		window.location.href = "main.html";
	}
	function goAdd() {
		window.location.href = "friendAdd.html"; }
	function goList(uemail) {
		window.location.href="friend.html";
	}
</script>
</body>
</html>