<script>
	var baseurl = "<?php echo base_url("index.php/"); ?>"; // Buat variabel baseurl    

</script>
<script src="<?php echo base_url("js/jquery.min.js"); ?>"></script> <!-- Load library jquery -->
<script src="<?php echo base_url("js/biayaformulir.js"); ?>"></script> <!-- Load file process.js -->

<SCRIPT LANGUAGE="JavaScript">
</SCRIPT>
<div class="modal fade" id="modal-default">
	<div class="modal-dialog modal-lg">
		<div class="modal-content">
			<div class="modal-header">
				<h4 class="modal-title">TRANSAKSI BIAYA TES</h4>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
			</div>
			<div class="modal-body">



				<div class="row">
					<div class="col-md-6">
						<?php echo form_open_multipart(base_url('biayaformulir/tambah'),'id="tambah"') ?>

						<h4 class="alert alert-info">Data Peserta</h4>
						<!-- <div class="form-group row">
							<label class="col-sm-3 control-label text-right">No. Pend <span class="text-danger">*</span></label>
							<div class="col-sm-9">
								<input type="text" name="no_pendaftaran" id="no_pendaftaran" class="form-control"
									placeholder="Masukkan No. Pendaftaran" required
									oninvalid="this.setCustomValidity('data tidak boleh kosong, diisi No. Pendaftaran')"
									oninput="setCustomValidity('')"> <button type="button" name="btn-search" class="btn btn-default"
									id="btn-search"><i class="fa fa-search"></i> </button> <span id="loading">LOADING...</span>
							</div>
						</div> -->

						<div class="form-group row">
							<label class="col-sm-3 control-label text-right">No. Pend <span class="text-danger">*</span></label>
    						<div class="col-sm-9">
        					<?php 
        						//$siswa = $this->db->get("data_siswa")->result();
								$siswa = $this->biayaformulir_model->get_siswa();
        					?>
        					<select name="no_pendaftaran" id="no_pendaftaran" class="form-control select2" required>
            				<option value="">Pilih No. Pendaftaran</option>
            					<?php foreach ($siswa as $value) { ?>
                					<option value="<?= $value->no_pendaftaran ?>"><?= $value->no_pendaftaran ?></option>
            					<?php } ?>
        					</select><br>
							<oninvalid="this.setCustomValidity(' data tidak boleh kosong, diisi No. Pendaftaran ')' 
								oninput="setCustomValidity('')"> <button type="button" name="btn-search" class="btn btn-warning btn-lg"
								id="btn-search"><i class="fa fa-search"></i> </button> <span id="loading">LOADING...</span>
							</div>
    					</div>    

						<div class="form-group row">
							<label class="col-sm-3 control-label text-right">Nama Lengkap <span class="text-danger"></span></label>
							<div class="col-sm-9">
								<input type="text" name="nama_lengkap" id="nama_lengkap" class="form-control" placeholder="Nama Lengkap"
									readonly required>
							</div>
						</div>

						<div class="form-group row">
							<label class="col-sm-3 control-label text-right">Sekolah Asal <span class="text-danger"></span></label>
							<div class="col-sm-9">
								<input type="text" name="sekolah_asal" id="sekolah_asal" class="form-control" placeholder="Sekolah Asal"
									readonly disabled>
							</div>
						</div>

						<div class="form-group row">
							<label class="col-sm-3 control-label text-right">Prog. Keahlian <span class="text-danger"></span></label>
							<div class="col-sm-9">
								<input type="text" name="kompetensi" id="kompetensi" class="form-control" placeholder="Prog. Keahlian"
									readonly disabled>
							</div>
						</div>


						<div class="form-group row">
							<label class="col-sm-3 control-label text-right">Gelombang <span class="text-danger"></span></label>
							<div class="col-sm-9">
								<input type="text" name="gelombang" id="gelombang" class="form-control" placeholder="Gelombang"
									readonly disabled>
							</div>
						</div>




					</div>

					<!--tab 2-->
					<div class="col-md-6">
						<h4 class="alert alert-info">Data Pembayaran</h4>

						<div class="form-group row">
							<label for="level" class="col-sm-4 col-form-label">No. Faktur</label>
							<div class="col-sm-8">
								<input type="text" name="no_faktur" value="<?= $notr ?>" class="form-control" readonly>
							</div>
						</div>

						<div class="form-group row">
							<label for="level" class="col-sm-4 col-form-label">Biaya Tes (Rp.)</label>
							<div class="col-sm-8">
								<input type="text" name="biaya_tes" value="<?= $biaya ?>" class="form-control" readonly>
							</div>
						</div>

						<div class="form-group row">
							<label for="diskon" class="col-sm-4 col-form-label">Diskon (Rp.)</label>
							<div class="col-sm-8">
								<input type="text" value="0" id="diskon" name="diskon" class="form-control" readonly>
								<button type="button" class="btn btn-primary mt-3" onclick="isiDiskon()">Diskon</button>
							</div>
						</div>

						<div class="form-group row">
							<label for="total" class="col-sm-4 col-form-label">Total (Rp.)</label>
							<div class="col-sm-8">
								<input type="text" value="<?= $biaya ?>" id="total" name="total" class="form-control" readonly>
							</div>
						</div>

						<div class="form-group row">
							<label for="bayar" class="col-sm-4 col-form-label">Bayar (Rp.)</label>
							<div class="col-sm-8">
								<input type="text" id="bayar" name="bayar" class="form-control" oninput="hitungKembalian()" required>
							</div>
						</div>

						<div class="form-group row">
							<label for="kembalian" class="col-sm-4 col-form-label">Kembalian (Rp.)</label>
							<div class="col-sm-8">
								<input type="text" id="kembalian" name="kembalian" class="form-control" readonly>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-3 control-label text-right"></label>
							<div class="col-sm-9">
								<div class="form-group btn-group text-right">
									<!-- <button  formtarget="_blank" type="submit" name="submit" class="btn btn-primary"><i class="fa fa-save"></i> Simpan</button> -->
									<button type="submit" name="submit" class="btn btn-primary"><i class="fa fa-save"></i> Simpan</button>
									<button type="reset" name="reset" class="btn btn-info"><i class="fa fa-cut"></i> Batal</button>
									<a href="<?php echo base_url('biayaformulir')?>" class="btn btn-default" data-dismiss="modal"><i
											class="fa fa-backward"></i> Kembali</a>
								</div>
							</div>
							<label class="col-sm-12 control-label text-right"><span class="text-danger">*</span> <small>Wajib
									diisi</small></label>
						</div>
						<?php 						
						echo form_close(); ?>
					</div>

			</div>
			</div>
		</div>
	</div>
</div>
<script>
   function removeNonNumericChars(input) {
        return input.replace(/\D/g, '');
    }

    // Fungsi untuk mengubah format input harga
    function formatHarga(input) {
        var num = removeNonNumericChars(input.value);
        var formattedNum = new Intl.NumberFormat('id-ID').format(num);
        input.value = formattedNum;
    }

    // Fungsi untuk menghapus format input harga sebelum disubmit
    function unformatHarga(input) {
        input.value = removeNonNumericChars(input.value);
    }

    // Mengaitkan fungsi formatHarga dengan event onkeyup untuk input harga
    document.getElementById('bayar').addEventListener('keyup', function() {
        formatHarga(this);
    });
	

    // Mengaitkan fungsi unformatHarga dengan event onsubmit untuk form
    document.getElementById('tambah').addEventListener('submit', function() {
		removeNonNumericChars(document.getElementById('bayar').value);
    });

	function isiDiskon() {
		document.getElementById('diskon').value = '<?= $diskon ?>';
		hitungTotal();
	}

	function hitungTotal() {
    var biaya = parseFloat(document.getElementsByName('biaya_tes')[0].value);
    var diskon = parseFloat(document.getElementById('diskon').value);

    // Pengecekan apakah nilai diskon kosong
    if (isNaN(diskon)) {
        diskon = 0; // Jika diskon kosong, atur nilainya menjadi 0
    }

    var total = biaya - diskon;
    document.getElementById('total').value = total;
}

	function hitungKembalian() {
       

		var bayar =  removeNonNumericChars(document.getElementById('bayar').value);
		console.log( removeNonNumericChars(document.getElementById('bayar').value));
		var total = parseFloat(document.getElementById('total').value);
		var kembalian = bayar - total;
		document.getElementById('kembalian').value = kembalian;
	}

</script>
