<?php
if (m_u_g('lang')){
   $lang = m_u_g('lang');
}else{
    $lang = lang_is_default('lang_code');
}
?>

<div class="row">
    <div class="col-xxl-12">
        <div class="alert alert-info alert-border-left alert-dismissible fade show" role="alert">
            <i class="ri-information-line me-3 align-middle fs-16"></i>
            <strong><?=lang('settings_general')?></strong> - <?=lang('settings_general_info')?>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-xxl-12">
        <div class="alert alert-primary alert-border-left alert-dismissible fade show lang_alert" role="alert">
            <i class="ri-error-warning-line me-3 align-middle fs-16"></i>
            <?=lang('form_editing_lang')?> <?=lang_name($lang)?>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-xxl-12">
        <div class="row align-items-center">
            <div class="col-md-10">
                <h5 class="mb-3" style="float: left;"><?=lang('settings_general')?></h5>
            </div>
            <div class="col-md-2">
                <ul class="list-wrap" style="float: right;">
                    <?php
                    $languages = languages('data');
                    foreach ($languages as $language){
                        ?>
                        <li style="display: inline-block; margin-right: 5px;">
                            <a href="?lang=<?=$language['lang_code']?>">
                                <img src="<?=ADMIN_FOLDER?>/model/languages/flags/<?=$language['lang_code']?>.svg">
                            </a>
                        </li>
                        <?php
                    }
                    ?>
                </ul>
            </div>
        </div>
    </div>
        <form action="<?=ADMIN_URL?>/settings" method="POST">
            <input type="hidden" name="csrf_token" value="<?php echo $_SESSION['csrf_token']['value']; ?>">
            <input type="hidden" name="get_lang" value="<?php echo $lang ?>">
            <input type="hidden" name="form_type" value="general">
            <div class="card">
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-3">
                            <div class="nav flex-column nav-pills text-left" id="v-pills-tab" role="tablist" aria-orientation="vertical">
                                <a class="nav-link mb-2 active" id="v-pills-general-tab" data-bs-toggle="pill" href="#v-pills-general" role="tab" aria-controls="v-pills-general" aria-selected="true">
                                    <svg class="icon me-2" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                        <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                                        <path d="M5 12l-2 0l9 -9l9 9l-2 0"></path>
                                        <path d="M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-7"></path>
                                        <path d="M9 21v-6a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v6"></path>
                                    </svg>
                                    <?=lang('settings_tab_general_info')?>
                                </a>
                                <a class="nav-link mb-2" id="v-pills-watermark-tab" data-bs-toggle="pill" href="#v-pills-watermark" role="tab" aria-controls="v-pills-watermark" aria-selected="false">
                                    <svg class="icon me-2" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                        <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                                        <path d="M15 8h.01"></path>
                                        <path d="M12.5 21h-6.5a3 3 0 0 1 -3 -3v-12a3 3 0 0 1 3 -3h12a3 3 0 0 1 3 3v6.5"></path>
                                        <path d="M3 16l5 -5c.928 -.893 2.072 -.893 3 0l4 4"></path>
                                        <path d="M14 14l1 -1c.928 -.893 2.072 -.893 3 0l2 2"></path>
                                        <path d="M19 22v-6"></path>
                                        <path d="M22 19l-3 -3l-3 3"></path>
                                    </svg>
                                    <?=lang('settings_tab_watermark')?>
                                </a>
                                <a class="nav-link mb-2" id="v-pills-email-tab" data-bs-toggle="pill" href="#v-pills-email" role="tab" aria-controls="v-pills-email" aria-selected="false">
                                    <svg class="icon me-2" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                        <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                                        <path d="M3 7h18l-9 7l-9 -7"></path>
                                        <path d="M3 7v10a2 2 0 0 0 2 2h14a2 2 0 0 0 2 -2v-10"></path>
                                    </svg>
                                    <?=lang('settings_tab_email')?>
                                </a>
                                <a class="nav-link mb-2" id="v-pills-analytics-tab" data-bs-toggle="pill" href="#v-pills-analytics" role="tab" aria-controls="v-pills-analytics" aria-selected="false">
                                    <svg class="icon me-2" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                        <path d="M3 3v18h18"/>
                                        <rect x="7" y="11" width="3" height="7"/>
                                        <rect x="12" y="7" width="3" height="11"/>
                                        <rect x="17" y="13" width="3" height="5"/>
                                    </svg>
                                    <?=lang('settings_tab_analytics', [], 'Analitik')?>
                                </a>
                                <a class="nav-link mb-2" id="v-pills-translate-tab" data-bs-toggle="pill" href="#v-pills-translate" role="tab" aria-controls="v-pills-translate" aria-selected="false">
                                    <svg class="icon me-2" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                        <path d="M4 5h16"/><path d="M9 3v2"/><path d="M15 3v2"/>
                                        <path d="M7 13l3-3l3 3"/><path d="M14 16l3 3l3-3"/>
                                    </svg>
                                    <?=lang('settings_tab_translate')?>
                                </a>
                                <a class="nav-link mb-2" id="v-pills-indexing-tab" data-bs-toggle="pill" href="#v-pills-indexing" role="tab" aria-controls="v-pills-indexing" aria-selected="false">
                                    <svg class="icon me-2" xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
                                        <path d="M3 3h18v4H3z"></path><path d="M7 13h10"></path><path d="M7 17h6"></path>
                                    </svg>
                                    <?=lang('settings_tab_indexing', [], 'Arama Motorları')?>
                                </a>
                                
                            </div>
                        </div><!-- end col -->
                        <div class="col-md-9">
                            <div class="tab-content text-muted mt-4 mt-md-0" id="v-pills-tabContent">
                                <div class="tab-pane fade show active" id="v-pills-general" role="tabpanel" aria-labelledby="v-pills-general-tab">
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="mb-3 input-group-v ">
                                                <label for="title" class="form-label"><?=lang('settings_site_title')?></label>
                                                <input type="text" name="site_title_<?=$lang?>"  class="form-control input-group-flat" id="site_title_<?=$lang?>"  value="<?=general_settings('site_title', $lang)?>"  placeholder="<?=lang('settings_site_title_placeholder')?>">
                                            </div>
                                        </div>
                                        <div class="col-md-12">
                                            <div class="mb-3 input-group-v ">
                                                <label for="seo_description" class="form-label"><?=lang('settings_seo_description')?></label>
                                                <textarea class="form-control input-group-flat " name="site_description_<?=$lang?>" maxlength="160" placeholder="<?=lang('settings_seo_description_placeholder')?>" id="seo_description" ><?=general_settings('site_description', $lang)?></textarea>
                                                <p class="character_count"><?=lang('settings_seo_remaining')?> <small id="seo_description_Remaining">160</small></p>
                                            </div>
                                        </div>
                                        <div class="col-md-12">
                                            <div class="mb-3 input-group-v ">
                                                <label for="seo_keywords" class="form-label"><?=lang('settings_site_keywords')?></label>
                                                <input class="form-control input-group-flat" name="site_keywords_<?=$lang?>" id="seo_keywords" data-choices="" data-choices-limit="3" data-choices-removeitem="" type="text" value="<?=general_settings('site_keywords', $lang)?>">
                                            </div>
                                        </div>
                                        <div class="col-md-12">
                                            <div class="mb-3 input-group-v ">
                                                <label for="default_og_image_<?=$lang?>" class="form-label"><?=lang('settings_default_og_image')?></label>
                                                <?php $ogimg = general_settings('default_og_image', $lang); ?>
                                                <input type="hidden" data-v-image name="default_og_image_<?=$lang?>" id="default_og_image_<?=$lang?>" value="<?= htmlspecialchars((string)$ogimg) ?>">
                                                <img data-v-image src="<?= $ogimg ? (DOMAIN.'/media'.$ogimg) : (ADMIN_THEME_URL.'/assets/images/placeholder.svg') ?>" alt="" id="default_og_image_<?=$lang?>-thumb" data-media-gallery data-target-input="#default_og_image_<?=$lang?>" data-target-thumb="#default_og_image_<?=$lang?>-thumb" data-type="single" class="img-thumbnail border-0 p-0 bg-light-subtle" width="150px">
                                                <div class="py-3" style="max-width: 250px">
                                                    <div class="btn-group w-100">
                                                        <button type="button" class="btn btn-primary btn-icon btn-sm w-100" data-media-gallery data-target-input="#default_og_image_<?=$lang?>" data-target-thumb="#default_og_image_<?=$lang?>-thumb" data-type="single">
                                                            <i class="la la-image la-lg"></i> <?=lang('set_media')?>
                                                        </button>
                                                        <button type="button" class="btn btn-outline-secondary btn-icon btn-sm" onclick="clearFeaturedMedia('default_og_image_<?=$lang?>')">
                                                            <i class="la la-times"></i>
                                                        </button>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v ">
                                                <label for="twitter_site_<?=$lang?>" class="form-label"><?=lang('settings_twitter_site')?></label>
                                                <input type="text" name="twitter_site_<?=$lang?>" class="form-control input-group-flat" id="twitter_site_<?=$lang?>" value="<?= htmlspecialchars((string)general_settings('twitter_site', $lang)) ?>" placeholder="<?=lang('settings_twitter_site_placeholder')?>">
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v ">
                                                <label for="twitter_creator_<?=$lang?>" class="form-label"><?=lang('settings_twitter_creator')?></label>
                                                <input type="text" name="twitter_creator_<?=$lang?>" class="form-control input-group-flat" id="twitter_creator_<?=$lang?>" value="<?= htmlspecialchars((string)general_settings('twitter_creator', $lang)) ?>" placeholder="<?=lang('settings_twitter_creator_placeholder')?>">
                                            </div>
                                        </div>
                                        
                                        <div class="col-md-12">
                                            <div class="mb-3 input-group-v ">
                                                <label for="title" class="form-label"><?=lang('settings_address')?></label>
                                                <input type="text" name="address_<?=$lang?>"  class="form-control input-group-flat" id="address_<?=$lang?>"  value="<?=general_settings('address', $lang)?>"  placeholder="<?=lang('settings_address_placeholder')?>">
                                            </div>
                                        </div>
                                        <div class="col-md-12">
                                            <div class="mb-3 input-group-v ">
                                                <label for="title" class="form-label"><?=lang('settings_phone')?></label>
                                                <input type="text" name="phone_<?=$lang?>"  class="form-control input-group-flat" id="phone_<?=$lang?>"  value="<?=general_settings('phone', $lang)?>"  placeholder="<?=lang('settings_phone_placeholder')?>">
                                            </div>
                                        </div>
                                        <div class="col-md-12">
                                            <div class="mb-3 input-group-v ">
                                                <label for="whatsapp_<?=$lang?>" class="form-label"><?=lang('settings_whatsapp')?></label>
                                                <input type="text" name="whatsapp_<?=$lang?>"  class="form-control input-group-flat" id="whatsapp_<?=$lang?>"  value="<?=general_settings('whatsapp', $lang)?>"  placeholder="<?=lang('settings_whatsapp_placeholder')?>">
                                                <small class="text-muted"><?=lang('settings_whatsapp_hint', [], 'Örn: 905xxxxxxxxx veya +905xxxxxxxxx')?></small>
                                            </div>
                                        </div>
                                        <div class="col-md-12">
                                            <div class="mb-3 input-group-v ">
                                                <label for="title" class="form-label"><?=lang('settings_email')?></label>
                                                <input type="text" name="email_<?=$lang?>"  class="form-control input-group-flat" id="email_<?=$lang?>"  value="<?=general_settings('email', $lang)?>"  placeholder="<?=lang('settings_email_placeholder')?>">
                                            </div>
                                        </div>
                                        
                                    </div>
                                </div>
                                
                                <div class="tab-pane fade" id="v-pills-watermark" role="tabpanel" aria-labelledby="v-pills-watermark-tab">
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="mb-3">
                                                <!-- Checkbox kapalıyken de 0 değeri gönderilsin -->
                                                <input type="hidden" name="watermark_enabled_<?=$lang?>" value="0">
                                                <div class="form-check form-switch">
                                                    <input class="form-check-input" type="checkbox" name="watermark_enabled_<?=$lang?>" id="watermark_enabled_<?=$lang?>" value="1" <?=general_settings('watermark_enabled', $lang) == '1' ? 'checked' : ''?>>
                                                    <label class="form-check-label" for="watermark_enabled_<?=$lang?>">
                                                        <?=lang('watermark_enable')?>
                                                    </label>
                                                </div>
                                            </div>
                                        </div>
                                        
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v">
                                                <label for="watermark_text_<?=$lang?>" class="form-label"><?=lang('watermark_text')?></label>
                                                <input type="text" name="watermark_text_<?=$lang?>" class="form-control input-group-flat" id="watermark_text_<?=$lang?>" value="<?=general_settings('watermark_text', $lang)?>" placeholder="<?=lang('watermark_text_placeholder')?>">
                                            </div>
                                        </div>
                                        
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v">
                                                <label for="watermark_position_<?=$lang?>" class="form-label"><?=lang('watermark_position')?></label>
                                                <select name="watermark_position_<?=$lang?>" class="form-control input-group-flat" id="watermark_position_<?=$lang?>">
                                                    <option value="bottom-right" <?=general_settings('watermark_position', $lang) == 'bottom-right' ? 'selected' : ''?>><?=lang('watermark_pos_bottom_right')?></option>
                                                    <option value="bottom-left" <?=general_settings('watermark_position', $lang) == 'bottom-left' ? 'selected' : ''?>><?=lang('watermark_pos_bottom_left')?></option>
                                                    <option value="top-right" <?=general_settings('watermark_position', $lang) == 'top-right' ? 'selected' : ''?>><?=lang('watermark_pos_top_right')?></option>
                                                    <option value="top-left" <?=general_settings('watermark_position', $lang) == 'top-left' ? 'selected' : ''?>><?=lang('watermark_pos_top_left')?></option>
                                                    <option value="center" <?=general_settings('watermark_position', $lang) == 'center' ? 'selected' : ''?>><?=lang('watermark_pos_center')?></option>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v">
                                                <label for="watermark_opacity_<?=$lang?>" class="form-label"><?=lang('watermark_opacity')?></label>
                                                <input type="range" name="watermark_opacity_<?=$lang?>" class="form-control input-group-flat" id="watermark_opacity_<?=$lang?>" min="10" max="100" value="<?=general_settings('watermark_opacity', $lang) ?: '50'?>" oninput="this.nextElementSibling.value = this.value + '%'">
                                                <output><?=general_settings('watermark_opacity', $lang) ?: '50'?>%</output>
                                            </div>
                                        </div>
                                        
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v">
                                                <label for="watermark_size_<?=$lang?>" class="form-label"><?=lang('watermark_font_size')?></label>
                                                <select name="watermark_size_<?=$lang?>" class="form-control input-group-flat" id="watermark_size_<?=$lang?>">
                                                    <option value="12" <?=general_settings('watermark_size', $lang) == '12' ? 'selected' : ''?>><?=lang('watermark_size_small')?></option>
                                                    <option value="16" <?=general_settings('watermark_size', $lang) == '16' ? 'selected' : ''?>><?=lang('watermark_size_medium')?></option>
                                                    <option value="20" <?=general_settings('watermark_size', $lang) == '20' ? 'selected' : ''?>><?=lang('watermark_size_large')?></option>
                                                    <option value="24" <?=general_settings('watermark_size', $lang) == '24' ? 'selected' : ''?>><?=lang('watermark_size_xlarge')?></option>
                                                </select>
                                            </div>
                                        </div>
                                        
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v">
                                                <label for="watermark_color_<?=$lang?>" class="form-label"><?=lang('watermark_color')?></label>
                                                <input type="color" name="watermark_color_<?=$lang?>" class="form-control input-group-flat" id="watermark_color_<?=$lang?>" value="<?=general_settings('watermark_color', $lang) ?: '#ffffff'?>">
                                            </div>
                                        </div>
                                        
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v">
                                                <label for="watermark_margin_<?=$lang?>" class="form-label"><?=lang('watermark_margin')?></label>
                                                <input type="number" name="watermark_margin_<?=$lang?>" class="form-control input-group-flat" id="watermark_margin_<?=$lang?>" value="<?=general_settings('watermark_margin', $lang) ?: '10'?>" min="0" max="100">
                                            </div>
                                        </div>
                                        
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v">
                                                <label for="watermark_image_size_<?=$lang?>" class="form-label"><?=lang('watermark_image_size')?></label>
                                                <input type="number" name="watermark_image_size_<?=$lang?>" class="form-control input-group-flat" id="watermark_image_size_<?=$lang?>" value="<?=general_settings('watermark_image_size', $lang) ?: '20'?>" min="5" max="80">
                                                <small class="form-text text-muted"><?=lang('watermark_image_size_help')?></small>
                                            </div>
                                        </div>
                                        
                                        <div class="col-md-12">
                                            <div class="mb-3">
                                                <label for="watermark_image_<?=$lang?>" class="form-label"><?=lang('watermark_image_label')?></label>
                                                <?php
                                                if (general_settings('watermark_image', $lang)){
                                                    ?>
                                                    <input type="hidden" data-v-image name="watermark_image_<?=$lang?>" id="watermark_image_<?=$lang?>" value="<?=general_settings('watermark_image', $lang)?>">
                                                    <img data-v-image src="<?=DOMAIN?>/media<?=general_settings('watermark_image', $lang)?>" alt="" id="watermark_image_<?=$lang?>-thumb" data-media-gallery data-target-input="#watermark_image_<?=$lang?>" data-target-thumb="#watermark_image_<?=$lang?>-thumb" data-type="single" class="img-thumbnail border-0 p-0 bg-light-subtle" width="150px">
                                                    <?php
                                                }else{
                                                    ?>
                                                    <input type="hidden" data-v-image name="watermark_image_<?=$lang?>" id="watermark_image_<?=$lang?>" value="">
                                                    <img data-v-image src="<?=ADMIN_THEME_URL?>/assets/images/placeholder.svg" alt="" id="watermark_image_<?=$lang?>-thumb" data-media-gallery data-target-input="#watermark_image_<?=$lang?>" data-target-thumb="#watermark_image_<?=$lang?>-thumb" data-type="single" class="img-thumbnail border-0 p-0 bg-light-subtle" width="150px">
                                                    <?php
                                                }
                                                ?>
                                                <div class="py-3 text-center" style="width: 250px">
                                                    <div class="btn-group w-100">
                                                        <button type="button" class="btn btn-primary btn-icon btn-sm w-100" data-media-gallery data-target-input="#watermark_image_<?=$lang?>" data-target-thumb="#watermark_image_<?=$lang?>-thumb" data-type="single">
                                                            <i class="la la-image la-lg"></i> <?=lang('set_media')?>
                                                        </button>
                                                        <button type="button" class="btn btn-outline-secondary btn-icon btn-sm" onclick="clearFeaturedMedia('watermark_image_<?=$lang?>')">
                                                            <i class="la la-times"></i>
                                                        </button>
                                                    </div>
                                                </div>
                                                <small class="form-text text-muted"><?=lang('watermark_image_note')?></small>
                                            </div>
                                        </div>
                                        
                                        <div class="col-md-12">
                                            <div class="alert alert-info">
                                                <strong><?=lang('watermark_info_title')?>:</strong> <?=lang('watermark_info_text')?>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="tab-pane fade" id="v-pills-email" role="tabpanel" aria-labelledby="v-pills-email-tab">
                                    <div class="row">
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v ">
                                                <label class="form-label"><?=lang('settings_smtp_host')?></label>
                                                <input type="text" name="smtp_host_<?=$lang?>" class="form-control input-group-flat" value="<?=general_settings('smtp_host', $lang)?>" placeholder="<?=lang('settings_smtp_host_placeholder')?>">
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <div class="mb-3 input-group-v ">
                                                <label class="form-label"><?=lang('settings_smtp_port')?></label>
                                                <input type="number" name="smtp_port_<?=$lang?>" class="form-control input-group-flat" value="<?=general_settings('smtp_port', $lang)?>" placeholder="<?=lang('settings_smtp_port_placeholder')?>">
                                            </div>
                                        </div>
                                        <div class="col-md-3">
                                            <div class="mb-3 input-group-v ">
                                                <label class="form-label"><?=lang('settings_smtp_secure')?></label>
                                                <select name="smtp_secure_<?=$lang?>" class="form-control input-group-flat">
                                                    <?php $sec = general_settings('smtp_secure', $lang); ?>
                                                    <option value="tls" <?=$sec==='tls'?'selected':''?>>TLS</option>
                                                    <option value="ssl" <?=$sec==='ssl'?'selected':''?>>SSL</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v ">
                                                <label class="form-label"><?=lang('settings_smtp_user')?></label>
                                                <input type="text" name="smtp_user_<?=$lang?>" class="form-control input-group-flat" value="<?=general_settings('smtp_user', $lang)?>" placeholder="<?=lang('settings_smtp_user_placeholder')?>">
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v ">
                                                <label class="form-label"><?=lang('settings_smtp_pass')?></label>
                                                <input type="password" name="smtp_pass_<?=$lang?>" class="form-control input-group-flat" value="<?=general_settings('smtp_pass', $lang)?>" placeholder="<?=lang('settings_smtp_pass_placeholder')?>">
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v ">
                                                <label class="form-label"><?=lang('settings_mail_from')?></label>
                                                <input type="email" name="mail_from_<?=$lang?>" class="form-control input-group-flat" value="<?=general_settings('mail_from', $lang)?>" placeholder="<?=lang('settings_mail_from_placeholder')?>">
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v ">
                                                <label class="form-label"><?=lang('settings_mail_from_name')?></label>
                                                <input type="text" name="mail_from_name_<?=$lang?>" class="form-control input-group-flat" value="<?=general_settings('mail_from_name', $lang)?>" placeholder="<?=lang('settings_mail_from_name_placeholder')?>">
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v ">
                                                <label class="form-label"><?=lang('settings_mail_admin')?></label>
                                                <input type="email" name="mail_admin_<?=$lang?>" class="form-control input-group-flat" value="<?=general_settings('mail_admin', $lang)?>" placeholder="<?=lang('settings_mail_admin_placeholder')?>">
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v ">
                                                <label class="form-label"><?=lang('settings_smtp_test')?></label>
                                                <div class="input-group">
                                                    <input type="email" id="smtp_test_email" class="form-control input-group-flat" placeholder="<?=lang('settings_smtp_test_placeholder')?>">
                                                    <button class="btn btn-outline-primary" type="button" id="smtp_test_btn"><?=lang('settings_smtp_test_button')?></button>
                                                </div>
                                                <small class="text-muted"><?=lang('settings_smtp_test_note')?></small>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="tab-pane fade" id="v-pills-analytics" role="tabpanel" aria-labelledby="v-pills-analytics-tab">
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="mb-3">
                                                <input type="hidden" name="analytics_enabled_<?=$lang?>" value="0">
                                                <div class="form-check form-switch">
                                                    <input class="form-check-input" type="checkbox" name="analytics_enabled_<?=$lang?>" id="analytics_enabled_<?=$lang?>" value="1" <?= general_settings('analytics_enabled', $lang) == '1' ? 'checked' : '' ?>>
                                                    <label class="form-check-label" for="analytics_enabled_<?=$lang?>">
                                                        <?=lang('analytics_enable', [], 'Analitik İzlemeyi Etkinleştir')?>
                                                    </label>
                                                </div>
                                                <small class="form-text text-muted"><?=lang('analytics_note', [], 'Kapalı ise izleme kodu eklenmez.')?></small>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v ">
                                                <label class="form-label" for="analytics_provider_<?=$lang?>"><?=lang('analytics_provider', [], 'Sağlayıcı')?></label>
                                                <?php $prov = general_settings('analytics_provider', $lang) ?: 'matomo'; ?>
                                                <select id="analytics_provider_<?=$lang?>" name="analytics_provider_<?=$lang?>" class="form-control input-group-flat">
                                                    <option value="none" <?=$prov==='none'?'selected':''?>><?=lang('analytics_provider_none', [], 'Kapalı')?></option>
                                                    <option value="matomo" <?=$prov==='matomo'?'selected':''?>><?=lang('analytics_provider_matomo', [], 'Matomo')?></option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-3 input-group-v ">
                                                <label class="form-label" for="analytics_site_id_<?=$lang?>"><?=lang('analytics_site_id', [], 'Site ID')?></label>
                                                <input type="number" name="analytics_site_id_<?=$lang?>" id="analytics_site_id_<?=$lang?>" class="form-control input-group-flat" value="<?= htmlspecialchars((string)general_settings('analytics_site_id', $lang)) ?>" placeholder="1">
                                            </div>
                                        </div>
                                        <div class="col-md-12">
                                            <div class="mb-3 input-group-v ">
                                                <label class="form-label" for="analytics_endpoint_<?=$lang?>"><?=lang('analytics_endpoint', [], 'Matomo Sunucu Adresi')?></label>
                                                <input type="text" name="analytics_endpoint_<?=$lang?>" id="analytics_endpoint_<?=$lang?>" class="form-control input-group-flat" value="<?= htmlspecialchars((string)general_settings('analytics_endpoint', $lang)) ?>" placeholder="https://analytics.vebusoft.com">
                                                <small class="form-text text-muted"><?=lang('analytics_save_hint', [], 'Değişiklikleri kaydettikten sonra sayfanızı yenileyin.')?></small>
                                            </div>
                                        </div>
                                        <div class="col-md-12">
                                            <h6 class="mt-2"><?=lang('analytics_privacy_title', [], 'Gizlilik')?></h6>
                                            <div class="row g-3">
                                                <div class="col-md-4">
                                                    <div class="mb-2 input-group-v ">
                                                        <label class="form-label" for="analytics_consent_style_<?=$lang?>"><?=lang('analytics_consent_style', [], 'Consent stili')?></label>
                                                        <?php $cstyle = (string)(general_settings('analytics_consent_style', $lang) ?: 'minimal'); ?>
                                                        <select name="analytics_consent_style_<?=$lang?>" id="analytics_consent_style_<?=$lang?>" class="form-control input-group-flat">
                                                            <option value="minimal" <?=$cstyle==='minimal'?'selected':''?>><?=lang('analytics_consent_style_minimal', [], 'Minimal (bar)')?></option>
                                                            <option value="advanced" <?=$cstyle==='advanced'?'selected':''?>><?=lang('analytics_consent_style_advanced', [], 'Advanced (modal)')?></option>
                                                        </select>
                                                    </div>
                                                </div>
                                                <div class="col-md-4">
                                                    <input type="hidden" name="analytics_dnt_<?=$lang?>" value="0">
                                                    <div class="form-check form-switch">
                                                        <input class="form-check-input" type="checkbox" name="analytics_dnt_<?=$lang?>" id="analytics_dnt_<?=$lang?>" value="1" <?= general_settings('analytics_dnt', $lang) == '1' ? 'checked' : ''?>>
                                                        <label class="form-check-label" for="analytics_dnt_<?=$lang?>"><?=lang('analytics_dnt', [], 'Do Not Track (DNT) sinyaline saygı duy')?></label>
                                                    </div>
                                                    <small class="text-muted"><?=lang('analytics_dnt_hint', [], 'Tarayıcının DNT sinyali varsa izleme pasif olur.')?></small>
                                                </div>
                                                <div class="col-md-4">
                                                    <input type="hidden" name="analytics_disable_cookies_<?=$lang?>" value="0">
                                                    <div class="form-check form-switch">
                                                        <input class="form-check-input" type="checkbox" name="analytics_disable_cookies_<?=$lang?>" id="analytics_disable_cookies_<?=$lang?>" value="1" <?= general_settings('analytics_disable_cookies', $lang) == '1' ? 'checked' : ''?>>
                                                        <label class="form-check-label" for="analytics_disable_cookies_<?=$lang?>"><?=lang('analytics_disable_cookies', [], 'Çerezsiz izleme (disableCookies)')?></label>
                                                    </div>
                                                    <small class="text-muted"><?=lang('analytics_disable_cookies_hint', [], 'Ziyaretçi çerezleri yazılmaz; gizlilik için önerilir.')?></small>
                                                </div>
                                                <div class="col-md-4">
                                                    <input type="hidden" name="analytics_consent_required_<?=$lang?>" value="0">
                                                    <div class="form-check form-switch">
                                                        <input class="form-check-input" type="checkbox" name="analytics_consent_required_<?=$lang?>" id="analytics_consent_required_<?=$lang?>" value="1" <?= general_settings('analytics_consent_required', $lang) == '1' ? 'checked' : ''?>>
                                                        <label class="form-check-label" for="analytics_consent_required_<?=$lang?>"><?=lang('analytics_consent_required', [], 'Açık rıza gereksin')?></label>
                                                    </div>
                                                    <small class="text-muted"><?=lang('analytics_consent_required_hint', [], 'Etkinse rıza verilene kadar izleme başlatılmaz.')?></small>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-md-12">
                                            <div class="mb-3 input-group-v ">
                                                <label class="form-label" for="analytics_api_token_<?=$lang?>"><?=lang('analytics_api_token', [], 'Matomo API Token')?></label>
                                                <input type="text" name="analytics_api_token_<?=$lang?>" id="analytics_api_token_<?=$lang?>" class="form-control input-group-flat" value="<?= htmlspecialchars((string)general_settings('analytics_api_token', $lang)) ?>" placeholder="<?=lang('analytics_api_token_placeholder', [], 'token_auth değeri')?>">
                                                <small class="form-text text-muted"><?=lang('analytics_api_token_note', [], 'Matomo > Kullanıcı Hesabım > API Authentication Token')?></small>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="tab-pane fade" id="v-pills-translate" role="tabpanel" aria-labelledby="v-pills-translate-tab">
                                    <div class="row">
                                        <?php
                                        // Tüm dilleri topla ve hedefleri hesapla (kaynak hariç)
                                        $allLangRows = languages('data');
                                        $allCodes = [];
                                        foreach ($allLangRows as $l){ $allCodes[] = $l['lang_code']; }
                                        // Kaynak dil daima varsayılan dil olsun
                                        $defaultLangCode = lang_is_default('lang_code');
                                        $sourcePref = $defaultLangCode;
                                        $computedTargets = [];
                                        foreach ($allCodes as $c){ if ($c !== $sourcePref) { $computedTargets[] = $c; } }
                                        $allCodesCsv = implode(',', $allCodes);
                                        $computedTargetsCsv = implode(',', $computedTargets);
                                        ?>
                                        <div class="col-md-6">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label"><?=lang('translate_provider_label')?></label>
                                                <?php $prov = general_settings('translate_provider', $lang) ?: 'none'; ?>
                                                <select id="translate_provider_sel" name="translate_provider_<?=$lang?>" class="form-control input-group-flat">
                                                    <option value="none" <?=$prov==='none'?'selected':''?>><?=lang('translate_provider_none')?></option>
                                                    <option value="libre" <?=$prov==='libre'?'selected':''?>><?=lang('translate_provider_libre')?></option>
                                                    <option value="google" <?=$prov==='google'?'selected':''?>><?=lang('translate_provider_google')?></option>
                                                    <option value="deepl" <?=$prov==='deepl'?'selected':''?>><?=lang('translate_provider_deepl')?></option>
                                                    <option value="azure" <?=$prov==='azure'?'selected':''?>><?=lang('translate_provider_azure')?></option>
                                                </select>
                                            </div>
                                            <div id="provider-note" class="alert alert-info py-2 px-3" style="font-size: 13px"></div>
                                        </div>
                                        <div class="col-md-6 provider-group provider-api d-none">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label"><?=lang('translate_api_key_label')?></label>
                                                <input type="text" name="translate_api_key_<?=$lang?>" class="form-control input-group-flat" value="<?=general_settings('translate_api_key', $lang)?>" placeholder="<?=lang('translate_api_key_placeholder')?>">
                                            </div>
                                        </div>
                                        <div class="col-md-12 provider-group provider-endpoint d-none">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label"><?=lang('translate_endpoint_label')?></label>
                                                <input type="text" name="translate_endpoint_<?=$lang?>" class="form-control input-group-flat" value="<?=general_settings('translate_endpoint', $lang)?>" placeholder="<?=lang('translate_endpoint_placeholder')?>">
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label"><?=lang('translate_source_label')?></label>
                                                <input type="text" id="translate_source_input" class="form-control input-group-flat" value="<?=htmlspecialchars($sourcePref)?>" placeholder="tr" data-all-codes="<?=htmlspecialchars($allCodesCsv)?>" disabled>
                                                <input type="hidden" name="translate_source_<?=$lang?>" value="<?=htmlspecialchars($sourcePref)?>">
                                                <small class="text-muted"><?=lang('translate_source_hint')?></small>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label"><?=lang('translate_targets_label')?></label>
                                                <input type="text" id="translate_targets_display" class="form-control input-group-flat" value="<?=htmlspecialchars($computedTargetsCsv)?>" placeholder="en,de,ar" disabled>
                                                <input type="hidden" id="translate_targets_hidden" name="translate_targets_<?=$lang?>" value="<?=htmlspecialchars($computedTargetsCsv)?>">
                                                <small class="text-muted"><?=lang('translate_targets_hint')?></small>
                                            </div>
                                        </div>
                                        <div class="col-md-12">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label"><?=lang('translate_glossary_label')?></label>
                                                <textarea name="translate_glossary_<?=$lang?>" class="form-control input-group-flat" rows="3" placeholder='["Air AEG","VebuSoft"]'><?=general_settings('translate_glossary', $lang)?></textarea>
                                                <small class="text-muted"><?=lang('translate_glossary_hint')?></small>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="tab-pane fade" id="v-pills-indexing" role="tabpanel" aria-labelledby="v-pills-indexing-tab">
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="alert alert-info">
                                                <?=lang('indexing.note')?>
                                            </div>
                                        </div>
                                        <div class="col-md-12">
                                            <button class="btn btn-secondary" type="button" id="btnPingSitemapTop"><?=lang('indexing.ping_now')?></button>
                                            <small class="text-muted d-block mt-2" id="lastPingInfoTop"><?=lang('indexing.last_ping')?> </small>
                                        </div>
                                        <hr class="mt-4 mb-3"/>
                                        <div class="col-md-12">
                                            <h6>IndexNow (Bing)</h6>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label">IndexNow Etkin</label>
                                                <input type="hidden" name="indexnow_enabled_<?=$lang?>" value="0">
                                                <div class="form-check form-switch">
                                                    <input class="form-check-input" type="checkbox" name="indexnow_enabled_<?=$lang?>" id="indexnow_enabled_<?=$lang?>" value="1" <?= general_settings('indexnow_enabled', $lang) == '1' ? 'checked' : ''?>>
                                                    <label class="form-check-label" for="indexnow_enabled_<?=$lang?>">Aç/Kapat</label>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label">IndexNow Endpoint</label>
                                                <?php $idxep = general_settings('indexnow_endpoint', $lang) ?: 'api'; ?>
                                                <select name="indexnow_endpoint_<?=$lang?>" class="form-control input-group-flat">
                                                    <option value="api" <?=$idxep==='api'?'selected':''?>>api.indexnow.org</option>
                                                    <option value="bing" <?=$idxep==='bing'?'selected':''?>>www.bing.com</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-12">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label">IndexNow Key</label>
                                                <input type="text" name="indexnow_key_<?=$lang?>" id="indexnow_key_<?=$lang?>" class="form-control input-group-flat" value="<?= htmlspecialchars((string)general_settings('indexnow_key', $lang)) ?>" placeholder="ör: a1b2c3d4... (dosya adı olarak kullanılacak)">
                                                <small class="text-muted">Kökte <code>{key}.txt</code> olarak bir dosya oluşturulacak ve içinde aynı anahtar yazacak.</small>
                                            </div>
                                            <button class="btn btn-outline-primary" type="button" id="btnWriteIndexNowKey">Key dosyasını yaz</button>
                                        </div>

                                        <hr class="mt-4 mb-3"/>
                                        <div class="col-md-12">
                                            <h6>Yandex Webmaster</h6>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label"><?=lang('indexing.yandex.method_label')?></label>
                                                <?php $yvm = general_settings('yandex_verify_method', $lang) ?: 'html'; ?>
                                                <select name="yandex_verify_method_<?=$lang?>" class="form-control input-group-flat">
                                                    <option value="html" <?=$yvm==='html'?'selected':''?>><?=lang('indexing.method.html')?></option>
                                                    <option value="meta" <?=$yvm==='meta'?'selected':''?>><?=lang('indexing.method.meta')?></option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-12 verify-yandex-group verify-yandex-html">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label"><?=lang('indexing.yandex.label_html_code')?></label>
                                                <input type="text" name="yandex_key_<?=$lang?>" id="yandex_key_<?=$lang?>" class="form-control input-group-flat" value="<?= htmlspecialchars((string)general_settings('yandex_key', $lang)) ?>" placeholder="ör: yandex_0123456789abcdef.html veya yandex_XXXXXX">
                                                <small class="text-muted"><?=lang('indexing.yandex.help_html_input')?></small>
                                            </div>
                                            <button class="btn btn-outline-primary" type="button" id="btnWriteYandexKey"><?=lang('indexing.yandex.write_file')?></button>
                                            <div class="alert alert-info py-2 px-3 mt-2 note-yandex-html"><?=lang('indexing.yandex.note_html')?></div>
                                        </div>
                                        <div class="col-md-6 verify-yandex-group verify-yandex-meta">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label"><?=lang('indexing.yandex.meta_token')?></label>
                                                <input type="text" name="yandex_meta_token_<?=$lang?>" id="yandex_meta_token_<?=$lang?>" class="form-control input-group-flat" value="<?= htmlspecialchars((string)general_settings('yandex_meta_token', $lang)) ?>" placeholder="ör: 0123456789abcdef">
                                                <small class="text-muted"><?=lang('indexing.yandex.help_meta_input')?></small>
                                            </div>
                                            <div class="alert alert-info py-2 px-3 mt-2 note-yandex-meta"><?=lang('indexing.yandex.note_meta')?></div>
                                        </div>

                                        <hr class="mt-4 mb-3"/>
                                        <div class="col-md-12">
                                            <h6>Google Search Console</h6>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label"><?=lang('indexing.google.method_label')?></label>
                                                <?php $gvm = general_settings('google_verify_method', $lang) ?: 'html'; ?>
                                                <select name="google_verify_method_<?=$lang?>" class="form-control input-group-flat">
                                                    <option value="html" <?=$gvm==='html'?'selected':''?>><?=lang('indexing.method.html')?></option>
                                                    <option value="meta" <?=$gvm==='meta'?'selected':''?>><?=lang('indexing.method.meta')?></option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-12 verify-google-group verify-google-html">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label"><?=lang('indexing.google.label_html_code')?></label>
                                                <input type="text" name="google_key_<?=$lang?>" id="google_key_<?=$lang?>" class="form-control input-group-flat" value="<?= htmlspecialchars((string)general_settings('google_key', $lang)) ?>" placeholder="ör: googleXXXXXXXXXXXX.html">
                                                <small class="text-muted"><?=lang('indexing.google.help_html_input')?></small>
                                            </div>
                                            <button class="btn btn-outline-primary" type="button" id="btnWriteGoogleKey"><?=lang('indexing.google.write_file')?></button>
                                            <div class="alert alert-info py-2 px-3 mt-2 note-google-html"><?=lang('indexing.google.note_html')?></div>
                                        </div>
                                        <div class="col-md-6 verify-google-group verify-google-meta">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label"><?=lang('indexing.google.meta_token')?></label>
                                                <input type="text" name="google_meta_token_<?=$lang?>" id="google_meta_token_<?=$lang?>" class="form-control input-group-flat" value="<?= htmlspecialchars((string)general_settings('google_meta_token', $lang)) ?>" placeholder="ör: abcdefghijklmn...">
                                                <small class="text-muted"><?=lang('indexing.google.help_meta_input')?></small>
                                            </div>
                                            <div class="alert alert-info py-2 px-3 mt-2 note-google-meta"><?=lang('indexing.google.note_meta')?></div>
                                        </div>

                                        <hr class="mt-4 mb-3"/>
                                        <div class="col-md-12">
                                            <h6><?=lang('indexing.bing.section_title')?></h6>
                                        </div>
                                        <div class="col-md-6">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label"><?=lang('indexing.bing.method_label')?></label>
                                                <?php $bvm = general_settings('bing_verify_method', $lang) ?: 'meta'; ?>
                                                <select name="bing_verify_method_<?=$lang?>" id="bing_verify_method_<?=$lang?>" class="form-control input-group-flat">
                                                    <option value="meta" <?=$bvm==='meta'?'selected':''?>><?=lang('indexing.method.meta')?></option>
                                                    <option value="html" <?=$bvm==='html'?'selected':''?>><?=lang('indexing.method.html')?> (XML)</option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="col-md-6 verify-bing-group verify-bing-meta">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label"><?=lang('indexing.bing.meta_token')?></label>
                                                <input type="text" name="bing_meta_token_<?=$lang?>" id="bing_meta_token_<?=$lang?>" class="form-control input-group-flat" value="<?= htmlspecialchars((string)general_settings('bing_meta_token', $lang)) ?>" placeholder="ör: 1234567890ABCDEF1234567890ABCDEF">
                                                <small class="text-muted"><?=lang('indexing.bing.help_meta_input')?></small>
                                            </div>
                                            <div class="alert alert-info py-2 px-3 mt-2 note-bing-meta"><?=lang('indexing.bing.note_meta')?></div>
                                        </div>
                                        <div class="col-md-12 verify-bing-group verify-bing-html">
                                            <div class="mb-2 input-group-v ">
                                                <label class="form-label"><?=lang('indexing.bing.html_content')?></label>
                                                <textarea name="bing_html_content_<?=$lang?>" id="bing_html_content_<?=$lang?>" class="form-control input-group-flat" rows="4" placeholder="Bing’in verdiği BingSiteAuth.xml içeriğini buraya yapıştırın."><?= htmlspecialchars((string)general_settings('bing_html_content', $lang)) ?></textarea>
                                                <small class="text-muted"><?=lang('indexing.bing.help_html_textarea')?></small>
                                            </div>
                                            <button class="btn btn-outline-primary" type="button" id="btnWriteBingKey"><?=lang('indexing.bing.write_file')?></button>
                                            <div class="alert alert-info py-2 px-3 mt-2 note-bing-html"><?=lang('indexing.bing.note_html')?></div>
                                        </div>

                                    </div>
                                </div>
                              
                            </div>
                        </div><!--  end col -->
                    </div>
                    <div class="card card-body" style="float: right">
                        <div class="saved">
                            <button class="btn btn-primary" type="submit" value="save_and_edit" name="save_and_edit">
                                <span class="icon-tabler-wrapper icon-left">
                                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-device-floppy" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                                      <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                                      <path d="M6 4h10l4 4v10a2 2 0 0 1 -2 2h-12a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2"></path>
                                      <path d="M12 14m-2 0a2 2 0 1 0 4 0a2 2 0 1 0 -4 0"></path>
                                      <path d="M14 4l0 4l-6 0l0 -4"></path>
                                    </svg>
                                </span>
                                <?=lang('save')?>
                            </button>
                        </div>
                    </div>
                </div><!-- end card-body -->
            </div>
        </form>
    </div>
</div>
<script>
document.getElementById('smtp_test_btn')?.addEventListener('click', function(){
  var email = document.getElementById('smtp_test_email').value.trim();
  if(!email){ alert('<?=lang('settings_smtp_test_email_placeholder')?>'); return; }
  fetch('<?=ADMIN_URL?>/ajax', {
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: new URLSearchParams({ action: 'smtp_test', email: email })
  }).then(r=>r.json()).then(function(res){
    if(res && res.status === 'success'){
      alert('<?=lang('settings_smtp_test_email_sent')?> ' + email);
    }else{
      alert('<?=lang('settings_smtp_test_email_failed')?> ' + (res && res.message ? res.message : '<?=lang('settings_smtp_test_email_unknown_error')?>'));
    }
  }).catch(function(err){ alert('<?=lang('settings_smtp_test_email_request_error')?> ' + err.message); });
});

// Çeviri ayarları: provider bazlı alan gösterimi ve notlar
(function(){
  const providerSel = document.getElementById('translate_provider_sel');
  const noteEl = document.getElementById('provider-note');
  const apiGroup = document.querySelector('.provider-group.provider-api');
  const endpointGroup = document.querySelector('.provider-group.provider-endpoint');

  function setNote(p){
    let text = '';
    if (p === 'none') text = '<?=lang('translate_provider_none_note')?>';
    if (p === 'libre') text = '<?=lang('translate_provider_libre_note')?>';
    if (p === 'google') text = '<?=lang('translate_provider_google_note')?>';
    if (p === 'deepl') text = '<?=lang('translate_provider_deepl_note')?>';
    if (p === 'azure') text = '<?=lang('translate_provider_azure_note')?>';
    noteEl.textContent = text;
  }

  function refreshVisibility(){
    const p = providerSel ? providerSel.value : 'none';
    if (!noteEl) return;
    setNote(p);
    // Varsayılan hepsi gizli
    if (apiGroup) apiGroup.classList.add('d-none');
    if (endpointGroup) endpointGroup.classList.add('d-none');

    if (p === 'libre') {
      if (endpointGroup) endpointGroup.classList.remove('d-none');
    } else if (p === 'google' || p === 'deepl') {
      if (apiGroup) apiGroup.classList.remove('d-none');
    } else if (p === 'azure') {
      if (apiGroup) apiGroup.classList.remove('d-none');
      if (endpointGroup) endpointGroup.classList.remove('d-none');
    }
  }

  if (providerSel) {
    refreshVisibility();
    providerSel.addEventListener('change', refreshVisibility);
  }

  // Kaynak dil değişirse hedefler otomatik güncellensin
  const srcInput = document.getElementById('translate_source_input');
  const tgtDisp = document.getElementById('translate_targets_display');
  const tgtHidden = document.getElementById('translate_targets_hidden');
  function recomputeTargets(){
    if (!srcInput || !tgtDisp || !tgtHidden) return;
    const allCodesCsv = srcInput.getAttribute('data-all-codes') || '';
    const all = allCodesCsv.split(',').map(s=>s.trim()).filter(Boolean);
    const src = (srcInput.value || '').trim();
    const targets = all.filter(c => c !== src);
    const csv = targets.join(',');
    tgtDisp.value = csv;
    tgtHidden.value = csv;
  }
  srcInput?.addEventListener('change', recomputeTargets);
  srcInput?.addEventListener('input', recomputeTargets);
  recomputeTargets();
})();

// Google key dosyası yaz
(function(){
  const btn = document.getElementById('btnWriteGoogleKey');
  if (!btn) return;
  btn.addEventListener('click', function(){
    btn.disabled = true; const old = btn.textContent; btn.textContent = 'İşleniyor...';
    fetch('<?=ADMIN_URL?>/settings', {
      method: 'POST',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      credentials: 'same-origin',
      body: new URLSearchParams({ action: 'google_write_key', csrf_token: '<?=$_SESSION['csrf_token']['value']?>' })
    }).then(function(r){
        if (r.redirected) { throw new Error('AUTH_REDIRECT'); }
        const ct = (r.headers.get('content-type') || '').toLowerCase();
        if (!r.ok) throw new Error('HTTP '+r.status);
        if (ct.indexOf('application/json') === -1) { throw new Error('NOT_JSON'); }
        return r.json();
      })
      .then(function(res){
        if (res && res.status === 'ok' && res.reload) { location.reload(); return; }
        if (!(res && res.status === 'ok')) {
          const msg = 'Dosya yazılamadı';
          (window.tosty ? tosty({type:'error', title:'Hata', text: msg}) : alert(msg));
        }
      }).catch(function(err){
        let msg = 'İstek hatası: ' + err.message;
        if ((''+err).includes('AUTH_REDIRECT') || (''+err).includes('HTTP 302') || (''+err).includes('NOT_JSON')) {
          msg = 'Oturum süresi dolmuş olabilir. Lütfen sayfayı yenileyip tekrar deneyin.';
        }
        (window.tosty ? tosty({type:'error', title:'Hata', text: msg}) : alert(msg));
      })
      .finally(function(){ btn.disabled = false; btn.textContent = old; });
  });
})();

// Sitemap ping – manuel tetik (Arama Motorları sekmesi)
(function(){
  const btn = document.getElementById('btnPingSitemapTop');
  const info = document.getElementById('lastPingInfoTop');
  if (!btn) return;
  btn.addEventListener('click', function(){
    btn.disabled = true; const old = btn.textContent; btn.textContent = 'İşleniyor...';
    fetch('<?=ADMIN_URL?>/settings', {
      method: 'POST',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      credentials: 'same-origin',
      body: new URLSearchParams({ action: 'sitemap_ping', csrf_token: '<?=$_SESSION['csrf_token']['value']?>' })
    }).then(function(r){
        if (r.redirected) { throw new Error('AUTH_REDIRECT'); }
        const ct = (r.headers.get('content-type') || '').toLowerCase();
        if (!r.ok) throw new Error('HTTP '+r.status);
        if (ct.indexOf('application/json') === -1) { throw new Error('NOT_JSON'); }
        return r.json();
      })
      .then(function(res){
        if (res && res.status === 'ok') {
          // Başarılı: sistem flash mesajı set edildi, sayfayı yenileyelim
          if (res.reload) { location.reload(); return; }
          info.textContent = '<?=lang('indexing.last_ping')?> ' + (res.now || '');
        } else {
          (window.tosty ? tosty({type:'error', title:'Hata', text:'İşlem tamamlanamadı'}) : alert('İşlem tamamlanamadı'));
        }
      }).catch(function(err){
        let msg = 'İstek hatası: ' + err.message;
        if ((''+err).includes('AUTH_REDIRECT') || (''+err).includes('HTTP 302') || (''+err).includes('NOT_JSON')) {
          msg = 'Oturum süresi dolmuş olabilir. Lütfen sayfayı yenileyip tekrar deneyin.';
        }
        (window.tosty ? tosty({type:'error', title:'Hata', text: msg}) : alert(msg));
      })
      .finally(function(){ btn.disabled = false; btn.textContent = old; });
  });
})();

// IndexNow key dosyası yaz
(function(){
  const btn = document.getElementById('btnWriteIndexNowKey');
  if (!btn) return;
  btn.addEventListener('click', function(){
    btn.disabled = true; const old = btn.textContent; btn.textContent = 'İşleniyor...';
    fetch('<?=ADMIN_URL?>/settings', {
      method: 'POST',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      credentials: 'same-origin',
      body: new URLSearchParams({ action: 'indexnow_write_key', csrf_token: '<?=$_SESSION['csrf_token']['value']?>' })
    }).then(function(r){
        if (r.redirected) { throw new Error('AUTH_REDIRECT'); }
        const ct = (r.headers.get('content-type') || '').toLowerCase();
        if (!r.ok) throw new Error('HTTP '+r.status);
        if (ct.indexOf('application/json') === -1) { throw new Error('NOT_JSON'); }
        return r.json();
      })
      .then(function(res){
        if (res && res.status === 'ok') {
          // Başarılı: sistem flash mesajı set edildi, sayfayı yenileyelim
          if (res.reload) { location.reload(); return; }
        } else {
          const msg = 'Key yazma başarısız: ' + (res && res.message ? res.message : 'Bilinmeyen hata');
          (window.tosty ? tosty({type:'error', title:'Hata', text: msg}) : alert(msg));
        }
      }).catch(function(err){
        let msg = 'İstek hatası: ' + err.message;
        if ((''+err).includes('AUTH_REDIRECT') || (''+err).includes('HTTP 302') || (''+err).includes('NOT_JSON')) {
          msg = 'Oturum süresi dolmuş olabilir. Lütfen sayfayı yenileyip tekrar deneyin.';
        }
        (window.tosty ? tosty({type:'error', title:'Hata', text: msg}) : alert(msg));
      })
      .finally(function(){ btn.disabled = false; btn.textContent = old; });
  });
})();

// Yandex key dosyası yaz
(function(){
  const btn = document.getElementById('btnWriteYandexKey');
  if (!btn) return;
  btn.addEventListener('click', function(){
    btn.disabled = true; const old = btn.textContent; btn.textContent = 'İşleniyor...';
    fetch('<?=ADMIN_URL?>/settings', {
      method: 'POST',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      credentials: 'same-origin',
      body: new URLSearchParams({ action: 'yandex_write_key', csrf_token: '<?=$_SESSION['csrf_token']['value']?>' })
    }).then(function(r){
        if (r.redirected) { throw new Error('AUTH_REDIRECT'); }
        const ct = (r.headers.get('content-type') || '').toLowerCase();
        if (!r.ok) throw new Error('HTTP '+r.status);
        if (ct.indexOf('application/json') === -1) { throw new Error('NOT_JSON'); }
        return r.json();
      })
      .then(function(res){
        if (res && res.status === 'ok' && res.reload) { location.reload(); return; }
        if (!(res && res.status === 'ok')) {
          const msg = 'Dosya yazılamadı';
          (window.tosty ? tosty({type:'error', title:'Hata', text: msg}) : alert(msg));
        }
      }).catch(function(err){
        let msg = 'İstek hatası: ' + err.message;
        if ((''+err).includes('AUTH_REDIRECT') || (''+err).includes('HTTP 302') || (''+err).includes('NOT_JSON')) {
          msg = 'Oturum süresi dolmuş olabilir. Lütfen sayfayı yenileyip tekrar deneyin.';
        }
        (window.tosty ? tosty({type:'error', title:'Hata', text: msg}) : alert(msg));
      })
      .finally(function(){ btn.disabled = false; btn.textContent = old; });
  });
})();

// Doğrulama yöntemi seçimlerine göre alan görünürlüğü
(function(){
  function toggleGroups(){
    const ySel = document.querySelector('select[name="yandex_verify_method_<?=$lang?>"]');
    const gSel = document.querySelector('select[name="google_verify_method_<?=$lang?>"]');
    const bSel = document.getElementById('bing_verify_method_<?=$lang?>');

    const yHtml = document.querySelectorAll('.verify-yandex-group.verify-yandex-html');
    const yMeta = document.querySelectorAll('.verify-yandex-group.verify-yandex-meta');
    const gHtml = document.querySelectorAll('.verify-google-group.verify-google-html');
    const gMeta = document.querySelectorAll('.verify-google-group.verify-google-meta');
    const bHtml = document.querySelectorAll('.verify-bing-group.verify-bing-html');
    const bMeta = document.querySelectorAll('.verify-bing-group.verify-bing-meta');

    function show(nodes, v){ nodes.forEach(n=>n.classList.toggle('d-none', !v)); }

    const yv = ySel ? ySel.value : 'html';
    show(yHtml, yv === 'html');
    show(yMeta, yv === 'meta');

    const gv = gSel ? gSel.value : 'html';
    show(gHtml, gv === 'html');
    show(gMeta, gv === 'meta');

    const bv = bSel ? bSel.value : 'meta';
    show(bMeta, bv === 'meta');
    show(bHtml, bv === 'html');
  }
  toggleGroups();
  document.querySelector('select[name="yandex_verify_method_<?=$lang?>"]')?.addEventListener('change', toggleGroups);
  document.querySelector('select[name="google_verify_method_<?=$lang?>"]')?.addEventListener('change', toggleGroups);
  document.getElementById('bing_verify_method_<?=$lang?>')?.addEventListener('change', toggleGroups);
})();

// Bing key dosyası (BingSiteAuth.xml) yaz
(function(){
  const btn = document.getElementById('btnWriteBingKey');
  if (!btn) return;
  btn.addEventListener('click', function(){
    btn.disabled = true; const old = btn.textContent; btn.textContent = 'İşleniyor...';
    fetch('<?=ADMIN_URL?>/settings', {
      method: 'POST',
      headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      credentials: 'same-origin',
      body: new URLSearchParams({ action: 'bing_write_key', csrf_token: '<?=$_SESSION['csrf_token']['value']?>' })
    }).then(function(r){
        if (r.redirected) { throw new Error('AUTH_REDIRECT'); }
        const ct = (r.headers.get('content-type') || '').toLowerCase();
        if (!r.ok) throw new Error('HTTP '+r.status);
        if (ct.indexOf('application/json') === -1) { throw new Error('NOT_JSON'); }
        return r.json();
      })
      .then(function(res){
        if (res && res.status === 'ok' && res.reload) { location.reload(); return; }
        if (!(res && res.status === 'ok')) {
          const msg = 'Dosya yazılamadı';
          (window.tosty ? tosty({type:'error', title:'Hata', text: msg}) : alert(msg));
        }
      }).catch(function(err){
        let msg = 'İstek hatası: ' + err.message;
        if ((''+err).includes('AUTH_REDIRECT') || (''+err).includes('HTTP 302') || (''+err).includes('NOT_JSON')) {
          msg = 'Oturum süresi dolmuş olabilir. Lütfen sayfayı yenileyip tekrar deneyin.';
        }
        (window.tosty ? tosty({type:'error', title:'Hata', text: msg}) : alert(msg));
      })
      .finally(function(){ btn.disabled = false; btn.textContent = old; });
  });
})();
</script>
