<?php
	$path="../../";
	include("../../map.php");
	//$uitems = json_decode(GetArr("kayit"), true);
	if ($_GET["islem"]=="dservice"){
		$sqlf=mysqli_query($conn, "select * from t4 where id=".$_GET["id"].""); $rsf=mysqli_fetch_assoc($sqlf);
		$uid=$rsf["s1"];
	}else{
		$uid=$_GET["id"];
	}
	$sql5=mysqli_query($conn, "select * from t5 where id=".$uid.""); $rs5=mysqli_fetch_assoc($sql5); $cariname=$rs5["t1"];
	$sqlx=mysqli_query($conn, "select id,t1 from ".$_SESSION["lang"]." where ne='client_project' and s13=1 order by t1 asc");
	if ($authority==0){ $dauth=0; }else{ $dauth=1; }
	if ($_GET["stn"]<>"" and $_GET["order"]<>""){
		$stn=$_GET["stn"];
		$order=$_GET["order"];
	}else{
		$stn="ts2";
		$order="asc";
	}
?>
<style>
<?php if ($_GET["islem"]=="dservice"){?>
#err {display:none !important}
<?php }?>
</style>
<script type="text/javascript">
	jQuery(document).keypress(function(e){
		if (e.keyCode == 27){
			jQuery("#myModal").modal('hide');
		}
	});
	jQuery(function() {
		jQuery(".mfrm").click(function (){
			jQuery("#myModal").fadeIn();
			var title = jQuery(this).attr("data-title");
			jQuery(".modal-title").html(title);
			var theModal = jQuery(this).data("target"),
			frmSRC = jQuery(this).attr("data-url");
			var refSRC = jQuery(this).attr("data-ref");
			if(refSRC != undefined){
				jQuery(theModal + ' .mcbtn').attr('onclick', 'location.href=\''+refSRC+'\'');
			}
			jQuery(theModal + ' iframe').attr('src', frmSRC);
			jQuery(theModal + ' .close').click(function () {
				jQuery(theModal + ' iframe').attr('src', 'about:blank');
			});
		});
	});
	jQuery(".cmdl").click(function (){
		event.preventDefault();
		jQuery('#myModal').fadeOut('slow');
		jQuery('#modalFull').fadeOut('slow');
		jQuery('.modal-backdrop').fadeOut('slow');
		loadFunction('modules/clients/ajax_client_service.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&id=<?php echo $_GET["id"]?>&ts1=<?php echo $_GET["ts1"];?>&ts2=<?php echo $_GET["ts2"];?>','#ihizmet', '');
	});
</script>
<form method="post" action="../clients/ajax_client_service.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&id=<?php echo $_GET["id"]?>&durum=yhizmet" name="aform" style="width:100%;">
<input type="hidden" name="ss2" id="ss2" value="0">
<input type="hidden" name="ss5" id="ss5" value="<?php if($_SESSION["ProjeID"]>0){echo $_SESSION["ProjeID"];}else{echo "0";}?>">
<table class="table mdn fs13 mfs12" style="">
	<thead>
		<tr>
			<th scope="col"><?php echo translate("Hesap Planı")?></th>
			<th class="w14 mw14"><?php echo $sdil[168]; //Tip?></th>
			<th class="w10 mw10">
				<span class="column-sort"> <a href="?islem=<?php echo $_GET["islem"]?>&ne=<?php echo $_GET["ne"]?>&id=<?php echo $_GET["id"]?>&stn=ts1&order=asc#tab-hizmet" title="A-Z" class="sort-up"></a> <a href="?islem=<?php echo $_GET["islem"]?>&ne=<?php echo $_GET["ne"]?>&id=<?php echo $_GET["id"]?>&stn=ts1&order=desc#tab-hizmet" title="Z-A" class="sort-down"></a> </span>
				<a onclick="toggle('.ts1');toggle('.ts2');" style="cursor:pointer;"><?php echo $sdil[115]; //Tarih?> <i class="fa fa-exchange" aria-hidden="true"></i></a>
			</th>
			<th class="w10 mw10"><?php echo ctranslate("Tekrar")?></th>
			<th class="w12 mw12 <?php //if($e8<>0 and (($_SESSION["kim"]=="Admin") or ($_SESSION["kim"]=="yonetici" and $_SESSION["y8"]<>0))){}else{echo "dn";}?>"> <span class="column-sort"> <a href="?islem=<?php echo $_GET["islem"]?>&ne=<?php echo $_GET["ne"]?>&id=<?php echo $_GET["id"]?>&stn=pb1&order=asc#tab-hizmet" title="A-Z" class="sort-up"></a> <a href="?islem=<?php echo $_GET["islem"]?>&ne=<?php echo $_GET["ne"]?>&id=<?php echo $_GET["id"]?>&stn=pb1&order=desc#tab-hizmet" title="Z-A" class="sort-down"></a> </span> <?php echo $sdil[144]; //Tutar?></th>
			<th class="w10 mw5"></th>
		</tr>
	</thead>
	<tbody id="nservice">
			<tr class="addtr" data-id="0" data-cke="0"  data-action="../clients/ajax_client_service.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&id=<?php echo $_GET["id"]?>&durum=yhizmet" data-url="../clients/ajax_client_service.php" data-did="#ihizmet">
				<td style="position:relative;">
					<div style="position:absolute; margin-top:-17px; right:10px; display:none;" id="bildirim0">
						<input title="Sms" type="checkbox" name="s4" style="height: 20px;width:20px;" value="1" <?php if($rsh["s4"]==1){?>Checked<?php }?> />
					</div>
					<input  onclick="toggle('#bildirim0');" type="text" name="sn1" class="form-control" title="Hizmet (n1)" placeholder="<?php echo translate("Plan")?>"></td>
					<td align="center">
						<input type="text" list="nst" name="st1" id="st1" class="form-control with-tip" title="<?php echo $sdil[168]; //Tip?> (t1)" placeholder="<?php echo $sdil[168]; //Tip?>" value="<?php echo $tipx["t1"]?>" autocomplete="off">
						<datalist id="nst" style="display:none;">
							<?php while($rsx=mysqli_fetch_assoc($sqlx)){?>
								<option id="<?php echo $rsx["id"]?>" value="<?php echo $rsx["t1"]?>"><?php echo $rsx["t1"]?></option>
							<?php }?>
							<?php if(mysqli_num_rows($sqlx)==0){?>
								<?php $sqlt=mysqli_query($conn, "select Distinct t11 from t4 where ne='hizmet' and t1<>''"); while($tip=mysqli_fetch_assoc($sqlt)){?>
									<option value="<?php echo $tip["t11"]?>" id="0"><?php echo $tip["t11"]?></option>
								<?php }?>
							<?php }?>
						</datalist>
						<script>
							jQuery("#st1").change(function() {
								var inputVal = jQuery(this).val();
								var id = jQuery("#nst option[value='"+inputVal+"']").attr("id");
								jQuery("#ss5").val(id);
							});
						</script>
					</td>
					<td class="">
						<input type="date" name="ts1" class="form-control ts1" value="<?php echo date("Y-m-d")?>" id="add_ts1" style="display:none;" title="Başlangıç Tarihi">
						<input type="date" name="ts2" class="form-control ts2" value="<?php echo date('Y-m-d', strtotime('+1 year'))?>" id="add_ts2" title="Bitiş Tarihi">
					</td>
					<td>
						<select name="s3" class="sna form-control notranslate" style="height: 34px;">
							<option value="0" <?php if($rsh["s3"]==0){echo "selected";}?>> Tekrar</option>
							<option value="1" <?php if($rsh["s3"]==1){echo "selected";}?>> Aylık</option>
							<option value="2" <?php if($rsh["s3"]==2){echo "selected";}?>> Yıllık</option>
						</select>
					</td>
					<td class="">
					<div class="v3-col-md-7 v3-col-xs-6" style="padding:0px;">
						<input type="text" name="spb1" id="spb1" value="0.00" style="text-align:right;" title="Tutar (pb1)" placeholder="Tutar" class="form-control">
					</div>
					<div class="v3-col-md-5 v3-col-xs-6" style="padding:0px;">
						<select name="st2" id="st2" class="sna sfa form-control" title="Birim (t2)" style="float:right">
							<option value="TRY" <?php if($kpb=="TRY"){echo "selected";}?>>&#xf195;</option>
							<option value="EUR" <?php if($kpb=="EUR"){echo "selected";}?>>&#xf153;</option>
							<option value="USD" <?php if($kpb=="USD"){echo "selected";}?>>&#xf155;</option>
							<option value="GBP" <?php if($kpb=="GBP"){echo "selected";}?>>&#xf154;</option>
						</select>
					</div>
					</td>
					<td class="tar">
						<a class="btn btn-warning btn-update-row" style="cursor:pointer; line-height:0px; padding:5px;"><i class="fa fa-plus "></i></a>
					</td>
				</tr>
		</table>
</form>
<script>
	jQuery("#st1").change(function(){
		var val = jQuery('#st1').val();
		var price = jQuery('#nsl option').filter(function() { return this.value == val; }).data('price');
		var pb = jQuery('#nsl option').filter(function() { return this.value == val; }).data('pb');
		var id = jQuery('#nsl option').filter(function() { return this.value == val; }).data('id');
		if (typeof id === "undefined") {
			jQuery("#spb1").val('0');
			jQuery("#st2").val('<?php echo $kpb?>');
			jQuery("#ss2").val('0');
		}else{
			jQuery("#spb1").val(price);
			jQuery("#st2").val(pb);
			jQuery("#ss2").val(id);
		}
	});
</script>
	<?php
	$sqlh=mysqli_query($conn, "select * from t4 where ne='hizmet' and s1=".$uid." $qry $luye order by $stn $order"); while($rsh=mysqli_fetch_assoc($sqlh)){
	if($rsh["t4"]=="0-00-00" or ($_GET["ts1"]=="" and $_GET["ts2"]=="") or ($_GET["ts1"]<>"" and $_GET["ts2"]<>"") and ($rsh["t4"]>=$_GET["ts1"] and $rsh["t4"]<=$_GET["ts2"])){
		$itarih = explode('.',$rsh["t3"]);
		$starih = explode('.',$rsh["t4"]);
		$fd1="".$rsh["t3"].""; $fd2="".$rsh["t4"]."";
		$hide = false;
		if (mysqli_num_rows($sqlx) > 0) {
			if ($_SESSION["kim"] == "yonetici") {
				// Yönetici: ProjeID boşsa veya eşleşmiyorsa gizle
				if ($_SESSION["ProjeID"] == "" || $_SESSION["ProjeID"] != $rsh["s5"]) {
					$hide = true;
				}
			} else {
				// Yönetici değilse: ProjeID eşleşmiyorsa gizle
				if ($_SESSION["ProjeID"] !="" && $_SESSION["ProjeID"] != $rsh["s5"]) {
					$hide = true;
				}
			}
		}
		?>
		<form method="post" action="../clients/ajax_client_service.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&id=<?php echo $_GET["id"]?>&hid=<?php echo $rsh["id"]?>&s5=<?php echo $rsh["s5"]?>&durum=ghizmet" name="aform" autocomplete="" id="eservice" style="width:100%; <?php if ($hide) { echo "display:none"; }?>">
		<input type="hidden" name="cs2" id="cs2_<?php echo $rsh["id"];?>" value="<?php echo $rsh["s2"]?>">
		<input type="hidden" name="cs5" id="cs5_<?php echo $rsh["id"];?>" value="<?php echo $rsh["s5"]?>">
			<table class="table ddn fs13 mfs12" style="">
				<tr class="">
					<td class="">
						<?php echo kisalt($rsh["n1"],15);?>
					</td>
					<td class="mw30 tar">
						<?php echo dtarih($fd2,"gg aa yyyy")?>
					</td>
					<td align="left" class="mw20 tar">
						<div style="margin-top:2px;">
							<a style="cursor:pointer; line-height:0px; padding:5px; " class="btn btn-danger btn-delete-row" <?php if ($auth<>3){echo "Disabled";}?> data-href="../clients/ajax_client_service.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&id=<?php echo $_GET["id"]?>&hid=<?php echo $rsh["id"]?>&s5=<?php echo $rsh["s5"]?>&durum=shizmet"><i class="fa fa-times " style=" padding:0px; "></i></a>
							<a style="cursor:pointer; line-height:0px; padding:5px;" class="btn btn-success mfrm" data-toggle="modal" data-target="#myModal" data-url="../../fonksiyon.php?islem=sdetail&id=<?php echo $_GET["id"]?>&iid=<?php echo $rsh["id"]?>&s5=<?php echo $rsh["s5"]?>"><i class="fa fa-pencil "></i></a>
						</div>
					</td>
				</tr>
			</table>
			<table class="table mdn fs13 mfs12" style="">
				<tr data-id="<?php echo $rsh["id"];?>" data-cke="0" data-href="../clients/ajax_client_service.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&id=<?php echo $_GET["id"]?>&hid=<?php echo $rsh["id"]?>&s5=<?php echo $rsh["s5"]?>" data-action="../clients/ajax_client_service.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&id=<?php echo $_GET["id"]?>&hid=<?php echo $rsh["id"]?>&s5=<?php echo $rsh["s5"]?>&durum=ghizmet" data-url="../clients/ajax_client_service.php" data-did="#ihizmet">
					<td style="position:relative;">
						<div style="position: absolute; margin-top: 0px; right: 20px; font-size: 24px; display:none;" id="wa<?php echo $rsh["id"];?>">
							<a target="_blank" href="https://web.whatsapp.com/send/?phone=+90<?php echo telefon($rs5["t4"]);?>&text=<?php echo translate("İyi çalışmalar, hizmet(ler)inizin yenileme tarihi gelmiştir, yenileme işlemi yapacaksanız dönüş yapmanızı rica ederiz.");?>"><i class="fa fa-whatsapp"></i></a>
						</div>
						<input onclick="toggle('#wa<?php echo $rsh["id"];?>')" type="text" name="sn1" value="<?php echo $rsh["n1"]?>" class="form-control">
					</td>
					<td class="w14 mw14">
						<input type="text" list="esl_<?php echo $rsh["id"]?>" id="st1_<?php echo $rsh["id"]?>" name="st1" value="<?php echo $rsh["t1"]?>" class="form-control">
						<input type="hidden" name="ss2" id="ss2_<?php echo $rsh["id"]?>" value="<?php echo $rsh["s2"]?>">
						<datalist id="esl_<?php echo $rsh["id"]?>" style="display:none;">
							<?php while($rsx=mysqli_fetch_assoc($sqlx)){?>
								<option id="<?php echo $rsx["id"]?>" value="<?php echo $rsx["t1"]?>"><?php echo $rsx["t1"]?></option>
							<?php }?>
							<?php if(mysqli_num_rows($sqlx)==0){?>
								<?php $sqlt=mysqli_query($conn, "select Distinct t11 from t4 where ne='hizmet' and t1<>''"); while($tip=mysqli_fetch_assoc($sqlt)){?>
									<option value="<?php echo $tip["t11"]?>" id="0"><?php echo $tip["t11"]?></option>
								<?php }?>
							<?php }?>
						</datalist>
						<script>
							jQuery("#st1_<?php echo $rsh["id"]?>").change(function() {
								var inputVal = jQuery(this).val();
								var id = jQuery("#esl_<?php echo $rsh["id"]?> option[value='"+inputVal+"']").attr("id");
								jQuery("#ss5_<?php echo $rsh["id"]?>").val(id);
							});
						</script>
					</td>
					<td class="w10 mw10">
					<input type="date" name="ts1" class="form-control ts1" value="<?php echo $fd1?>" id="edit_ts1" style="display:none;" title="Başlangıç Tarihi">
					<input type="date" name="ts2" class="form-control ts2" value="<?php echo $fd2?>" id="edit_ts2" title="<?php echo tfark2(date("Y-m-d"),$fd2,"text");?> kaldı">
					</td>
				<td  class="w10 mw10">
					<select name="s3" class="sna form-control notranslate">
						<option value="0" <?php if($rsh["s3"]==0){echo "selected";}?>> Tekrar</option>
						<option value="1" <?php if($rsh["s3"]==1){echo "selected";}?>> Aylık</option>
						<option value="2" <?php if($rsh["s3"]==2){echo "selected";}?>> Yıllık</option>
					</select>
				</td>
					<td class="w12 mw12">
					<div class="v3-col-md-7 v3-col-xs-6" style="padding:0px;">
						<input type="text" name="spb1" id="spb1_<?php echo $rsh["id"];?>" value="<?php echo wprice($rsh["pb1"]);?>" style="text-align:right;" title="<?php if($rsh["t2"]=="USD"){echo wprice($rsh["pb1"]*$usd); echo " ".$kpb;}?>" class="form-control">
					</div>
					<div class="v3-col-md-5 v3-col-xs-6" style="padding:0px;">
						<select name="st2" id="st2_<?php echo $rsh["id"];?>" class="sna sfa form-control" title="Birim (t2)" style="">
							<option value="TRY" <?php if($rsh["t2"]=="TL" or $rsh["t2"]=="TRY"){?>Selected<?php }?>>&#xf195;</option>
							<option value="EUR" <?php if($rsh["t2"]=="EUR"){?>Selected<?php }?>>&#xf153;</option>
							<option value="USD" <?php if($rsh["t2"]=="USD"){?>Selected<?php }?>>&#xf155;</option>
							<option value="GBP" <?php if($rsh["t2"]=="GBP"){?>Selected<?php }?>>&#xf154;</option>
						</select>
					</div>
					</td>
					<td  class="w10 mw5 tar">
						<div style="margin-top:2px;">
							<a style="cursor:pointer;  line-height:0px; padding:5px; " class="btn btn-warning btn-update-row"><i class="fa fa-refresh "></i></a>
							<a style="cursor:pointer; line-height:0px; padding:5px;" class="btn btn-success mfrm" data-toggle="modal" data-target="#myModal" data-url="../../fonksiyon.php?islem=sdetail&id=<?php echo $_GET["id"]?>&iid=<?php echo $rsh["id"]?>"><i class="fa fa-pencil "></i></a>
						<?php if ($authority==0){?>
							<a style="cursor:pointer; line-height:0px; padding:5px;" class="btn btn-danger"><i class="fa fa-lock" style="padding:0px;"></i></a>
						<?php }else{?>
							<a style="cursor:pointer; line-height:0px; padding:5px;" class="btn btn-danger btn-delete-row" data-href="../clients/ajax_client_service.php?ne=<?php echo $_GET["ne"]?>&islem=<?php echo $_GET["islem"]?>&id=<?php echo $_GET["id"]?>&hid=<?php echo $rsh["id"]?>&durum=shizmet"><i class="fa fa-times " style="padding:0px;"></i></a>
						<?php }?>
						</div>
					</td>
				</tr>
			</tbody>
		</table>
	</form>
<script>
jQuery("#st1_<?php echo $rsh["id"]?>").change(function() {
    var $esl = jQuery('#esl_<?php echo $rsh["id"]?>');
    var val = jQuery(this).val();
    var option = $esl.find('option[value="' + val + '"]');
    var price = option.data('price') || 0;
    var pb = option.data('pb') || '<?php echo $kpb?>';
    var id = option.data('id') || 0;
    jQuery("#spb1_<?php echo $rsh["id"]?>").val(price);
    jQuery("#st2_<?php echo $rsh["id"]?>").val(pb);
    jQuery("#ss2_<?php echo $rsh["id"]?>").val(id);
});
</script>
<?php }}?>
<?php
$ss2 = !empty($_POST["ss2"]) ? $_POST["ss2"] : 0;
$ss3 = !empty($_POST["ss3"]) ? $_POST["ss3"] : 0;
$ss4 = !empty($_POST["ss4"]) ? $_POST["ss4"] : 0;
$ss5 = !empty($_POST["ss5"]) ? $_POST["ss5"] : 0;
if ($_GET["durum"] == "yhizmet") {
    $lt1 = "{$_POST["sn1"]} > {$_POST["st1"]} > {$_POST["ts1"]} > {$_POST["ts2"]} > Tutar : " . sprice($_POST["spb1"]);
    $sql = $conn->query("INSERT INTO t4(ne, t1, t2, t3, t4, pb1, n1, s1, s2, s3, s4, s5, s19) VALUES ('hizmet', '{$_POST["st1"]}', '{$_POST["st2"]}', '{$_POST["ts1"]}', '{$_POST["ts2"]}', '" . sprice($_POST["spb1"]) . "', '{$_POST["sn1"]}', '$uid', '$ss2', '$ss3', '$ss4', '$ss5', '{$_SESSION["user_id"]}')");
    
    if ($_POST["spb1"] > 0 && $e8 != 0) {
        $sql = $conn->query("INSERT INTO t4(ne, t1, t2, t4, t12, t6, t11, pb1, pb2, pb3, pb4, s1, s2, s5, s19, n1, ts1) VALUES ('cari', 'Nakit', 'hizmet', '{$_POST["ts1"]}', '{$_POST["ts2"]}', '{$_POST["st2"]}', '{$_POST["st1"]}', '" . sprice($_POST["spb1"]) . "', '0', '0', '0', '$uid', '{$conn->insert_id}', '$ss5', '{$_SESSION["user_id"]}', '{$_POST["sn1"]} ({$_POST["st1"]})', NOW())");
    }

} elseif ($_GET["durum"] == "ghizmet") {
    $lt1 = "{$_POST["sn1"]} > {$_POST["st1"]} > {$_POST["ts1"]} > {$_POST["ts2"]} > Tutar : " . sprice($_POST["spb1"]);
    $Rs = $conn->query("UPDATE t4 SET t1='{$_POST["st1"]}', t2='{$_POST["st2"]}', t3='{$_POST["ts1"]}', t4='{$_POST["ts2"]}', pb1='" . sprice($_POST["spb1"]) . "', n1='{$_POST["sn1"]}', s2='$ss2', s3='$ss3', s4='$ss4', s5='$ss5' WHERE id={$_GET["hid"]}");
    
    if ($_POST["spb1"] > 0 && $e8 != 0) {
        $sqlc = $conn->query("SELECT * FROM t4 WHERE ne='cari' AND s5='{$_GET["hid"]}'");
        if ($sqlc->num_rows == 0) {
            $sql = $conn->query("INSERT INTO t4(ne, t1, t2, t4, t12, t6, t11, pb1, pb2, pb3, pb4, s1, s2, s5, s19, n1, ts1) VALUES ('cari', 'Nakit', 'hizmet', '{$_POST["ts1"]}', '{$_POST["ts2"]}', '{$_POST["st2"]}', '{$_POST["st1"]}', '" . sprice($_POST["spb1"]) . "', '0', '0', '0', '$uid', '{$_GET["hid"]}', '$ss5', '{$_SESSION["user_id"]}', '{$_POST["sn1"]} ({$_POST["st1"]})', NOW())");
        } else {
            $Rs = $conn->query("UPDATE t4 SET pb1='" . sprice($_POST["spb1"]) . "', n1='{$_POST["sn1"]} ({$_POST["st1"]})' WHERE s5={$_GET["hid"]}");
        }
    } elseif ($_POST["spb1"] == 0 && $e8 != 0) {
        $conn->query("DELETE FROM t4 WHERE s5='{$_GET["hid"]}'");
    }

} elseif ($_GET["durum"] == "shizmet") {
    $dsql = $conn->query("SELECT * FROM t4 WHERE id='{$_GET["hid"]}'");
    $drs = $dsql->fetch_assoc();
    $lt1 = "{$drs["n1"]} > {$drs["t1"]} > {$drs["t3"]} > {$drs["t4"]} > Tutar : {$drs["pb1"]}";
    $conn->query("DELETE FROM t4 WHERE s2='{$_GET["hid"]}'");
    $conn->query("DELETE FROM t4 WHERE id='{$_GET["hid"]}'");

} elseif ($_GET["durum"] == "cs_clear") {
    $lt1 = "Tüm planlar temizlendi";
    $conn->query("DELETE FROM t4 WHERE ne='hizmet' AND s1={$_GET["id"]}");
}
?>
<?php include("../public/log.php");?>