<?php
   $path="../../";
   include("../../map.php");
   include("../../scripts.php");
   $sql = mysqli_query($conn, "select * from t2 where id=".$_GET["id"].""); $rs=mysqli_fetch_assoc($sql);
   $notify = array ('desktop' => '1','ndesktop' => '1','browser' => '1','nbrowser' => '1');
   $rjsn = json_encode($notify, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
   $Up = mysqli_query($conn, "update t2 set n12='".addslashes(stripslashes($rjsn))."' where id=".$_GET["id"]."");
   $jsi = json_decode($rs["n2"], true);
	function creq($metin){
		$metin = str_replace("_", " ", $metin);
		$metin = str_replace("Attach ", "", $metin);
		return $metin;
	}
	if ($authority==0){ $dauth=0; }else{ $dauth=1; }
   ?>
<?php if($_GET["durum"] == "" || $_GET["durum"] == "ok"){?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $rs["t2"]?></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="#">Talep</a></li>
						<li class="breadcrumb-item active" aria-current="page"><?php echo $rs["t2"]?> <?php if($_GET["durum"]=="ok"){echo "{".translate("İşlem Başarılı")."}";}?></li>
					</ol>
				</nav>
			</div>
			<?php if($module["request_path"]<>"" || file_exists("../../../request/request.php")){?>
				 <button type="button" onclick="window.open('<?php echo !empty($module["request_path"]) ? "../../".$module["request_path"] : (file_exists("../../../request/request.php") ? "../../../request/request.php" : "modules/requests/request.php"); ?>?id=<?php echo $rs["id"]?>')" class="v3-btn v3-btn-warning noprint"><i class="fa fa-eye"></i></button>
			<?php }?>
			<?php if($jsi["Gsm"]<>""){?>
				 <button type="button" onclick="window.open('https://wa.me/send?phone=<?php echo telefon($jsi["Gsm"])?>&text=<?php echo urlencode($jsi["Konu"]." hakkında")?>')" class="v3-btn v3-btn-success noprint"><i class="fab fa-whatsapp"></i></button>
			<?php }?>
			<?php if($jsi["Email"]<>""){?>
				 <button type="button" onclick="window.open('mailto:<?php echo $jsi["Email"] ?>?subject=<?php echo urlencode($jsi["Konu"]." hakkında")?>')" class="v3-btn v3-btn-warning noprint"><i class="fas fa-envelope"></i></button>
			<?php }?>
				 <button type="button" onclick="window.parent.linkinmodal('?ne=request&islem=<?php echo $rs["ne"]?>&durum=filtre&fID=<?php echo $rs["s1"]?>&key=<?php echo $jsi["Request_No"];?>')" class="v3-btn v3-btn-info fas fa-list-ul noprint"></button>
				 <button type="button" onclick="divyaz('yazdir');" class="v3-btn v3-btn-info noprint fas fa-print"></button>
                <button type="button" class="btn btn-warning" onclick="<?php echo ($dauth==0 && $jsy["y17_action"]!==1) ? 'return false;' : "AjaxKaydet('', '');"; ?>" <?php if($dauth==0 && $jsy["y17_action"]!==1){echo "disabled";} ?>><i class="fas fa-save"></i> Kaydet</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 method="post" action="?islem=<?php echo $_GET['islem']?>&durum=isle&id=<?php echo $_GET["id"]?>" id="asave" name="aform">
						 <div class="v3-panel" style="margin-bottom: 0px;">
							<div class="v3-panel-heading v3-<?php if($rs["s4"]==0){echo "bg-warning";}elseif($rs["s4"]==1){echo "bg-success";}elseif($rs["s4"]==2){echo "bg-danger";}else{echo "bg-info";}?>" style="font-size: 16px;">
								<i class="fa-solid fa-receipt"></i> <span class="mdn"><?php echo translate("Talep Bilgisi")?></span>
								<span style="float:right; margin-top: -5px; width:200px; " class="mdn"><input type="text" name="Date" class="v3-form-control" value="<?php echo $jsi["Date"];?>" style="text-align:right;"></span>
								<?php
								$statuses = [];
								for ($i = 0; $i <= 9; $i++) {
									$key = "#".$_GET['islem']."_statu".$i;
									if (hasTranslation($key)) { $statuses[$i] = translate($key); }
								}
								?>
								<select name="s4" class="v3-form-control" style="width:200px; float:right;margin-top: -5px; <?php if($dauth==0 && $jsy["y17_status"]!==1){echo "display:none;";} ?>">
									<option value="0" <?php if($_GET["s4"]==""){echo "Selected";}?>><?php echo translate("#Durum")?></option>
									<?php foreach ($statuses as $key => $label): ?>
										<option value="<?= $key; ?>" <?= ($rs['s4'] == $key) ? "selected" : ""; ?>><?php echo $label?></option>
									<?php endforeach; ?>
								</select>
							</div>
							<div class="v3-panel-body">
							   <div class="v3-col-md-3 v3-col-xs-12" style="margin-bottom:5px;border-left: 1px solid #ccc;">
								  <b><?php echo translate("Talep Eden")?></b><br>
								  <select size="1" name="s3" style="" class="v3-form-control" onchange="this.form.submit()">
									 <option value="0">...</option>
									 <?php $sqlm=mysqli_query($conn, "select * from t5 $luye order by t1 asc"); while($rsm=mysqli_fetch_assoc($sqlm)){?>
									 <option value="<?php echo $rsm["id"];?>" title="<?php echo $rsm["t1"];?>" alt="<?php echo $rsm["t1"];?>" style="color:#6699FF" <?php if ($rsm["id"]==$rs["s3"]) {?>Selected<?php }?>><?php echo $rsm["t1"];?></option>
									 <?php }?>
								  </select>
							   </div>
							   <div class="v3-col-md-3 v3-col-xs-12" style="margin-bottom:5px;border-left: 1px solid #ccc;">
								  <b><?php echo translate("İşlem Sorumlusu")?></b><br>
								  <select size=1 name="s19[]" class="form-control chosen-select" tabindex="8" multiple>
										<option value="0" <?php echo ($rs["s19"] == '0' || in_array('0', multivalues($rs["s19"]))) ? 'selected' : '';?>>Admin</option>
										<?php $sql=mysqli_query($conn, "select s2,id,t1 from t5 where (s9=1 or s1=1) $luye"); while($rsm=mysqli_fetch_assoc($sql)){?>
											<option class="" value="<?php echo $rsm["id"];?>" title="<?php echo $rsm["t1"];?>" alt="<?php echo $rsm["t1"];?>" <?php echo in_array($rsm["id"], multivalues($rs["s19"])) ? 'selected' : '';?>><?php echo $rsm["t1"];?></option>
										<?php }?>
									</select>
							   </div>
							   <div class="v3-col-md-3 v3-col-xs-12" style="margin-bottom:5px;border-left: 1px solid #ccc;">
								  <b><?php echo translate("Request No")?></b><br>
								  <input type="text" name="Request_No" class="v3-form-control" value="<?php echo $jsi["Request_No"];?>">
							   </div>
							   <div class="v3-col-md-3 v3-col-xs-12" style="margin-bottom:5px;border-left: 1px solid #ccc;">
								  <b><?php echo translate("Request Tarih")?></b><br>
								  <input type="datetime-local" name="ts2" class="v3-form-control" value="<?php echo dtarih($rs["ts2"],"yyyy-aa-ggTtime");?>">
							   </div>
							</div>
						 </div>
						 <div class="v3-panel">
							<div class="v3-panel-heading v3-bg-success" style="font-size: 16px;"><i class="fas fa-info-circle"></i> <?php echo translate("Talep Detay")?></div>
							<div class="v3-panel-body">
							   <!-- Tek boyutlu alanlar (düz inputlar) -->
								<div class="row">
									<?php foreach($jsi as $key => $value): ?>
										<?php
											$bg = ""; $dn = ""; $val = "";
											$_key = $key;
											if (substr($key, 0, 2) == "i_")      { $_key = str_replace("i_", " ", $key); $bg = "bg-info cw"; }
											elseif (substr($key, 0, 2) == "w_")  { $_key = str_replace("w_", " ", $key); $bg = "bg-warning cw"; }
											elseif (substr($key, 0, 2) == "d_")  { $_key = str_replace("d_", " ", $key); $bg = "bg-danger cw"; }
											elseif (substr($key, 0, 2) == "s_")  { $_key = str_replace("s_", " ", $key); $bg = "bg-success cw"; }
											if (DateTime::createFromFormat('Y-m-d', $value) !== false) { $type = "date"; }
											elseif (DateTime::createFromFormat('H:i', $value) !== false) { $type = "time"; }
											else { $type = "text"; }
											if (in_array($key, ["uid", "ne", "Subject", "nedir", "Currency", "Request_No", "Date", "flang", "User_s19", "empty_val"])) { $type = "hidden"; $dn = "dn"; }
											$colClass = in_array($key, ["Mesaj", "Message"]) ? "col-md-12 col-xs-12" : "col-md-6 col-xs-12";
										?>
										<?php if (!is_array($value)): ?>
											<div class="col-md-6 <?php echo $dn?>">
												<div class="form-group">
													<label><?= translate(creq($key)) ?></label>
													<?php if (substr($key, 0, 6) == "Attach"): ?>
														<a href="<?= $value ?>" data-fancybox="gallery">Attachment</a>
														<input type="hidden" name="<?= $key ?>" class="v3-form-control" value="<?= $value ?>">
													<?php elseif (substr($key, 0, 4) == "Ulke" || substr($key, 0, 5) == "Sehir" || substr($key, 0, 4) == "Ilce"): ?>
														<input type="hidden" name="<?= $key ?>" class="v3-form-control" value="<?= $value ?>">
														<input type="<?= $type ?>" name="<?= $key ?>" class="v3-form-control" value="<?= loc($value) ?>" readonly>
													<?php elseif ($key == "Total"): ?>
														<div class="v3-input-group">
															<input type="<?= $type ?>" name="<?= $key ?>" class="v3-form-control" value="<?= wprice($value) ?>">
															<span class="v3-input-group-addon" style="background-color: transparent; border: 0px;">
																<select name="Currency">
																	<option value="">...</option>
																	<option value="TRY" <?= $jsi["Currency"] == "TRY" ? "selected" : "" ?>>TRY</option>
																	<option value="USD" <?= $jsi["Currency"] == "USD" ? "selected" : "" ?>>USD</option>
																	<option value="EUR" <?= $jsi["Currency"] == "EUR" ? "selected" : "" ?>>EUR</option>
																	<option value="GBP" <?= $jsi["Currency"] == "GBP" ? "selected" : "" ?>>GBP</option>
																</select>
															</span>
														</div>
													<?php else: ?>
														<input type="text" name="<?= $key ?>" value="<?= htmlspecialchars($value) ?>" class="form-control">
													<?php endif; ?>
												</div>
											</div>
										<?php endif; ?>
										<!-- Çok boyutlu alanlar (tablolar) -->
										<?php if (is_array($value) && isset($value[0]) && is_array($value[0])): ?>
										<div  style="clear:both;" class="v3-col-md-12 v3-col-xs-12">
											<h4 class="mt-4"><?= translate(creq($key)) ?></h4>
											<div class="table-responsive">
												<table class="table table-bordered table-striped">
													<thead>
														<tr>
															<?php foreach (array_keys($value[0]) as $field): ?>
																<th><?= translate($field) ?></th>
															<?php endforeach; ?>
														</tr>
													</thead>
													<tbody>
														<?php foreach ($value as $i => $row): ?>
															<tr>
																<?php foreach ($row as $field => $val): ?>
																	<td>
																		<input type="text" name="<?= $key ?>[<?= $i ?>][<?= $field ?>]" value="<?= htmlspecialchars($val) ?>" class="form-control input-sm">
																	</td>
																<?php endforeach; ?>
															</tr>
														<?php endforeach; ?>
														<?php if (count($value) == 0): ?>
															<tr>
																<?php foreach (array_keys($value[0]) as $field): ?>
																	<td>
																		<input type="text" name="<?= $key ?>[0][<?= $field ?>]" class="form-control input-sm" placeholder="<?= translate($field) ?>">
																	</td>
																<?php endforeach; ?>
															</tr>
														<?php endif; ?>
													</tbody>
												</table>
											</div>
										</div>
										<?php endif; ?>
									<?php endforeach; ?>
								   <div class="v3-col-md-12 v3-col-xs-12" style="margin-top:10px;">
									  <b><?php echo translate("Yanıt / Not")?></b><br>
									  <input type="text" name="n3" class="v3-form-control" value="<?php echo $rs["n3"];?>">
								   </div>
								</div>
							</div>
						 </div>
					  </form>
                 </div>
            </div>
</body>
<?php
   }elseif($_GET["durum"] == "isle"){
	$arr = array();
	foreach ($_POST as $key => $value) {
		if (!in_array($key, ["n3", "s19", "s3", "s4", "ts2"])) {
			if (is_array($value)) {
				$arr[$key] = $value;
			} else {
				$arr[$key] = trim($value);
			}
		}
	}
	$s3 = isset($_POST["s3"]) ? intval($_POST["s3"]) : 0;
	$s4 = isset($_POST["s4"]) ? intval($_POST["s4"]) : 0;
	$sqlku = mysqli_query($conn, "SELECT * FROM t5 WHERE id=" . $s3); $rsku = mysqli_fetch_assoc($sqlku);
	$konu = translate($jski["Konu"]);
	if ($_POST["s4"] != $rs["s4"]) {
		$mesaj = "";
		switch ($_POST["s4"]) {
			case 0:
				$mesaj = "<center>#{$jski["Request_No"]} " . translate("+#Talebiniz Beklemede") . "</center>";
				break;
			case 1:
				$mesaj = "<center>#{$jski["Request_No"]} " . translate("+#Talebiniz İşleme Alındı") . "</center>";
				break;
			case 3:
				$mesaj = "<center>#{$jski["Request_No"]} " . translate("+#Talebiniz Tamamlandı") . "</center>";
				break;
			case 2:
				$mesaj = "<center>#{$jski["Request_No"]} " . translate("+#Talebiniz İptal Edildi") . "</center>";
				break;
		}
		if ($module["user_bildirim_sms"] > 0) {
			smsgonder($mesaj, telefon($rsku["t4"]));
		}
		if ($module["user_bildirim_email"] > 0) {
			$path = "../";
			$mesaj = $design_top . $mesaj . $design_bottom;
			emailgonder(v3d($rsku["t2"]), $konu, '', $mesaj);
		}
		if ($module["user_bildirim_notification"] > 0) {
			sNotification($mesaj, $konu, "Crm");
		}
	}
	$rjsi = json_encode($arr, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
	$s19 = isset($_POST["s19"]) ? implode(',', array_map(function($value) { return "{" . $value . "}"; }, $_POST["s19"])) : '';
	$ts2 = isset($_POST["ts2"]) && !empty($_POST["ts2"]) ? addslashes($_POST["ts2"]) : '0000-00-00 00:00:00';
	$id = intval($_GET["id"]);
	$sql = " UPDATE t2 SET n2 = '" . addslashes(stripslashes($rjsi)) . "', n3 = '" . addslashes(stripslashes($_POST["n3"])) . "', s3 = '$s3', s4 = '$s4', s19 = '$s19', ts2 = '$ts2' WHERE id = $id ";
	$Rs = mysqli_query($conn, $sql);
	if (!$Rs) {
		echo "Hata: " . mysqli_error($conn);
	}
	echo location("?islem=".$_GET['islem']."&id=$id&durum=ok", self, 500, 1);
}
   include("../public/log.php");
?>
<?php echo $footer;?>
</html>