<?php
require_once (dirname(__DIR__, 2) . '/init.php');
//if (!m_admin_check()) {
//    m_redirect(ADMIN_URL . '/login');
//}
$action = m_u_p('action');
$data = m_u_p('data');
$email = m_u_p('email');
$lang = m_get_session('lang') ? m_get_session('lang') : lang_is_default('lang_code');


switch ($action) {
    case $action:
        if ($action === 'brands'){

            $brands = json_decode(settings('brand', $lang));
            ob_start();
            include '../include/brands.php';
            $brands_content = ob_get_clean();
            $data = [
                'brands' => $brands,
            ];

            echo $brands_content;
        } elseif ($action === 'smtp_test') {
            // SMTP test maili gönder
            $lang = m_get_session('lang') ? m_get_session('lang') : lang_is_default('lang_code');
            $smtpHost = general_settings('smtp_host', $lang);
            $smtpPort = general_settings('smtp_port', $lang) ?: 587;
            $smtpUser = general_settings('smtp_user', $lang);
            $smtpPass = general_settings('smtp_pass', $lang);
            $smtpSecure = general_settings('smtp_secure', $lang) ?: 'tls';
            $mailFrom = general_settings('mail_from', $lang) ?: $smtpUser;
            $mailFromName = general_settings('mail_from_name', $lang) ?: 'SMTP Test';
            $to = filter_var($email, FILTER_VALIDATE_EMAIL) ? $email : null;
            if (!$to) { echo json_encode(['status'=>'error','message'=>'Geçersiz e-posta']); break; }
            if (!$smtpHost || !$smtpUser || !$smtpPass) { echo json_encode(['status'=>'error','message'=>'SMTP ayarları eksik']); break; }
            require_once ROOT_FOLDER.'/includes/m_lib/plugins/PHPMailer/class.phpmailer.php';
            require_once ROOT_FOLDER.'/includes/m_lib/plugins/PHPMailer/class.smtp.php';
            $mailer = new PHPMailer();
            $mailer->isSMTP();
            $mailer->Host = $smtpHost;
            $mailer->Port = (int)$smtpPort;
            $mailer->SMTPAuth = true;
            $mailer->SMTPSecure = $smtpSecure;
            $mailer->Username = $smtpUser;
            $mailer->Password = $smtpPass;
            $mailer->CharSet = 'UTF-8';
            $mailer->setFrom($mailFrom, $mailFromName);
            $mailer->addAddress($to);
            $mailer->Subject = 'SMTP Test';
            $mailer->Body = 'Bu bir test e-postasıdır.';
            $mailer->isHTML(false);
            try { $ok = $mailer->send(); } catch (Exception $e) { $ok = false; $err=$e->getMessage(); }
            if ($ok) { echo json_encode(['status'=>'success']); } else { echo json_encode(['status'=>'error','message'=>$err ?? 'Gönderilemedi']); }
        } else{
            $post = post($data);
            ob_start();
            include '../include/'.$action.'.php';
            $content = ob_get_clean();
            $data = [
                $action => $post,
            ];
            $json = json_encode($content);
            echo $json;
        }

        break;

    default:
        echo json_encode(['error' => 'Geçersiz işlem']);
        break;
}