<?php

header("X-Content-Type-Options: nosniff");
header("X-XSS-Protection: 1; mode=block");
header("X-Frame-Options: DENY");
header('Expires: Sun, 01 Jan 2000 00:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');

ini_set('session.cookie_httponly', 'on');
ini_set('session.cookie_secure', 'on');

session_name("SIGNADMAUTH");
session_start();

include "common/config/config.php";
include "common/config/dbconfig.php";
include "common/functions/funcconnect.php";
include "common/functions/funcvalidateinput.php";
include "common/functions/funcauthen.php";
openDB();

if (!authen())
{ header ("location: login.php");
		}
else
{ $idpesanan=isset($_GET['idp']) ? cleanval($_GET['idp']) : NULL;
		$nomorpesanan=isset($_GET['oid']) ? base64_decode(cleanval($_GET['oid'])) : NULL;
		$qord="SELECT *
									FROM $db_pesanan
									WHERE idpesanan='".$idpesanan."'
															AND nomorpesanan='".$nomorpesanan."'
															AND statusorder<4
									";
		$rord=mysqli_query($dbConnection, $qord);
		if (mysqli_num_rows($rord)>0)
		{ $dord=mysqli_fetch_array($rord);
				// ambil data produk yang telah disetujui
				$dataproduk="<table border='0' cellspacing='0' cellpadding='0'>
																		<tr>
																			<td></td>
																			<td align='left' valign='middle' style='padding:0'>
																				<b>Qty</b>
																			</td>
																			<td align='left' valign='middle' style='padding:0'>
																				<b>Merek</b>
																			</td>
																			<td align='left' valign='middle' style='padding:0'>
																				<b>Model</b>
																			</td>
																			<td align='left' valign='middle' style='padding:0'>
																				<b>Warna</b>
																			</td>
																			<td align='left' valign='middle' style='padding:0'>
																				<b>Wilayah</b>
																			</td>
																			<td align='left' valign='middle' style='padding:0'>
																				<b>Tahun</b>
																			</td>
																			<td align='left' valign='middle' style='padding:0' colspan='2'>
																				<b>Harga Jual</b>
																			</td>
																			<td align='left' valign='middle' style='padding:0'>
																				<b>Diskon per Unit</b>
																			</td>
																		</tr>	
																		";
				$qoi="SELECT a.*,
																	b.kodekategoribarang, b.namakategoribarang,
																	c.*,
																	d.*,
																	e.*,
																	f.*,
																	g.*,
																	h.*,
																	i.*
										FROM $db_pdetailapproval i
										INNER JOIN $db_item a ON i.iditem=a.iditem
										INNER JOIN $db_kategoribarang b ON a.idkategoribarang=b.idkategoribarang
										INNER JOIN $db_merek c ON a.idmerek=c.idmerek
										INNER JOIN $db_model d ON a.idmodel=d.idmodel
										INNER JOIN $db_warna e ON a.idwarna=e.idwarna
										INNER JOIN $db_thnproduksi f ON a.idthnproduksi=f.idthnproduksi
										INNER JOIN $db_itemprice g ON d.idmodel=g.idmodel AND i.iditemprice=g.iditemprice
										INNER JOIN $db_wilayahotr h ON g.idwilayahotr=h.idwilayahotr
										WHERE i.nomorpesanan='".$dord['nomorpesanan']."'
																AND i.sesipesanan='".$dord['sesipesanan']."'
										ORDER BY i.idpdetailapproval DESC
										LIMIT 1
										";
				$roi=mysqli_query($dbConnection, $qoi);
				if (mysqli_num_rows($roi)>0)
				{ while ($doi=mysqli_fetch_array($roi))
						{ if (empty($doi['ontheroad']))
								{ $ontrharga="";
										}
								else
								{ $ontrharga="OTR";
										}
								if ($doi['tolaktgl']===NULL && $doi['setujutgl']===NULL)
								{ if ($doi['datasesuai']===NULL)
										{ $tombolbayar="<input type='button' id='tombolubahproduk' value=' Ubah Data ' style='cursor:pointer' onclick='enableProduct()'>";
												$labelubahproduk="<a href='produk_ubah_batal.php?ida=".$doi['idpdetailapproval']."&idp=".$dord['idpesanan']."&oid=".base64_encode($dord['nomorpesanan'])."' alt='Batalkan Perubahan' title='Batalkan Perubahan'>
																															<span class='material-icons-outlined' style='color:#ffcc01'>backspace</span>
																														</a>
																														";
												}
										else
										{ $tombolsesuaiproduk="<input type='button' id='tombolprodukok' value=' Data Sesuai ' style='margin-right:6px' disabled>";
												$tombolubahproduk="<input type='button' id='tombolubahproduk' value=' Ubah Data ' disabled>";
												$labelubahproduk="<span class='material-icons-outlined' style='color:#ffcc01'>check_box</span>";
												}
										}
								else if ($doi['tolaktgl']!==NULL && $doi['setujutgl']===NULL)
								{ $tombolsesuaiproduk="<input type='button' id='tombolprodukok' value=' Data Sesuai ' style='cursor:pointer; margin-right:6px' onclick=\"document.location='produk_ok.php?idp=".$dord['idpesanan']."&oid=".base64_encode($dord['nomorpesanan'])."'\">";
										$tombolubahproduk="<input type='button' id='tombolubahproduk' value=' Ubah Data ' style='cursor:pointer' onclick='enableProduct()'>";
										if ($doi['tolaksebab']=="BATAL UBAH")
										{ $labelubahproduk="<span class='material-icons-outlined' style='color:#ffcc01'>disabled_by_default</span>";
												}
										else
										{ $labelubahproduk="<span class='material-icons-outlined' style='color:#ff5475'>disabled_by_default</span>";
												}
										}
								else if ($doi['tolaktgl']===NULL && $doi['setujutgl']!==NULL)
								{ $tombolsesuaiproduk="<input type='button' id='tombolprodukok' value=' Data Sesuai ' style='margin-right:6px' disabled>";
										$tombolubahproduk="<input type='button' id='tombolubahproduk' value=' Ubah Data ' disabled>";
										$labelubahproduk="<span class='material-icons-outlined' style='color:#33aa88'>check_box</span>";
										}
								$dataproduk.="<tr>
																							<td align='left' valign='middle' style='padding:8px 13px 5px 0'>
																								".$labelubahproduk."
																							</td>
																							<td align='left' valign='middle' style='padding:8px 13px 5px 0'>
																								".$doi['jumlahpesan']."
																							</td>
																							<td align='left' valign='middle' style='padding:8px 13px 5px 0'>
																								".$doi['namamerek']."
																							</td>
																							<td align='left' valign='middle' style='padding:8px 13px 5px 0'>
																								".$doi['namamodel']."
																							</td>
																							<td align='left' valign='middle' style='padding:8px 13px 5px 0'>
																								".$doi['namawarna']."
																							</td>
																							<td align='left' valign='middle' style='padding:8px 13px 5px 0'>
																								".$doi['namawilayahotr']."
																							</td>
																							<td align='left' valign='middle' style='padding:8px 13px 5px 0'>
																								".$doi['thnproduksi']."
																							</td>
																							<td align='left' valign='middle' style='padding:8px 13px 5px 0'>
																								Rp. ".number_format($doi['hargajual'], 0, ",", ".")."
																							</td>
																							<td align='left' valign='middle' style='padding:8px 13px 5px 0'>
																								".$ontrharga."
																							</td>
																							<td align='left' valign='middle' style='padding:8px 13px 5px 0'>
																								Rp. ".number_format($doi['jumlahdiskon'], 0, ",", ".")."
																							</td>
																						</tr>
																						";
								}
						}
				else
				{ $tombolsesuaiproduk="<input type='button' id='tombolprodukok' value=' Data Sesuai ' style='cursor:pointer; margin-right:6px' onclick=\"document.location='produk_ok.php?idp=".$dord['idpesanan']."&oid=".base64_encode($dord['nomorpesanan'])."'\">";
						$tombolubahproduk="<input type='button' id='tombolubahproduk' value=' Ubah Data ' style='cursor:pointer' onclick='enableProduct()'>";
						}
				$dataproduk.="</table>";
				
				// catatan selama proses konfirmasi
				$qcp="SELECT * 
										FROM $db_pesanannote
										WHERE nomorpesanan='".$dord['nomorpesanan']."'
																AND sesipesanan='".$dord['sesipesanan']."'
																AND statusorder='2'
										ORDER BY catattgl DESC
										";
				$rcp=mysqli_query($dbConnection, $qcp);
				if (mysqli_num_rows($rcp)>0)
				{ $catatan="";
						while ($dcp=mysqli_fetch_array($rcp))
						{ $catatan.="<b>".date("d M Y H:i:s", strtotime($dcp['catattgl']))." - ".$dcp['catatoleh']."</b><br>".str_replace("\n", "", $dcp['catatan'])."<br><br>";
								}
						}
				else
				{ $catatan="(Belum ada catatan)";
						}
				
				$script="<script>
														function enablePay()
														{ document.getElementById('jumlahpesan').disabled=false;
																document.getElementById('idmr').disabled=false;
																document.getElementById('idmd').disabled=false;
																document.getElementById('idw').disabled=false;
																document.getElementById('idwl').disabled=false;
																document.getElementById('ontr').disabled=false;
																document.getElementById('idtp').disabled=false;
																document.getElementById('jumlahdiskon').disabled=false;
																document.getElementById('tombolprodukok').style.display='none';
																document.getElementById('tombolubahproduk').style.display='none';
																document.getElementById('simpanubahproduk').style.display='';
																document.getElementById('tombolbatalubahproduk').style.display='';
																}
														function disablePay()
														{ document.getElementById('jumlahpesan').disabled=true;
																document.getElementById('idmr').disabled=true;
																document.getElementById('idmd').disabled=true;
																document.getElementById('idw').disabled=true;
																document.getElementById('idwl').disabled=true;
																document.getElementById('ontr').disabled=true;
																document.getElementById('idtp').disabled=true;
																document.getElementById('jumlahdiskon').disabled=true;
																document.getElementById('tombolprodukok').style.display='';
																document.getElementById('tombolubahproduk').style.display='';
																document.getElementById('simpanubahproduk').style.display='none';
																document.getElementById('tombolbatalubahproduk').style.display='none';
																}
													</script>
													";
				
				$msg=$script.
									"<table border='0' cellpadding='0' cellspacing='0' width='100%'>
											<tr>
												<td align='left' valign='middle' width='50%'>
													<h1>NO. PESANAN ".$nomorpesanan."</h1> 
												</td>
												<td align='right' valign='top' width='50%'>
													<input type='button' value=' Batalkan Pesanan ' style='cursor:pointer' onclick=\"document.location='pesanan_batal.php?idp=".$dord['idpesanan']."&oid=".base64_encode($dord['nomorpesanan'])."'\">
												</td>
											</tr>
										</table>
										<span class='material-icons-outlined' style='color:#ffcc01; vertical-align:middle; font-size:16pt'>check_box</span>&nbsp; Ada Pembayaran, Menunggu Persetujuan
										&nbsp; &nbsp; &nbsp;
										<span class='material-icons-outlined' style='color:#ffcc01; vertical-align:middle; font-size:16pt'>disabled_by_default</span>&nbsp; Pembayaran Dibatalkan
										&nbsp; &nbsp; &nbsp;
										<span class='material-icons-outlined' style='color:#33aa88; vertical-align:middle; font-size:16pt'>check_box</span>&nbsp; Sudah Disetujui
										&nbsp; &nbsp; &nbsp;
										<span class='material-icons-outlined' style='color:#ff5475; vertical-align:middle; font-size:16pt'>disabled_by_default</span>&nbsp; Tidak Disetujui
										<br><br><hr size='1'><br>
										<table border='0' cellpadding='0' cellspacing='0' width='100%'>
											<tr>
												<td align='left' valign='top' width='50%' style='padding-right:10px'>
													<h2>CATATAN ADMIN</h2>
													Masukan semua informasi yang terkait proses konfirmasi data di kotak catatan di bawah ini seperti telepon tidak diangkat / tidak aktif, 
													ada data yang ingin diubah, dan lainnya.<br><br>
													<form method='post' action='pesanan_konfirmasi_note.php?idp=".$idpesanan."&oid=".base64_encode($nomorpesanan)."'>
														<textarea name='catatan' style='resize:none; width:100%; height:65px' value=''></textarea>
														<br><br>
														<input type='submit' value=' Simpan Catatan ' style='cursor:pointer'>
													</form>
												</td>
												<td align='left' valign='top' width='50%' style='padding-left:10px; padding-top:10px'>
													<b style='font-size:9pt'>Catatan Sebelumnya:</b><br><br>
													<div style='width:95%; height:135px; padding:10px; border:1px solid #cfcfcf; line-height:17px; overflow-y:scroll'>
														".$catatan."
													</div>
												</td>
											</tr>
										</table>
										<br><hr size='1'><br>
										<div style='border:2px solid #ffaaaa; padding:10px; background-color:#ffefef; line-height:18px; font-weight:bold; font-size:10pt'>
											Berikut ini adalah data pelunasan pembayaran yang perlu dipastikan kembali ke pemesan.
											Jika ada data yang ingin diubah, sebelum menyimpan data baru tersebut, pastikan data baru yang dimasukan 
											tidak ada yang salah ketik.
										</div><br>
										<h2>DATA PEMBAYARAN</h2>
										<form id='formuinputbayar' method='post' action='bayar_ubah.php?idp=".$idpesanan."&oid=".base64_encode($nomorpesanan)."'>
											".$tombolbayar."
											<input type='submit' id='simpanbayar' value=' Simpan ' style='cursor:pointer; display:none'> &nbsp;
											<input type='button' id='tombolbatalbayar' value=' Batal Ubah ' style='cursor:pointer; display:none' onclick='disablePay()'>
											<br><br>
											<div style='width:1050px; padding:15px 10px 15px 10px; border:1px solid #cfcfcf; overflow-x:scroll'>
												".$dataproduk."
											</div>	
										</form>	
										";
				}
		else
		{ $msg="Data pesanan tidak ditemukan.<br><br>
										Biasanya terjadi karena proses pelunasan sudah terlewati atau pesanan sudah dibatalkan.<br><br>".$linkback;
				}

		$content="<table border='0' cellpadding='0' cellspacing='0' width='100%' height='100%'>
													<tr>
														<td align='left' valign='top' width='100%' height='100%'>
															<b class='title'>LUNAS BAYAR</b>
															<br><hr size='1' color='#000000'><br>
															<!--
															<table border='0' cellpadding='0' cellspacing='0'>
																<tr>
																	<td align='left' valign='middle'>
																		<span class='material-icons'>arrow_circle_left</span> &nbsp;
																	</td>
																	<td align='left' valign='middle'>
																		".$linkback."
																	</td>
																</tr>
															</table>
															<br>
															-->
															".$msg."
														</td>
													</tr>
												</table>
												";
		}

include "webmenu.php";
include "common/include/template.php";
$temp=new template;
$temp->define_theme("themes/theme1.html");
$temp->define_tag("{PAGETITLE}", $namaaplikasi);
$temp->define_tag("{NAMAUSER}", strtoupper($_SESSION['SES_FULLNAME']));
$temp->define_tag("{APPMENU}", $webmenu);
$temp->define_tag("{APPCONTENT}", $content);
$temp->parse();
$temp->printproses();

closeDB();

?>