<!DOCTYPE html>
<html lang="ja">

<head>
	<meta charset="UTF-8">
	<title<%=title %>
		</title>
		<link rel="stylesheet" href="/styles.css">
		<script src="/script.js"></script>
</head>

<body>
	<h1>
		<%=title %>
	</h1>
	<div class="container">
		<div class="card">
			<p class="title" id="message">
				<%=message %>
			</p>
			<label for="id">ID</label>
			<input type="number" id="id" name="id" onchange="doChange(this.value)">
			<label for="name">名前</label>
			<input type="text" id="name" name="name">
			<label for="email">メール</label>
			<input type="email" id="email" name="email">
			<button onclick="doAction();">更新</button>
		</div>
		<script>
			const message = document.querySelector('#message');
			const id = document.querySelector('#id');
			const name = document.querySelector('#name');
			const email = document.querySelector('#email');


			function doChange(id) {
				getPersonalDataById(id).then(res => {
					name.value = res.row.name;
					email.value = res.row.email;
					message.textContent = `id=${id} のレコードを編集します。`;
				})
			}

			function doAction() {
				const data = {
					id: id.value,
					name: name.value,
					email: email.value
				};
				updatePersonalData(data).then(res => {
					message.textContent = `${res.result.changes}個のレコードを更新しました。`;
				})
			}
		</script>
</body>

</html>