function addSubProjectItem(){
	const modal = document.querySelector('#modal-sub_project_item')
	const inputs = modal.querySelectorAll('.form-control')
	inputs.forEach((input) => {
		input.value = ''
	})
	show_add_modal(modal)
}
function editSubProjectItem(id) {
	const row = document.querySelector(`tr[data-id='${id}']`)
	const modal = document.querySelector('#modal-sub_project_item')
	modal.querySelector('#item_id').value = id
	modal.querySelector('#device_name').value = row.querySelector('.device_name .form-control').value
	modal.querySelector('#device_model').value = row.querySelector('.device_model .form-control').value
	modal.querySelector('#device_count').value = row.querySelector('.device_count .form-control').value
	modal.querySelector('#device_unit').value = row.querySelector('.device_unit .form-control').value
	modal.querySelector('#standard_no').value = row.querySelector('.standard_no .form-control').value

	show_edit_modal(modal)
}
function saveSubProjectItem(sub_id) {
	const modal = document.querySelector('#modal-sub_project_item')
	const id = modal.querySelector('#item_id').value
	const deviceName = modal.querySelector('#device_name').value
	const deviceModel = modal.querySelector('#device_model').value
	const deviceUnit = modal.querySelector('#device_unit').value
	const deviceCount = modal.querySelector('#device_count').value
	const standardNo = modal.querySelector('#standard_no').value
	const isQuery = modal.querySelector('#is_query').checked
	if (deviceName === '') {
		alert('名称不能为空')
		return
	}
	if (deviceModel === '') {
		alert('规格型号不能为空')
		return
	}
	if (deviceCount === '') {
		alert('数量不能为空')
		return
	}
	if (deviceUnit === '') {
		alert('单位不能为空')
		return
	}
	// if (standardNo === '') {
	//     alert('标准编号不能为空');
	//     return;
	// }

	fetch(`/save_sub_project_item`, {
		method: 'POST',
		headers: {
			'Content-Type': 'application/json',
		},
		body: JSON.stringify({
			id: id,
			sub_id: sub_id,
			device_name: deviceName,
			device_model: deviceModel,
			device_unit: deviceUnit,
			device_count: deviceCount,
			standard_no: standardNo ,
			is_query: isQuery
		}),
	})
		.then((response) => response.json())
		.then((data) => {
			if (data.success) {
				alert('保存成功')
				window.location.href = `/project_item_list/${sub_id}`
			} else {
				alert('保存失败:' + data.error)
			}
		})
		.catch((error) => {
			console.error('保存失败:', error)
		})
}

function deleteSubProjectItem(itemId) {
	if (confirm('确定要删除该设备吗？')) {
		fetch(`/delete_sub_project_item/${itemId}`, {
			method: 'POST',
			headers: {
				'Content-Type': 'application/json',
			},
		})
			.then((response) => response.json())
			.then((data) => {
				if (data.success) {
					alert('删除成功')
					// 删除对应的行
					const row = document.querySelector(`tr[data-id="${itemId}"]`)
					if (row) {
						row.remove()
					}
				} else {
					alert('删除失败:' + data.error)
				}
			})
			.catch((error) => {
				console.error('删除失败:', error)
				alert('删除失败')
			})
	}
}

function startProcessItem(itemId){
	_confirm('确定要开始处理吗？', `/start_process_item/${itemId}`)
}
function startSendItem(itemId){
	_confirm('确定要开始发送吗？',`/start_send_item/${itemId}`)
}


