goiintra/public_html/lib/customer_access_display.php

136 lines
5.9 KiB
PHP

<?php
/* =====================================================================
* /lib/customer_access_display.php
* - Access Time / Closing Days 읽기 전용 표시 조각 (두 개의 <tr> 반환)
* - customer_detail.php 와 동일한 칩 스타일 (CSS 는 부모 화면이 보유)
* - AJAX 로 모달 등에 주입해서 재사용 (map.php 등)
* - 각 박스는 .box-clickable[data-popup] 이라 부모의 클릭 핸들러가 편집 팝업을 연다
* ===================================================================== */
include getenv("DOCUMENT_ROOT")."/include/session_include.php";
require_once getenv("DOCUMENT_ROOT")."/assets/dbCon.php";
require_once getenv("DOCUMENT_ROOT")."/lib/customer_open_status.php";
$c_uid = isset($_GET['c_uid']) ? intval($_GET['c_uid']) : 0;
// 지금 닫힘 여부 (Access Time 제목 옆 경고 배지용)
$openStat = ($c_uid > 0) ? customerIsOpenNow($c_uid) : ['restricted'=>false,'open'=>true,'reason'=>''];
$closedNow = (!empty($openStat['restricted']) && empty($openStat['open']));
// Restricted 토글 현재값
$isRestricted = false;
if ($c_uid > 0) {
$rR = qry("SELECT c_time_restricted AS f FROM tbl_customer WHERE c_uid = $c_uid LIMIT 1");
$rowR = fetch_array($rR);
$isRestricted = ($rowR && $rowR['f'] === 'Y');
}
$accessTimeMap = [];
$closingDays = [];
if ($c_uid > 0) {
$res = qry("SELECT ca_day_of_week, ca_seq, ca_open_time, ca_close_time, ca_note
FROM tbl_customer_accesstime
WHERE ca_customer_uid = $c_uid AND ca_status = 'A'
ORDER BY ca_day_of_week, ca_seq");
while ($row = fetch_array($res)) { $accessTimeMap[(int)$row['ca_day_of_week']][] = $row; }
// 지나간 휴무(종료일 < 오늘)는 제외
$res2 = qry("SELECT ccd_start_date, ccd_end_date, ccd_note
FROM tbl_customer_closing_days
WHERE ccd_customer_uid = $c_uid AND ccd_end_date >= CURDATE()
ORDER BY ccd_start_date");
while ($row = fetch_array($res2)) { $closingDays[] = $row; }
}
$dowNames = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
$todayDow = (int)date('w'); // 0=Sun .. 6=Sat (서버 기준, open-status 와 동일)
if (!function_exists('fmtAccT')) { function fmtAccT($t){ return $t ? substr($t, 0, 5) : ''; } }
?>
<tr>
<td class="td-title-info">Access Time<?php if ($closedNow): ?> <span class="closed-badge" title="Closed right now">!</span><?php endif; ?><span class="restricted-toggle" style="float:right; font-weight:normal; font-size:13px;">Restricted <input type="checkbox" class="ca-restricted-chk" data-cuid="<?=$c_uid?>" <?= $isRestricted ? 'checked' : '' ?>></span></td>
<td colspan="2" class="td-text-info">
<div class="at-field box-clickable" data-popup="accesstime" title="Click to edit">
<div class="at-wrap">
<?php for ($d = 0; $d < 7; $d++):
$wins = isset($accessTimeMap[$d]) ? $accessTimeMap[$d] : [];
?>
<div class="at-row<?= empty($wins) ? ' at-off' : '' ?><?= ($d === $todayDow) ? ' at-today' : '' ?>">
<span class="at-dow<?= ($d == 0 || $d == 6) ? ' at-wknd' : '' ?>"><?=$dowNames[$d]?></span>
<span class="at-win">
<?php if (empty($wins)): ?>
<span class="at-dash">&mdash;</span>
<?php else: foreach ($wins as $w):
$o = fmtAccT($w['ca_open_time']);
$c = fmtAccT($w['ca_close_time']);
$label = $c ? ($o.'&ndash;'.$c) : ($o.' &rarr;');
?>
<span class="at-chip"<?= !empty($w['ca_note']) ? ' title="'.htmlspecialchars($w['ca_note'], ENT_QUOTES).'"' : '' ?>><?=$label?></span>
<?php endforeach; endif; ?>
</span>
</div>
<?php endfor; ?>
</div>
</div>
</td>
</tr>
<tr>
<td class="td-title-info">Closing Days</td>
<td colspan="2" class="td-text-info">
<div class="cd-field box-clickable" data-popup="closingdays" title="Click to edit">
<div class="cd-wrap">
<?php if (empty($closingDays)): ?>
<span class="cd-empty">&mdash;</span>
<?php else: foreach ($closingDays as $cd):
$s = $cd['ccd_start_date'];
$e = $cd['ccd_end_date'];
$range = ($s === $e) ? $s : ($s.' ~ '.$e);
?>
<span class="cd-chip">
<span class="cd-range"><?=$range?></span>
<?php if (!empty($cd['ccd_note'])): ?><em class="cd-note"><?=htmlspecialchars($cd['ccd_note'])?></em><?php endif; ?>
</span>
<?php endforeach; endif; ?>
</div>
</div>
</td>
</tr>
<script>
/* Restricted 체크박스 → c_time_restricted 즉시 저장 (map / shortInfo 공용)
- 조각이 .load() 로 주입될 때마다 이 스크립트가 실행되지만,
window 플래그로 문서 위임 바인딩은 한 번만 건다. */
if (!window.__caRestrictedBound) {
window.__caRestrictedBound = true;
jQuery(document).on('change', '.ca-restricted-chk', function () {
var $chk = jQuery(this);
var cuid = $chk.data('cuid');
var val = this.checked ? 'Y' : 'N';
if (!cuid) return;
$chk.prop('disabled', true);
jQuery.ajax({
url: '/assets/api/customer.php',
method: 'POST',
dataType: 'json',
data: { action: 'patchTimeRestricted', c_uid: cuid, value: val },
success: function (res) {
if (res && res.ok) {
// 표시 조각 다시 로드 → "!" 배지/상태 갱신
var $tb = $chk.closest('tbody');
if ($tb.attr('id')) {
$tb.load('/lib/customer_access_display.php?c_uid=' + encodeURIComponent(cuid));
} else {
$chk.prop('disabled', false);
}
} else {
$chk.prop('checked', !$chk.prop('checked')).prop('disabled', false);
alert('Failed to update Restricted.');
}
},
error: function () {
$chk.prop('checked', !$chk.prop('checked')).prop('disabled', false);
alert('Failed to update Restricted.');
}
});
});
}
</script>