<?php
include_once(__DIR__.'/../_common.php');
if (!defined('_GNUBOARD_')) exit;
if (!$is_admin) die('권한이 없습니다.');
$bo_table = isset($_GET['bo_table']) ? preg_replace('/[^a-zA-Z0-9_]/', '', $_GET['bo_table']) : '';
$wr_id = isset($_GET['wr_id']) ? (int)$_GET['wr_id'] : 0;
if ($bo_table === '' || $wr_id < 1) die('잘못된 접근입니다.');
$write_table = $g5['write_prefix'] . $bo_table;
$write = sql_fetch(" select wr_subject from {$write_table} where wr_id = '{$wr_id}' ");
if (!isset($write['wr_subject']) || trim($write['wr_subject']) === '') die('대상 글이 없습니다.');
$event_name = trim($write['wr_subject']);
$event = sql_fetch(" SELECT * FROM information_schema.EVENTS WHERE EVENT_NAME = '".sql_real_escape_string($event_name)."' ORDER BY EVENT_SCHEMA LIMIT 1 ");
if (!isset($event['EVENT_NAME'])) die('이벤트 정보를 찾지 못했습니다.');
function x2_dt_local($value)
{
$value = trim((string)$value);
if ($value === '' || $value === '0000-00-00 00:00:00') return '';
$ts = strtotime($value);
return $ts ? date('Y-m-d\TH:i', $ts) : '';
}
$event_schema = (string)$event['EVENT_SCHEMA'];
$event_status = strtoupper((string)$event['STATUS']);
$event_type = strtoupper((string)$event['EVENT_TYPE']);
$interval_value = isset($event['INTERVAL_VALUE']) ? (string)$event['INTERVAL_VALUE'] : '';
$interval_field = strtoupper((string)$event['INTERVAL_FIELD']);
$execute_at = x2_dt_local(isset($event['EXECUTE_AT']) ? $event['EXECUTE_AT'] : '');
$starts = x2_dt_local(isset($event['STARTS']) ? $event['STARTS'] : '');
$ends = x2_dt_local(isset($event['ENDS']) ? $event['ENDS'] : '');
$completion = strtoupper((string)$event['ON_COMPLETION']);
$definer = (string)$event['DEFINER'];
$comment = (string)$event['EVENT_COMMENT'];
$definition = (string)$event['EVENT_DEFINITION'];
$popup_dir_url = rtrim(str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])), '/');
$update_url = $popup_dir_url.'/event.edit.update.php';
?>
<!doctype html>
<html lang="ko">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>마리아DB 이벤트 편집</title>
<style>
body{margin:0;padding:20px;background:#020617;color:#e2e8f0;font-family:Pretendard,sans-serif}
.wrap{max-width:940px;margin:0 auto;background:#0b1220;border:1px solid #1e293b;border-radius:8px;box-shadow:0 14px 30px rgba(0,0,0,.45)}
.head{padding:16px 20px;border-bottom:1px solid #1e293b;background:#111827;font-weight:800}
.sec{padding:18px 20px;border-bottom:1px solid #1e293b}
.row{display:grid;grid-template-columns:160px 1fr;gap:10px;align-items:center;margin-bottom:10px}
.row:last-child{margin-bottom:0}
label{font-size:.8rem;color:#38bdf8;font-weight:700}
input,select,textarea{width:100%;box-sizing:border-box;background:#050505;border:1px solid #334155;color:#fff;border-radius:6px;padding:9px 12px}
textarea{min-height:160px;resize:vertical}
.readonly{opacity:.8}
.grid2{display:grid;grid-template-columns:1fr 1fr;gap:10px}
.actions{padding:16px 20px;display:flex;justify-content:flex-end;gap:8px}
.btn{border:1px solid #334155;background:#0f172a;color:#e2e8f0;padding:10px 14px;border-radius:6px;cursor:pointer;font-weight:700}
.btn.save{background:#38bdf8;color:#020617;border:none}
.hide{display:none}
.msg{font-size:.78rem;color:#94a3b8;margin-top:6px}
</style>
</head>
<body>
<div class="wrap">
<div class="head">마리아DB 이벤트 편집 (이벤트명 고정)</div>
<form id="eventEditForm">
<input type="hidden" name="bo_table" value="<?php echo $bo_table; ?>">
<input type="hidden" name="wr_id" value="<?php echo $wr_id; ?>">
<input type="hidden" name="event_schema" value="<?php echo get_text($event_schema); ?>">
<div class="sec">
<div class="row">
<label>이벤트 이름</label>
<input type="text" class="readonly" value="<?php echo get_text($event_name); ?>" readonly>
<input type="hidden" name="event_name" value="<?php echo get_text($event_name); ?>">
</div>
<div class="grid2">
<div class="row">
<label>상태</label>
<select name="status">
<option value="ENABLE" <?php echo $event_status === 'ENABLED' ? 'selected' : ''; ?>>ENABLE</option>
<option value="DISABLE" <?php echo $event_status !== 'ENABLED' ? 'selected' : ''; ?>>DISABLE</option>
</select>
</div>
<div class="row">
<label>완료 처리</label>
<select name="completion">
<option value="NOT PRESERVE" <?php echo $completion !== 'PRESERVE' ? 'selected' : ''; ?>>NOT PRESERVE</option>
<option value="PRESERVE" <?php echo $completion === 'PRESERVE' ? 'selected' : ''; ?>>PRESERVE</option>
</select>
</div>
</div>
<div class="row">
<label>이벤트 종류</label>
<select name="event_type" id="event_type">
<option value="RECURRING" <?php echo $event_type === 'RECURRING' ? 'selected' : ''; ?>>RECURRING</option>
<option value="ONE TIME" <?php echo $event_type === 'ONE TIME' ? 'selected' : ''; ?>>ONE TIME</option>
</select>
</div>
</div>
<div class="sec" id="scheduleRecurring">
<div class="grid2">
<div class="row">
<label>실행 간격 값</label>
<input type="number" min="1" name="interval_value" value="<?php echo get_text($interval_value); ?>">
</div>
<div class="row">
<label>실행 간격 단위</label>
<select name="interval_field">
<?php
$fields = ['SECOND','MINUTE','HOUR','DAY','WEEK','MONTH','YEAR'];
foreach($fields as $f) {
echo '<option value="'.$f.'" '.($interval_field === $f ? 'selected' : '').'>'.$f.'</option>';
}
?>
</select>
</div>
</div>
<div class="grid2">
<div class="row">
<label>시작</label>
<input type="datetime-local" name="starts" value="<?php echo $starts; ?>">
</div>
<div class="row">
<label>완료</label>
<input type="datetime-local" name="ends" value="<?php echo $ends; ?>">
</div>
</div>
</div>
<div class="sec" id="scheduleOnetime">
<div class="row">
<label>실행 시각(AT)</label>
<input type="datetime-local" name="execute_at" value="<?php echo $execute_at; ?>">
</div>
</div>
<div class="sec">
<div class="row">
<label>디파이너</label>
<input type="text" name="definer" value="<?php echo get_text($definer); ?>" placeholder="user@host">
</div>
<div class="row">
<label>설명</label>
<textarea name="event_comment"><?php echo get_text($comment, 0); ?></textarea>
</div>
<div class="row">
<label>정의</label>
<textarea name="event_definition"><?php echo htmlspecialchars($definition); ?></textarea>
</div>
<div class="msg">저장 시 ALTER EVENT가 실행됩니다.</div>
</div>
<div class="actions">
<button type="button" class="btn" onclick="window.close();">닫기</button>
<button type="submit" class="btn save">저장</button>
</div>
</form>
</div>
<script>
(function() {
var form = document.getElementById('eventEditForm');
var typeEl = document.getElementById('event_type');
var recurring = document.getElementById('scheduleRecurring');
var onetime = document.getElementById('scheduleOnetime');
function toggleType() {
var isOneTime = typeEl.value === 'ONE TIME';
recurring.style.display = isOneTime ? 'none' : 'block';
onetime.style.display = isOneTime ? 'block' : 'none';
recurring.querySelectorAll('input,select').forEach(function(el){ el.disabled = isOneTime; });
onetime.querySelectorAll('input').forEach(function(el){ el.disabled = !isOneTime; });
}
typeEl.addEventListener('change', toggleType);
toggleType();
form.addEventListener('submit', function(e) {
e.preventDefault();
var fd = new FormData(form);
fetch('<?php echo $update_url; ?>', {
method: 'POST',
body: fd,
credentials: 'same-origin'
})
.then(function(res){ return res.json(); })
.then(function(json){
if (!json || !json.ok) {
alert(json && json.message ? json.message : '저장 실패');
return;
}
alert('저장되었습니다.');
if (window.opener && !window.opener.closed) {
window.opener.location.reload();
}
window.close();
})
.catch(function(){ alert('요청 실패'); });
});
})();
</script>
</body>
</html>