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

$plan_id = $_POST['plan_id'];
$shift = $_POST['shift'];
$kode = $_POST['kode'];
$nolot = $_POST['nolot'];
$jml = $_POST['jml'];
$material = $_POST['material'];
$material2 = $_POST['material2'];

$datasloc = mysqli_query ($koneksi, "SELECT product.SLoc
                              FROM plan
                              INNER JOIN product
                              ON plan.ProductID=product.ProductID
                                WHERE PlanID ='$plan_id'");
$row2= mysqli_fetch_array($datasloc);

if ($kode == '') {
    $data['error']['kode'] = 'Kode Product STPJ Tidak Boleh Kosong';
}
if ($nolot == '') {
    $data['error']['nolot'] = 'Nomor Lot STPJ Tidak Boleh Kosong';
}
if ($jml == '') {
    $data['error']['jml'] = 'Jumlah STPJ Tidak Boleh Kosong';
}
if ($material2 == '') {
    $mat = $material;
} else {
	$mat = $material2;
}


if (empty($data['error'])) {
	for ($i = 0; $i<$jml; $i++) {
		if($row2['SLoc']==='BOX2') {
		$kode1 = $kode+$i;
		$query = "insert INTO stpj SET
										PlanID = '$plan_id',
										Shift = '$shift',
										Kode = '$kode1',
										NoLot = '$nolot',
										Material = '$mat',
										Qty=15
										";

		mysqli_query($koneksi, $query)
		or die ("Gagal Perintah SQL".mysql_error());
		} else {
		$kode1 = $kode+$i;
		$query = "insert INTO stpj SET
										PlanID = '$plan_id',
										Shift = '$shift',
										Kode = '$kode1',
										NoLot = '$nolot',
										Material = '$mat'
										";

		mysqli_query($koneksi, $query)
		or die ("Gagal Perintah SQL".mysql_error());
		}
	}
    $data['hasil'] = 'sukses';
    
} else {
    
    $data['hasil'] = 'gagal';
}

echo json_encode($data);


?>