<?php include('header.php'); ?> 
	<body>
		<div class="header">
			<div class="logo">
				<center> 
					<table border="0" cellspacing="0" cellpadding="0" >
						<tr>
							<td height="40">
								<img src="<?=$IP_SERVER?>img/fmd.png">	
							</td>
							<td>
								ระบบแจ้งปัญหาห้องน้ำ 
								<b><font color="red">อหท.</font></b>
							</td>
						</tr>
					</table>
				</center>
			</div>
			<div class="location">
<?php
				if($_GET['id']){
					$sql_place = "SELECT * FROM place,worker WHERE 
						place.workerID = worker.IDworker and place.ID_place = '".$_GET['id']."'";
				}else{
					$sql_place = "SELECT * FROM place,worker WHERE 
						place.workerID = worker.IDworker and place.building = '".$_GET['building']."' and 
						place.floors = '".$_GET['floors']."' and 
						place.gender = '".$_GET['gender']."' and 
						place.remark = '".$_GET['remark']."' 
					";
				}
				$query_place = mysqli_query($conn,$sql_place);
				$result_place=mysqli_fetch_array($query_place);
				if (mysqli_num_rows($query_place) == 0 || mysqli_num_rows($query_place) > 1) {header('Location: '.$IP_SERVER.'no_url/');}
				
?>	
				
				<center>
					<table  border="0" cellspacing="0" cellpadding="0">
						<tr >
							<td rowspan="2" align="center" >
								
									<img src="<?=$result_place['url_img']?>">&nbsp;&nbsp;
							</td>
							<td height="50" align="center">
								ห้องน้ำ<?php if($result_place['gender']=="f"){ echo"หญิง"; }else if($result_place['gender']=="m"){echo"ชาย";}else{echo"รวม";} ?> <?=$result_place['remark']?> ชั้น <?=$result_place['floors']?> ตึก <?=$result_place['building']?>
							</td>
						</tr>
						<tr>
							<td valign="top" align="center" >
								<?=$result_place['title']?><?=$result_place['Fname']?> <?=$result_place['Lname']?>
							</td>
						</tr>
					</table>
				</center>
			</div>
		</div>
		<div style="width: auto; height: 160px;">
			
		</div>
		<div class="data" id="data">
			<div class="CONTACT">	
				<h3>
					<center>ยืนยันสถานที่ห้องน้ำ</center> 
				</h3>
				<div class="CONTACT_from">
					
					<br />
					<center>
						ใส่หมายเลขใต้ QR-Code
						<br /><br />
						<input type="number" id="confirm_number" name="confirm_number" style="width: 100px; height: 30px;" >
						<br /><br />
						<button class="send_data" style="color: #ffffff;font-size: 20px;" onclick="javascript:confirm_number();">ยืนยัน</button>
					</center>
					<br />
				</div>	
			</div>				
		</div>
		<br />
		<!---------------------popup---------------------------->
		<div id="myModal" class="modal" onclick="javascript:close_popup();" >
			<div class="modal-content">
				<span class="close"></span>

				<div id="popup"></div>
			</div>
		</div>
		<div id="no_close_pop" class="no_close" ></div>
		<div id="url_upload_ok" style="display: none;">
			<textarea id="url_ok_1">no_img</textarea>
			<div id="url_error">error</div>
		</div>
		<div id="cache_img" style="display: none;">
			<img src="<?=$IP_SERVER?>img/load.gif">
			<img src="<?=$IP_SERVER?>img/error.png">
			<img src="<?=$IP_SERVER?>img/warning.png">
			<img src="<?=$IP_SERVER?>img/warning2.png">
			<img src="<?=$IP_SERVER?>img/save.png">
			<img src="<?=$IP_SERVER?>img/qa.png">
		</div>
		<!---------------------popup---------------------------->
		<script type="text/javascript">
			var placeID = '<?=$result_place['ID_place']?>';
			var modal = document.getElementById("myModal");
			var span = document.getElementsByClassName("close")[0];
			var reload_ok = 0;
			function vote(vote){
				var html_code = '<br /><button class="send_data" onclick="javascript:send_data_vote(\'vote\','+vote+',\'ไม่มีข้อมูล\','+vote+');">ยืนยัน</button>  <button class="send_data" onclick="javascript:popup_close();" style="background-color: #F8F8FF; color : #000000 ;">ยกเลิก</button>';

 				modal.style.display = "block";
 				document.getElementById("popup").innerHTML = '<br />'+document.getElementsByClassName("vote")[vote-1].innerHTML+html_code;
			}
			function btn(No,id,detail,bruised){
				var html_code_bruised = '';
				if(bruised == 'yes'){
					html_code_bruised = '<center><font style="font-size: 18px;"  color="red">* ปัญหานี้ได้รับแจ้งแล้ว คุณต้องการแจ้งปัญหาอีกครั้ง</font></center>'
				}else{
					html_code_bruised = '<br />';
				}
				var html_code = '<button class="send_data" onclick="javascript:send_data_vote(\'Inform\','+id+',\''+detail+'\','+No+');">ยืนยัน</button>  <button class="send_data" onclick="javascript:popup_close();" style="background-color: #F8F8FF; color : #000000 ;">ยกเลิก</button>';

 				modal.style.display = "block";
 				document.getElementById("popup").innerHTML = html_code_bruised+document.getElementsByClassName("vote_data")[No-1].innerHTML+html_code;
			}
			span.onclick = function() {
  				modal.style.display = "none";
			}
			window.onclick = function(event) {
  				if (event.target == modal) {
    				modal.style.display = "none";
    				if(reload_ok == 1){
    					reload_ok = 0;
    					//location.reload();
    				}
  				}
			}
			function close_popup(){
 				
			}
			function popup_close(){
 				modal.style.display = "none";
 				//location.reload();
			}
			function popup_close_ok(){
 				modal.style.display = "none";
 				//location.reload();
			}
			//----------------------------------------------------------
			var noclose = document.getElementById("no_close_pop");
			function send_data_vote(type,work,detail,No){
				noclose.style.display = "block";
				document.getElementById("popup").innerHTML = '<img src="<?=$IP_SERVER?>img/load.gif"><br />กำลังบันทึกข้อมูล';
				var confirm_number_text = document.getElementById("confirm_number_text").innerHTML;

				$.ajax({
					type: "POST",
					url: "sql/save.php",
					data: {
						type : type,
						work : work,
						placeID : placeID,
						confirm_number_text : confirm_number_text,
						detail : detail
					},
					success: function(result) {
						reload_ok = 1;
						noclose.style.display = "none";
						if(result=='ok'){
							if(type=='vote'){
								document.getElementById("rate").innerHTML = '<br /><img src="<?=$IP_SERVER?>img/save.png" width="50"><br />ระบบทำการบันทึกข้อมูลแล้ว<br /><br />';
							}else{
								document.getElementsByClassName("vote_data")[No-1].style.backgroundColor  = '#b2b2b2';
								document.getElementById("vote_data_div").style.display = "none";
								document.getElementById("vote_data_div_confirm").style.display = "block";
							}
							
							
							popup_close_ok();
						}else{
							var html_code = '<br /><img src="<?=$IP_SERVER?>img/error.png"><br />ระบบขัดข้องกรุณาแจ้งภายหลัง<br /><br /><button class="send_data" onclick="javascript:popup_close();" >ตกลง</button>';
							document.getElementById("popup").innerHTML = html_code;
						}
						
					}
				});
			}
			function ChangeText(oFileInput, sTargetID) {
				document.getElementById("load_image").style.display = "none";
				document.getElementById("image_file").style.display = "block";
				document.getElementById(sTargetID).innerHTML = '<span>'+oFileInput.value.replace(/.*(\/|\\)/, '')+'</span>';

			}
			//---------------------updload----------------------------------------
			function close_load_image(){
				document.getElementById("load_image").style.display = "block";
				document.getElementById("image_file").style.display = "none";
				document.getElementById("btn-file-name").innerHTML = "";
				document.getElementById("imageInput").value = "";	
			}
			function upload_send_data(){
				modal.style.display = "block";
				var html_code = '<br /><img src="<?=$IP_SERVER?>img/qa.png"><br />คุณต้องการ<br />ส่งความคิดเห็นเพิ่มเติม<br /><button class="send_data" onclick="javascript:start_upload();" >ยืนยัน</button>  <button class="send_data" onclick="javascript:popup_close();" style="background-color: #F8F8FF; color : #000000 ;" >ยกเลิก</button>';
				document.getElementById("popup").innerHTML = html_code;
			}
			function start_upload(){
				if(document.getElementById("CONTACT_from_txt").value!=""){
					var html_code = '<center><div id="loading"><div id="loadingup"></div></div></center><br />'
					document.getElementById("popup").innerHTML = '<img src="<?=$IP_SERVER?>img/load.gif"><br />กำลังบันทึกข้อมูล'+html_code;
					document.getElementById("no_close_pop").style.display = "block";
					if(document.getElementById("imageInput").value!=""){
						var fsize = $('#imageInput')[0].files[0].size; //get file size
	 					var ftype = $('#imageInput')[0].files[0].type;// get file type
	 					//ftype=='image/png' || ftype=='image/gif' ||
						if(  ftype=='image/jpeg' || ftype=='image/pjpeg'){
							if(fsize > (5*1048576)){
								var html_code = '<br /><img src="<?=$IP_SERVER?>img/warning2.png"><br />ไฟล์รูปภาพขนาดเกิน 5 MB<br /><button class="send_data" onclick="javascript:popup_close();" >ตกลง</button>';
								document.getElementById("popup").innerHTML = html_code;	
								document.getElementById("no_close_pop").style.display = "none";
							}else{
								document.getElementById("no_close_pop").style.display = "block";
								document.getElementById("loading").style.display = "block";

								var fd = new FormData();
						        var files = $('#imageInput')[0].files;
						        fd.append("file[]", files[0]);
						        $.ajax({
						            url: 'sql/multiple_upload.php',
						            type: 'post',
						            data: fd,
						            contentType: false,
						            processData: false,
						            xhr: function () {
						            	var xhr = new window.XMLHttpRequest();
										xhr.upload.addEventListener("progress", function(evt){
											if (evt.lengthComputable) {
										 		var percentComplete = evt.loaded / evt.total;
												document.getElementById("loadingup").style.width = (Math.round((percentComplete * 100)-10) + "%");
											}
										}, false);
										xhr.addEventListener("progress", function(evt){
											if (evt.lengthComputable) {
												var percentComplete = evt.loaded / evt.total;
												document.getElementById("loadingup").style.width = (Math.round((percentComplete * 100)-10) + "%");
											}
										}, false);
										return xhr;
									},
						            success: function(data){						            	
						            		document.getElementById("loadingup").style.width = "98%";
						            		document.getElementById("url_upload_ok").innerHTML= data;
						            		var url_ok = document.getElementById("url_ok_1").value;
						            		var url_error = document.getElementById("url_error").innerHTML;
						            		if(url_error=="no_error"){
						            			start_send_data(url_ok);
						            		}else{
						            			document.getElementById("no_close_pop").style.display = "none";
						            			var html_code = '<br /><img src="<?=$IP_SERVER?>img/error.png"><br />ระบบขัดข้องกรุณาแจ้งภายหลัง<br /><button class="send_data" onclick="javascript:popup_close();">ตกลง</button>';
												document.getElementById("popup").innerHTML = html_code;
						            		}
						            },
									error: function (error) {
										document.getElementById("no_close_pop").style.display = "none";
				            			var html_code = '<br /><img src="<?=$IP_SERVER?>img/error.png"><br />ระบบขัดข้องกรุณาแจ้งภายหลัง<br /><button class="send_data" onclick="javascript:popup_close();">ตกลง</button>';
										document.getElementById("popup").innerHTML = html_code;
									}
								});
							}
						}else{
							var html_code = '<br /><img src="<?=$IP_SERVER?>img/warning2.png"><br />กรุณาเลือกไฟล์ชนิดรูปภาพ<br /><button class="send_data" onclick="javascript:popup_close();" >ตกลง</button>';
							document.getElementById("popup").innerHTML = html_code;	
							document.getElementById("no_close_pop").style.display = "none";
						}
					}else{
						start_send_data('no_img');
					}
				}else{
					var html_code = '<br /><img src="<?=$IP_SERVER?>img/warning2.png"><br />คุณยังไม่ได้ใส่ข้อความ<br /><button class="send_data" onclick="javascript:popup_close();">ตกลง</button>';
					document.getElementById("popup").innerHTML = html_code;
				}
			}
			function start_send_data(url_img){
				if(url_img!='no_img'){
					document.getElementById("loadingup").style.width = "99%";
				}
				var contact_txt = document.getElementById("CONTACT_from_txt").value;
				contact_txt = escapeHtml(contact_txt);
				var confirm_number_text = document.getElementById("confirm_number_text").innerHTML;
				$.ajax({
					type: "POST",
					url: "sql/save.php",
					data: {
						type : '1',
						work : contact_txt,
						url_img : url_img,
						placeID : placeID,
						confirm_number_text : confirm_number_text,
						detail : 'แจ้งปัญหาเพิ่มเติม / แนะนำ / ติชม'
					},
					success: function(result) {
						reload_ok = 1;
						document.getElementById("no_close_pop").style.display = "none";
						if(url_img!='no_img'){
							document.getElementById("loadingup").style.width = "100%";
						}
						if(result=='ok'){
							document.getElementById("CONTACT_from").innerHTML = '<br /><img src="<?=$IP_SERVER?>img/save.png" width="50"><br />ระบบทำการบันทึกข้อมูลแล้ว<br /><br />';
							popup_close_ok();

						}else{
							var html_code = '<br /><img src="<?=$IP_SERVER?>img/error.png"><br />ระบบขัดข้องกรุณาแจ้งภายหลัง<br /><button class="send_data" onclick="javascript:popup_close();" >ตกลง</button>';
							document.getElementById("popup").innerHTML = html_code;
						}
					}
				});

				function escapeHtml (string) {
					var entityMap = {
						'&': '&amp;',
						'<': '&lt;',
						'>': '&gt;',
						'"': '&quot;',
						"'": '&#39;',
						'/': '&#x2F;',
						'`': '&#x60;',
						'=': '&#x3D;',
						'\\': '&#92;'
					};
					return String(string).replace(/[&<>"'`=\/\\]/g, function (s) {
						return entityMap[s];
					});
				}
			}
			function confirm_number(){
				
				var confirm_number = document.getElementById("confirm_number").value;
				$.ajax({
					type: "POST",
					url: "sql/data_menu.php",
					data: {
						placeID : '<?=$result_place['ID_place']?>',
						confirm_number : confirm_number
					},
					
					success: function(result) {
						document.getElementById("data").innerHTML = result;
					}
				});
			}
			function vote_data_div_show(){
				document.getElementById("vote_data_div").style.display = "block";
				document.getElementById("vote_data_div_confirm").style.display = "none";
			}
			//---------------------updload----------------------------------------
			
		</script>

		<?php include('footer.php'); ?>