<div class="keranjang" id="tambah-pembelian">
	<form autocomplete="off" class="form-add" id="form-add-beli">
		<div class="row">
			<div class="col-sm">
				<div class="form-group">
					<label for="formGroupExampleInput">No. Transaksi</label>
					<input type="text" class="form-control form-control-sm flat" name="no_trans" id="no_trans" value="<?=$no_trans;?>" disabled>
				</div>
			</div>
			<div class="col-sm">
				<div class="form-group">
					<label for="jenis_bayar">Jenis Bayar</label>
					<select name="jenis_bayar" id="jenis_bayar"  class="custom-select form-control-sm flat" disabled>
						<option value="1" <?php echo ($row->id_jenis == 1) ? 'selected' : ''; ?>>Kas</option>
						<option value="2" <?php echo ($row->id_jenis == 2) ? 'selected' : ''; ?>>Hutang Usaha</option>
					</select>
				</div>
			</div>
			<div class="col-sm">
				<div class="form-group">
					<label for="dari_kas">Keluar dari akun</label>
					<select name="dari_kas" id="dari_kas"  class="custom-select form-control-sm flat"  data-valueKey="id" data-displayKey="name" disabled>
					</select>
				</div>
			</div>
			<div class="col-sm">
				<div class="form-group text-right">
					<label for="saldo_kas">Saldo Kas</label>
					<input type="text" class="form-control form-control-sm flat text-right" name="saldo_kas" id="saldo_kas" disabled>
				</div>
			</div>
		</div>
		<div class="row">
			<div class="col-sm">			 
				<label for="tanggal_beli">Tanggal</label>
				<div class="input-group input-group-sm flat">
					<div class="input-group-prepend">
						<button class="btn btn-outline-secondary" type="button" disabled><i class="fa fa-calendar"></i></button>
					</div>
					<input type="text" class="form-control flat" name="tanggal_beli" id="tanggal_beli" value="<?=$tanggal;?>" disabled>
				</div>
			</div>
			<div class="col-sm">
				<div class="form-group">
					<label for="keterangan_kas">Keterangan</label>
					<input type="text" class="form-control form-control-sm flat" name="keterangan_kas" id="keterangan_kas" value="<?=$keterangan;?>" disabled>
				</div>
			</div>
			<div class="col-sm">
				<div class="form-group text-right">
					<label for="jumlah_kas">Jumlah Kas</label>
					<input type="text" class="form-control form-control-sm flat text-right" name="jumlah_kas"  id="jumlah_kas" value="<?=rprp($total_bayar);?>"  disabled>
				</div>
			</div>
			<div class="col-sm">
				<div class="form-group">
					<label>Bukti Nota</label>
					<?php if (!empty($row->bukti_nota ?? '')) : ?>
						<div>
							<a href="<?= base_url('uploads/bukti_pembelian/' . ($row->bukti_nota ?? '')); ?>" target="_blank" class="btn btn-sm btn-outline-primary flat">
								<i class="fa fa-file"></i> Lihat / Unduh
							</a>
						</div>
					<?php else : ?>
						<input type="text" class="form-control form-control-sm flat" value="(Tidak ada)" disabled>
					<?php endif; ?>
				</div>
			</div>

		</div>
		<div class='row'>
			<div class="col-md-12">
				<table class="table table-striped table-sm" id="table-beli">
					<thead>
						<tr>
							<td width="10" class="p-0 align-middle text-center"><button class="btn btn-primary flat add-more" type="button" id="add-more" disabled><i class="fa fa-plus"></i></button></td>
							<td class="w-20 pl-2">Item Beli</td>
							<td>Keterangan Rincian</td>
							<td class="w-10 text-right">Jumlah</td>
							<td class="w-20 text-right">Harga</td>
							<td class="w-20 text-right">Total Pembelian</td>
						</tr>
					</thead>
					<tbody id="insert-form-kas">
						<?php foreach ($data as $da) : 
							$total = $da['jumlah'] * $da['harga'];
						?>	
						<tr id='rowCount' class="rowCount" >
							<td align="center">
								<button type="button" data-no=''
								class='btn btn-light bg-white btn-sm text-danger shadow-none flat' data-id="0" disabled><i class="fa fa-times"></i></button>
							</td>
							
							<td>
								<select type="text" class="form-control input-sm flat" id="idAkun_0" name="idAkun[]" disabled>
									<?php foreach ($item->result_array() as $ak) : ?>
									<?php $selected = in_array($da['id_bahan'], $ak) ? " selected " : null;?>
									<option value="<?= $ak['id']; ?>" <?=$selected?>><?= $ak['title']; ?></option>
									<?php endforeach; ?>
								</select>
							</td>
							<td>
								<select type="text" class="form-control bootstrap-select input-sm flat" id="supplier_0" name="supplier[]" title="Supplier" data-container="body" data-live-search="true" disabled>
									<?php foreach ($supplier->result_array() as $aks) : ?>
									 
									<?php $selected = in_array($da['id_supplier'], $aks) ? " selected " : null;?>
									<option value="<?= $aks['id_supplier']; ?>" <?=$selected?>><?= $aks['nama_perusahaan']; ?></option>
									<?php endforeach; ?>
								</select>
							</td>
							<td>
								<div class="form-group p-0 m-0">
									<input type="text" id="keterangan_0" name="keterangan[]" class="form-control input-sm flat" value="<?=$da['keterangan'];?>" disabled />
								</div>
							</td>
							
							<td>
								<div class="form-group p-0 m-0">
									<input type="text" id="jumlah_0" name="jumlah[]" class="form-control input-sm flat text-right" value="<?=$da['jumlah'];?>" disabled />
								</div>
							</td>
							
							<td>
								<div class="form-group p-0 m-0">
									<input type="text" id="harga_0" name="harga[]" class="form-control input-sm flat text-right jumlah" value="<?=rprp($da['harga']);?>" disabled />
								</div>
							</td>
							
							<td>
								<div class="form-group p-0 m-0">
									<input type="text" id="total_0" name="total[]" class="form-control input-sm flat text-right jumlah" value="<?=rprp($total);?>" disabled />
								</div>
							</td>
						</tr>
						<?php endforeach; ?>
					</tbody>
				</table>
			</div>
		</div>
	</form>
</div>

<script>
	var dari_kas = <?=$dari_kas;?>
	 
	// Menangani tambah data transakdi +
	
	// menangani otomatis nomor akun dan nama akun
	$(document).ready(function(){
		$(".bootstrap-select").selectpicker();
		
		$(document).on("keyup", ".jumlah, #jumlah_kas", function() {
			//Menangani Input otomatis Format Ribuan
			let rupiah = $(this).val();
			
			$(this).val(formatRupiah(rupiah, 'Rp. '));
			
			/* Fungsi formatRupiah */
			function formatRupiah(angka, prefix){
				let number_string = angka.replace(/[^,\d]/g, '').toString(),
				split   		= number_string.split(','),
				sisa     		= split[0].length % 3,
				rupiah     		= split[0].substr(0, sisa),
				ribuan     		= split[0].substr(sisa).match(/\d{3}/gi);
				
				// tambahkan titik jika yang di input sudah menjadi angka ribuan
				if(ribuan){
					separator = sisa ? '.' : '';
					rupiah += separator + ribuan.join('.');
				}
				
				rupiah = split[1] != undefined ? rupiah + ',' + split[1] : rupiah;
				return prefix == undefined ? rupiah : (rupiah ? 'Rp. ' + rupiah : '');
			}
		});
	});
	
	$("#dari_kas").filter(function() {
		$.ajax({
			url : base_url + "pembelian/akun_kas",
			type : "post",
			data : {
				id : dari_kas
			},
			dataType : "json",
			success : function(response) {
				var msize = response.length;
				$(".dari_kas").attr("disabled", false);
				$("#dari_kas").append("<option value=''>Pilih</option>");
				/** @type {number} */
				var i = 0;
				for (; i < msize; i++) {
					var teg = response[i]["id"];
					var last_supr = response[i]["name"];
					
					$("#dari_kas").append("<option value='" + teg + "'>" + last_supr + "</option>");
				}
				$("#dari_kas").val(dari_kas).change();
			}
		});
	});
	$('.simpan_pembelian').attr('disabled',true).hide();
</script>
