<?php
declare(strict_types=1);
require_once __DIR__ . '/app/Helpers/bootstrap.php';
require_once __DIR__ . '/app/Helpers/categories.php';
$user=require_login();if(!in_array($user['role'],['main_admin','admin'],true)){http_response_code(403);exit(t('image.error_access'));}
$types=['attraction'=>t('dashboard.attractions'),'accommodation'=>t('dashboard.accommodations'),'restaurant'=>t('dashboard.food_drink'),'custom'=>t('dashboard.custom_points')];$type=(string)($_GET['type']??'');$id=(int)($_GET['id']??0);if(!isset($types[$type])||$id<1){header('Location: /categories.php');exit;}$q=db()->prepare('SELECT id,name FROM '.table('categories').' WHERE id=:id AND type=:type');$q->execute([':id'=>$id,':type'=>$type]);$category=$q->fetch();if(!$category){header('Location: /categories.php?type='.urlencode($type));exit;}$languages=triptale_available_languages();$error='';$notice='';$translations=triptale_category_translations($id);
if($_SERVER['REQUEST_METHOD']==='POST'){verify_csrf();$submitted=[];foreach($languages as$code=>$label){$name=trim((string)($_POST['translation_'.$code]??''));if($name!==''){$check=db()->prepare('SELECT c.id FROM '.table('categories').' c INNER JOIN '.table('category_translations').' ct ON ct.category_id=c.id WHERE c.type=:type AND ct.language_code=:language AND ct.name=:name AND c.id<>:id LIMIT 1');$check->execute([':type'=>$type,':language'=>$code,':name'=>$name,':id'=>$id]);if($check->fetchColumn()){$error=t('category.error_duplicate');break;}}$submitted['translation_'.$code]=$name;}if($error===''){triptale_save_category_translations($id,$submitted);header('Location: /category-translations.php?type='.urlencode($type).'&id='.$id.'&saved=1');exit;}}
if(isset($_GET['saved']))$notice=t('category.saved');
?><!doctype html><html lang="<?=htmlspecialchars(language(),ENT_QUOTES,'UTF-8')?>"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title><?=htmlspecialchars(t('category.translate'),ENT_QUOTES,'UTF-8')?> · TripTale</title><link rel="stylesheet" href="/assets/triptale-ui.css?v=19"><style>body{font-family:system-ui,sans-serif;background:#f4f6f8;margin:0;padding:32px;color:#17202a}main{max-width:680px;margin:auto;background:#fff;padding:28px;border-radius:14px}.button,.save{display:inline-block;padding:9px 12px;background:#126e82;color:#fff;border:0;border-radius:7px;text-decoration:none;font:inherit;font-weight:700}.back{background:#e3ebee;color:#126e82}label{display:block;font-weight:600;margin-top:12px}input{box-sizing:border-box;width:100%;padding:10px;margin-top:5px;border:1px solid #b9c1c8;border-radius:7px;font:inherit}.grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:0 12px}.notice{background:#e6f5e9;padding:12px;border-radius:7px}.error{background:#ffe6e3;padding:12px;border-radius:7px}@media(max-width:600px){body{padding:12px}main{padding:16px}.grid{grid-template-columns:1fr}}</style></head><body><main><a class="button back" href="/categories.php?type=<?=urlencode($type)?>"><?=htmlspecialchars(t('users.back'),ENT_QUOTES,'UTF-8')?></a><h1><?=htmlspecialchars(t('category.translate'),ENT_QUOTES,'UTF-8')?></h1><h2><?=htmlspecialchars($category['name'],ENT_QUOTES,'UTF-8')?></h2><p><?=htmlspecialchars(t('category.translation_intro'),ENT_QUOTES,'UTF-8')?></p><?php if($error):?><p class="error"><?=htmlspecialchars($error,ENT_QUOTES,'UTF-8')?></p><?php endif;?><?php if($notice):?><p class="notice"><?=htmlspecialchars($notice,ENT_QUOTES,'UTF-8')?></p><?php endif;?><form method="post"><input type="hidden" name="csrf_token" value="<?=htmlspecialchars(csrf_token(),ENT_QUOTES,'UTF-8')?>"><div class="grid"><?php foreach($languages as$code=>$label):?><label><?=htmlspecialchars($label,ENT_QUOTES,'UTF-8')?><input name="translation_<?=$code?>" value="<?=htmlspecialchars((string)($translations[$code]??''),ENT_QUOTES,'UTF-8')?>"></label><?php endforeach;?></div><button class="save" type="submit"><?=htmlspecialchars(t('common.save'),ENT_QUOTES,'UTF-8')?></button></form></main></body></html>
