<?php
if($id<>"" and $fid==""){
    // Form ID boş değilse
    $sql=mysqli_query($conn, "select * from ".$_SESSION['lang']." where id=".$id.""); 
    $rs=mysqli_fetch_assoc($sql);
    $konu=meta($sayfa[0],$id);
}
if($fid==""){$fid=$rs["s16"];}

// Form verilerini çek
$sqlf=mysqli_query($conn, "select * from ".$_SESSION['lang']." where id=".$fid." or t8='".$fid."'"); $rsf=mysqli_fetch_assoc($sqlf);

if($id=="" and $fid<>""){
    // Form ID boş değilse
    $konu=$rsf["m1"]; $id="0"; 
}
if($id<>"" and $fid<>""){
    // Başlıkta yıldız (*) kontrolü
    if(substr($rsf["m1"],0,1)=="*"){ 
        $konu=ltrim($rsf["m1"],"*"); 
    }else{ 
        $konu=meta($sayfa[0],$id); 
    }
}

// JSON verilerini çözümle
$jstn = json_decode($rsf["n1"], true);
$jssi = json_decode($rsf["n2"], true);

// Zorunlu alan kontrolü için sayaç
if(mysqli_num_rows($sqlf)>0) { 
    $c=0; 
    for ( $i=1 ; $i < 99 ; $i++ ){
        if($jssi["s".$i.""]==1){ $c++; }
    }
}
$rID=date("dmYHis");
?>

<style type="text/css">
    :root {
        --wa-gold: #cca352;
        --wa-dark: #333;
        --wa-grey: #f8f9fa;
        --wa-border: #e0e0e0;
        --wa-focus: rgba(204, 163, 82, 0.15);
        --wa-error: #ff4d4d;
    }

    /* Konteyner ve Genel Yapı */
    .wa-form-container { font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif; }
    .wa-form-group { margin-bottom: 20px; position: relative; }
    
    /* Başlık ve Açıklamalar */
    .fbaslik {
        display: block; font-weight: 600; color: var(--wa-dark);
        margin-bottom: 8px; font-size: 14px; letter-spacing: 0.3px;
    }
    .fbaslik span.req { color: var(--wa-error); margin-left: 3px; }
    .faciklama { display: block; font-size: 11px; color: #888; margin-top: 5px; font-style: normal; line-height: 1.4; }

    /* Form Elemanları (Input, Select, Textarea) */
    .v3-form-control { width: 100%; height: 46px !important; padding: 10px 15px; border: 1px solid var(--wa-border); border-radius: 8px; background-color: #fff; color: #333; font-size: 14px; transition: all 0.3s ease; box-shadow: 0 2px 4px rgba(0,0,0,0.01); }
    .v3-form-control:focus { border-color: var(--wa-gold); box-shadow: 0 0 0 4px var(--wa-focus); outline: none; }
    textarea.v3-form-control { height: auto !important; min-height: 120px; resize: vertical; line-height: 1.5; }
    
    /* Özel Durumlar */
    .fzorunlu { border-color: var(--wa-error) !important; background-color: #fffafa; }
    .dn { display: none !important; }
    
    /* Checkbox ve Radio Grupları */
    .wa-check-group { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 5px; }
    .wa-check-item { display: flex; align-items: center; background: var(--wa-grey); padding: 10px 15px; border-radius: 6px; cursor: pointer; border: 1px solid transparent; transition: 0.2s; user-select: none; width: 100%; /* Mobil öncelikli */ }
    .wa-check-item:hover { border-color: #ddd; background: #fff; }
    .wa-check-item.active { border-color: var(--wa-gold); background: #fffcf5; }
    .wa-check-item input[type="radio"], .wa-check-item input[type="checkbox"] { margin-right: 10px; transform: scale(1.1); accent-color: var(--wa-gold); }

    /* Submit Butonu */
    .wa-btn-submit { background: var(--wa-dark); color: var(--wa-gold); border: none; padding: 0 30px; height: 50px; font-weight: 700; text-transform: uppercase; letter-spacing: 1px; border-radius: 8px; cursor: pointer; transition: 0.3s; display: flex; align-items: center; justify-content: center; width: 100%; font-size: 15px; }
    .wa-btn-submit:hover { background: var(--wa-gold); color: #fff; transform: translateY(-2px); box-shadow: 0 10px 20px rgba(0,0,0,0.1); }

    /* Grid İyileştirmeleri */
    @media (min-width: 768px) {
        .wa-check-item { width: auto; } /* Masaüstünde yan yana */
        .wa-btn-submit { width: auto; float: right; min-width: 180px; }
    }
    
    /* Input içi mini inputlar (Diğer: [___] gibi) */
    .input-sm-inline { border: none; border-bottom: 1px solid #ccc; background: transparent; margin-left: 5px; padding: 2px 5px; font-size: 13px; width: 100px; }
    .input-sm-inline:focus { border-bottom-color: var(--wa-gold); outline: none; }

    /* Admin tarafından eklenen ekstra CSS */
    <?php echo $rsf["n4"];?>
</style>

<div class="v3-row v3-form wa-form-container">
    <form enctype="multipart/form-data" id="f<?php echo $rsf["id"]?>" class="<?php echo $rsf["t4"];?> <?php if ($c>0){echo "fcontrol";}?>" autocomplete="off">
        
        <input type="hidden" value="<?php echo $rsf["m4"];?>" name="nedir" id="nedir">
        <input type="hidden" value="<?php echo $konu;?>" name="Konu" id="subject">
        <input type="hidden" value="<?php echo ltrim($rsf["m1"],"*");?>" name="fname" id="fname">
        <input type="hidden" value="<?php echo $rsf["t2"];?>" name="malici" id="malici">
        <input type="hidden" value="<?php echo $rsf["t3"];?>" name="cc" id="cc">
        <input type="hidden" value="<?php echo $rsf["t5"];?>" name="bcc" id="bcc">
        <input type="hidden" value="<?php echo $rsf["id"];?>" name="fid" id="fid">
        <input type="hidden" value="<?php echo $id;?>" name="sid" id="sid">
        <?php if($rsf["m6"]==1){ ?><input type="hidden" value="<?php echo $rID?>" name="rID" id="rID"><?php }?>
        <input type="hidden" value="tr" name="flang" id="lang">
        <input type="hidden" value="<?php echo $rsf["m2"];?>" name="ftip" id="ftip">
        <?php foreach ($_POST as $key => $value){?><input type="hidden" value="<?php echo $value;?>" name="<?php echo $key;?>"><?php }?>

        <?php 
        // --- FORM DÖNGÜSÜ ---
        if(mysqli_num_rows($sqlf)>0) { 
            for ( $i=1 ; $i < 99 ; $i++ ){ 
                
                // Grid Hesaplama (v3-col-md yapısı)
                $cols = explode(",", $jstn["c".$i.""]);
                if(isset($cols[1]) && $cols[1]!="") {
                    $colmd=$cols[0]; $colsm=$cols[1]; $colxs=$cols[2];
                } else {
                    $colmd=$jstn["c".$i.""]; $colsm="12"; $colxs="12";
                }
                // Grid sınıfını oluştur
                $gridClass = "v3-col-md-".$colmd." v3-col-sm-".$colsm." v3-col-xs-".$colxs;
        ?>

        <?php if(!empty($jstn["t".$i.""])){ ?>

            <?php if($jssi["i".$i.""]=="6"){ ?>
                <div class="<?php echo $gridClass; ?> <?php echo $jstn["class".$i.""]?> wa-form-group">
                    <div class="fbaslik"><?php echo translate(str_replace(" / ",", ",$jstn["t".$i.""]));?></div>
                    <?php if(!empty($jstn["n".$i.""])){?>
                        <div class="faciklama" style="font-size:13px; color:#666;"><?php echo translate($jstn["n".$i.""]);?></div>
                    <?php }?>
                </div>
            <?php }?>

            <?php if($jssi["i".$i.""]=="0"){ ?>
                <div class="<?php echo $gridClass; ?> <?php echo $jstn["class".$i.""]?> wa-form-group <?php if($jstn["type".$i.""]=="hidden"){echo "dn";}?>">
                    <label class="fbaslik" for="<?php echo ($jssi["id".$i.""]) ? $jssi["id".$i.""] : "i".$i.""; ?>">
                        <?php echo translate(str_replace(" / ",", ",$jstn["t".$i.""]));?> 
                        <?php if($jssi["s".$i.""]==1){echo "<span class='req'>*</span>";}?>
                    </label>
                    <input type="<?php echo ($jstn["type".$i.""]) ? $jstn["type".$i.""] : "text"; ?>" id="<?php echo ($jssi["id".$i.""]) ? $jssi["id".$i.""] : "i".$i.""; ?>" name="<?php echo ($jstn["name".$i.""]) ? $jstn["name".$i.""] : cevir4(str_replace(" / ",", ",$jstn["t".$i.""])); ?>" placeholder="<?php echo $jstn["p".$i.""]?>" <?php if($jstn["mask".$i.""]<>""){?>data-input-mask="<?php echo $jstn["mask".$i.""]?>"<?php }?> <?php echo $jstn["attr".$i.""]?> value="<?php echo rlang($jstn["v".$i.""])?>" class="v3-form-inputs v3-form-control <?php echo $jstn["class".$i.""]?> <?php if($jssi["s".$i.""]==1){echo "icontrol";}?> <?php if($jstn["mask".$i.""]<>""){echo "phone";}?>">
                    <?php if(!empty($jstn["n".$i.""])){?> <div class="faciklama"><?php echo translate($jstn["n".$i.""]);?></div> <?php }?>
                </div>
            <?php }?>

            <?php if($jssi["i".$i.""]=="1"){ ?>
                <div class="<?php echo $gridClass; ?> <?php echo $jstn["class".$i.""]?> wa-form-group">
                    <label class="fbaslik" for="<?php echo ($jssi["id".$i.""]) ? $jssi["id".$i.""] : "n".$i.""; ?>">
                        <?php echo translate(str_replace(" / ",", ",$jstn["t".$i.""]));?> 
                        <?php if($jssi["s".$i.""]==1){echo "<span class='req'>*</span>";}?>
                    </label>
                    <textarea name="<?php echo ($jstn["name".$i.""]) ? $jstn["name".$i.""] : cevir4(str_replace(" / ",", ",$jstn["t".$i.""])); ?>" id="<?php echo ($jssi["id".$i.""]) ? $jssi["id".$i.""] : "n".$i.""; ?>" placeholder="<?php echo $jstn["p".$i.""]?>" <?php echo $jstn["attr".$i.""]?> class="v3-form-inputs v3-form-control <?php echo $jstn["class".$i.""]?> <?php if($jssi["s".$i.""]==1){echo "icontrol";}?>"><?php echo $jstn["v".$i.""]?></textarea>
                    <?php if(!empty($jstn["n".$i.""])){?> <div class="faciklama"><?php echo translate($jstn["n".$i.""]);?></div> <?php }?>
                </div>
            <?php }?>

            <?php if($jssi["i".$i.""]=="2" and $jstn["type".$i.""]==""){ ?>
                <div class="<?php echo $gridClass; ?> <?php echo $jstn["class".$i.""]?> wa-form-group">
                    <label class="fbaslik">
                        <?php echo translate(str_replace(" / ",", ",$jstn["t".$i.""]));?> 
                        <?php if($jssi["s".$i.""]==1){echo "<span class='req'>*</span>";}?>
                    </label>
                    <div style="position:relative;">
                        <select name="<?php echo ($jstn["name".$i.""]) ? $jstn["name".$i.""] : cevir4(str_replace(" / ",", ",$jstn["t".$i.""])); ?>" id="<?php echo ($jssi["id".$i.""]) ? $jssi["id".$i.""] : "s".$i.""; ?>" <?php echo $jstn["attr".$i.""]?> class="v3-form-inputs v3-form-control <?php echo $jstn["class".$i.""]?> <?php if($jssi["s".$i.""]==1){echo "icontrol";}?>">
                            <option value=""><?php echo translate("Lütfen Seçiniz")?></option>
                            <?php $parcala=$jstn["n".$i.""]; $n= substr_count($parcala, ','); $s= 0; while ( $s <= $n ) {$baslik=explode(",",$parcala);?>
                            <option value="<?php echo translate($baslik[$s]);?>" <?php if($baslik[$s]==$jstn["v".$i.""]){echo "Selected";}?>><?php echo translate(str_replace(" / ",", ",$baslik[$s]));?></option>
                            <?php $s ++; }?>
                        </select>
                        <i class="fa fa-chevron-down" style="position:absolute; right:15px; top:50%; transform:translateY(-50%); pointer-events:none; color:#999; font-size:12px;"></i>
                    </div>
                </div>
                
                <script type="text/javascript">
                    <?php if($jssi["id".$i.""]=="country"){ ?>
                        jQuery(document).ready(function() { jQuery('#<?php echo $jssi["id".$i.""]?>').load('<?php echo $weburl;?>/deppo/tools/script/location.php?ne=ulke&sID=<?php echo $module["countrycode"]?>'); });
                    <?php }?>
                    <?php if($jssi["id".$i.""]=="city"){ ?>
                        jQuery(document).ready(function() { jQuery('#<?php echo $jssi["id".$i.""]?>').load('<?php echo $weburl;?>/deppo/tools/script/location.php?ne=il&ID=<?php echo $module["countrycode"];?>'); });
                        jQuery('#country').on('change', function(){
                            var ID = jQuery('#country option:selected').attr('ID');
                            jQuery('#<?php echo $jssi["id".$i.""]?>').load('<?php echo $weburl;?>/deppo/tools/script/location.php?ne=il&ID='+ID);
                        });
                    <?php }?>
                    <?php if($jssi["id".$i.""]=="town"){ ?>
                        jQuery(document).ready(function() { jQuery('#<?php echo $jssi["id".$i.""]?>').load('<?php echo $weburl;?>/deppo/tools/script/location.php?ne=ilce&ID=<?php echo $module["countrycode"]?>'); });
                        jQuery('#city').on('change', function(){
                            var ID = jQuery('#city option:selected').attr('ID');
                            jQuery('#<?php echo $jssi["id".$i.""]?>').load('<?php echo $weburl;?>/deppo/tools/script/location.php?ne=ilce&ID='+ID);
                        });
                    <?php }?>
                </script>
            <?php }?>

            <?php if(($jssi["i".$i.""]=="3" || $jssi["i".$i.""]=="4")){ 
                // Grid mantığı
                $subCols = explode("/", $jstn["c".$i.""]);
                $subColClass = isset($subCols[1]) ? "v3-col-md-".$subCols[1] : "auto"; // Auto ile içeriğe göre genişlik
            ?>
                <div class="<?php echo $gridClass; ?> <?php echo $jstn["class".$i.""]?> wa-form-group">
                    <div class="fbaslik">
                        <?php if($jssi["i".$i.""]=="4" && $jstn["type".$i.""]==""){ ?>
                            <input type="checkbox" class="all<?php echo $i;?>" style="margin-right:5px; vertical-align:middle;">
                        <?php } ?>
                        <?php echo translate(str_replace(" / ",", ",$jstn["t".$i.""]));?> 
                        <?php if($jssi["s".$i.""]==1){echo "<span class='req'>*</span>";}?>
                    </div>

                    <div class="wa-check-group">
                        <?php $parcala=$jstn["n".$i.""]; $n= substr_count($parcala, ','); $s= 0; while ( $s <= $n ) {$baslik=explode(",",$parcala);
                            // {input} değiştirme mantığı (Diğer: [___])
                            $labelTxt = translate(str_replace(" / ",", ",$baslik[$s]));
                            $labelTxt = str_replace("{input}","<input type=\"text\" name=\"".translate("ex_inp_name_".$s."")."\" class=\"input-sm-inline\" placeholder=\"...\">", $labelTxt);
                        ?>
                            <label class="wa-check-item <?php echo $subColClass; ?>">
                                <input type="<?php echo ($jssi["i".$i.""]=="3") ? "radio" : "checkbox"; ?>" id="<?php echo ($jssi["i".$i.""]=="3") ? "rb" : "cb"; ?><?php echo $i.$s;?>" name="<?php echo ($jstn["name".$i.""]) ? $jstn["name".$i.""] : cevir4(str_replace(" / ",", ",$jstn["t".$i.""])); ?><?php echo ($jssi["i".$i.""]=="4") ? "[]" : ""; ?>" value="<?php echo translate(str_replace(" / ",", ",$baslik[$s]));?>" <?php if($baslik[$s]==$jstn["v".$i.""]){echo "checked";}?> class="<?php echo ($jssi["i".$i.""]=="3") ? "r" : "c"; ?><?php echo $i;?>">
                                <span><?php echo $labelTxt;?></span>
                            </label>
                        <?php $s++;}?>
                    </div>
                </div>
                
                <?php if($jssi["i".$i.""]=="4" && $jstn["type".$i.""]==""){ ?>
                <script type="text/javascript">
                    jQuery('.all<?php echo $i;?>').click(function(event) {   
                        var state = this.checked;
                        jQuery('.c<?php echo $i;?>').each(function() { this.checked = state; });
                    });                     
                </script>
                <?php } ?>
            <?php }?>

            <?php if($jssi["i".$i.""]=="5"){ ?>
                <div class="<?php echo $gridClass; ?> <?php echo $jstn["class".$i.""]?> wa-form-group <?php if($jstn["type".$i.""]=="hidden"){echo "dn";}?>">
                    <label class="fbaslik"><?php echo translate(str_replace(" / ",", ",$jstn["t".$i.""]));?> <?php if($jssi["s".$i.""]==1){echo "<span class='req'>*</span>";}?></label>
                    <div style="background:#fff; border:1px dashed var(--wa-border); padding:10px; border-radius:8px;">
                        <input type="file" <?php if($jssi["id".$i.""]<>""){echo "id=\"".$jssi["id".$i.""]."\"";}?> name="<?php echo ($jstn["name".$i.""]) ? $jstn["name".$i.""] : cevir4(str_replace(" / ",", ",$jstn["t".$i.""])); ?>" class="v3-form-control <?php echo $jstn["class".$i.""]?> <?php if($jssi["s".$i.""]==1){echo "icontrol";}?>" style="border:none; height:auto !important; padding:5px; box-shadow:none;">
                    </div>
                    <?php if(!empty($jstn["n".$i.""])){?> <div class="faciklama">* <?php echo translate($jstn["n".$i.""]);?></div> <?php }?>
                </div>
            <?php }?>

        <?php } // t check ?>
        <?php } // for loop ?>
        <?php } // sql check ?>

        <div class="v3-col-md-12" style="margin-top:10px; padding:0;">
            <div class="v3-col-md-6 hidden-xs">
                <?php // Captcha alanı (Gerekirse açılabilir) include("".$path."deppo/tools/script/captcha.php");?>
            </div>
            <div class="v3-col-md-6 v3-col-xs-12" style="padding:0;">
                <button type="button" class="wa-btn-submit" data-url="<?php echo $cpath?>config/func.php?islem=fgonder" <?php if(!empty($rsf["n5"])){ ?>data-aurl="<?php echo $path?>config/func.php?islem=fanswer"<?php } ?> <?php if($_GET["burl"]<>""){?>data-burl="<?php echo $path."".v3d($_GET["burl"])?>"<?php }elseif($rsf["t10"]<>""){?>data-burl="<?php echo $rsf["t10"]?>"<?php }?> <?php if($rsf["m6"]==1){?>data-burl="?result/<?php echo $rID?>"<?php }?> data-msg="<?php echo ($rsf["t9"]<>"") ? translate($rsf["t9"]) : translate("+İletiniz Bize Ulaştı");?>" data-form="#f<?php echo $rsf["id"]?>">
                    <span><?php echo translate("GÖNDER");?></span> 
                    <i class="fa fa-paper-plane" style="margin-left:10px;"></i>
                </button>
            </div>                  
        </div>

    </form>
</div>