false, 'msg' => 'invalid c_uid']); exit; } $rows = json_decode($_POST['data'] ?? '[]', true); if (!is_array($rows)) $rows = []; $clean = []; foreach ($rows as $r) { $start = trim($r['start'] ?? ''); $end = trim($r['end'] ?? ''); if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $start)) continue; // start date required if ($end === '') $end = $start; // empty end = same day if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $end)) continue; if ($end < $start) { $tmp = $start; $start = $end; $end = $tmp; } // auto-fix if reversed $note = isset($r['note']) ? trim($r['note']) : ''; $note = utf8_cut($note, 200); $clean[] = ['start' => $start, 'end' => $end, 'note' => $note]; } usort($clean, function ($a, $b) { return strcmp($a['start'], $b['start']); }); $uEsc = addslashes($user); // full replace // full replace — 현재/미래만 관리, 과거 행은 건드리지 않음 qry("DELETE FROM tbl_customer_closing_days WHERE ccd_customer_uid = " . $c_uid . " AND ccd_end_date >= CURDATE()"); foreach ($clean as $r) { $noteV = ($r['note'] === '') ? "NULL" : "'" . addslashes($r['note']) . "'"; qry("INSERT INTO tbl_customer_closing_days (ccd_customer_uid, ccd_start_date, ccd_end_date, ccd_note, ccd_createdby, ccd_created_at) VALUES ($c_uid, '{$r['start']}', '{$r['end']}', $noteV, '$uEsc', NOW())"); } echo json_encode(['ok' => true]); exit; } /* ===================================================================== * GET: load existing (current/future only — past is not shown) * ===================================================================== */ $existing = []; if ($c_uid > 0) { $res = 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($res)) { $existing[] = [ 'start' => $row['ccd_start_date'], 'end' => $row['ccd_end_date'], 'note' => $row['ccd_note'], ]; } } $existingJson = json_encode($existing, JSON_UNESCAPED_UNICODE); ?> Closing Days

Closing Days