function addProject() {
	const modal = document.querySelector('#modal-project')
	const inputs = modal.querySelectorAll('.form-control')
	inputs.forEach((input) => {
		input.value = ''
	})
	show_add_modal(modal)
}
function editProject(projectId) {
	const row = document.querySelector(`tr[data-id='${projectId}']`)
	const modal = document.querySelector('#modal-project')
	modal.querySelector('#project_id').value = projectId
	modal.querySelector('#project_name').value = row.querySelector('.project-name .form-control').value
	modal.querySelector('#project_desc').value = row.querySelector('.project-desc .form-control').value
	show_edit_modal(modal)
}

function saveProject() {
	const projectId = document.getElementById('project_id').value
	const projectName = document.getElementById('project_name').value
	const projectDesc = document.getElementById('project_desc').value

	try {
		fetch('/save_project', {
			method: 'POST',
			headers: {
				'Content-Type': 'application/json',
			},
			body: JSON.stringify({
				project_id: projectId,
				project_name: projectName,
				project_desc: projectDesc,
			}),
		})
			.then((response) => response.json())
			.then((data) => {
				if (data.success) {
					alert('保存成功')
					window.location.reload()
				} else {
					alert('保存失败:' + data.error)
				}
			})
			.catch((error) => {
				alert('保存失败')
			})
	} catch (e) {
		alert('网络请求异常')
	}
}

function confirmDelete(projectId) {
	if (confirm('确定要删除该项目吗？')) {
		try {
			fetch(`/delete_project/${projectId}`, {
				method: 'POST',
			})
				.then((response) => response.json())
				.then((data) => {
					if (data.success) {
						alert('删除成功')
						window.location.reload()
					} else {
						alert('删除失败:' + data.error)
					}
				})
				.catch((error) => {
					console.error('删除失败:', error)
					alert('删除失败')
				})
		} catch (e) {
			alert('网络请求异常')
		}
	}
}
