<?php
ob_start();
session_start();
include "../../../../includes/config/config.php";
date_default_timezone_set('Europe/Istanbul');
$settings = $db->prepare("SELECT * from ayarlar where id='1'");
$settings->execute();
$ayar = $settings->fetch(PDO::FETCH_ASSOC);

include_once '../../secure_post.php';
if ($adminsorgusu->rowCount() === 0) {
    header("Location: 404");
    exit;
}
if (isset($_GET['gorselsil']) && $_GET['gorselsil'] == 'success') {
    $id = 1; 
    $sorgu = $db->prepare("SELECT resim FROM hizmet_ayar WHERE id=:id");
    $sorgu->execute(['id' => $id]);
    $cek_resim = $sorgu->fetch(PDO::FETCH_ASSOC);
    if ($cek_resim['resim'] && file_exists("../../../../images/uploads/" . $cek_resim['resim'])) {
        @unlink("../../../../images/uploads/" . $cek_resim['resim']);
    }
    $guncelle = $db->prepare("UPDATE hizmet_ayar SET resim=:resim WHERE id=:id");
    $guncelle->execute(['resim' => '', 'id' => $id]);
    header("Location: ../../../pages.php?sayfa=hizmetmodul&status=success");
    exit;
}

if (isset($_POST['moduldegis'])) {
    $eski_bg = $_POST['eski_bg'] ?? '';
    $dosya_adi = $eski_bg;

    if (!empty($_FILES['resim']['name'])) {
        $allowed = ['jpg', 'jpeg', 'png', 'webp', 'gif'];
        $filename = $_FILES['resim']['name'];
        $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
        if (in_array($ext, $allowed)) {
            if ($eski_bg && file_exists("../../../../images/uploads/" . $eski_bg)) {
                @unlink("../../../../images/uploads/" . $eski_bg);
            }
            $new_name = "hizmet_bg_" . uniqid() . "." . $ext;
            move_uploaded_file($_FILES['resim']['tmp_name'], "../../../../images/uploads/" . $new_name);
            $dosya_adi = $new_name;
        }
    }

    $ayarkaydet = $db->prepare("UPDATE hizmet_ayar SET
        baslik_renk=:baslik_renk,
        spot_renk=:spot_renk,
        divider=:divider,
        font1=:font1,
        font2=:font2,
        baslik_harf=:baslik_harf,
        bg_durum=:bg_durum,
        kutu_arkaplan=:kutu_arkaplan,
        kutu_border=:kutu_border,
        kutu_yazi_renk=:kutu_yazi_renk,
        kutu_radius=:kutu_radius,
        buton_bg=:buton_bg,
        hizmet_limit=:hizmet_limit,
        padding=:padding,
        margin=:margin,
        resim_w=:resim_w,
        resim_h=:resim_h,
        metaDesc=:metaDesc,
        tags=:tags,
        bg_tip=:bg_tip,
        bg_color=:bg_color,
        resim=:resim,
        bg_display=:bg_display,
        overlay=:overlay
        WHERE id='1'");

    $update = $ayarkaydet->execute([
        'baslik_renk' => $_POST['baslik_renk'],
        'spot_renk' => $_POST['spot_renk'],
        'divider' => $_POST['divider'],
        'font1' => $_POST['font1'],
        'font2' => $_POST['font2'],
        'baslik_harf' => $_POST['baslik_harf'],
        'bg_durum' => $_POST['bg_durum'],
        'kutu_arkaplan' => $_POST['kutu_arkaplan'],
        'kutu_border' => $_POST['kutu_border'],
        'kutu_yazi_renk' => $_POST['kutu_yazi_renk'],
        'kutu_radius' => $_POST['kutu_radius'],
        'buton_bg' => $_POST['buton_bg'],
        'hizmet_limit' => $_POST['hizmet_limit'],
        'padding' => $_POST['padding'],
        'margin' => $_POST['margin'] ?? 0,
        'resim_w' => $_POST['resim_w'],
        'resim_h' => $_POST['resim_h'],
        'metaDesc' => $_POST['metaDesc'] ?? '',
        'tags' => $_POST['tags'] ?? '',
        'bg_tip' => $_POST['bg_tip'],
        'bg_color' => $_POST['bg_color'],
        'resim' => $dosya_adi,
        'bg_display' => $_POST['bg_display'] ?? 0,
        'overlay' => $_POST['overlay'] ?? 0
    ]);

    if ($update) {
        header("Location: ../../../pages.php?sayfa=hizmetmodul&status=success");
    } else {
        header("Location: ../../../pages.php?sayfa=hizmetmodul&status=warning");
    }
}
?>
