<?php
require_once '../includes/auth.php';
require_once '../includes/functions.php';

require_login('customs');
$page_title = 'Visa Management - ANATL Portal';
$user_id = $_SESSION['user_id'];
$lang = $_GET['lang'] ?? 'id';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (!validate_csrf($_POST['csrf_token'] ?? '')) {
        die('Invalid request');
    }

    $content = trim($_POST['content'] ?? '');

    if ($content) {
        $stmt = $conn->prepare('
            INSERT INTO customs_content (section, lang, content, updated_by)
            VALUES (?, ?, ?, ?)
            ON DUPLICATE KEY UPDATE content = ?, updated_by = ?, updated_at = NOW()
        ');
        $section = 'visa';
        $stmt->bind_param('sssssi', $section, $lang, $content, $user_id, $content, $user_id);

        if ($stmt->execute()) {
            set_flash('success', 'Konten visa berhasil disimpan');
        } else {
            set_flash('error', 'Gagal menyimpan konten');
        }
        $stmt->close();
    }

    header('Location: ' . BASE_URL . '/customs/visa.php?lang=' . $lang);
    exit;
}

// Get visa content
$stmt = $conn->prepare('SELECT content FROM customs_content WHERE section = ? AND lang = ?');
$section = 'visa';
$stmt->bind_param('ss', $section, $lang);
$stmt->execute();
$result = $stmt->get_result();
$content = $result->fetch_assoc();
$stmt->close();
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?= $page_title ?></title>
    <link rel="stylesheet" href="<?= BASE_URL ?>/assets/css/style.css">
</head>
<body>
<?php require '../includes/header.php'; ?>

<div class="main">
    <div class="portal">
        <div class="portal__sidebar">
            <div class="sidebar__title">Menu Customs</div>
            <a href="<?= BASE_URL ?>/customs/index.php" class="sidebar-item">📊 Dashboard</a>
            <a href="<?= BASE_URL ?>/customs/visa.php" class="sidebar-item sidebar-item--active">📋 Visa</a>
            <a href="<?= BASE_URL ?>/customs/barang.php" class="sidebar-item">📦 Barang Bawaan</a>
            <a href="<?= BASE_URL ?>/customs/deklarasi.php" class="sidebar-item">📝 Deklarasi</a>
        </div>

        <div class="portal__content">
            <h1 style="color: #003A70; margin-bottom: 32px;">Visa Management</h1>

            <?php if ($flash = get_flash()): ?>
            <div class="alert alert--<?= esc($flash['type']) ?>">
                <?= esc($flash['msg']) ?>
            </div>
            <?php endif; ?>

            <div style="display: flex; gap: 8px; margin-bottom: 32px;">
                <a href="?lang=id" class="btn <?= $lang === 'id' ? 'btn--primary' : 'btn--secondary' ?>">Indonesia</a>
                <a href="?lang=en" class="btn <?= $lang === 'en' ? 'btn--primary' : 'btn--secondary' ?>">English</a>
                <a href="?lang=pt" class="btn <?= $lang === 'pt' ? 'btn--primary' : 'btn--secondary' ?>">Português</a>
                <a href="?lang=tet" class="btn <?= $lang === 'tet' ? 'btn--primary' : 'btn--secondary' ?>">Tetum</a>
            </div>

            <div class="card">
                <form method="POST" class="form">
                    <input type="hidden" name="csrf_token" value="<?= csrf_token() ?>">

                    <div class="form-group">
                        <label for="content">Konten Visa (<?= strtoupper($lang) ?>)</label>
                        <textarea id="content" name="content" style="min-height: 400px;"><?= esc($content['content'] ?? '') ?></textarea>
                    </div>

                    <button type="submit" class="btn btn--primary">Simpan</button>
                </form>
            </div>
        </div>
    </div>
</div>

<?php require '../includes/footer.php'; ?>
