하석형 하석형 2023-02-21
230221 하석형 장비 조회 조건문 작업 중
@037cd36f2edf532eae2a186bedd7ba71c09d31ad
client/views/pages/equipment/EquipmentManagementSelect.jsx
--- client/views/pages/equipment/EquipmentManagementSelect.jsx
+++ client/views/pages/equipment/EquipmentManagementSelect.jsx
@@ -36,15 +36,6 @@
 
 	const navigate = useNavigate();
 
-	// 함수 실행 함수
-	React.useEffect(() => {
-		equipmentSelectList();
-
-		agencyEquipmentSelectList();
-	}, [])
-
-
-
 	/********************************** 관리자 장비 (시작) **********************************/
 	// 관리자 전체 장비 목록
 	const [equipmentList, setEquipmentList] = React.useState([]);
@@ -63,18 +54,6 @@
 		}).catch((error) => {
 			console.log('equipmentSelectList error : ', error);
 		});
-	}
-
-	// 장비 목록 조회 확인
-	const equipmentListData = () => {
-		console.log('equipmentSelectList Function Run', equipmentList);
-		console.log('equipmentSelectList Function Run', equipmentList.length);
-		console.log('content1 before data: ', content1);
-		for(let i = 0; i < equipmentList.length; i++) {
-			content1.equipmentNo = i + 1;
-			content1.equipmentName = equipmentList[i].equipment_name;
-		}
-		console.log('content1 after data: ', content1);
 	}
 
 	const thead1 = [
@@ -189,17 +168,22 @@
 	/********************************** 시행기관 장비 (시작) **********************************/
 	//로그 확인
 	const logCheck = () => {
-		console.log('agencyEquipmentList - change: ', agencyEquipmentList);
+		console.log('seniorList - change: ', seniorList);
 	};
 
 	// 시행기관 전체 장비 목록
 	const [agencyEquipmentList, setAgencyEquipmentList] = React.useState([]);
-
+	// 시행기관 대상자 장비 목록
+	const [agencySeniorEquipmentList, setAgencySeniorEquipmentList] = React.useState([]);
+	// 시행기관 재고 장비 목록
+	const [agencyStockEquipmentList, setAgencyStockEquipmentList] = React.useState([]);
+	// 선택 대상자 장비
+	const [seniorEquipment, setSeniorEquipment] = React.useState({});
+	// 대상자 목록
+	const [seniorList, setSeniorList] = React.useState([]);
 	// 대상자 장비 등록 모달창
 	const [seniorMatchModal, setSeniorMatchModal] = React.useState(false);
-
-	// 대상자 장비
-	const [seniorEquipment, setSeniorEquipment] = React.useState({});
+	
 
 	// 대상자 장비 등록 모달창 열기
 	const seniorMatchModalOpen = (item) => {
@@ -213,6 +197,16 @@
 
 	// 대상자 장비 등록 - 대상자
 	const seniorEquipmentSeniorInsert = (e) => {
+		console.log("senior: ", e.target.value);
+
+		// // Name 속성
+		// const userName = e.target.value.split('(')[0].trim();
+		// // ID 속성
+		// const second = e.target.value.split('(')[1]; // ')' 자르기 전
+		// const userId = second.substr(0, second.length-1);
+		// console.log("senior id: ", userId);
+		// console.log("senior name: ", userName);
+
 		// 원본 데이터 복사 및 수정
 		let data = {
 			...seniorEquipment,
@@ -245,6 +239,7 @@
 		setSeniorEquipment(data);
 	};
 
+
 	// 시행기관 전체 장비 목록 조회
 	const agencyEquipmentSelectList = () => {
 		console.log('agencyEquipmentSelectList Function Run');
@@ -264,22 +259,83 @@
 		});
 	}
 
+	// 시행기관 대상자 장비 목록 조회
+	// const agencySeniorEquipmentSelectList = () => {
+	// 	console.log('agencySeniorEquipmentSelectList Function Run');
+	// 	fetch("/equipment/agencySeniorEquipmentSelectList.json", {
+	// 		method: "POST",
+	// 		headers: {
+	// 			'Content-Type': 'application/json; charset=UTF-8'
+	// 		},
+	// 		body: JSON.stringify({
+	// 			agency_id: 'AGENCY01',
+	// 		}),
+	// 	}).then((response) => response.json()).then((data) => {
+	// 		console.log('agencySeniorEquipmentSelectList response : ', data);
+	// 		setAgencySeniorEquipmentList(data);
+	// 	}).catch((error) => {
+	// 		console.log('agencySeniorEquipmentSelectList error : ', error);
+	// 	});
+	// }
+
 	// 대상자 장비 등록
 	const seniorEquipmentInsert = () => {
 		console.log('seniorEquipmentInsert Function Run');
+
+		// 대상자 아이디 유효성 검사
+		let check = 0;
+		for(let i = 0; i < seniorList.length; i++) {
+			// 입력한 데이터가 대상자 목록에 있을 경우
+			if(seniorList[i]['user_id'] == seniorEquipment['senior_id']) {
+				console.log('success: ', seniorList[i]['user_id']);
+				// 원본 데이터 복사 및 수정
+				let data = {
+					...seniorEquipment,
+					user_name: seniorList[i]['user_name']
+				}
+				// 복사 데이터를 원본 데이터에 덮어쓰기
+				setSeniorEquipment(data);
+
+				check = 1;
+			}
+		}
+		// 입력한 데이터가 대상자 목록에 없을 경우
+		if(check != 1) {
+			console.log('fail: ', seniorList, seniorEquipment['senior_id']);
+			alert('존재하지 않는 대상자입니다.');
+			return;
+		}
+
 		fetch("/equipment/seniorEquipmentInsert.json", {
 			method: "POST",
 			headers: {
 				'Content-Type': 'application/json; charset=UTF-8'
 			},
-			body: JSON.stringify({
-				agency_id: 'AGENCY01',
-			}),
+			body: JSON.stringify(seniorEquipment),
 		}).then((response) => response.json()).then((data) => {
 			console.log('seniorEquipmentInsert response : ', data);
-			setSeniorEquipment(data);
+			// 대상자 장비 데이터 초기화
+			setSeniorEquipment({});
+			alert('등록이 완료됐습니다.');
 		}).catch((error) => {
 			console.log('seniorEquipmentInsert error : ', error);
+			alert('등록에 실패했습니다.');
+		});
+	}
+
+	// 대상자 목록 조회
+	const seniorSelectList = () => {
+		console.log('seniorSelectList Function Run');
+		fetch("/user/selectSubjectList.json", {
+			method: "POST",
+			headers: {
+				'Content-Type': 'application/json; charset=UTF-8'
+			},
+		}).then((response) => response.json()).then((data) => {
+			console.log('seniorSelectList response : ', data);
+			setSeniorList(data);
+		}).catch((error) => {
+			console.log('seniorSelectList error : ', error);
 		});
 	}
 
@@ -306,13 +362,34 @@
 
 	const thead22 = [
 		"No",
+		"모델 명",
+		"시리얼 넘버",
+		"상태",
+		"대상자",
+		"대여일",
+		"반납예정일",
+		"반납일",
+		"관리"
+	];
+	const key22 = [
+		"equipment_name",
+		"equipment_serial_number",
+		"equipment_state",
+		"user_name",
+		"equipment_rental_start_date",
+		"equipment_rental_end_date",
+		"equipment_rental_return_date"
+	];
+
+	const thead33 = [
+		"No",
 		"장비명",
 		"시리얼 넘버",
 		"입고일자",
 		"납품 기관",//관리기관, 올잇메디
 		"대상자",//시행기관
 	];
-	const key22 = [
+	const key33 = [
 		"No",
 		"equipment",
 		"serialNumber",
@@ -320,7 +397,7 @@
 		"agency",
 		"senior",
 	];
-	const content22 = [
+	const content33 = [
 		{
 			No: 1,
 			equipment: "스마트약상자",
@@ -360,36 +437,29 @@
 			),
 		},
 	];
-	const thead33 = [
-		"No",
-		"장비명",
-		"시리얼 넘버",
-		"대여일",
-		"배터리 잔량",
-		"보유기관",//관리기관, 올잇메디
-		"담당자",//시행기관
-	];
-	const key33 = [
-		"No",
-		"equipment",
-		"serialNumber",
-		"date",
-		"battery",
-		"agency",
-		"protector",
-	];
-	const content33 = [
-		{
-			No: 1,
-			equipment: "스마트약상자",
-			serialNumber: "ABCD-1",
-			date: "2022.12.02",
-			battery: "10%",
-			agency: "A복지관",
-			protector: "김복지"
 
-		},
-	];
+	// 마운트 시 실행 함수
+	React.useEffect(() => {
+		equipmentSelectList();
+
+		agencyEquipmentSelectList();
+		// agencySeniorEquipmentSelectList();
+		seniorSelectList();
+	}, [])
+
+	// 대상자, 재고 장비 목록 
+	React.useEffect(() => {
+		console.log('@@@', agencyEquipmentList);
+		let data = [];
+		for(let i = 0; i < agencyEquipmentList.length; i++) {
+			if(agencyEquipmentList[i]['senior_id'] != null) {
+				data.push(agencyEquipmentList[i]);
+			}
+		}
+		console.log('@@@data', data);
+		setAgencySeniorEquipmentList(data);
+	}, [agencyEquipmentList])
+
 	/********************************** 시행기관 장비 (끝) **********************************/
 
 	const thead4 = [
@@ -564,15 +634,6 @@
 							/>
 						</div>
 					</div>
-					{/* <Table
-						className={"caregiver-user"}
-						head={thead11}
-						contents={agencyEquipmentList}
-						contentKey={key11}
-						onClick={() => {
-							navigate("/EquipmentManagementSelectOne");
-						}}
-					/> */}
 					<table class = "caregiver-user">
 						<thead>
 							<tr>
@@ -614,21 +675,71 @@
 		},
 		{
 			id: 5,
-			title: "재고 장비(시행기관)" + "(" + ")", //에이젼시로 들어가면 미사용 장비(5)로 나와야 함
+			title: "대상자 장비(시행기관)" + "(" + agencySeniorEquipmentList.length + ")",
 			description: (
 				<div>
-					<Table
-						className={"caregiver-user"}
-						head={thead22}
-						contents={content22}
-						contentKey={key22}
-					/>
+					{/* <table class = "caregiver-user">
+						<thead>
+							<tr>
+							{thead22.map((i) => {
+								return <th>{i}</th>;
+							})}
+							</tr>
+						</thead>
+						<tbody>
+							{agencySeniorEquipmentList.map((item, index) => {
+							return (
+								<tr key={index}>
+									<td>{agencySeniorEquipmentList.length - index}</td>
+									{key22.map((kes) => {
+										return <td>{item[kes]}</td>
+									})}
+									<td>
+										<Button
+											className={"btn-small green-btn"}
+											btnName={"삭제"}
+											onClick={seniorMatchDelete}
+										/>
+									</td>
+								</tr>
+							);
+							})}
+						</tbody>
+					</table> */}
+					<table class = "caregiver-user">
+						<thead>
+							<tr>
+							{thead22.map((i) => {
+								return <th>{i}</th>;
+							})}
+							</tr>
+						</thead>
+						<tbody>
+							{agencySeniorEquipmentList.map((item, index) => {
+							return (
+								<tr key={index}>
+									<td>{agencySeniorEquipmentList.length - index}</td>
+									{key22.map((kes) => {
+										return <td>{item[kes]}</td>
+									})}
+									<td>
+										<Button
+											className={"btn-small green-btn"}
+											btnName={"삭제"}
+											onClick={seniorMatchDelete}
+										/>
+									</td>
+								</tr>
+							);
+							})}
+						</tbody>
+					</table>
 				</div>
 			),
 		},
 		{
 			id: 6,
-			title: "납품 장비(시행기관)" + "(" + ")",//에이젼시로 들어가면 내가 사용중인 장비(5)로 나와야 함
+			title: "재고 장비(시행기관)" + "(" + ")",
 			description: (
 				<Table
 					className={"caregiver-user"}
@@ -677,11 +788,13 @@
 							<tbody>
 								<tr>
 									<td>
-										<input type="text" list="senior_list" />
+										<input type="text" list="senior_list" placeholder="대상자를 입력해 주세요" onChange={seniorEquipmentSeniorInsert}/>
 										<datalist id="senior_list">
-											<option value="대상자1(ID)"></option>
-											<option value="대상자2(ID)"></option>
-											<option value="대상자3(ID)"></option>
+											{seniorList.map((item) => {
+												// return <option value={item['user_name'] + " (" + item['user_id'] + ")"}></option>
+												return <option value={item['user_id']}>{item['user_name']}</option>
+
+											})}
 										</datalist>
 									</td>
 									<td>
Add a comment
List