-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
384 lines (349 loc) · 12.3 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>홈</title>
<!-- jQuery 라이브러리 추가 -->
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<link rel="stylesheet" href="./css/all.css" />
<link rel="stylesheet" href="./css/index.css" />
<script src="../js/dateFormat.js" defer></script>
<script src="../js/moneyFormat.js" defer></script>
<script src="../js/currencySymbol.js" defer></script>
</head>
<body>
<img src="./img/banner.png" id="banner" />
<img src="./img/banner-bubble.png" id="bubble" />
<!-- header -->
<header>
<div id="logo-box">
<img src="./img/logo.png" id="logo" />
<p id="logo-title">Wevel</p>
</div>
<a href="../html/notice.html">
<img src="./img/bell.png" id="bell" />
</a>
</header>
<!-- main -->
<main>
<p id="recent-title">최근 여행</p>
<a id="recent-link" href="./html/recent.html">
<section id="recent-container">
<div>
<p id="title"></p>
<p id="spend-money">
사용금액 <span id="money"></span>
<span class="currency"></span>
</p>
<p id="trip-date">여행기간</p>
<p id="date"></p>
</div>
<img src="./img/right.png" id="right" />
</section>
</a>
<section id="budget-container">
<div id="budget">
<img src="./img/default-trip.png" id="trip-img" />
<p id="trip-title"></p>
<p id="budget-cash">
예산금액 <span id="cash"></span><span class="currency"></span>
</p>
</div>
<div id="budget-line"></div>
<div id="remain">
<div id="remain-box1">
<p id="remain-money">남은금액</p>
<p id="remain-amount"></p>
<span class="currency"></span>
</div>
<div id="remain-box2">
<p id="use"><span id="percentage"></span> 사용</p>
<div id="progress-bar">
<div id="progress"></div>
</div>
</div>
</div>
</section>
<section id="memo-container">
<p id="memo-title">메모 <span id="memo-cnt"></span></p>
<div id="memo-menu">
<p onclick="latestTripId('all')">전체</p>
<p onclick="latestTripId('give')">줘야하는 돈</p>
<p onclick="latestTripId('receive')">받아야하는 돈</p>
</div>
<div class="no-memos">
<img src="./img/no-memos.png" alt="No memos" />
<p class="no-memo-text">아직 메모가 없습니다</p>
</div>
<div id="memo-list"></div>
<!-- <div class="memo-box">
<div>
<p class="memo" id="give">줘야하는 돈</p>
<p class="memo-content">지현이에게 200엔 줘야함</p>
<p class="memo-date">2024.01.16</p>
</div>
<div class="check-box">
<img src="./img/check-not.png" class="check" />
</div>
</div>
<div class="memo-box">
<div>
<p class="memo" id="receive">받아야하는 돈</p>
<p class="memo-content">가윤이에게 300엔 받아야함</p>
<p class="memo-date">2024.01.16</p>
</div>
<div class="check-box">
<img src="./img/check-not.png" class="check" />
</div>
</div> -->
</section>
</main>
<script>
const userId = localStorage.getItem("userId");
const apiUrl = localStorage.getItem("apiUrl");
// 여행 정보 API 연동
function fetchData() {
$.ajax({
url: `${apiUrl}/api/trips/latest/${userId}`,
type: "GET",
success: function (data) {
if (data) {
fillData(data);
tripId = data.tripId;
loadMemos("all", data.tripId); // Trip ID를 설정한 후에 메모를 불러옴
} else {
blankData();
}
},
error: function () {
// 에러가 발생했을 때 실행되는 코드
console.error("Failed to fetch data from the server.");
},
});
}
let tripName = "";
function blankData() {
$("#title").html("아직 여행이<br>등록되지 않았어요!");
$("#title").css({
"font-style": "normal",
"text-align": "left",
"font-size": "20px",
});
$("#spend-money").text("");
$("#trip-title").text("없음");
$("#money").text("");
$("#trip-date").text("");
$("#date").text("");
$("#cash").text("0 원");
$("#remain-amount").text("0 원");
$("#percentage").text("0%");
$("#progress").css("width", "0%");
$("#recent-link").attr("href", "#");
}
// 받아온 데이터를 HTML 엘리먼트에 채워넣는 함수
function fillData(data) {
var formattedStartDate = formatDate(data.startDate);
var formattedEndDate = formatDate(data.endDate);
var country = data.country;
var currencySymbol = getCurrencySymbol(country);
var spentAmount = formatNumberWithCommas(data.spentAmount);
let totalBudget = formatNumberWithCommas(data.totalBudget);
let remainingAmount = formatNumberWithCommas(data.remainingAmount);
let tripName = data.tripName;
console.log("화폐 기호:", currencySymbol);
let imagePath = "";
switch (country) {
case "일본":
imagePath = "./img/nation-japen.png";
break;
case "태국":
imagePath = "./img/nation-thailand.png";
break;
case "베트남":
imagePath = "./img/nation-vietnam.png";
break;
case "미국":
imagePath = "./img/nation-usa.png";
break;
default:
// 기본 이미지 경로 설정 (해당 국가에 대한 이미지가 없는 경우)
imagePath = "./img/default-trip.png";
break;
}
// 나라 이름 업데이트
$("#title").text(data.tripName);
$("#trip-title").text(data.tripName);
// 사용금액 업데이트
$("#money").text(spentAmount + currencySymbol);
// 여행기간 업데이트
$("#date").text(formattedStartDate + " ~ " + formattedEndDate);
// 예산금액 업데이트
$("#cash").text(totalBudget + currencySymbol);
// 남은금액 업데이트
$("#remain-amount").text(remainingAmount + currencySymbol);
// 사용 비율 및 프로그레스 업데이트
var percentage = (data.spentAmount / data.totalBudget) * 100;
if (percentage <= 100) $("#progress").css("width", percentage + "%");
else $("#progress").css("width", "100%");
$("#percentage").text(percentage.toFixed(2) + "%");
// 이미지 경로를 설정한 후에 이미지 엘리먼트에 적용
$("#trip-img").attr("src", imagePath);
// 링크에 tripId 추가
$("#recent-link").attr(
"href",
"./html/recent.html?tripId=" + data.tripId
);
}
// 페이지 로드 시 데이터 가져오기
$(document).ready(function () {
fetchData();
});
function latestTripId(menu) {
$.ajax({
url: `${apiUrl}/api/trips/latest/${userId}`,
type: "GET",
dataType: "json",
success: function (data) {
loadMemos(menu, data.tripId);
},
error: function (error) {
console.error("Error:", error);
},
});
}
// 메모 API 연동
function loadMemos(menu, tripId) {
$.ajax({
url: `${apiUrl}/api/memo/${menu}/${userId}/${tripId}`,
type: "GET",
dataType: "json",
success: function (data) {
if (
data &&
(data.length > 0 ||
(data.givenMemos && data.givenMemos.length > 0) ||
(data.receivedMemos && data.receivedMemos.length > 0))
) {
displayMemos(data, menu);
} else {
displayNoMemosMessage();
}
},
error: function (error) {
console.error("Error:", error);
},
});
}
function removeNoMemos() {
var noMemosElement = document.querySelector(".no-memos");
if (noMemosElement) {
noMemosElement.remove();
}
}
function displayMemos(memos, menu, tripId) {
const memoListDiv = $("#memo-list");
removeNoMemos();
memoListDiv.empty(); // 이전 메모 삭제
let memoCount = 0;
let memoType;
if (menu === "all") {
memoCount = memos.givenMemos.length + memos.receivedMemos.length;
displayMemoType(memos.givenMemos, "give");
displayMemoType(memos.receivedMemos, "receive");
} else if (menu === "give" || menu === "receive") {
memoCount = memos.length;
displayMemoType(memos, menu);
}
$("#memo-cnt").text(memoCount);
function displayMemoType(memoArray, type) {
memoType = type === "give" ? "줘야하는 돈" : "받아야하는 돈";
memoArray.forEach((memo) => {
const formattedDate = formatDate(memo.date);
const memoBoxDiv = createMemoBox(type, memo, formattedDate);
memoListDiv.append(memoBoxDiv);
});
}
}
function createMemoBox(type, memo, formattedDate) {
const memoBoxDiv = $("<div>").addClass("memo-box");
const contentDiv = $("<div>");
const id = type === "give" ? "give" : "receive";
const money = type === "give" ? "줘야하는 돈" : "받아야하는 돈";
const completed = type === "give" ? memo.gcompleted : memo.rcompleted;
console.log(completed);
console.log(memo);
const checkImagePath = completed
? "./img/check.png"
: "./img/check-not.png";
const checkDiv = $(`
<div class="check-box">
<img src="${checkImagePath}" class="check" />
</div>
`);
contentDiv.html(`
<p class="memo" id="${id}">${money}</p>
<p class="memo-content">${
memo[type === "give" ? "amountGiven" : "amountReceived"]
}</p>
<p class="memo-date">${formattedDate}</p>
`);
checkDiv.find(".check").on("click", function () {
const currentSrc = $(this).attr("src");
const newSrc =
currentSrc === "./img/check.png"
? "./img/check-not.png"
: "./img/check.png";
$(this).attr("src", newSrc);
const checkApiUrl = `http://localhost:8085/api/memo/check/${type}/${memo.memoId}`;
$.ajax({
url: checkApiUrl,
type: "PATCH",
success: function (response) {
console.log("Memo checked successfully:", response);
},
error: function (error) {
console.error("Error checking memo:", error);
},
});
});
memoBoxDiv.append(contentDiv);
memoBoxDiv.append(checkDiv);
memoBoxDiv.data("memo-id", memo.memoId); // 메모 ID 저장
return memoBoxDiv;
}
</script>
<!-- footer -->
<footer>
<nav>
<ul id="nav-list">
<a href="../index.html">
<li class="nav-item">
<img src="./img/home-index.png" class="nav-img" />
<p id="page">홈</p>
</li>
</a>
<a href="./html/travel.html">
<li class="nav-item">
<img src="./img/travel.png" class="nav-img" />
<p>여행</p>
</li>
</a>
<a href="./html/scan.html">
<li class="nav-item">
<img src="./img/scan.png" class="nav-img" />
<p>스캔</p>
</li>
</a>
<a href="./html/mypage.html">
<li class="nav-item">
<img src="./img/my.png" class="nav-img" />
<p>마이페이지</p>
</li>
</a>
</ul>
</nav>
</footer>
<script src="./js/index.js"></script>
</body>
</html>