<?php
if (!function_exists('satu_harga_format')) {
	function satu_harga_format($value) {
		$value = (float) $value;
		$formatted = rtrim(rtrim(number_format($value, 4, '.', ''), '0'), '.');
		if ($formatted === '') {
			return '0';
		}
		$parts = explode('.', $formatted);
		$whole = number_format((float) $parts[0], 0, ',', '.');
		return count($parts) > 1 ? $whole.','.$parts[1] : $whole;
	}
}
?>
<div class="harga-single-form">
<h5 class="harga-pane-title">Satu Harga</h5>
<div class="form-group row mb-1">
	<label for="load_satuan" class="col-sm-3 col-form-label">Satuan Dasar</label>
	<div class="col-sm-6">
		<select name="load_satuan" id="satuan_load" class="custom-select form-control input-sm flat"  onchange="save_satu_harga()" required>
			<?php foreach($satuan AS $val):
				if($val->id == $bahan->id_satuan){
					$selected = 'selected';
					}else{
					$selected = '';
				}
			?>
			<option value="<?=$val->id;?>" <?=$selected;?>><?=$val->satuan;?></option>
			<?php endforeach; ?>
		</select>
	</div>
</div>
<div class="form-group row mb-1">
	<label for="harga_beli" class="col-sm-3 col-form-label">Harga Beli</label>
	<div class="col-sm-6">
		<input type="text" name="harga_beli" id="harga_beli" value="<?=!empty($row->harga_beli) ? satu_harga_format($row->harga_beli) : satu_harga_format($bahan->harga_modal);?>" class="form-control input-sm flat" onchange="hitung()" oninput="hargaBahanInputDecimal(this)" required>
	</div>
</div>
<div class="form-group row mb-1">
	<label for="harga_pokok" class="col-sm-3 col-form-label">Harga Pokok</label>
	<div class="col-sm-6">
		<input type="text" name="harga_pokok" id="harga_pokok" value="<?=!empty($row->harga_pokok) ? satu_harga_format($row->harga_pokok) : satu_harga_format($bahan->harga);?>" class="form-control input-sm flat" onchange="hitung()" oninput="hargaBahanInputDecimal(this)" required>
	</div>
</div>
<div class="form-group row mb-0 harga-hpp-group">
	<label for="hpp" class="col-sm-3 col-form-label">Komponen HPP</label>
	<div class="col-sm-6">
		<div class="row">
			<div class="col-md-12">
				<div class="form-group">
					<form name="add_name" id="add_name">
						<input type="hidden" id="id_bahan" name="id_bahan" value="<?=$id_bahan;?>" required />
						<table class="table table-bordered table-hover harga-hpp-table" id="dynamic_field">
							<tr>
								<th class="align-middle">Komponen</th>
								<th class="align-middle">Harga</th>
								<th><button type="button" name="add" id="add" class="btn btn-primary btn-sm flat"><i class="fa fa-plus"></i></button></th>
							</tr>
							
							<?php 
								$total_hpp = 0; 
								if(!empty($detail_hpp)){
									$na = 0; 
									foreach($detail_hpp->data AS $key=>$val) {
										if($val->id==0){
											$readonly = 'disabled';
											}else{
											$readonly = '';
										}
										$total_hpp += $val->harga;
									?>
									<tr id='rowCountDetail<?=$na;?>' class="rowCountFinish" >
										<td>
											
											<input class="form-control input-sm flat" type='text' id='title_<?=$na;?>' name="title[<?=$na;?>]" value="<?=$val->title;?>" required />
											<input type="hidden" id="id_detail_<?=$val->id;?>" name="id_detail[<?=$na;?>]" value="<?=$val->id;?>" required />
											
										</td>
										<td>
											
											<input class="form-control input-sm text-right flat text-right" type='text' id='harga_<?=$na;?>' name="harga[<?=$na;?>]" value="<?=satu_harga_format($val->harga);?>" oninput="hargaBahanInputDecimal(this);sum_harga()" required />
											
										</td>
										<td><button type="button" name="remove" id="<?=$na;?>" class="btn btn-danger btn-sm flat btn_remove" <?=$readonly;?>>X</button></td> 
									</tr>
									<?php $na++; 
									}  }else{ ?>
									<tr>
										<td><input type="hidden" id="id_detail_0" name="id_detail[0]" value="0" /><input type="text" name="title[]" class="form-control input-sm flat" required /></td>
										<td><input type="text" name="harga[]" value="" id="harga_0" class="form-control input-sm text-right flat" oninput="hargaBahanInputDecimal(this);sum_harga()" required /></td>
									</tr>
							<?php } ?>
							
							<tfoot>
								<tr>
									<td class="align-middle">Total HPP</td>
									<td><input type="text" name="total_hpp"  id="total_hpp" value="<?=satu_harga_format($total_hpp);?>" class="form-control input-sm flat text-right" readonly /></td>
									<td class="align-middle"><button type="submit" class="btn btn-success btn-sm flat" name="submit" id="submit_hpp"  title="Update"><i class="fa fa-refresh"></i></button></td>
								</tr>
							</tfoot>
						</table>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>

<div class="form-group row mb-1">
	<label for="persen" class="col-sm-3 col-form-label">Margin</label>
	<div class="col-sm-6">
		<div class="input-group input-group-sm flat">
			<input type="number" name="persen" id="persen" value="<?=!empty($row->persen) ? $row->persen : 0;?>" class="form-control input-sm flat" onchange="hitung()" min="0" required>
			<div class="input-group-append flat">
				<span class="input-group-text flat">%</span>
			</div>
		</div>
	</div>
	
</div>
<div class="form-group row">
	<label for="harga_jual" class="col-sm-3 col-form-label">Harga Jual</label>
	<div class="col-sm-6">
		<div class="input-group mb-3">
			<div class="input-group-prepend">
				<div class="input-group-text">
					<input type="checkbox" name="checkbox" id="ck_hargajual" aria-label="Checkbox for following text input">
				</div>
			</div>
			<input type="text" name="harga_jual" id="harga_jual" value="<?=!empty($row->harga_jual) ? satu_harga_format($row->harga_jual) : 0;?>"  class="form-control input-sm flat" oninput="hargaBahanInputDecimal(this)" onchange="save_satu_harga()" required readonly>
		</div>
		
	</div>
</div>
</div>
<script>
	
	$(document).ready(function() {
		
		$('input').click(function() {
			this.select();
		});
		$("#ck_hargajual").change(function() {
			
			if (this.checked) {	 
				$("#harga_jual").attr("readonly", false);
				$("#persen").val(0).attr("readonly", true);
				}else{
				$("#harga_jual").attr("readonly", true);
				$("#persen").val(0).attr("readonly", false);
			}
		});
	});
	
	$(document).ready(function(){
		$("#add").click(function(){
			var i = $("#dynamic_field > tbody").children().length - 1;
			var cols = '<tbody id="tbody'+i+'">';
			cols +='<tr id="rowCountDetail'+i+'"><td><input type="hidden" id="id_detail_'+i+'" name="id_detail['+i+']" value="'+i+'" /><input type="text" name="title['+i+']" id="title_'+i+'" class="form-control input-sm flat " required /></td><td><input type="text" name="harga['+i+']" id="harga_'+i+'" value=""  class="form-control input-sm text-right flat" oninput="hargaBahanInputDecimal(this);sum_harga()"  required /></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn-sm flat btn_remove">X</button></td></tr>'; 
			cols += "</tbody>";
			$('#dynamic_field').append(cols);
			i++;
		});
		
		$(document).on('click', '.btn_remove', function(){  
			var button_id = $(this).attr("id");     
			$('#rowCountDetail'+button_id+'').remove();  
			$('#tbody'+button_id+'').remove();  
			sum_harga()
		});
		
		
		$("#add_name").submit(function(event) {
			
			var formdata = $("#add_name").serialize();
			// console.log(formdata);
			
			event.preventDefault()
			
			$.ajax({
				url   : base_url+"produk/update_hpp",
				type  :"POST",
				data  :formdata,
				cache :false,
				success:function(result){
					showNotif('bottom-right','Update Harga','sukses','success');
					hitung();
					save_satu_harga();
				}
			});
			
		});
	});
	
	function sum_harga() {
		// var str = $("#id_invoice").val();
		
		var harga_pokok = parseFloat(hargaDecimalValue($("#harga_pokok").val()) || 0);
		var persen = parseFloat(hargaDecimalValue($("#persen").val()) || 0);
		var b = $("#dynamic_field > tbody").children().length - 1;	
		var total_jual = 0;
		var jum_hpp = 0;
		var r = 0;
		var total_harga_hpp = [];
		for (; r < b; r++) {
			var harga = parseFloat(hargaDecimalValue($("#harga_" + r).val()) || 0);
			total_harga_hpp[r] = harga;
			jum_hpp = jum_hpp + total_harga_hpp[r];
			
		}
		if(persen > 0){
			total_persen = (jum_hpp * persen) / 100;
			total_jual = harga_pokok + jum_hpp + total_persen;
			}else{
			total_jual = harga_pokok + jum_hpp;
		}
		$("#total_hpp").val(hargaFormatDecimalDisplay(jum_hpp));
		// $("#harga_jual").val(formatMoney(total_jual, 0, "Rp."));
		
	}
</script>								
