<?php
declare(strict_types=1);
require_once __DIR__ . '/app/Helpers/bootstrap.php';
require_once __DIR__ . '/app/Helpers/trip-changes.php';
ob_start(static function (string $html): string {
    if (language() === 'de') { return $html; }
    return strtr($html, ['<html lang="de">' => '<html lang="en">', '<title>Programmpunkt hinzufügen</title>' => '<title>' . htmlspecialchars(t('custom_points.add_title'), ENT_QUOTES, 'UTF-8') . '</title>', '>← Zurück<' => '>' . t('users.back') . '<', '>Eigenen Programmpunkt hinzufügen<' => '>' . t('custom_points.add_title') . '<', '>Programmpunkt<select' => '>' . t('custom_points.select') . '<select', '>Bitte auswählen …<' => '>' . t('team.select') . '<', '>Uhrzeit<input' => '>' . t('custom_points.time') . '<input', '>Zum Reiseplan hinzufügen<' => '>' . t('custom_points.add_to_trip') . '<']);
});
$user=require_login();$dayId=filter_input(INPUT_GET,'id',FILTER_VALIDATE_INT,['options'=>['min_range'=>1]]);if(!$dayId){exit(t('custom_points.error_not_found'));}
$q=db()->prepare('SELECT d.id,d.trip_id,d.day_number,d.title,t.created_by FROM '.table('trip_days').' d JOIN '.table('trips').' t ON t.id=d.trip_id WHERE d.id=:id');$q->execute([':id'=>$dayId]);$day=$q->fetch();if(!$day){exit(t('custom_points.error_not_found'));}
$canEdit=$user['role']==='main_admin'||(int)$day['created_by']===(int)$user['id'];
if(!$canEdit){$adminCheck=db()->prepare('SELECT 1 FROM '.table('trip_admins').' WHERE trip_id=:trip_id AND user_id=:user_id');$adminCheck->execute([':trip_id'=>$day['trip_id'],':user_id'=>$user['id']]);$canEdit=(bool)$adminCheck->fetchColumn();}
if(!$canEdit){http_response_code(403);exit(t('image.error_access'));}
$items=db()->query("SELECT id,name,city FROM ".table('custom_points')." WHERE status='active' ORDER BY name")->fetchAll();$error=null;
if($_SERVER['REQUEST_METHOD']==='POST'){verify_csrf();$id=filter_input(INPUT_POST,'entry_id',FILTER_VALIDATE_INT,['options'=>['min_range'=>1]]);$time=trim((string)($_POST['time']??''));if(!$id){$error=t('custom_points.error_select');}else{$nameQuery=db()->prepare('SELECT name FROM '.table('custom_points')." WHERE id=:id AND status='active'");$nameQuery->execute([':id'=>$id]);$entryName=$nameQuery->fetchColumn();if(!$entryName){$error=t('custom_points.error_unavailable');}else{$order=(int)db()->query('SELECT COALESCE(MAX(sort_order),0)+1 FROM '.table('trip_day_items').' WHERE trip_day_id='.(int)$dayId)->fetchColumn();$q=db()->prepare("INSERT INTO ".table('trip_day_items')." (trip_day_id,item_type,reference_id,time,sort_order) VALUES (:day,'catalog_custom',:id,:time,:order)");$q->execute([':day'=>$dayId,':id'=>$id,':time'=>$time?:null,':order'=>$order]);$changeId=triptale_log_trip_change((int)$day['trip_id'],(int)$user['id'],'item_added','Eigener Programmpunkt „'.$entryName.'“ wurde zu Tag '.(int)$day['day_number'].' hinzugefügt.');header('Location: /day-program.php?id='.(int)$dayId.'&saved=1');exit;}}}
?><!doctype html><html lang="de"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Programmpunkt hinzufügen</title><style>body{font-family:system-ui,sans-serif;background:#f4f6f8;padding:24px}main{max-width:650px;margin:auto;background:#fff;padding:24px;border-radius:14px}label{display:block;margin-top:14px;font-weight:700}input,select{box-sizing:border-box;width:100%;padding:10px;margin-top:5px}.button,button{display:inline-block;padding:10px 13px;background:#126e82;color:#fff;border:0;border-radius:7px;text-decoration:none;font-weight:700}.back{background:#e3ebee;color:#126e82}.error{background:#ffe6e3;padding:10px}.save{margin-top:20px}</style><main><a class="button back" href="/day-program.php?id=<?= (int)$dayId ?>">← Zurück</a><h1>Eigenen Programmpunkt hinzufügen</h1><?php if($error):?><p class="error"><?= htmlspecialchars($error,ENT_QUOTES,'UTF-8') ?></p><?php endif;?><form method="post"><input type="hidden" name="csrf_token" value="<?= htmlspecialchars(csrf_token(),ENT_QUOTES,'UTF-8') ?>"><label>Programmpunkt<select name="entry_id" required><option value="">Bitte auswählen …</option><?php foreach($items as$item):?><option value="<?= (int)$item['id'] ?>"><?= htmlspecialchars($item['name'].($item['city']?' · '.$item['city']:''),ENT_QUOTES,'UTF-8') ?></option><?php endforeach;?></select></label><label>Uhrzeit<input type="time" name="time"></label><button class="save">Zum Reiseplan hinzufügen</button></form></main>
