<?php
	header('Content-Type: text/html; charset=utf-8');
	error_reporting(0);
	$path="../../";
	include("../../map.php");
	include("../../scripts.php");
	include("../../secure.php");
	//findDirectoryPath("deppo")

	if($_GET["id"]<>""){
		$sqlf=mysqli_query($conn, "select * from t4 where id=".$_GET["id"].""); $rsf=mysqli_fetch_assoc($sqlf); $jsi = json_decode($rsf["n17"],true);
		$sqlm=mysqli_query($conn, "select * from t5 where id=".$rsf["s1"].""); $rs5=mysqli_fetch_assoc($sqlm);
		$fcariID=$rsf["s1"]; $fcariGsm=$rs5["t4"]; $fcariName=$rs5["t1"]; $fcariEmail=v3d($rs5["t2"]);
	}
?>
<!DOCTYPE html>
<html lang="en">
<style>
    body { background: #f7f7f7; }
    .panel { border-radius: 10px; }
    .panel-heading { font-weight: bold; }
    .form-section { margin-bottom: 15px; }
    .toggle-add { cursor: pointer; color: #337ab7; font-size: 16px; margin-left: 10px; }
  </style>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $module["repairs_text"];?></title>
</head>

<body>
    <div class="qmodal">
        <div class="qmodal-content">
		<div class="alert alert-warning" id="result" style="display:none;"></div>

            <!-- Header with Action Buttons -->
            <div class="qmodal-header">
			<div class="header-title">
				<nav aria-label="breadcrumb">
					<ol class="breadcrumb">
						<li class="breadcrumb-item"><a href="?"><i class="fa fa-home"></i></a></li>
						<li class="breadcrumb-item"><a href="#"><?php echo $module["repairs_text"];?></a></li>
						<li class="breadcrumb-item active" aria-current="page">Form</li>
					</ol>
				</nav>
			</div>

                <button type="button" class="btn btn-warning" onclick="AjaxKaydet('','');"><i class="fas fa-save"></i> Kaydet</button>
                <button type="button" onclick="window.open('../print/print_repair.php?id=<?php echo $rsf["id"];?>')" class="btn btn-warning"><i class="fas fa-print"></i> Yazdır</button>
            </div>

            <!-- Left Tab Menu -->
            <div class="v8-columns">
                <!-- Content Area -->
                <div class="" ts1="<?php echo dtarih($rs["ts1"],"gg ka yy time");?>" ts2="<?php echo dtarih($rs["ts2"],"gg ka yy time");?>" ts3="<?php echo dtarih($rs["ts3"],"gg ka yy time");?>">
					<form class="form" action="?ne=<?php echo $_GET["ne"];?>&islem=<?php echo $_GET["islem"];?>&id=<?php echo $_GET["id"];?>" id="asave" name="aform" method="post" autocomplete="false" style="width:100%">
					  <div class="panel-group" id="accordion" style="margin-top: 15px;">

						<!-- Müşteri Bilgileri -->
						<div class="panel panel-primary">
						  <div class="panel-heading">
							<a data-toggle="collapse" data-parent="#accordion" href="#musteriPanel" style="color:white;display:block;">
							  <i class="fas fa-user"></i> Müşteri Bilgileri
							</a>
						  </div>
						  <div id="musteriPanel" class="panel-collapse collapse in">
							<div class="panel-body">

							  <div class="form-group">
								<label>Ad Soyad</label>
								<input type="text" name="Ad_Soyad" class="form-control" value="<?php echo $jsi["Ad_Soyad"] ?? "";?>">
							  </div>

							  <div class="row">
								<div class="col-sm-6">
								  <div class="form-group">
									<label>Telefon</label>
									<input type="text" name="Gsm" class="form-control" data-plugin-masked-input="" data-input-mask="(999) 999-9999" value="<?php echo $jsi["Gsm"] ?? "";?>">
								  </div>
								</div>
								<div class="col-sm-6">
								  <div class="form-group">
									<label>E-posta</label>
									<input type="email" name="Email" class="form-control" value="<?php echo $jsi["Email"] ?? "";?>">
								  </div>
								</div>
							  </div>

							</div>
						  </div>
						</div>

						<!-- Cihaz Bilgileri -->
						<div class="panel panel-info">
						  <div class="panel-heading">
							<a data-toggle="collapse" data-parent="#accordion" href="#cihazPanel" style="display:block;">
							  <i class="fas fa-mobile"></i> Cihaz Bilgileri
							</a>
						  </div>
						  <div id="cihazPanel" class="panel-collapse collapse">
							  <div class="panel-body">
								<div class="row">
								  <div class="form-group col-md-6">
									<label>Cihaz Adı / Modeli</label>
									<input type="text" name="Cihaz_Adi_Modeli" class="form-control" 
										   value="<?php echo $jsi["Cihaz_Adi_Modeli"] ?? "";?>">
								  </div>
								  <div class="form-group col-md-6">
									<label>Seri No</label>
									<input type="text" name="Seri_No" class="form-control" value="<?php echo $jsi["Seri_No"] ?? "";?>">
								  </div>
								</div>
								<div class="form-group">
								  <label>Arıza Açıklaması</label>
								  <textarea name="Aciklama" class="form-control"><?php echo $jsi["Aciklama"] ?? "";?></textarea>
								</div>
							  </div>
							</div>

						  </div>
						</div>

						<!-- Onarım Detayları -->
						<div class="panel panel-success" style="margin-top: 5px;">
						  <div class="panel-heading">
							<a data-toggle="collapse" data-parent="#accordion" href="#onarimPanel" style="display:block;">
							  <i class="fas fa-screwdriver"></i> Onarım Detayları
							</a>
						  </div>
						  <div id="onarimPanel" class="panel-collapse collapse">
							  <div class="panel-body">

								<div class="row">
								  <div class="form-group v3-col-md-6">
									<label>Yaklaşık Ücret</label>
									<div class="input-group">
									  <input type="number" name="Ucret" class="form-control" placeholder="₺" value="<?php echo $jsi["Ucret"] ?? "";?>">
									  <span class="input-group-addon">₺</span>
									</div>
								  </div>
								  <div class="form-group v3-col-md-6">
									<label>Tahmini Onarım Süresi</label>
									<input type="text" name="Sure" class="form-control" placeholder="örn: 3 Gün" value="<?php echo $jsi["Sure"] ?? "";?>">
								  </div>
								</div>

								<div class="row">
								  <div class="form-group v3-col-md-6">
									<label>İlgili Personel</label>
									<select name="s19" class="form-control">
									  <option value="0">-- Personel Seçin --</option>
									  <?php $sql5 = mysqli_query($conn, "SELECT * FROM t5 WHERE (s9=1 OR s1=1) ORDER BY t1 ASC"); while($rs5 = mysqli_fetch_assoc($sql5)){ ?>
										<option value="<?php echo $rs5["id"];?>"
										  <?php if(($rsf["s19"] ?? 0) == $rs5["id"]){ echo "selected"; } ?>>
										  <?php echo $rs5["t1"];?>
										</option>
									  <?php } ?>
									</select>
								  </div>

								  <div class="form-group v3-col-md-6">
									<label>Servis Durumu</label>
									<select name="s4" class="form-control">
									  <option value="0" <?php if(($rsf["s4"] ?? "")=="0") echo "selected";?>>Beklemede</option>
									  <option value="4" <?php if(($rsf["s4"] ?? "")=="4") echo "selected";?>>İşlemde</option>
									  <option value="3" <?php if(($rsf["s4"] ?? "")=="3") echo "selected";?>>Tamamlandı</option>
									  <option value="1" <?php if(($rsf["s4"] ?? "")=="1") echo "selected";?>>Teslim Edildi</option>
									  <option value="2" <?php if(($rsf["s4"] ?? "")=="2") echo "selected";?>>İptal Edildi</option>
									</select>
								  </div>
								</div>

								<div class="form-group">
								  <label>Ek Notlar</label>
								  <textarea name="Notlar" class="form-control"><?php echo $jsi["Notlar"] ?? "";?></textarea>
								</div>

							  </div>
							</div>

						  </div>
						</div>

					  </div>
					</form>

                 </div>
            </div>
</body>

</html>

<?php
if($_POST){
    $musteri_id = intval($_POST["s1"]);

    // Yeni müşteri girildiyse kontrol et
	if ($musteri_id == 0 && (!empty($_POST["ut4"]) || !empty($_POST["ut2"]))) {
		$tel  = mysqli_real_escape_string($conn, trim($_POST["ut4"]));
		$mail = mysqli_real_escape_string($conn, trim($_POST["ut2"]));
		$ad   = mysqli_real_escape_string($conn, trim($_POST["ut1"]));

		// Öncelik: Telefon numarası
		if ($tel != "") {
			$sqlc = mysqli_query($conn, "SELECT id FROM t5 WHERE t4='$tel' LIMIT 1");
			if ($rsc = mysqli_fetch_assoc($sqlc)) {
				$musteri_id = $rsc["id"];
			}
		}

		// Eğer telefon ile bulunamadıysa mail kontrol et
		if ($musteri_id == 0 && $mail != "") {
			$sqlc = mysqli_query($conn, "SELECT id FROM t5 WHERE t2='$mail' LIMIT 1");
			if ($rsc = mysqli_fetch_assoc($sqlc)) {
				$musteri_id = $rsc["id"];
			}
		}

		// Telefon + Mail hiç yoksa yeni müşteri ekle
		if ($musteri_id == 0) {
			mysqli_query($conn, "INSERT INTO t5 (t1,t2,t4) VALUES ('$ad','$mail','$tel')");
			$musteri_id = mysqli_insert_id($conn);
		}
	}

    // JSON verilerini hazırla (i* inputlar)
    $jsondata = json_encode($_POST, JSON_UNESCAPED_UNICODE);
    $s19 = mysqli_real_escape_string($conn,$_POST["s19"]);
    $now = date("Y-m-d H:i:s");

    // Kaydet / Güncelle
    if($_GET["id"]<>""){
        $sqlu = "UPDATE t4 SET s1='$musteri_id', s4='".$_POST["s4"]."', s19='$s19', n17='$jsondata', ts1='$now' WHERE id=".intval($_GET["id"]); mysqli_query($conn,$sqlu);
    }else{
        $sqlu = "INSERT INTO t4 (ne,s1,s4,s19,n17,ts1) VALUES ('repair','$musteri_id','".$_POST["s4"]."','$s19','$jsondata','$now')"; mysqli_query($conn,$sqlu);
    }
	echo location("?ne=".$_GET["ne"]."&islem=".$_GET["islem"]."&id=".$_GET["id"]."&durum=ok",self,500,1);
}
?>
<?php echo $footer?>