<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
h1,h2,h3,body,div{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.flex-row{
display: flex;
flex-direction: row;
align-items:left;
justify-content: left;
}
.flex-col{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.list{
cursor: pointer;
width: 300px;
height: 700px;
background-color: aqua;
}
.body{
width: 700px;
background-color: yellowgreen
}
.list-modify>input{
border: none;
background-color:transparent;
width: 180px;
height: 20px;
font-size: 16px;
color: tomato;
font-weight: 500;
cursor: pointer;
margin-left: 0px;
margin-right: auto;
}
.list-modify>input:focus-visible{
outline: none;
}
.list-modify>button{
margin: auto;
font-size: 7px;
font-weight: 600;
cursor: pointer;
background-color: transparent;
border: none;
}
li{
width: 150px;
background-color: bisque;
}
.btn{
margin: 0ox 8px;
font-size: 10px;
font-weight: 600;
cursor: pointer;
background-color: transparent;
border: none;
color: hotpink;
}
</style>
<script>
$(document).ready(()=>{
listContent()
})
function save(){
let title = $('#title').val();
let body = $('#body').val();
let formData = new FormData()
formData.append("title_give",title)
formData.append("body_give", body)
fetch('/post', { method: "POST", body: formData }).then(res => res.json()).then(data => {
alert(data["msg"])
window.location.reload()
})
}
function listContent(){
fetch("/list").then(res => res.json()).then(data => {
let rows = data['result'];
// $('#list').empty()
rows.forEach((row)=>{
let title =row['title']
let _id = row['_id']
let body =row['body']
let temp_html = `<div id="${_id}-1" class="list-modify class="flex-row">
<li onclick="showUpdateFrom('${_id}')">${title}</li>
<button class="btn" onclick="deleteContent('${_id}')">삭제</button><button id="btn" onclick="readContent('${_id}')">조회</button>
</div>
<div id="${_id}-2" style="display: none;" class="list-modify flex-row">
<input type="text" value="${title}">
<button onclick="modifyContent('${_id}')">수정</button>
<button onclick="hideUpdateFrom('${_id}')">취소</button>
</div>
`
$('#list').append(temp_html)
})
})
}
function showUpdateFrom(_id){
$(`#${_id}-1`).hide();
$(`#${_id}-2`).show();
}
function hideUpdateFrom(_id){
$(`#${_id}-1`).show();
$(`#${_id}-2`).hide();
}
function modifyContent(_id){
let new_title = $(`#${_id}-2>input`).val();
let formData = new FormData()
formData.append("id_give",_id)
formData.append("title_give",new_title)
fetch('/modify', { method: "POST", body: formData }).then(res => res.json()).then(data => {
alert(data["msg"])
window.location.reload()
})
}
function deleteContent(_id){
let formData = new FormData()
formData.append("id_give",_id)
fetch('/delete', { method: "POST", body: formData }).then(res => res.json()).then(data => {
alert(data["msg"])
window.location.reload()
})
}
</script>
</head>
<body>
<div class="flex-row">
<div class="list">
<h1><a href="/">WEB</a></h1>
<ol id="list">
<!-- <li onclick="showUpdateFrom()" id="122">플라스크배우기</li>
<div id="122" style="display: none;" class="list-modify flex-row">
<input type="text" value="플라스크배우기">
<button>수정</button>
<button onclick="hideUpdateFrom()">취소</button>
</div> -->
</ol>
</div>
<div class="body">
<h2>welcome</h2>
hello,web
</div>
<div class="from">
<h2>수업추가하기</h2>
<p>프로그램명<input id="title" type="text" placeholder="title"></p>
<p>수업 소개</p><textarea id="body" placeholder="body" cols="30" rows="5"></textarea>
<button onclick="save()">저장</button>
</div>
</div>
</body>
</html>