<!--
//index.php
!-->
<?php
include "koneksi.php";
if (!isset($_SESSION["UserID"])) {
	header("location:login.php?p=3");
}
?>
<html>  
    <head>  
        <title>PT. MULTI HIDRACHROME BALIKPAPAN</title>  
		<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
        <link rel="stylesheet" href="bootstrap.min.css" />
		<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
		<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
		<link rel="icon" type="image/png" href="images/mhb.jpeg">
    </head>  
	<?php
	include "part/menu2.php";
	?>
	
    <body>  
        <div class="container">
			<br />
			<br><br><br><br><br>
			<h3 align="center">Sending Daily Report to operasional</a></h3><br />
			<br />
			<?php
			$tanggal = date('M, d Y');
			echo $tanggal;
			?>
			<br><br><br>
			<a href="historiTambahDaily.php"><button class="btn btn-info btn-xs">Histori</button></a>
			<br />
			<div align="right" style="margin-bottom:5px;">
				<button type="button" name="add" id="add" class="btn btn-success btn-xs">+ Add</button>
			</div>
			<br />
			<form method="post" id="user_form">
				<div class="table-responsive">
					<table class="table table-striped table-bordered" id="user_data">
						<tr>
							<th>Job Activity</th>
							<th>Progress</th>
							<th>MHB-id</th>
							<th>Remarks</th>
							<th>PIC</th>
							
						</tr>
					</table>
				</div>
				<div align="center">
					<input type="submit" name="insert" id="insert" class="btn btn-primary" value="Send" />
				</div>
			</form>

			<br />
		</div>
		<div id="user_dialog" title="Add Data">
			<div class="form-group">
				<label>Enter Job Activity</label>
				<input type="text" name="first_name" id="first_name" class="form-control" />
				<span id="error_first_name" class="text-danger"></span>
			</div>
			<div class="form-group">
				<label>Enter Progress</label>
				<input type="text" name="last_name" id="last_name" class="form-control" />
				<span id="error_last_name" class="text-danger"></span>
			</div>
			<div class="form-group">
				<label>Enter MHB - id</label>
				<input type="text" name="mhb_id" id="mhb_id" class="form-control" />
				<span id="error_mhb_id" class="text-danger"></span>
			</div>
			<div class="form-group">
				<label>Enter Remarks</label>
				<input type="text" name="remarks" id="remarks" class="form-control" />
				<span id="error_remarks" class="text-danger"></span>
			</div>
			<div>
			<label>NIK PIC</label>
			<select readonly type="text" class="form-control" name="pic" id="pic">
							<?php

							$b = "SELECT * FROM user WHERE id='". $_SESSION["UserID"] ."'";

							$res = mysqli_query($conn, $b);
							while ($row = mysqli_fetch_assoc($res)) 
							{
								$selected = "";
							?>
								<option <?php echo $selected; ?> value="<?php echo $row["nik_data_karyawan"]; ?>">
									<?php
									echo $row["nik_data_karyawan"];
									?>
								</option>
							<?php
							}
							?>
			</select>
			<br>
			<label>Departement</label>
			<select readonly type="text" class="form-control" name="departement" id="departement">
							<?php

							$b = "SELECT * FROM user WHERE id='". $_SESSION["UserID"] ."'";
							$resb = mysqli_query($conn, $b);
							$datb=mysqli_fetch_assoc($resb);
							$nik_data_karyawan = $datb["nik_data_karyawan"];
							
							$d = "SELECT * FROM data_karyawan WHERE nik='". $nik_data_karyawan."'";
							$resd = mysqli_query($conn, $d);
							
							while ($rowd = mysqli_fetch_assoc($resd)) 
							{
								$selected = "";
							?>
								<option <?php echo $selected; ?> value="<?php echo $rowd["departement"]; ?>">
									<?php
									echo $rowd["departement"];
									?>
								</option>
							<?php
							}
							?>
			</select>
			</div>
			<br>
			<div class="form-group" align="center">
				<input type="hidden" name="row_id" id="hidden_row_id" />
				<button type="button" name="save" id="save" class="btn btn-info">Save</button>
			</div>
		</div>
		<div id="action_alert" title="Action">

		</div>
    </body>  
</html>  




<script>  
$(document).ready(function(){ 
	
	var count = 0;

	$('#user_dialog').dialog({
		autoOpen:false,
		width:400
	});

	$('#add').click(function(){
		$('#user_dialog').dialog('option', 'title', 'Add Daily');
		$('#first_name').val('');
		$('#last_name').val('');
		$('#mhb_id').val('');
		$('#remarks').val('');
		$('#pic').val('');
		$('#departement').val('');
		
		$('#error_first_name').text('');
		$('#error_last_name').text('');
		$('#error_mhb_id').text('');
		$('#error_remarks').text('');
		$('#error_pic').text('');
		$('#error_departement').text('');
		
		$('#first_name').css('border-color', '');
		$('#last_name').css('border-color', '');
		$('#mhb_id').css('border-color', '');
		$('#remarks').css('border-color', '');
		$('#pic').css('border-color', '');
		$('#departement').css('border-color', '');
		
		$('#save').text('Save');
		$('#user_dialog').dialog('open');
	});

	$('#save').click(function(){
		var error_first_name = '';
		var error_last_name = '';
		var first_name = '';
		var last_name = '';
		var mhb_id = '';
		var remarks = '';
		var pic = '';
		var departement = '';
		if($('#first_name').val() == '')
		{
			error_first_name = 'First Name is required';
			$('#error_first_name').text(error_first_name);
			$('#first_name').css('border-color', '#cc0000');
			first_name = '';
		}
		else
		{
			error_first_name = '';
			$('#error_first_name').text(error_first_name);
			$('#first_name').css('border-color', '');
			first_name = $('#first_name').val();
		}	
		if($('#last_name').val() == '')
		{
			error_last_name = 'Last Name is required';
			$('#error_last_name').text(error_last_name);
			$('#last_name').css('border-color', '#cc0000');
			last_name = '';
		}
		else
		{
			error_last_name = '';
			$('#error_last_name').text(error_last_name);
			$('#last_name').css('border-color', '');
			last_name = $('#last_name').val();
		}
		
		if($('#mhb_id').val() == '')
		{
			error_mhb_id = 'mhb id is required';
			$('#error_mhb_id').text(error_mhb_id);
			$('#mhb_id').css('border-color', '#cc0000');
			mhb_id = '';
		}
		else
		{
			error_mhb_id = '';
			$('#error_mhb_id').text(error_mhb_id);
			$('#mhb_id').css('border-color', '');
			mhb_id = $('#mhb_id').val();
		}
		if($('#remarks').val() == '')
		{
			error_remarks = 'remarks is required';
			$('#error_remarks').text(error_remarks);
			$('#remarks').css('border-color', '#cc0000');
			remarks = '';
		}
		else
		{
			error_remarks = '';
			$('#error_remarks').text(error_remarks);
			$('#remarks').css('border-color', '');
			remarks = $('#remarks').val();
		}
		if($('#pic').val() == '')
		{
			error_pic = 'pic is required';
			$('#error_pic').text(error_pic);
			$('#pic').css('border-color', '#cc0000');
			pic = '';
		}
		else
		{
			error_pic = '';
			$('#error_pic').text(error_pic);
			$('#pic').css('border-color', '');
			pic = $('#pic').val();
		}
		if($('#departement').val() == '')
		{
			error_departement = 'departement is required';
			$('#error_departement').text(error_departement);
			$('#departement').css('border-color', '#cc0000');
			departement = '';
		}
		else
		{
			error_departement = '';
			$('#error_departement').text(error_departement);
			$('#departement').css('border-color', '');
			departement = $('#departement').val();
		}
		
		
		
		if(error_first_name != '' || error_last_name != '' || error_mhb_id != '' || error_remarks != '' || error_pic != '' || error_departement != '')
		{
			return false;
		}
		else
		{
			if($('#save').text() == 'Save')
			{
				count = count + 1;
				output = '<tr id="row_'+count+'">';
				output += '<td>'+first_name+' <input type="hidden" name="hidden_first_name[]" id="first_name'+count+'" class="first_name" value="'+first_name+'" /></td>';
				output += '<td>'+last_name+' <input type="hidden" name="hidden_last_name[]" id="last_name'+count+'" value="'+last_name+'" /></td>';
				output += '<td>'+mhb_id+' <input type="hidden" name="hidden_mhb_id[]" id="mhb_id'+count+'" value="'+mhb_id+'" /></td>';
				output += '<td>'+remarks+' <input type="hidden" name="hidden_remarks[]" id="remarks'+count+'" value="'+remarks+'" /></td>';
				output += '<td>'+pic+' <input type="hidden" name="hidden_pic[]" id="pic'+count+'" value="'+pic+'" /></td>';
				output += '<td>'+departement+' <input type="hidden" name="hidden_departement[]" id="departement'+count+'" value="'+departement+'" /></td>';
			
		
				
				output += '<td><button type="button" name="view_details" class="btn btn-warning btn-xs view_details" id="'+count+'">View</button></td>';
				output += '<td><button type="button" name="remove_details" class="btn btn-danger btn-xs remove_details" id="'+count+'">Remove</button></td>';
				output += '</tr>';
				$('#user_data').append(output);
			}
			else
			{
				var row_id = $('#hidden_row_id').val();
				output = '<td>'+first_name+' <input type="hidden" name="hidden_first_name[]" id="first_name'+row_id+'" class="first_name" value="'+first_name+'" /></td>';
				output += '<td>'+last_name+' <input type="hidden" name="hidden_last_name[]" id="last_name'+row_id+'" value="'+last_name+'" /></td>';
				output += '<td>'+mhb_id+' <input type="hidden" name="hidden_mhb_id[]" id="mhb_id'+row_id+'" value="'+mhb_id+'" /></td>';
				output += '<td>'+remarks+' <input type="hidden" name="hidden_remarks[]" id="remarks'+row_id+'" value="'+remarks+'" /></td>';
				output += '<td>'+pic+' <input type="hidden" name="hidden_pic[]" id="pic'+row_id+'" value="'+pic+'" /></td>';
				output += '<td>'+departement+' <input type="hidden" name="hidden_departement[]" id="departement'+row_id+'" value="'+departement+'" /></td>';
				
				output += '<td><button type="button" name="view_details" class="btn btn-warning btn-xs view_details" id="'+row_id+'">View</button></td>';
				output += '<td><button type="button" name="remove_details" class="btn btn-danger btn-xs remove_details" id="'+row_id+'">Remove</button></td>';
				$('#row_'+row_id+'').html(output);
			}

			$('#user_dialog').dialog('close');
		}
	});

	$(document).on('click', '.view_details', function(){
		var row_id = $(this).attr("id");
		var first_name = $('#first_name'+row_id+'').val();
		var last_name = $('#last_name'+row_id+'').val();
		var mhb_id = $('#mhb_id'+row_id+'').val();
		var remarks = $('#remarks'+row_id+'').val();
		var pic = $('#pic'+row_id+'').val();
		var departement = $('#departement'+row_id+'').val();
		$('#first_name').val(first_name);
		$('#last_name').val(last_name);
		$('#mhb_id').val(mhb_id);
		$('#remarks').val(remarks);
		$('#pic').val(pic);
		$('#departement').val(departement);
		$('#save').text('Edit');
		$('#hidden_row_id').val(row_id);
		$('#user_dialog').dialog('option', 'title', 'Edit Data');
		$('#user_dialog').dialog('open');
	});

	$(document).on('click', '.remove_details', function(){
		var row_id = $(this).attr("id");
		if(confirm("Are you sure you want to remove this row data?"))
		{
			$('#row_'+row_id+'').remove();
		}
		else
		{
			return false;
		}
	});

	$('#action_alert').dialog({
		autoOpen:false
	});

	$('#user_form').on('submit', function(event){
		event.preventDefault();
		var count_data = 0;
		$('.first_name').each(function(){
			count_data = count_data + 1;
		});
		if(count_data > 0)
		{
			var form_data = $(this).serialize();
			$.ajax({
				url:"insert.php",
				method:"POST",
				data:form_data,
				success:function(data)
				{
					$('#user_data').find("tr:gt(0)").remove();
					$('#action_alert').html('<p>Daily Report Send Successfully</p>');
					$('#action_alert').dialog('open');
				}
			})
		}
		else
		{
			$('#action_alert').html('<p>Please Add atleast one data</p>');
			$('#action_alert').dialog('open');
		}
	});
	
});  
</script>