<?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/funcauthen.php";
include "common/functions/funcvalidateinput.php";
openDB();

if (!authen())
{ header ("location: login.php");
		}
else
{ $idpesanan=isset($_GET['idp']) ? cleanval($_GET['idp']) : NULL;
		$nomorpesanan=isset($_GET['oid']) ? cleanval(base64_decode($_GET['oid'])) : NULL;
		$konfirmasi=isset($_GET['konfirmasi']) ? cleanval($_GET['konfirmasi']) : NULL;
		$sebab=isset($_POST['sebab']) ? cleanval($_POST['sebab']) : NULL;
		$refund=isset($_POST['refund']) ? cleanval($_POST['refund']) : NULL;
		$script="";
		
		// cek id dan nomor pesanan
		$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);
				if (empty($konfirmasi))
				{ $msg="<form method='post' action='pesanan_batal.php?idp=".$idpesanan."&oid=".base64_encode($nomorpesanan)."&konfirmasi=1'>
													Masukan sebab pembatalan pesanan dan jumlah pengembalian uang pembayaran jika ada.<br><br>
													<table border='0' cellpadding='0' cellspacing='0'>
														<tr>
															<td align='left' valign='top' width='100%' style='line-height:25px; padding-bottom:10px'>
																<b>Sebab Pembatalan Pesanan</b><br>
																<textarea name='sebab' style='width:500px; height:75px; padding:5px'></textarea>
															</td>
														</tr>
														<tr>
															<td align='left' valign='top' width='100%' style='line-height:25px; padding-bottom:10px'>
																<b>Pengembalian Uang</b><br>
																Rp. &nbsp; <input type='text' name='refund' value='0' style='width:150px; text-align:right; padding:5px'>
															</td>
														</tr>
													</table>
													<br><hr size='1'><br><br>
													<b style='color:#ff0000'>[!] PENTING: Pesanan yang telah dibatalkan, tidak dapat dikembalikan lagi.</b>
													<br><br>
													Yakin ingin membatalkan pesanan ini?<br><br>
													<input type='submit' value=' Ya, Batalkan ' style='cursor:pointer'> &nbsp;
													<input type='button' value=' Tidak ' style='cursor:pointer' onclick='history.back(-1)'>
												</form>	
												";
						}
				else if (empty($sebab))
				{ $msg="Untuk melakukan pembatalan pesanan, sebab pembatalan tidak boleh kosong.<br><br>";
						}
				else
				{ /*
						// INI HANYA DILAKUKAN SAAT APPROVAL PEMBATALAN
						$qup="UPDATE $db_pesanan
												SET statusorder='8'
												WHERE nomorpesanan='".$dord['nomorpesanan']."'
																		AND sesipesanan='".$dord['sesipesanan']."'
												";
						$rup=mysqli_query($dbConnection, $qup);
						*/
						$qib="INSERT INTO $db_pbatal (idrekanan,
																																				idtoko,
																																				sesipesanan,
																																				nomorpesanan,
																																				bataloleh,
																																				bataltgl,
																																				batalsebab,
																																				batalrefund)
												VALUES ('".$dord['idrekanan']."',
																				'".$dord['idtoko']."',
																				'".$dord['sesipesanan']."',
																				'".$dord['nomorpesanan']."',
																				'".$_SESSION['SES_USERNAME']."',
																				'".$wktsekarangdb."',
																				'".$sebab."',
																				'".$refund."')
												";
						$rib=mysqli_query($dbConnection, $qib);
						// echo $qib."<br><br>".mysqli_error($dbConnection)."<br><br>";
						if ($rib)
						{ $catatan="Membatalkan pesanan (ID: ".$idpesanan." / No.: ".$nomorpesanan.").";
								$qic="INSERT INTO $db_pesanannote (nomorpesanan,
																																											sesipesanan,
																																											idrekanan,
																																											idtoko,
																																											statusorder,
																																											catatan,
																																											catattgl,
																																											catatoleh)
														VALUES ('".$dord['nomorpesanan']."',
																						'".$dord['sesipesanan']."',
																						'".$dord['idrekanan']."',
																						'".$dord['idtoko']."',
																						'".$dord['statusorder']."',
																						'".$catatan."',
																						'".$wktsekarangdb."',
																						'".$_SESSION['SES_USERNAME']."')
														";
								$ric=mysqli_query($dbConnection, $qic);
								$msg="Sukses membatalkan pesanan.";
								$script.="<script>
																			var goBack = function ()
																																{ setTimeout(function()
																																													{ window.location='index.php';
																																															}, 
																																													500);
																																		};
																			window.onload = goBack();
																		</script>
																		";
								}
						else
						{ $msg="Gagal membatalkan pesanan<br><br>".$linkback;
								// $msg.="<br><br>".$qup.mysqli_error($dbConnection);
								}
						}
				}
		else
		{ $msg="Pesanan tidak ditemukan.<br><br>
										Biasanya terjadi karena pesanan sudah selesai diproses atau dibatalkan.<br><br>".$linkback;
				}
		
		$content=$script.
											"<table border='0' cellpadding='0' cellspacing='0' width='100%' height='100%'>
													<tr>
														<td align='left' valign='top' width='100%' height='100%'>
															<b class='title'>KONFIRMASI DATA &gt; BATAL PESANAN</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();

?>