<?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']) ? base64_decode(cleanval($_GET['oid'])) : NULL;
		$namalengkap=isset($_POST['namalengkap']) ? cleanval($_POST['namalengkap']) : NULL; // nama lengkap pemesan sesuai ktp
		$alamattinggal=isset($_POST['alamattinggal']) ? cleanval($_POST['alamattinggal']) : NULL; // alamat tinggal sesuai ktp
		$nohandphone=isset($_POST['nohandphone']) ? cleanval($_POST['nohandphone']) : NULL; // nomor handphone pemesan
		$noktp=isset($_POST['noktp']) ? cleanval($_POST['noktp']) : NULL; // nomor ktp pemesan, bisa berbeda dengan nomor ktp bpkb / stnk
		$fotoktp=isset($_FILES['fotoktp']) ? $_FILES['fotoktp'] : NULL; // foto ktp pemesan
		$carabayar=isset($_POST['carabayar']) ? cleanval($_POST['carabayar']) : NULL; // cara bayar (tunai / cicilan)
		$email=isset($_POST['email']) ? cleanval($_POST['email']) : NULL; // alamat email pemesan
		$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);
				$qup="UPDATE $db_papproval
										SET tolaktgl='".$wktsekarangdb."',
														tolakoleh='OTOMATIS',
														tolaksebab='BATAL UBAH'
										WHERE nomorpesanan='".$dord['nomorpesanan']."'
																AND sesipesanan='".$dord['sesipesanan']."'
																AND tolaktgl IS NULL
										";
				$rup=mysqli_query($dbConnection, $qup);
				if ($rup)
				{ // simpan file ktp
						$simpanktp=1;
						if (!empty($fotoktp['name']))
						{ $ktpextension=end(explode(".", $fotoktp['name']));
								$ktpfilename=date("YmdHis").strtoupper($nomorpesanan).".".$ktpextension;
								$filepath="images/ktpfile/".$ktpfilename;
								if (!move_uploaded_file($fotoktp['tmp_name'], $filepath))
								{ $simpanktp=0;
										}
								}
						else
						{ $ktpfilename=$dord['fotoktp'];
								}
						// namalengkap='".$namalengkap."',
						// noktp='".$noktp."',
						// fotoktp='".$ktpfilename."',
						// alamatlengkap='".$alamattinggal."',
						// nohandphone='".$nohandphone."',
						// email='".$email."',
						// idcarabayarlunas='".$carabayar."',
						
						if ($simpanktp==1)
						{ $qidp="INSERT INTO $db_papproval (idrekanan,
																																										idtoko,
																																										sesipesanan,
																																										nomorpesanan,
																																										namalengkap,
																																										noktp,
																																										fotoktp,
																																										alamatlengkap,
																																										nohandphone,
																																										email,
																																										totaltagihan,
																																										totaldiskon,
																																										idcarabayarlunas,
																																										mintatgl,
																																										mintaoleh)
															VALUES ('".$dord['idrekanan']."',
																							'".$dord['idtoko']."',
																							'".$dord['sesipesanan']."',
																							'".$dord['nomorpesanan']."',
																							'".$namalengkap."',
																							'".$noktp."',
																							'".$ktpfilename."',
																							'".$alamattinggal."',
																							'".$nohandphone."',
																							'".$email."',
																							'".$dord['totaltagihan']."',
																							'".intval($dord['totaldiskon'])."',
																							'".$carabayar."',
																							'".$wktsekarangdb."',
																							'".$_SESSION['SES_USERNAME']."')
															";
								$ridp=mysqli_query($dbConnection, $qidp);
								if ($ridp)
								{ $catatan="Mengubah data pemesan.";
										$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 menyimpan perubahan data pemesan.";
										$script.="<script>
																					var goBack = function ()
																																		{ setTimeout(function()
																																															{ window.location='konfirmasidata.php?idp=".$dord['idpesanan']."&oid=".base64_encode($dord['nomorpesanan'])."';
																																																	}, 
																																															1000);
																																				};
																					window.onload = goBack();
																				</script>
																				";
										}
								else
								{ $msg="Gagal menyimpan perubahan data pemesan.<br><br>".$linkback;
										// echo $qidp."<br>".mysqli_error($dbConnection);
										}
								}
						else
						{ $msg="Gagal menyimpan file KTP.<br><br>".$linkback;
								}							
						}
				else
				{ $msg="Gagal membatalkan perubahan data pemesan sebelumnya.<br><br>".$linkback;
						// $msg.="<br><br>".$qup.mysqli_error($dbConnection);
						}
				}
		else
		{ $msg="Data pesanan tidak ditemukan.<br><br>
										Biasanya terjadi karena proses konfirmasi data sudah terlewati atau pesanan sudah 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; UBAH DATA PEMESAN</b>
															<br><hr size='1' color='#000000'><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();

?>