<?php
$admin_page = 'ayarlar';
$admin_title = 'Site Ayarları';
include 'admin_header.php';

$msg = ''; $msg_type = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $fields = ['site_name','site_tagline','about_text','about_mission','contact_address','contact_email','contact_phone','footer_text','stats_products','stats_clients','stats_years','stats_projects','meta_description'];
    try {
        foreach($fields as $f) {
            $val = trim($_POST[$f] ?? '');
            $stmt = db()->prepare("INSERT INTO vs_settings (`key`,`value`) VALUES (?,?) ON DUPLICATE KEY UPDATE `value`=?");
            $stmt->execute([$f,$val,$val]);
        }
        // Logo yükleme
        if (!empty($_FILES['site_logo']['name'])) {
            $file = $_FILES['site_logo'];
            $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
            $allowed = ['jpg','jpeg','png','gif','svg','webp'];
            if (in_array($ext, $allowed) && $file['size'] < 2*1024*1024) {
                $filename = 'logo_' . time() . '.' . $ext;
                $dest = dirname(__DIR__) . '/assets/uploads/' . $filename;
                if (move_uploaded_file($file['tmp_name'], $dest)) {
                    // eski logoyu sil
                    $old_logo = setting('site_logo','');
                    if ($old_logo && file_exists(dirname(__DIR__).'/assets/uploads/'.$old_logo)) {
                        @unlink(dirname(__DIR__).'/assets/uploads/'.$old_logo);
                    }
                    $stmt = db()->prepare("INSERT INTO vs_settings (`key`,`value`) VALUES ('site_logo',?) ON DUPLICATE KEY UPDATE `value`=?");
                    $stmt->execute([$filename,$filename]);
                }
            } else {
                $msg = 'Logo: Sadece JPG/PNG/GIF/SVG/WEBP, max 2MB.'; $msg_type='error';
            }
        }
        // Logo sil butonu
        if (isset($_POST['delete_logo'])) {
            $old_logo = setting('site_logo','');
            if ($old_logo && file_exists(dirname(__DIR__).'/assets/uploads/'.$old_logo)) {
                @unlink(dirname(__DIR__).'/assets/uploads/'.$old_logo);
            }
            $stmt = db()->prepare("INSERT INTO vs_settings (`key`,`value`) VALUES ('site_logo','') ON DUPLICATE KEY UPDATE `value`=''");
            $stmt->execute();
        }
        if (empty($msg)) { $msg = 'Ayarlar kaydedildi.'; $msg_type = 'success'; }
    } catch(Exception $e) { $msg = 'Hata: '.$e->getMessage(); $msg_type='error'; }
}
?>
<?php if ($msg): ?><div class="alert alert-<?= $msg_type ?>"><?= clean($msg) ?></div><?php endif; ?>
<div class="page-header"><h1>Site Ayarları</h1></div>

<form method="POST" enctype="multipart/form-data">
<div style="display:grid;grid-template-columns:1fr 1fr;gap:24px;">

    <div style="display:flex;flex-direction:column;gap:20px;">
        <div class="admin-card" style="padding:24px;">
            <h3 style="font-size:14px;font-weight:700;margin-bottom:20px;">Genel</h3>
            <div style="display:flex;flex-direction:column;gap:14px;">
                <div class="admin-form-group"><label>Site Adı</label><input type="text" name="site_name" value="<?= clean(setting('site_name','VOLKER SAVUNMA')) ?>"></div>
                <div class="admin-form-group"><label>Slogan</label><input type="text" name="site_tagline" value="<?= clean(setting('site_tagline')) ?>"></div>
                <div class="admin-form-group">
                    <label>Site Logosu</label>
                    <?php $cur_logo = setting('site_logo',''); ?>
                    <?php if($cur_logo): ?>
                    <div style="margin-bottom:10px;display:flex;align-items:center;gap:12px;">
                        <img src="<?= SITE_URL ?>assets/uploads/<?= clean($cur_logo) ?>" style="height:50px;background:#1a1a1a;padding:6px;border-radius:6px;border:1px solid rgba(255,255,255,0.1);" alt="Mevcut Logo">
                        <button type="submit" name="delete_logo" value="1" onclick="return confirm('Logoyu silmek istediğinize emin misiniz?')" style="background:#c0392b;color:#fff;border:none;padding:6px 12px;border-radius:4px;cursor:pointer;font-size:12px;">Logoyu Sil</button>
                    </div>
                    <?php else: ?>
                    <p style="font-size:12px;color:#666;margin-bottom:8px;">Henüz logo yüklenmemiş. Yüklenmezse varsayılan V ikonu gösterilir.</p>
                    <?php endif; ?>
                    <input type="file" name="site_logo" accept="image/*" style="background:#1a1a1a;border:1px dashed rgba(255,255,255,0.15);border-radius:6px;padding:10px;color:#aaa;width:100%;cursor:pointer;">
                    <small style="color:#555;font-size:11px;">JPG, PNG, SVG, WEBP — Maks. 2MB. Önerilen boyut: 200x50px</small>
                </div>
                <div class="admin-form-group"><label>Meta Açıklama (SEO)</label><textarea name="meta_description"><?= clean(setting('meta_description')) ?></textarea></div>
                <div class="admin-form-group"><label>Footer Yazısı</label><input type="text" name="footer_text" value="<?= clean(setting('footer_text')) ?>"></div>
            </div>
        </div>

        <div class="admin-card" style="padding:24px;">
            <h3 style="font-size:14px;font-weight:700;margin-bottom:20px;">İstatistikler (Hero)</h3>
            <div style="display:grid;grid-template-columns:1fr 1fr;gap:12px;">
                <div class="admin-form-group"><label>Ürün Sayısı</label><input type="text" name="stats_products" value="<?= clean(setting('stats_products','50+')) ?>"></div>
                <div class="admin-form-group"><label>Müşteri</label><input type="text" name="stats_clients" value="<?= clean(setting('stats_clients','120')) ?>"></div>
                <div class="admin-form-group"><label>Yıl</label><input type="text" name="stats_years" value="<?= clean(setting('stats_years','10')) ?>"></div>
                <div class="admin-form-group"><label>Proje</label><input type="text" name="stats_projects" value="<?= clean(setting('stats_projects','500+')) ?>"></div>
            </div>
        </div>
    </div>

    <div style="display:flex;flex-direction:column;gap:20px;">
        <div class="admin-card" style="padding:24px;">
            <h3 style="font-size:14px;font-weight:700;margin-bottom:20px;">İletişim</h3>
            <div style="display:flex;flex-direction:column;gap:14px;">
                <div class="admin-form-group"><label>Adres</label><input type="text" name="contact_address" value="<?= clean(setting('contact_address')) ?>"></div>
                <div class="admin-form-group"><label>E-Posta</label><input type="email" name="contact_email" value="<?= clean(setting('contact_email')) ?>"></div>
                <div class="admin-form-group"><label>Telefon</label><input type="text" name="contact_phone" value="<?= clean(setting('contact_phone')) ?>"></div>
            </div>
        </div>

        <div class="admin-card" style="padding:24px;">
            <h3 style="font-size:14px;font-weight:700;margin-bottom:20px;">Hakkımızda Metinleri</h3>
            <div style="display:flex;flex-direction:column;gap:14px;">
                <div class="admin-form-group"><label>Hakkımızda</label><textarea name="about_text" rows="5"><?= clean(setting('about_text')) ?></textarea></div>
                <div class="admin-form-group"><label>Misyon</label><textarea name="about_mission" rows="4"><?= clean(setting('about_mission')) ?></textarea></div>
            </div>
        </div>
    </div>

</div>
<div style="margin-top:24px;">
    <button type="submit" class="btn btn-primary">
        <svg width="14" height="14" fill="none" stroke="currentColor" stroke-width="2.5" viewBox="0 0 24 24"><path d="M19 21H5a2 2 0 01-2-2V5a2 2 0 012-2h11l5 5v11a2 2 0 01-2 2z"/><polyline points="17 21 17 13 7 13 7 21"/></svg>
        Ayarları Kaydet
    </button>
</div>
</form>
<?php include 'admin_footer.php'; ?>
