Files
dnssash/adm/order_manage/expert_visit_schedule.php
2026-06-11 18:47:38 +09:00

285 lines
14 KiB
PHP

<?php
$sub_menu = "800850"; // 메뉴 코드 (admin.menu800.order_manage.php와 일치)
include_once('./_common.php');
auth_check($auth[$sub_menu], 'r');
$g5['title'] = '전문가 방문 스케줄 관리';
// --- 액션 처리 (저장, 삭제) ---
$action = isset($_REQUEST['action']) ? clean_xss_tags($_REQUEST['action']) : '';
$id = isset($_REQUEST['id']) ? (int)$_REQUEST['id'] : 0;
if ($action && $_SERVER['REQUEST_METHOD'] === 'POST') {
auth_check($auth[$sub_menu], 'w');
if ($action === 'save') {
$expert_id = $_POST['expert_id'] ?? '';
$rule_type = $_POST['rule_type'] ?? 'weekly';
$day_of_week = ($rule_type === 'weekly') ? (int)($_POST['day_of_week'] ?? 0) : 'NULL';
$specific_date = ($rule_type === 'specific') ? clean_xss_tags($_POST['specific_date']) : '';
$start_time = clean_xss_tags($_POST['start_time']);
$end_time = clean_xss_tags($_POST['end_time']);
$time_slot = (int)$_POST['time_slot'];
$max_persons = (int)$_POST['max_persons'];
$is_available = (int)$_POST['is_available'];
$temp_2 = clean_xss_tags($_POST['temp_2']); // 휴무사유
$temp_3 = clean_xss_tags($_POST['temp_3']); // 특별일정명
$sql_expert_id = $expert_id ? "'" . sql_real_escape_string($expert_id) . "'" : "NULL";
$sql_specific_date = $specific_date ? "'" . sql_real_escape_string($specific_date) . "'" : "NULL";
$sql_common = "
expert_id = {$sql_expert_id},
day_of_week = {$day_of_week},
specific_date = {$sql_specific_date},
start_time = '" . sql_real_escape_string($start_time) . "',
end_time = '" . sql_real_escape_string($end_time) . "',
time_slot = '{$time_slot}',
max_persons = '{$max_persons}',
is_available = '{$is_available}',
temp_2 = '" . sql_real_escape_string($temp_2) . "',
temp_3 = '" . sql_real_escape_string($temp_3) . "',
updated_at = NOW(),
updated_by = '{$member['mb_id']}'
";
if ($id > 0) { // 수정
$sql = "UPDATE expert_visit_schedules SET {$sql_common} WHERE id = '{$id}'";
} else { // 생성
$sql = "INSERT INTO expert_visit_schedules SET {$sql_common}, created_at = NOW(), created_by = '{$member['mb_id']}'";
}
sql_query($sql);
goto_url('./expert_visit_schedule.php');
} elseif ($action === 'delete') {
if ($id > 0) {
sql_query("UPDATE expert_visit_schedules SET is_deleted = 1, updated_at = NOW(), updated_by = '{$member['mb_id']}' WHERE id = '{$id}'");
}
goto_url('./expert_visit_schedule.php');
}
}
$schedule_to_edit = null;
if ($id > 0) {
$schedule_to_edit = sql_fetch("SELECT * FROM expert_visit_schedules WHERE id = '{$id}'");
}
// --- 데이터 조회 ---
$experts_result = sql_query("SELECT mb_id, mb_name FROM {$g5['member_table']} WHERE mb_level = 8 AND mb_leave_date = '' ORDER BY mb_name ASC");
$experts = [];
while($row = sql_fetch_array($experts_result)) {
$experts[] = $row;
}
$schedules_result = sql_query("SELECT * FROM expert_visit_schedules WHERE is_deleted = 0 ORDER BY specific_date DESC, day_of_week ASC, start_time ASC");
$schedules = [];
while($row = sql_fetch_array($schedules_result)) {
$schedules[] = $row;
}
$week_days = [1 => '월요일', 2 => '화요일', 3 => '수요일', 4 => '목요일', 5 => '금요일', 6 => '토요일', 7 => '일요일'];
include_once(G5_ADMIN_PATH . '/admin.head.php');
?>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
<div class="local_desc01 local_desc">
<p>
전문가가 방문 가능한 시간을 설정합니다. '요일별' 규칙은 주간 반복 스케줄이며, '특정일' 규칙은 해당 날짜에만 적용되는 우선순위가 높은 스케줄입니다.<br>
'예약 가능'을 '아니오'로 설정하면 해당 시간을 휴무로 처리할 수 있습니다.
</p>
</div>
<!-- 스케줄 등록/수정 폼 -->
<form name="fschedule" method="post">
<input type="hidden" name="action" value="save">
<input type="hidden" name="id" value="<?php echo $schedule_to_edit['id'] ?? 0; ?>">
<div class="tbl_frm01 tbl_wrap">
<table>
<caption><?php echo $id ? '스케줄 수정' : '새 스케줄 등록'; ?></caption>
<colgroup>
<col class="grid_4">
<col>
</colgroup>
<tbody>
<tr>
<th scope="row"><label for="expert_id">전문가</label></th>
<td>
<select name="expert_id" id="expert_id">
<option value="">전체 전문가 공통</option>
<?php foreach ($experts as $c): ?>
<option value="<?php echo $c['mb_id']; ?>" <?php echo get_selected($schedule_to_edit['expert_id'] ?? '', $c['mb_id']); ?>><?php echo htmlspecialchars($c['mb_name']); ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<th scope="row">규칙 종류</th>
<td>
<label><input type="radio" name="rule_type" value="weekly" <?php echo (($schedule_to_edit['specific_date'] ?? '') ? '' : 'checked'); ?>> 요일별</label>
<label><input type="radio" name="rule_type" value="specific" <?php echo (($schedule_to_edit['specific_date'] ?? '') ? 'checked' : ''); ?>> 특정일</label>
</td>
</tr>
<tr id="row_day_of_week">
<th scope="row"><label for="day_of_week">요일</label></th>
<td>
<select name="day_of_week" id="day_of_week">
<?php foreach ($week_days as $num => $day): ?>
<option value="<?php echo $num; ?>" <?php echo get_selected($schedule_to_edit['day_of_week'] ?? '', $num); ?>><?php echo $day; ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr id="row_specific_date" style="display:none;">
<th scope="row"><label for="specific_date">특정 날짜</label></th>
<td>
<input type="text" name="specific_date" id="specific_date" value="<?php echo $schedule_to_edit['specific_date'] ?? ''; ?>" class="frm_input" style="width:120px;">
</td>
</tr>
<tr>
<th scope="row"><label for="start_time">방문 시간</label></th>
<td>
<input type="time" name="start_time" id="start_time" value="<?php echo $schedule_to_edit['start_time'] ?? '09:00'; ?>" required> ~
<input type="time" name="end_time" id="end_time" value="<?php echo $schedule_to_edit['end_time'] ?? '18:00'; ?>" required>
</td>
</tr>
<tr>
<th scope="row"><label for="time_slot">예약 단위</label></th>
<td>
<input type="number" name="time_slot" id="time_slot" value="<?php echo $schedule_to_edit['time_slot'] ?? '60'; ?>" class="frm_input" style="width:80px;" required> 분
</td>
</tr>
<tr>
<th scope="row"><label for="max_persons">최대 인원</label></th>
<td>
<input type="number" name="max_persons" id="max_persons" value="<?php echo $schedule_to_edit['max_persons'] ?? '1'; ?>" class="frm_input" style="width:80px;" required> 명
</td>
</tr>
<tr>
<th scope="row"><label for="is_available">예약 가능</label></th>
<td>
<select name="is_available" id="is_available">
<option value="1" <?php echo get_selected($schedule_to_edit['is_available'] ?? '1', '1'); ?>>예</option>
<option value="0" <?php echo get_selected($schedule_to_edit['is_available'] ?? '1', '0'); ?>>아니오 (휴무)</option>
</select>
</td>
</tr>
<tr id="row_off_reason" style="display:none;">
<th scope="row"><label for="temp_2">휴무 사유</label></th>
<td>
<input type="text" name="temp_2" id="temp_2" value="<?php echo htmlspecialchars($schedule_to_edit['temp_2'] ?? ''); ?>" class="frm_input" style="width:300px;" placeholder="예: 정기 휴무">
</td>
</tr>
<tr>
<th scope="row"><label for="temp_3">일정명</label></th>
<td>
<input type="text" name="temp_3" id="temp_3" value="<?php echo htmlspecialchars($schedule_to_edit['temp_3'] ?? ''); ?>" class="frm_input" style="width:300px;" placeholder="예: 오전 근무, 단축 운영">
</td>
</tr>
</tbody>
</table>
</div>
<div class="btn_confirm01 btn_confirm">
<input type="submit" value="<?php echo $id ? '수정' : '등록'; ?>" class="btn_submit">
<?php if ($id): ?>
<a href="./expert_visit_schedule.php" class="btn_cancel">새로 등록</a>
<?php endif; ?>
</div>
</form>
<!-- 스케줄 목록 -->
<div class="tbl_head01 tbl_wrap" style="margin-top: 30px;">
<table>
<caption>스케줄 목록</caption>
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">전문가</th>
<th scope="col">규칙</th>
<th scope="col">시간</th>
<th scope="col">예약단위</th>
<th scope="col">최대인원</th>
<th scope="col">상태</th>
<th scope="col">관리</th>
</tr>
</thead>
<tbody>
<?php if (empty($schedules)): ?>
<tr><td colspan="8" class="empty_table">등록된 스케줄이 없습니다.</td></tr>
<?php else: ?>
<?php foreach ($schedules as $sch): ?>
<tr>
<td><?php echo $sch['id']; ?></td>
<td><?php echo $sch['expert_id'] ? (get_member($sch['expert_id'])['mb_name'] ?? $sch['expert_id']) : '전체 공통'; ?></td>
<td>
<?php
if ($sch['specific_date']) {
echo '<strong>[특정일]</strong> ' . $sch['specific_date'] . ($sch['temp_3'] ? ' ('.$sch['temp_3'].')' : '');
} else {
echo '<strong>[요일별]</strong> ' . ($week_days[$sch['day_of_week']] ?? '알수없음');
}
?>
</td>
<td><?php echo substr($sch['start_time'], 0, 5) . ' ~ ' . substr($sch['end_time'], 0, 5); ?></td>
<td><?php echo $sch['time_slot']; ?>분</td>
<td><?php echo $sch['max_persons']; ?>명</td>
<td>
<?php if ($sch['is_available']): ?>
<span style="color:blue;">예약가능</span>
<?php else: ?>
<span style="color:red;">휴무</span>
<?php if($sch['temp_2']) echo ' (' . htmlspecialchars($sch['temp_2']) . ')'; ?>
<?php endif; ?>
</td>
<td class="td_mng td_mng_s">
<a href="?id=<?php echo $sch['id']; ?>" class="btn btn_03">수정</a>
<form name="fdelete_<?php echo $sch['id']; ?>" method="post" style="display:inline;">
<input type="hidden" name="action" value="delete">
<input type="hidden" name="id" value="<?php echo $sch['id']; ?>">
<button type="submit" class="btn btn_02" onclick="return confirm('정말로 이 스케줄을 삭제하시겠습니까?');">삭제</button>
</form>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
// 날짜 선택기 초기화
flatpickr("#specific_date", { dateFormat: "Y-m-d" });
const ruleTypeRadios = document.querySelectorAll('input[name="rule_type"]');
const dayOfWeekRow = document.getElementById('row_day_of_week');
const specificDateRow = document.getElementById('row_specific_date');
const isAvailableSelect = document.getElementById('is_available');
const offReasonRow = document.getElementById('row_off_reason');
function toggleRuleTypeFields() {
const selectedType = document.querySelector('input[name="rule_type"]:checked').value;
dayOfWeekRow.style.display = (selectedType === 'weekly') ? '' : 'none';
specificDateRow.style.display = (selectedType === 'specific') ? '' : 'none';
}
function toggleOffReasonField() {
offReasonRow.style.display = (isAvailableSelect.value === '0') ? '' : 'none';
}
ruleTypeRadios.forEach(radio => radio.addEventListener('change', toggleRuleTypeFields));
isAvailableSelect.addEventListener('change', toggleOffReasonField);
// 페이지 로드 시 초기 상태 설정
toggleRuleTypeFields();
toggleOffReasonField();
});
</script>
<?php
include_once(G5_ADMIN_PATH . '/admin.tail.php');
?>