<?php
$path="../../";
include("../../map.php");
include("../../../deppo/tools/script/SimpleImage.php");
$id=$_GET["id"];
if ($_GET["cmd"]==""){
$sqlc=mysqli_query($conn, "select id,t1 from t5 where id=".$id.""); $rsc=mysqli_fetch_assoc($sqlc);
	if($_SESSION["User_ID"]<>""){
		$User_ID=$_SESSION["User_ID"];
	}elseif($_SESSION["user_id"]<>""){
		$User_ID=$_SESSION["user_id"];
	}
?>
<div id="messages_result"></div>
<div class="v3-panel-heading bg-warning" style="border-radius:0px !important; height: 50px;">
	<?php if($mobile<>""){?><i class="fa fa-arrow-left" aria-hidden="true" onclick="hide('#messages_mobile')"></i><?php }else{?><i class="fa fa-user"></i><?php }?> <?php echo $rsc["t1"];?>
	<a style="position: absolute; right: 40px; top: 20px;" href="#" onclick="jQuery('#mlist').load('messages_ajax.php?islem=<?php echo $_GET["islem"]?>&ne=<?php echo $_GET["ne"]?>&durum=user_clear&id=<?php echo $id;?>');" class="fa fa-trash"></a>
</div>
<div class="v3-panel-body" style="padding:0px;">
<form id="fmessage" style="" enctype='multipart/form-data'>
	<input type="file" name="afile[]" id="afile" accept="*" style="display:none;" multiple="multiple">
	<div class="">
		<input type="hidden" name="s2" value="<?php echo $id?>" />
		<input type="hidden" name="s3" value="0" />
		<div class="v3-col-md-<?php if($m8<>2){echo "12";}else{echo "9";}?>"  style="">
			<input type="hidden" class="v3-form-control mt5 mb5" name="t1" value="" placeholder="Konu" />
		</div>
		<div class="v3-col-md-12" id="mlist" style="width:100%; height: 82vh; overflow: auto; overflow-x: hidden; filter: drop-shadow(2 px 4 px 6 px black); border: 1px solid #ccc; border-radius: 0px 0px 0px 0px ; background-image: url(https://cdn.crmplus.com.tr/images/message_bg.jpg);">
			<div id="majax" data-id="<?php echo $_GET["id"]?>"></div>
		</div>
		<div class="v3-col-md-12" style="padding: 10px;">
			<div class="v3-col-md-1 v3-col-xs-1"  style="<?php if($mobile<>""){echo "padding:0px";}?>">
				<a class="v3-btn" style="width: 90%;"><i class="fa fa-paperclip" aria-hidden="true" style="font-size: 24px;"></i></a>
			</div>
			<div class="v3-col-md-9  v3-col-xs-9"  style="">
				<textarea type="text" class="v3-form-control" id="cn1" name="n1" placeholder="<?php echo translate("Yeni Mesaj")?>" style="<?php if($mobile<>""){echo "height:65px;";}?> border: 1px solid grey !important; border-radius: 20px;"></textarea>
			</div>
			<div class="v3-col-md-2 v3-col-xs-2 mt5 mmt5 mb5" style="<?php if($mobile<>""){echo "padding:0px";}?>">
				<button type="button" id="scbtn" onclick="AjaxPResult('messages_form.php?islem=<?php echo $_GET["islem"]?>&ne=<?php echo $_GET["ne"]?>&id=<?php echo $_GET["id"]?>&cmd=send_message','#fmessage','#messages_result','nopl')" class="v3-btn v3-btn-warning" style="width: 100%;<?php if($mobile<>""){echo "height: 40px; margin-top:-2px;";}else{echo "height: 50px;";}?>"><i class="fa fa-paper-plane" aria-hidden="true"></i><span class="mdn"> <?php echo translate("Gönder")?></span></button>
			</div>
		</div>
	</div>
</form>
</div>
<script type="text/JavaScript" language="JavaScript">
	jQuery(document).ready(function() {
		jQuery('#cn1').focus();
		var id= jQuery('#majax').attr("data-id");
		jQuery('#majax').load('messages_ajax.php?islem=<?php echo $_GET["islem"]?>&ne=<?php echo $_GET["ne"]?>&id='+id+'');
	});
	var timeout = setInterval(reloadChat, 5000);
	function reloadChat () {
		var messageBody = document.querySelector('#mlist');
		messageBody.scrollTop = messageBody.scrollHeight - messageBody.clientHeight;
		var id= jQuery('#majax').attr("data-id");
		jQuery('#majax').load('messages_ajax.php?islem=<?php echo $_GET["islem"]?>&ne=<?php echo $_GET["ne"]?>&id='+id+'');
	}
	jQuery('.fa-paperclip').click(function(){ jQuery('#afile').trigger('click'); });
	jQuery('#cn1').keypress(function (e) {
	 var key = e.which;
	 if(key == 13){
		jQuery('#scbtn').trigger('click');
		//AjaxPResult('<?php echo $path.$cpath?>account/account_message_form.php?id=<?php echo $_GET["id"]?>&islem=send_message','#fmessage','#messages_result');
		return false;
	  }
	}); 
</script>
<?php
}elseif ($_GET["cmd"]=="send_message"){
	$iarr = array();
	$files = array_filter($_FILES['afile']['tmp_name']);
	$total = count($_FILES['afile']['tmp_name']);
	if(count($files)>0){
	for( $i=0 ; $i < $total ; $i++ ) {
		$kaydedilecekyer = "../../../deppo/dosya";
		$kaynak		= $_FILES["afile"]["tmp_name"][$i];
		$isim	 		= $_FILES["afile"]["name"][$i];
		$uzanti		= explode('.',$_FILES['afile']['name'][$i]);
		$rand  		= substr(md5(uniqid(rand())),0,10);
		$yeni  		= "attach_message_".$rand.".".$uzanti[count($uzanti)-1]."";
		$image 		= new SimpleImage();
		$image->load("../../../deppo/dosya/".$yeni."");
		$image->resizeToWidth("".$a9."");
		$image->save("../../../deppo/dosya/".$yeni."");
		move_uploaded_file ($_FILES["afile"]["tmp_name"][$i], $kaydedilecekyer . "/".$yeni);
		$iarr = $iarr + array($i => $yeni);
	}}
	$rjsi = json_encode($iarr, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
	foreach ($_POST as $key => $value){
		if($key[0]=="n"){ $n1=$value; }
	}
	$s1=$_SESSION['user_id'];
	if ($_POST["s2"]==""){$s2="0";}else{$s2=$_POST["s2"];}
	if ($_POST["s3"]==""){$s3="0";}else{$s3=$_POST["s3"];}
	$Sql = $conn->query("insert into t2(s1,s2,s3,ne,t1,t2,t4,n1,n6,ts1) values ('".$s1."', '".$s2."', '".$s3."', 'mesaj', '".addslashes(stripslashes($_POST["t1"]))."', '".$uploads."', '".date("Y-m-d")."', '".addslashes(stripslashes($n1))."', '".addslashes(stripslashes($rjsi))."', now())");
	$sqlc=mysqli_query($conn, "select * from t2 where ne='mesaj' order by id desc"); $rsc=mysqli_fetch_assoc($sqlc);
	$mesaj	= "
		<div class=\"col-xs-12 col-md-12\">Konu : ".$_POST["t1"]."</div><hr/>
		<div class=\"col-xs-12 col-md-12\"><b>Detay</b><br>".$_POST["n1"]."</div><div style=\"clear:both;\"></div>
	";
	$gonderen	= $firma;
	$mgonderen = $mail;
	$msifre     	= tanim('email','n2',0,'form');
	if($_POST["malici"]<>""){
		$malici	= $_POST["malici"];
	}else{
		$malici = tanim('email','n1',0,'form');
	}
	$konu		= $_POST["t1"];
	if($_POST["cc"]<>""){
		$cc			= $_POST["cc"];
	}elseif($_POST["Email"]<>""){
		$cc			= $_POST["Email"];
	}
	if($_POST["bcc"]<>""){
		$bcc			= $_POST["bcc"];
	}
if($s2<>0){
	$sql19 = mysqli_query($conn, "select * FROM t5 where id=".$s2.""); $rs19=mysqli_fetch_assoc($sql19);
	$gonderen   	= $firma;
	$konu		   	= translate("+Yeni Mesajınız Var");
	$mesaj      	= $n1;
	$mesaj			= $design_top.$mesaj.$design_bottom;
	$path			= "../../../";
	emailgonder(base64_decode($rs19["t2"]),$konu,$cc,$mesaj);
}else{
	sNotification("".translate("Destek Talebi")." [".$_SESSION["User_Name"]."]", "".$n1."", "Crm");
	$gonderen   	= "Web Servisi";
	$konu		   	= translate("+Yeni Destek Talebi");
	$mesaj      	= $n1;
	$mesaj			= $design_top.$mesaj.$design_bottom;
	$path			= "../../../";
	emailgonder($mail,$konu,$cc,$mesaj);
}
?>
<script type="text/JavaScript" language="JavaScript">
	jQuery(document).ready(function () {
		Load_Message_Form('<?php if($id<>"undefined"){echo $id;}else{echo $s2;}?>');
	});
</script> 
<?php }?> 