#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// EMPLOYEES 테이블 구조 기반으로 재정의
struct EMPLOYEE {
	char emp_id[10];	// EMPL_ID
	char full_name[30];	// FULL_NAME
	char hire_date[15];	// HIRE_DATE
	char married[5];	// MARRIED
	char gender[5];	// GENDER
	int on_leave;	// 재직 상태(0: 재직 중, 1 : 휴직/퇴사)
};
typedef struct EMPLOYEE EMPLOYEE;

// 함수 프로토타입
int print_all_employees(EMPLOYEE* emp_list, int total_num_emp); // 전체 목록 조회
int print_employees_detail(EMPLOYEE* emp_list, int total_num_emp);	// 특정 사원 상세 정보 출력

// 직원 정보 출력 헬퍼 함수
void display_employee_info(EMPLOYEE emp, int detail_mode);

// 문자열 비교 함수(이전 코드와 동일)
char compare(char* str1, char* str2) {
	while (*str1 && *str2) {
		if (*str1 != *str2) {
			return 0;
		}
		str1++;
		str2++;
	}
	if (*str1 == '\\0' && *str2 == '\\0') return 1;
	return 0;
}

// -------------------------------------------------------------
// * 직원 정보 출력 헬퍼 함수 구현 *
// -------------------------------------------------------------

void display_employee_info(EMPLOYEE emp, int detail_mode) {
	// detail_mode 가 0일 때 : 전체 목록 간략 출력
	if (detail_mode == 0) {
		printf("%-10s // %-20s // %s\\n",
			emp.emp_id, emp.full_name,
			(emp.on_leave == 0) ? "재직 중" : "휴직/퇴사");
	}
	// detail_mode 가 1일 때 : 상세 정보 출력
	else {
		printf("\\n--- 직원 상세 정보 ---\\n");
		printf("  - 사번 (EMP_ID)  : %s\\n", emp.emp_id);
		printf("  - 이름 (FULL_NAME): %s\\n", emp.full_name);
		printf("  - 입사일 (HIRE_DATE): %s\\n", emp.hire_date);
		printf("  - 결혼 여부     : %s\\n", emp.married);
		printf("  - 성별 (GENDER) : %s\\n", emp.gender);
		printf("  - 재직 상태     : %s\\n", (emp.on_leave == 0) ? "재직 중" : "휴직/퇴사");
		printf("----------------------\\n");
	}
}

// -------------------------------------------------------------
// * 1. 전체 목록 조회 기능 구현 *
// -------------------------------------------------------------
int print_all_employees(EMPLOYEES* emp_list, int total_num_emp) {
	if (total_num_emp == 0) {
		printf("등록된 직원 정보가 없습니다.\\n");
		return 0;
	}

	printf("\\n============================================\\n");
	printf("     ⭐ 전체 직원 목록 (간략 보기) ⭐\\n");
	printf("============================================\\n");
	printf("%-10s // %-20s // %s\\n", "사번", "이름", "상태");
	printf("--------------------------------------------\\n");

	for (int i = 0; i < total_num_emp; i++) {
		display_employee_info(emp_list[i], 0); // 0 : 간략 모드
	}
	printf("==============================================\\n");
	return 0;
}

// -------------------------------------------------------------
// * 2. 특정 사원 상세 정보 출력 기능 구현 *
// -------------------------------------------------------------
int print_employees_detail(EMPLOYEES* emp_list, int total_num_emp) {
	char search_id[10];

	if (total_num_emp == 0) {
		printf("등록된 직원 정보가 없습니다. \\n");
		return 0;
	}

	printf("조회할 사원의 사번(EMP_ID)을 입력하세요: ");
	scanf("%s", search_id);

	for (int i = 0; i < total_num_emp; i++) {
		if (compare(emp_list[i].emp_id, search_id)) {
			// 사번이 일치하면 상세 정보 출력 (1: 상세 모드)
			display_employee_info(emp_list[i], 1);
			return 0; // 찾았으니 함수 종료
		}
	}

	printf("\\n ❌ [%s]에 해당하는 직원을 찾을 수 없습니다.\\n", search_id);
	return -1;
}
// -------------------------------------------------------------
// * Main 함수 (메인 루프 및 더미 데이터) *
// -------------------------------------------------------------
int main() {
	int user_choice;
	int num_total_emp = 0; // 현재 직원 수
	int max_employees = 5; // 최대 5명만 가정
	EMPLOYEE* emp_list;

	// 최대 직원 수 만큼 메모리 할당
	emp_list = (EMPLOYEE*)malloc(sizeof(EMPLOYEE) * max_employees);
	if (emp_list == NULL) return 1;

	// --- ⭐ 테스트를 위한 더미 데이터 등록 ⭐ ---
	strcpy(emp_list[0].emp_id, "A001");
	strcpy(emp_list[0].full_name, "김철수");
	strcpy(emp_list[0].hire_date, "2020-03-01");
	strcpy(emp_list[0].married, "YES");
	strcpy(emp_list[0].gender, "M");
	emp_list[0].on_leave = 0;
	num_total_emp++;

	strcpy(emp_list[1].emp_id, "B007");
	strcpy(emp_list[1].full_name, "이영희");
	strcpy(emp_list[1].hire_date, "2022-09-15");
	strcpy(emp_list[1].married, "NO");
	strcpy(emp_list[1].gender, "W");
	emp_list[1].on_leave = 1; // 휴직 상태
	num_total_emp++;
	// --------------------------------------------

	while (1) {
		printf("\\n================================\\n");
		printf(" 직원 정보 조회 프로그램\\n");
		printf("================================\\n");
		printf("1. 전체 직원 목록 조회\\n");
		printf("2. 특정 사원 상세 정보 출력 (사번 입력)\\n");
		printf("3. 프로그램 종료\\n");
		printf("--------------------------------\\n");
		printf("당신의 선택은: ");

		if (scanf("%d", &user_choice) != 1) {
			// 숫자가 아닌 입력 처리
			printf("\\n잘못된 입력입니다. 숫자를 입력해주세요.\\n");
			while (getchar() != '\\n');
			continue;
		}
		// 입력 버퍼 비우기
		while (getchar() != '\\n');

		if (user_choice == 1) {
			print_all_employees(emp_list, num_total_emp);
		}
		else if (user_choice == 2) {
			print_employee_detail(emp_list, num_total_emp);
		}
		else if (user_choice == 3) {
			printf("\\n프로그램을 종료합니다.\\n");
			break;
		}
		else {
			printf("\\n잘못된 메뉴 선택입니다. 다시 선택해주세요.\\n");
		}
	}

	free(emp_list);
	return 0;
}

// employees로 실습하기.cpp : 이 파일에는 'main' 함수가 포함됩니다. 거기서 프로그램 실행이 시작되고 종료됩니다.
//

#include <iostream>

int main()
{
    std::cout << "Hello World!\\n";
}

// 프로그램 실행: <Ctrl+F5> 또는 [디버그] > [디버깅하지 않고 시작] 메뉴
// 프로그램 디버그: <F5> 키 또는 [디버그] > [디버깅 시작] 메뉴

// 시작을 위한 팁: 
//   1. [솔루션 탐색기] 창을 사용하여 파일을 추가/관리합니다.
//   2. [팀 탐색기] 창을 사용하여 소스 제어에 연결합니다.
//   3. [출력] 창을 사용하여 빌드 출력 및 기타 메시지를 확인합니다.
//   4. [오류 목록] 창을 사용하여 오류를 봅니다.
//   5. [프로젝트] > [새 항목 추가]로 이동하여 새 코드 파일을 만들거나, [프로젝트] > [기존 항목 추가]로 이동하여 기존 코드 파일을 프로젝트에 추가합니다.
//   6. 나중에 이 프로젝트를 다시 열려면 [파일] > [열기] > [프로젝트]로 이동하고 .sln 파일을 선택합니다.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// EMPLOYEES 테이블 구조 기반으로 재정의
struct EMPLOYEE {
	char emp_id[10];	// EMPL_ID
	char full_name[30];	// FULL_NAME
	char hire_date[15];	// HIRE_DATE
	char married[5];	// MARRIED
	char gender[5];	// GENDER
	int on_leave;	// 재직 상태(0: 재직 중, 1 : 휴직/퇴사)
};
typedef struct EMPLOYEE EMPLOYEE;

// 함수 프로토타입 (수정 완료)
int print_all_employees(EMPLOYEE* emp_list, int total_num_emp);
int print_employee_detail(EMPLOYEE* emp_list, int total_num_emp); // 함수명 통일

// 직원 정보 출력 헬퍼 함수
void display_employee_info(EMPLOYEE emp, int detail_mode);

// 문자열 비교 함수
char compare(char* str1, char* str2) {
	while (*str1 && *str2) {
		if (*str1 != *str2) {
			return 0;
		}
		str1++;
		str2++;
	}
	if (*str1 == '\\0' && *str2 == '\\0') return 1;
	return 0;
}

// -------------------------------------------------------------
// * 직원 정보 출력 헬퍼 함수 구현 *
// -------------------------------------------------------------
void display_employee_info(EMPLOYEE emp, int detail_mode) {
	if (detail_mode == 0) {
		printf("%-10s // %-20s // %s\\n",
			emp.emp_id, emp.full_name,
			(emp.on_leave == 0) ? "재직 중" : "휴직/퇴사");
	}
	else {
		printf("\\n--- 직원 상세 정보 ---\\n");
		printf("  - 사번 (EMP_ID)  : %s\\n", emp.emp_id);
		printf("  - 이름 (FULL_NAME): %s\\n", emp.full_name);
		printf("  - 입사일 (HIRE_DATE): %s\\n", emp.hire_date);
		printf("  - 결혼 여부     : %s\\n", emp.married);
		printf("  - 성별 (GENDER) : %s\\n", emp.gender);
		printf("  - 재직 상태     : %s\\n", (emp.on_leave == 0) ? "재직 중" : "휴직/퇴사");
		printf("----------------------\\n");
	}
}

// -------------------------------------------------------------
// * 1. 전체 목록 조회 기능 구현 (수정 완료) *
// -------------------------------------------------------------
int print_all_employees(EMPLOYEE* emp_list, int total_num_emp) { // EMPLOYEE로 수정
	if (total_num_emp == 0) {
		printf("등록된 직원 정보가 없습니다.\\n");
		return 0;
	}

	printf("\\n============================================\\n");
	printf("     ⭐ 전체 직원 목록 (간략 보기) ⭐\\n");
	printf("============================================\\n");
	printf("%-10s // %-20s // %s\\n", "사번", "이름", "상태");
	printf("--------------------------------------------\\n");

	for (int i = 0; i < total_num_emp; i++) {
		display_employee_info(emp_list[i], 0);
	}
	printf("============================================\\n");
	return 0;
}

// -------------------------------------------------------------
// * 2. 특정 사원 상세 정보 출력 기능 구현 (수정 완료) *
// -------------------------------------------------------------
int print_employee_detail(EMPLOYEE* emp_list, int total_num_emp) { // 함수명 및 EMPLOYEE로 수정
	char search_id[10];

	if (total_num_emp == 0) {
		printf("등록된 직원 정보가 없습니다. \\n");
		return 0;
	}

	printf("조회할 사원의 사번(EMP_ID)을 입력하세요: ");
	scanf("%s", search_id);

	for (int i = 0; i < total_num_emp; i++) {
		if (compare(emp_list[i].emp_id, search_id)) {
			display_employee_info(emp_list[i], 1);
			return 0;
		}
	}

	printf("\\n 사번 [%s]에 해당하는 직원을 찾을 수 없습니다.\\n", search_id); // 문구 정정
	return -1;
}

// -------------------------------------------------------------
// * Main 함수 (메인 루프 및 더미 데이터) *
// -------------------------------------------------------------
int main() {
	int user_choice;
	int num_total_emp = 0;
	int max_employees = 5;
	EMPLOYEE* emp_list;

	emp_list = (EMPLOYEE*)malloc(sizeof(EMPLOYEE) * max_employees);
	if (emp_list == NULL) return 1;

	// --- ⭐ 테스트를 위한 더미 데이터 등록 ⭐ ---
	strcpy(emp_list[0].emp_id, "A001");
	strcpy(emp_list[0].full_name, "김철수");
	strcpy(emp_list[0].hire_date, "2020-03-01");
	strcpy(emp_list[0].married, "YES");
	strcpy(emp_list[0].gender, "M");
	emp_list[0].on_leave = 0;
	num_total_emp++;

	strcpy(emp_list[1].emp_id, "B007");
	strcpy(emp_list[1].full_name, "이영희");
	strcpy(emp_list[1].hire_date, "2022-09-15");
	strcpy(emp_list[1].married, "NO");
	strcpy(emp_list[1].gender, "W");
	emp_list[1].on_leave = 1;
	num_total_emp++;
	// --------------------------------------------

	while (1) {
		printf("\\n================================\\n");
		printf(" 직원 정보 조회 프로그램\\n");
		printf("================================\\n");
		printf("1. 전체 직원 목록 조회\\n");
		printf("2. 특정 사원 상세 정보 출력 (사번 입력)\\n");
		printf("3. 프로그램 종료\\n");
		printf("--------------------------------\\n");
		printf("당신의 선택은: ");

		if (scanf("%d", &user_choice) != 1) {
			printf("\\n잘못된 입력입니다. 숫자를 입력해주세요.\\n");
			while (getchar() != '\\n');
			continue;
		}
		while (getchar() != '\\n');

		if (user_choice == 1) {
			print_all_employees(emp_list, num_total_emp);
		}
		else if (user_choice == 2) {
			print_employee_detail(emp_list, num_total_emp);
		}
		else if (user_choice == 3) {
			printf("\\n프로그램을 종료합니다.\\n");
			break;
		}
		else {
			printf("\\n잘못된 메뉴 선택입니다. 다시 선택해주세요.\\n");
		}
	}

	free(emp_list);
	return 0;
}