<?php
include "../koneksi.php";

$no_pbb_id = $_POST['no_pbb_id'];
$plan_id = $_POST['plan_id'];
$mat_id = $_POST['mat_id'];
$bln = $_POST['bln'];
$thn = $_POST['thn'];
$no_pbb = $_POST['no_pbb'];

$no_pbb = $no_pbb.'/MT-GPL/'.$bln.'/'.$thn;

if (empty($data['error'])) {
		if ($no_pbb_id!=="") {
			$query = "UPDATE no_pbb SET
									No_PBB = '$no_pbb'
									WHERE NoPBB_ID = '$no_pbb_id'
									";

		mysqli_query($koneksi, $query)
		or die ("Gagal Perintah SQL".mysql_error());
		} else {
		$query = "insert INTO no_pbb SET
									No_PBB = '$no_pbb'
									";

		mysqli_query($koneksi, $query)
		or die ("Gagal Perintah SQL".mysql_error());
		}
		
    $data['hasil'] = 'sukses';
    
} else {
    
    $data['hasil'] = 'gagal';
}
	if ($no_pbb_id!=="") {
	} else {
		$data = mysqli_query ($koneksi, " select *
										from no_pbb	
										where No_PBB = '$no_pbb'
										order by NoPBB_ID DESC");
		$row = mysqli_fetch_array ($data);
		$no_pbb_id1 = $row['NoPBB_ID'];

		$query = "UPDATE plan SET
									NoPBB_ID = '$no_pbb_id1'
									WHERE PlanID = '$plan_id'
									";

		mysqli_query($koneksi, $query)
		or die ("Gagal Perintah SQL".mysql_error());
	}

echo json_encode($data);
header('Location:pbb.php?id='.$plan_id.'&mat='.$mat_id);

?>

