Files
dnssash/adm/consultant_manage/components/reservation_check.php
T
2026-06-11 18:47:38 +09:00

402 lines
20 KiB
PHP

<?php
if (isset($_POST['action'])) {
include_once('../_common_con.php'); // 💡 [수정] 컴포넌트용 공통 파일 포함
header('Content-Type: application/json');
$action = $_POST['action'] ?? '';
// 예약 조회
if ($action === 'find_reservations') {
$customer_name = trim($_POST['customer_name'] ?? '');
$customer_phone = trim($_POST['customer_phone'] ?? '');
if (!$customer_name || !$customer_phone) {
echo json_encode(['success' => false, 'message' => '이름과 연락처를 모두 입력해주세요.']);
exit;
}
// 💡 [수정] 리소스 정보를 함께 조회하기 위해 LEFT JOIN 추가
$sql = " SELECT r.*, res.name as resource_name
FROM consultant_reservations r
LEFT JOIN consultant_resources res ON r.resource_id = res.id
WHERE r.customer_name = '" . sql_real_escape_string($customer_name) . "'
AND r.customer_phone = '" . sql_real_escape_string($customer_phone) . "'
AND r.is_deleted = 0
ORDER BY r.reservation_date DESC, r.reservation_time DESC ";
$result = sql_query($sql);
$reservations = [];
$cancel_deadline_hours = (int)consultant_get_config('cancel_deadline_hours', 24);
while ($row = sql_fetch_array($result)) {
$reservation_timestamp = strtotime($row['reservation_date'] . ' ' . $row['reservation_time']);
$cancellable_until = $reservation_timestamp - ($cancel_deadline_hours * 3600);
$row['is_cancellable'] = (time() < $cancellable_until && in_array($row['status'], ['payment_pending', 'reserved']));
$reservations[] = $row;
}
echo json_encode(['success' => true, 'data' => $reservations]);
exit;
}
// 예약 취소
if ($action === 'cancel_reservation') {
$reservation_id = (int)($_POST['reservation_id'] ?? 0);
$customer_name = trim($_POST['customer_name'] ?? '');
$customer_phone = trim($_POST['customer_phone'] ?? '');
if (!$reservation_id || !$customer_name || !$customer_phone) {
echo json_encode(['success' => false, 'message' => '필수 정보가 누락되었습니다.']);
exit;
}
$sql = " SELECT * FROM consultant_reservations
WHERE id = '{$reservation_id}'
AND customer_name = '" . sql_real_escape_string($customer_name) . "'
AND customer_phone = '" . sql_real_escape_string($customer_phone) . "'
AND is_deleted = 0 ";
$reservation = sql_fetch($sql);
if (!$reservation) {
echo json_encode(['success' => false, 'message' => '예약 정보를 찾을 수 없거나, 입력하신 정보와 일치하지 않습니다.']);
exit;
}
$cancel_deadline_hours = (int)consultant_get_config('cancel_deadline_hours', 24);
$reservation_timestamp = strtotime($reservation['reservation_date'] . ' ' . $reservation['reservation_time']);
$cancellable_until = $reservation_timestamp - ($cancel_deadline_hours * 3600);
if (time() >= $cancellable_until) {
echo json_encode(['success' => false, 'message' => '예약 취소 가능 시간이 지났습니다. 관리자에게 문의해주세요.']);
exit;
}
if (!in_array($reservation['status'], ['payment_pending', 'reserved'])) {
echo json_encode(['success' => false, 'message' => '이미 처리되었거나 취소된 예약입니다.']);
exit;
}
$sql_update = " UPDATE consultant_reservations
SET status = 'cancelled', updated_at = NOW()
WHERE id = '{$reservation_id}' ";
if (sql_query($sql_update)) {
consultant_log("고객 예약 취소: ID {$reservation_id} (고객: {$customer_name})");
echo json_encode(['success' => true, 'message' => '예약이 성공적으로 취소되었습니다.']);
} else {
echo json_encode(['success' => false, 'message' => '예약 취소 중 오류가 발생했습니다. 다시 시도해주세요.']);
}
exit;
}
}
if (!defined('_GNUBOARD_')) exit;
include_once(G5_ADMIN_PATH . '/consultant_manage/_common_con.php'); // 💡 [수정] 컴포넌트용 공통 파일 포함
// 💡 [추가] 상담 유형 한글 이름을 JavaScript에서 사용하기 위해 배열을 정의합니다.
$consultant_types_map = json_decode(consultant_get_config('consultation_types', '{"onsite":"현장 상담"}'), true);
if (!is_array($consultant_types_map)) {
$consultant_types_map = ['onsite' => '현장 상담']; // 파싱 실패 시 기본값
}
$ajax_url = G5_ADMIN_URL . '/consultant_manage/components/reservation_check.php';
?>
<!-- 예약 확인/취소 팝업 -->
<div id="reservation-check-popup" class="reservation-modal-overlay">
<div class="reservation-modal-content">
<div class="reservation-modal-header">
<h2>예약 확인 및 취소</h2>
<button type="button" class="reservation-modal-close" aria-label="팝업 닫기">&times;</button>
</div>
<div class="reservation-modal-body">
<div class="loading-overlay" style="display: none;">
<div class="loading-spinner"></div>
</div>
<!-- 💡 [개선] 조회 폼과 결과 화면을 분리합니다. -->
<div id="reservation-check-view-form">
<div class="check-form-wrap">
<p>예약 시 입력하신 이름과 연락처로 예약 내역을 조회할 수 있습니다.</p>
<form id="reservation-check-form">
<div class="form-group">
<label for="check_customer_name">예약자명</label>
<input type="text" id="check_customer_name" name="customer_name" class="form-control" required>
</div>
<div class="form-group">
<label for="check_customer_phone">연락처</label>
<input type="tel" id="check_customer_phone" name="customer_phone" class="form-control" placeholder="010-1234-5678" required>
</div>
<button type="submit" class="btn btn-primary">예약 조회</button>
</form>
</div>
</div>
<div id="reservation-check-view-results" style="display: none;">
<div class="results-header">
<h3>조회된 예약 내역</h3>
<button type="button" class="btn btn-secondary btn-sm" id="back-to-search-btn">새로 조회</button>
</div>
<div id="reservation-results">
<!-- 검색 결과가 여기에 표시됩니다. -->
</div>
</div>
</div>
</div>
</div>
<style>
/* 💡 [개선] 예약 신청 팝업과 스타일을 통일합니다. */
.check-form-wrap { background: #fff; padding: 20px; border-radius: 8px; margin-bottom: 20px; }
.form-group { margin-bottom: 15px; }
.form-group label { display: block; font-weight: 500; color: #495057; margin-bottom: 8px; }
.form-control { width: 100%; padding: 12px; border: 1px solid #ced4da; border-radius: 6px; font-size: 16px; box-sizing: border-box; } /* iOS 줌 방지 */
.form-control:focus { outline: none; border-color: #007bff; box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25); }
.btn {
display: block;
width: 100%;
padding: 12px 20px;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 16px;
font-weight: 600;
transition: background-color 0.2s ease;
/* 💡 [수정] 버튼 내 텍스트가 잘려보이는 현상을 해결합니다. */
height: auto;
line-height: 1.5;
}
.btn-primary { background: #007bff; color: white; }
.btn-primary:hover { background: #0056b3; }
#reservation-results { margin-top: 20px; }
.reservation-item { border: 1px solid #ddd; border-radius: 8px; padding: 20px; margin-bottom: 15px; }
.reservation-item-header { display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #eee; padding-bottom: 10px; margin-bottom: 10px; }
.reservation-status { padding: 4px 8px; border-radius: 12px; font-size: 12px; font-weight: bold; }
.status-reserved { background: #d4edda; color: #155724; }
/* 💡 [개선] 예약 상세 정보 스타일 */
.reservation-details-grid {
margin: 15px 0;
font-size: 15px; /* 글씨 크기 키움 */
line-height: 1.6; /* 줄 간격 확보 */
}
.detail-item { display: flex; margin-bottom: 10px; } /* 간격 조정 */
.detail-item .label { font-weight: 600; color: #555; width: 90px; flex-shrink: 0; } /* 너비 조정 */
.detail-item .value { color: #333; }
/* 💡 [추가] 예약 취소 버튼/안내문 영역 스타일 */
.reservation-actions {
margin-top: 20px;
text-align: right;
}
.reservation-actions .btn-danger {
width: auto; /* 버튼이 전체 너비를 차지하지 않도록 */
display: inline-block;
}
.cancel-notice { font-size: 13px !important; color: #666 !important; margin-top: 15px; text-align: right; padding: 10px; background-color: #fff; border-radius: 4px;}
/* 💡 [개선] 예약 조회 결과 헤더 스타일 */
.results-header { display: flex; align-items: center; gap: 1rem; margin-bottom: 15px; padding-bottom: 10px; border-bottom: 2px solid #eee; }
.results-header h3 { margin: 0; font-size: 18px; flex-grow: 1; } /* 제목이 남는 공간을 모두 차지하도록 설정 */
.results-header .btn { width: auto; flex-shrink: 0; } /* 헤더 안의 버튼은 내용만큼만 너비를 갖도록 설정 */
.btn-sm { padding: 5px 10px; font-size: 12px; }
.status-payment_pending { background: #fff3cd; color: #856404; }
.status-cancelled { background: #f8d7da; color: #721c24; }
.status-completed { background: #cce5ff; color: #004085; }
.btn-danger { background: #dc3545; color: white; }
.no-results { text-align: center; color: #666; padding: 40px 0; }
</style>
<script>
const ReservationCheckPopup = {
elements: {},
// 💡 [추가] PHP에서 전달받은 상담 유형 맵
consultantTypes: <?php echo json_encode($consultant_types_map, JSON_UNESCAPED_UNICODE); ?>,
state: {
name: '',
phone: ''
},
config: {
ajaxUrl: '<?php echo $ajax_url; ?>'
},
init() {
this.elements.popup = document.getElementById('reservation-check-popup');
if (!this.elements.popup) return;
this.elements.loading = this.elements.popup.querySelector('.loading-overlay');
this.elements.form = this.elements.popup.querySelector('#reservation-check-form');
this.elements.resultsContainer = this.elements.popup.querySelector('#reservation-results');
// 💡 [추가] 화면 전환용 요소
this.elements.formView = this.elements.popup.querySelector('#reservation-check-view-form');
this.elements.resultsView = this.elements.popup.querySelector('#reservation-check-view-results');
this.elements.backBtn = this.elements.popup.querySelector('#back-to-search-btn');
this.addEventListeners();
},
addEventListeners() {
const closeBtn = this.elements.popup.querySelector('.reservation-modal-close');
if (closeBtn) closeBtn.addEventListener('click', () => this.close());
this.elements.popup.addEventListener('click', e => {
if (e.target === this.elements.popup) this.close();
});
if (this.elements.form) {
this.elements.form.addEventListener('submit', e => {
e.preventDefault();
// 💡 [개선] 조회 버튼을 누를 때만 state를 업데이트하고, 그 state를 기반으로 조회합니다.
this.state.name = this.elements.form.querySelector('#check_customer_name').value;
this.state.phone = this.elements.form.querySelector('#check_customer_phone').value;
this.findReservations(); // state에 저장된 정보로 조회
});
}
if (this.elements.backBtn) {
this.elements.backBtn.addEventListener('click', () => {
this.elements.resultsView.style.display = 'none';
this.elements.formView.style.display = 'block';
this.elements.resultsContainer.innerHTML = '';
});
}
if (this.elements.resultsContainer) {
this.elements.resultsContainer.addEventListener('click', e => {
if (e.target.classList.contains('btn-cancel-reservation')) {
const reservationId = e.target.dataset.id;
if (confirm('정말 이 예약을 취소하시겠습니까?')) {
this.cancelReservation(reservationId);
}
}
});
}
},
open() {
if (!this.elements.popup) return;
this.elements.popup.classList.add('active');
document.body.style.overflow = 'hidden';
},
close() {
if (!this.elements.popup) return;
this.elements.popup.classList.remove('active');
document.body.style.overflow = '';
if (this.elements.form) this.elements.form.reset();
// 💡 [추가] 팝업을 닫을 때 화면 상태를 초기화합니다.
if (this.elements.resultsContainer) this.elements.resultsContainer.innerHTML = '';
if (this.elements.resultsView) this.elements.resultsView.style.display = 'none';
if (this.elements.formView) this.elements.formView.style.display = 'block';
},
async findReservations() {
this.showLoading();
// 💡 [개선] state에 저장된 정보로 FormData를 생성하여 일관성을 유지합니다.
const formData = new FormData();
formData.append('action', 'find_reservations');
formData.append('customer_name', this.state.name);
formData.append('customer_phone', this.state.phone);
try {
const response = await fetch(this.config.ajaxUrl, { method: 'POST', body: formData });
const result = await response.json();
if (result.success) {
// 💡 [추가] 조회 성공 시 화면을 전환합니다.
this.elements.formView.style.display = 'none';
this.elements.resultsView.style.display = 'block';
this.renderResults(result.data);
} else {
this.elements.resultsContainer.innerHTML = `<p class="no-results">${result.message}</p>`;
}
} catch (error) {
console.error("조회 오류:", error);
this.elements.resultsContainer.innerHTML = '<p class="no-results">조회 중 오류가 발생했습니다.</p>';
} finally {
this.hideLoading();
}
},
renderResults(reservations) {
if (reservations.length === 0) {
this.elements.resultsContainer.innerHTML = '<p class="no-results">조회된 예약 내역이 없습니다.</p>';
return;
}
// 💡 [수정] h3 태그는 정적 HTML로 이동했으므로 제거합니다.
let html = '';
const statusLabels = {
'payment_pending': '입금대기', 'reserved': '예약확정',
'completed': '상담완료', 'cancelled': '예약취소'
};
reservations.forEach(res => {
const date = new Date(res.reservation_date + ' ' + res.reservation_time);
const options = { year: 'numeric', month: 'long', day: 'numeric', weekday: 'long', hour: '2-digit', minute: '2-digit' };
const formattedDate = date.toLocaleDateString('ko-KR', options);
// 💡 [개선] 상담 유형의 한글 이름을 맵에서 찾아옵니다.
const consultationTypeName = this.consultantTypes[res.consultation_type] || res.consultation_type;
// 💡 [추가] 리소스 이름 표시
const resourceName = res.resource_name ? res.resource_name : '미지정 (빠른 배정)';
html += `
<div class="reservation-item" id="reservation-${res.id}">
<div class="reservation-item-header">
<strong>예약 번호: #${res.id}</strong>
<span class="reservation-status status-${res.status}">${statusLabels[res.status] || res.status}</span>
</div>
<div class="reservation-details-grid">
<div class="detail-item"><span class="label">상담 일시:</span><span class="value">${formattedDate}</span></div>
<div class="detail-item"><span class="label">상담 유형:</span><span class="value">${consultationTypeName}</span></div>
<div class="detail-item"><span class="label">담당/공간:</span><span class="value">${resourceName}</span></div>
</div>
<div class="reservation-actions">`;
if (res.is_cancellable) {
html += `<button type="button" class="btn btn-danger btn-cancel-reservation" data-id="${res.id}">예약 취소</button>`;
} else if (res.status !== 'cancelled' && res.status !== 'completed') {
html += `<p class="cancel-notice">예약 취소 가능 시간이 지났습니다. 변경/취소는 관리자에게 문의해주세요.</p>`;
}
html += `</div></div>`;
});
this.elements.resultsContainer.innerHTML = html;
},
async cancelReservation(reservationId) {
this.showLoading();
const formData = new FormData();
formData.append('action', 'cancel_reservation');
formData.append('reservation_id', reservationId);
formData.append('customer_name', this.state.name);
formData.append('customer_phone', this.state.phone);
try {
const response = await fetch(this.config.ajaxUrl, { method: 'POST', body: formData });
const result = await response.json();
alert(result.message);
if (result.success) {
await this.findReservations(); // 💡 [수정] await를 추가하여 목록 새로고침이 완료될 때까지 기다립니다.
}
} catch (error) {
console.error("취소 오류:", error);
alert('예약 취소 처리 중 오류가 발생했습니다.');
} finally {
// 💡 [제거] findReservations()가 자체적으로 로딩을 숨기므로 중복 호출을 제거합니다.
}
},
showLoading() { if (this.elements.loading) this.elements.loading.style.display = 'flex'; },
hideLoading() { if (this.elements.loading) this.elements.loading.style.display = 'none'; },
};
document.addEventListener('DOMContentLoaded', () => ReservationCheckPopup.init());
function openReservationCheckPopup() {
ReservationCheckPopup.open();
}
</script>