<?php
//form edit open
echo form_open(base_url('invoice/simpanedit/'.$invoice->no_pendaftaran));
?>

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

<div class="row">
	<div class="col-md-12">
		<div class="form-group row">
			<label class="col-sm-2 control-label text-left">No. Pend <span class="text-danger">*</span></label>
			<div class="col-sm-10">
				<input type="text" value="<?= $invoice->no_pendaftaran ?>" name="no_pendaftaran" id="no_pendaftaran" class="form-control" placeholder="Masukkan No. Pendaftaran"  readonly> 
			</div>
		</div>

		<div class="form-group row">
			<label class="col-sm-2 control-label text-left">Nama Lengkap <span class="text-danger"></span></label>
			<div class="col-sm-10">
				<input type="text"  value="<?= $invoice->nama_lengkap ?>" 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-2 control-label text-left">Sekolah Asal <span class="text-danger"></span></label>
			<div class="col-sm-10">
				<input type="text"  value="<?= $invoice->sekolah_asal ?>" name="sekolah_asal" id="sekolah_asal" class="form-control" placeholder="Sekolah Asal"
					readonly>
			</div>
		</div>
		<div class="form-group row">
			<label class="col-sm-2 control-label text-left">Kompetensi<span class="text-danger"></span></label>
			<div class="col-sm-10">
				<input type="text"  value="<?= $invoice->kompetensi ?>" name="kompetensi" id="kompetensi" class="form-control" placeholder="Kompetensi"
					readonly>
			</div>
		</div>
		<div class="form-group row">
			<label class="col-sm-2 control-label text-left">No. Invoice<span class="text-danger"></span></label>
			<div class="col-sm-10">
				<input type="text"  value="<?= $invoice->no_invoice ?>" name="noinvoice" id="noinvoice" class="form-control"
					placeholder="Sekolah Asal" readonly>
			</div>
		</div>

		<div class="form-group row">
			<label class="col-sm-2 control-label text-left">Biaya PPDB Rp.<span class="text-danger"></span></label>
			<div class="col-sm-10">
				<input type="text" value="<?php 
                echo number_format($invoice->biaya, 0, ',', '.');
                
                ?>" name="biayappdb" id="biayappdb" class="form-control" placeholder="Biaya PPDB"
					readonly>
				<input type="hidden" name="realbiaya" id="realbiaya" value="<?= $invoice->biaya ?>">
			</div>
		</div>
		<div class="form-group row">
							<label class="col-sm-2 control-label text-left">Status<span
									class="text-danger"></span></label>
							<div class="col-sm-8">
								<div class="form-group">
									
									<select class="form-control" id="potongan" name="potongan">
										<option value="">Pilih Jenis Potongan</option>
										<option value="Yatim">Yatim</option>
										<option value="Kembar">Kembar</option>
										<option value="Yatim & Kembar">Yatim & Kembar</option>
										<option value="Yayasan">Yayasan</option>
										<option value="SKTM">SKTM</option>
									</select>

									
								</div>

							</div>

							<div class="col-sm-2">
								<div class="form-group">
									<button id="btn-potongan" class="btn btn-md btn-info" >Input</button>
								</div>
							</div>
						</div>
						<div class="form-group row">
							<label class="col-sm-2 control-label text-left">Potongan DP<span
									class="text-danger"></span></label>
							<div class="col-sm-8">
								<div class="form-group">
									
									<select class="form-control" id="potongandp" name="potongandp" >
										<option value="">Potongan DP</option>
										<option value="YA">YA</option>
										<option value="TIDAK">TIDAK</option>
									</select>			
								</div>
							</div>
							<div class="col-sm-2">
								<div class="form-group">
									<button id="btn-potongan-dp" class="btn btn-md btn-info" >Input</button>
								</div>
							</div>
						</div>

	</div>
</div>
<hr>
<h5>Ketentuan</h5>
<div class="row">


	<div class="col-md-6">
		<div class="form-group row">
			<label for="level" class="col-sm-5 col-form-label">Potongan Gel. 1</label>
			<div class="col-sm-7">
				<input type="text" value="<?= $invoice->pot_gel ?>" onchange="calculateTotal()" name="potongan_gel_1" id="potongan_gel_1"
					class="form-control">
			</div>
		</div>

		<div class="form-group row">
			<label for="level" class="col-sm-5 col-form-label">Potongan DP Gel. 1</label>
			<div class="col-sm-7">
				<input type="text" onchange="calculateTotal()" value="<?= $invoice->pot_dp ?>" name="potongan_dp" id="potongan_dp"
					class="form-control">
			</div>
		</div>

		<div class="form-group row">
			<label for="potongan_yatim" class="col-sm-5 col-form-label">Potongan YATIM</label>
			<div class="col-sm-7">
				<input type="text" onchange="calculateTotal()" value="<?= $invoice->pot_yatim ?>" id="potongan_yatim" name="potongan_yatim"
					class="form-control">
			</div>
		</div>

		<div class="form-group row">
			<label for="total" class="col-sm-5 col-form-label">Potongan SPP 50%</label>
			<div class="col-sm-7">
				<input type="text" onchange="calculateTotal()" value="<?= $invoice->pot_spp ?>" id="potongan_spp" name="potongan_spp"
					class="form-control">
			</div>
		</div>

		<div class="form-group row">
			<label for="total" class="col-sm-5 col-form-label">Potongan Kembar</label>
			<div class="col-sm-7">
				<input type="text" onchange="calculateTotal()" value="<?= $invoice->pot_kembar ?>" id="potongan_kembar" name="potongan_kembar"
					class="form-control">
			</div>
		</div>




	</div>
	<div class="col-md-6">
		<div class="form-group row">
			<label for="total" class="col-sm-5 col-form-label">Potongan Yayasan</label>
			<div class="col-sm-7">
				<input type="text" onchange="calculateTotal()" value="<?= $invoice->pot_yayasan ?>" id="potongan_yayasan" name="potongan_yayasan"
					class="form-control">
			</div>
		</div>
		<div class="form-group row">
			<label for="total" class="col-sm-5 col-form-label">Potongan SKTM</label>
			<div class="col-sm-7">
				<input type="text" onchange="calculateTotal()" value="<?= $invoice->pot_sktm ?>" id="potongan_sktm" name="potongan_sktm"
					class="form-control">
			</div>
		</div>

		<div class="form-group row">
			<label for="total" class="col-sm-5 col-form-label">Potongan Lainnya</label>
			<div class="col-sm-7">
				<input type="text" onchange="calculateTotal()" value="<?= $invoice->pot_lain ?>" id="potongan_lainnya" name="potongan_lainnya"
					class="form-control">
			</div>
		</div>

		<div class="form-group row">
			<label for="total" class="col-sm-5 col-form-label">Ketentuan Bayar</label>
			<div class="col-sm-7">
				<input type="text" id="total" name="total"  value="<?= $invoice->ketentuan ?>" class="form-control" readonly>
			</div>
		</div>


	</div>
</div>

<!---simpan,reset,kembali-->
<div class="form-group row">
	<label for="ok" class="col-sm-2 col-form-label"></label>
	<div class="col-sm-9">
		<button type="submit" class="btn btn-primary">
			<i class="fa fa-save"></i> Simpan Data
		</button>
		
		<a href="<?php echo base_url('invoice') ?>" class="btn btn-default">
			<i class="fa fa-backward"></i> Kembali
		</a>
	</div>
</div>

<?php
//form close
echo form_close();
?>

<script>
$(document).ready(function() {
    $('#btn-potongan').click(function() {
		event.preventDefault();
        var potongan = $('#potongan').val(); // Mendapatkan nilai dari select
        var nopendaftaran = $('#no_pendaftaran').val(); // Mendapatkan nilai dari input
        
        // Periksa apakah nilai-nilai tersebut didefinisikan sebelum menggunakan trim()
        if (potongan !== '' && nopendaftaran !== '') {
            $.ajax({
                type: 'POST',
                url: '<?php echo base_url("invoice/caripotongan"); ?>', // Sesuaikan dengan URL controller
                data: { 
                    potongan: potongan,
                    nopendaftaran: nopendaftaran
                },
				dataType: "json",
                success: function(response) {
                    console.log(response.potkembar); // Menampilkan respon dari server di konsol
                    // Lakukan sesuatu dengan respon dari server (jika diperlukan)
					$("#potongan_kembar").val(response.potkembar); // set textbox        
                  $("#potongan_yatim").val(response.potyatim); // set textbox        
                  $("#potongan_spp").val(response.potspp); // set textbox    
				  calculateTotal();
                },
                error: function(xhr, status, error) {
                    console.error(xhr.responseText); // Menampilkan pesan error di konsol
                }
            });
        } else {
            alert('Masukkan Nomor Pendaftaran! / Pilih Opsi Status');
        }
    });

	$('#btn-potongan-dp').click(function() {
		event.preventDefault();
        var potongan = $('#potongandp').val(); // Mendapatkan nilai dari select
        var nopendaftaran = $('#no_pendaftaran').val(); // Mendapatkan nilai dari input
        
        // Periksa apakah nilai-nilai tersebut didefinisikan sebelum menggunakan trim()
        if (potongan !== '' && nopendaftaran !== '') {
            $.ajax({
                type: 'POST',
                url: '<?php echo base_url("invoice/caripotongandp"); ?>', // Sesuaikan dengan URL controller
                data: { 
                    potongan: potongan,
                    nopendaftaran: nopendaftaran
                },
				dataType: "json",
                success: function(response) {
                    console.log(response.potkembar); // Menampilkan respon dari server di konsol
                    // Lakukan sesuatu dengan respon dari server (jika diperlukan)
				   
                  $("#potongan_dp").val(response.potdp); // set textbox    
				  calculateTotal();
                },
                error: function(xhr, status, error) {
                    console.error(xhr.responseText); // Menampilkan pesan error di konsol
                }
            });
        } else {
            alert('Masukkan Nomor Pendaftaran! / Pilih Opsi Potongan DP');
        }
    });
});

</script>

<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('potongan_yayasan').addEventListener('keyup', function() {
        formatHarga(this);
    });
	document.getElementById('potongan_sktm').addEventListener('keyup', function() {
        formatHarga(this);
    });
	document.getElementById('potongan_lainnya').addEventListener('keyup', function() {
        formatHarga(this);
    });
	
	
	function calculateTotal() {
		var realbiaya = parseInt(document.getElementById("realbiaya").value);
		var potongan_gel_1 = parseInt(document.getElementById("potongan_gel_1").value);
		var potongan_dp = parseInt(document.getElementById("potongan_dp").value);
		var potongan_yatim = parseInt(document.getElementById("potongan_yatim").value);
		var potongan_spp = parseInt(document.getElementById("potongan_spp").value);
		var potongan_kembar = parseInt(document.getElementById("potongan_kembar").value);
		var potongan_yayasan = parseInt(removeNonNumericChars(document.getElementById('potongan_yayasan').value));
		var potongan_sktm = parseInt(removeNonNumericChars(document.getElementById("potongan_sktm").value));
		var potongan_lainnya = parseInt(removeNonNumericChars(document.getElementById("potongan_lainnya").value));

		var total_potongan = potongan_gel_1 + potongan_dp + potongan_yatim + potongan_spp + potongan_kembar +
			potongan_yayasan + potongan_sktm + potongan_lainnya;
		var total = realbiaya - total_potongan;

		document.getElementById("total").value = total;
	}
</script>