<?php include "../koneksi.php";
if ($_POST['shift']==4) {
 ?>
	<table class="table-dtm table-bordered" style="width:100%; background-color: white; text-align: center">
		<thead>
			<th style="text-align: center;" width="22%">Shift</th>
			<th style="text-align: center;" width="55%">Down Time</th>
			<th style="text-align: center;" width="22%">Waktu</th>
		</thead>
		<form role="form" id="form-dt-machine" method="post" action="update.php">
		<?php 
		
			$data1 = mysqli_query ($koneksi, " select * 
											from downtime
											where PlanID='$_POST[id]' AND Jenis='$_POST[jenis]'");
			$hitung = mysqli_num_rows ($data1);
			if($hitung>0) {
			while ($row1 = mysqli_fetch_array ($data1)) {
			?>
			<tr>
			<td><?php echo $row1['Shift']; ?></td>
			<td><?php echo $row1['Reason']; ?></td>
			<td><?php echo date("H:i", strtotime($row1['Time'])); ?></td>
			</tr>
		<?php }} ?>
		<?php 	
		} else { 
		?>
	<table class="table-dtm" style="width:100%; background-color: white;">
		<thead>
			<th width="55%">Down Time</th>
			<th width="20%">Waktu</th>
		</thead>
		<form role="form" id="form-dt-machine" method="post" action="update.php">
		<?php
		$data1 = mysqli_query ($koneksi, " select * 
											from downtime
											where PlanID='$_POST[id]' AND Shift='$_POST[shift]' AND Jenis='$_POST[jenis]'");
		$hitung = mysqli_num_rows ($data1);
		if($hitung>0) {
		while ($row1 = mysqli_fetch_array ($data1))
		{
		?>
		<tr>
			<td><input type="hidden" name="planid" id="planid" value="<?php echo $_POST['id']; ?>">
				<input type="hidden" name="shift" id="shift" value="<?php echo $_POST['shift']; ?>">
				<input type="hidden" name="jenis" id="jenis" value="<?php echo $_POST['jenis']; ?>">
				<input type="hidden" name="dt_id[]" id="dt_id[]" value="<?php echo $row1['DT_ID']; ?>">
				<textarea class="form-control" id="mydtm[]" name="mydtm[]" rows="1"><?php echo $row1['Reason'] ?></textarea></td>
			<td><input class="form-control" type="time" name="mytime[]" id="mytime[]" value="<?php echo $row1['Time'] ?>"></td>
		</tr>
		<?php }} ?>
		<tr>
			<td>
				<input type="hidden" name="planid" id="planid" value="<?php echo $_POST['id']; ?>">
				<input type="hidden" name="shift" id="shift" value="<?php echo $_POST['shift']; ?>">
				<input type="hidden" name="jenis" id="jenis" value="<?php echo $_POST['jenis']; ?>">
				<input type="hidden" name="dt_id[]" id="dt_id[]" value="">
				<textarea class="form-control" id="mydtm[]" name="mydtm[]" rows="1"></textarea></td>
			<td><input class="form-control" type="time" name="mytime[]" id="mytime[]"></td>
		</tr>
		</form>
	</table>
</div>
<button class="add_form_field btn btn-primary"> Tambah Properties &nbsp; 
	<span style="font-size:16px; font-weight:bold;">+ </span>
</button>
<?php } ?>

<script>
	$(document).ready(function() {
    var max_fields = 35;
    var wrapper = $(".table-dtm");
    var add_button = $(".add_form_field");

    var x = 1;
    $(add_button).click(function(e) {
        e.preventDefault();
        if (x < max_fields) {
            x++;
            $(wrapper).append('<tr><td><input type="hidden" name="planid" id="planid" value="<?php echo $_POST['id']; ?>"><input type="hidden" name="shift" id="shift" value="<?php echo $_POST['shift']; ?>"><input type="hidden" name="jenis" id="jenis" value="<?php echo $_POST['jenis']; ?>"><input type="hidden" name="dt_id[]" id="dt_id[]" value=""><textarea class="form-control" id="mydtm[]" name="mydtm[]" rows="1"></textarea></td><td><input class="form-control" type="time" name="mytime[]" id="mytime[]"></td></tr>');
        } else {
            alert('You Reached the limits')
        }
    });

    // $(wrapper).on("click", ".delete", function(e) {
    //     e.preventDefault();
    //     $(this).grandparent().remove();
    //     x--;
    // })
	});
</script>