<style type="text/css">
	.width-td{width: 100px;}
	/*.winput{width: 100px;}*/
	input {
		text-align: center;
	}
</style>

<style type="text/css">
  .table > thead > tr > th {
     vertical-align: middle;
     text-align: center;
  }
  .table > tbody > tr > td {
     vertical-align: middle;
     text-align: center;
  }
</style>

<div class="row">
    <!-- begin col-8 -->
    <div class="col-sm-12">
        <!-- begin panel -->
        <form method="post" id="form_edit" action="">
        	<div class="panel panel-grey">
	            <div class="panel-heading">
	        		<div class="panel-heading-btn">
	                  	<a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-grey" data-click="panel-expand"><i class="fa fa-expand"></i></a>
	                  	<a href="javascript:;" class="btn btn-xs btn-icon btn-circle btn-grey" data-click="panel-remove"><i class="fa fa-times"></i></a>
	              	</div>
	              	<?php echo menu_parameter('kalsium', true, ($method == 'edit' ? null : $url_batch)); ?>
					<button type="button" onclick="save_analis();" class="btn btn-warning">Simpan</button>
					<hr>

					<table class="table bg-inverse">
						<tr>
							<th>Suhu (ᵒC)</th>
							<td>:</td>
							<th><input style="width:150px;" name='suhu[]' value="<?php echo $data[0]['suhu'] ?>"></th>

							<th>Terbitan/Revisi</th>
							<td>:</td>
							<th><input style="width:150px;" name='terbitan_revisi_ke[]' value="<?php echo $data[0]['terbitan_revisi_ke'] ?>"></th>

							<th style="width:10px;"></th>
						</tr>
						<tr>
							<th>RH (%)</th>
							<td>:</td>
							<th><input style="width:150px;" name='rh[]' value="<?php echo $data[0]['rh'] ?>"></th>

							<th>Tanggal Terbitan</th>
							<td>:</td>
							<th>
								<!-- <input type="date" style="width:150px;" name='tanggal_terbitan[]' value="<?php echo $data[0]['tanggal_terbitan'] ?>"> -->

								<input style="width:50px;" class="mulai_uji" onchange="klik_tgl('tanggal_terbitan');" type="date" id="tanggal_terbitan" name="tanggal_terbitan[]" value="<?php echo $data[0]['tanggal_terbitan']; ?>">
								<input id="c_tanggal_terbitan" class="" value="<?php echo toi_date03($data[0]['tanggal_terbitan']); ?>" readonly>
							</th>

							<th style="width:10px;"></th>
						</tr>
						<tr>
							<th>Metode Uji</th>
							<td>:</td>
							<th>
								AOAC 2012 Bab 4 Butir 4.8.03 Metode 927.02 
							</th>

							<th>Tanggal Revisi</th>
							<td>:</td>
							<th>
								<!-- <input style="width:150px;" type="date" name='tanggal_revisi[]' value="<?php echo $data[0]['tanggal_revisi'] ?>"> -->

								<input style="width:50px;" class="mulai_uji" onchange="klik_tgl('tanggal_revisi');" type="date" id="tanggal_revisi" name="tanggal_revisi[]" value="<?php echo $data[0]['tanggal_revisi']; ?>">
								<input id="c_tanggal_revisi" value="<?php echo toi_date03($data[0]['tanggal_revisi']); ?>" readonly>
							</th>

							<th style="width:10px;"></th>
						</tr>
					</table>
            	</div>

            	<div class="panel panel-body table-responsive">
            		<button class="btn btn-primary" type="button" onclick="add_kontrol_sampel()">
						Tambah Kontrol Sampel
					</button>
					<label><input type="radio" id="t1" name="letak[]" value="t1_">Sebelum</label>
					<label><input type="radio" id="t3" name="letak[]" value="t3_">Sesudah</label>
					<select id="letak_id" name="letak_id[]">
						<option disable>Pilih sampel</option>
						<?php foreach($data as $d) { ?>
							<?php if(!is_null($d['id_sampel'])) { ?>
								<option value="<?php echo $d['id_sampel']; ?>"><?php echo $d['id_sampel']; ?></option>
							<?php } ?>
						<?php } ?>
					</select>
					<hr>

                	<table class="table table-bordered table-hover table-striped table-td-valign-middle">
                		<thead>
							<tr>
								<tr>
										<th rowspan="2" class="text-center">No Urut</th>
										<th colspan="2" class="text-center">Mulai Pengujian</th>
										<th rowspan="2" class="text-center">No Sampel</th>
										<th rowspan="2" class="text-center">Kode Krusibel</th>
										<th rowspan="2" class="text-center">Berat Sampel<br/>(mg)<br/>(W)</th>
										<th rowspan="2" class="text-center">Volume Titrasi Sampel<br/>(ml)<br/>(VS)</th>
										<th rowspan="2" class="text-center">Volume Titrasi Blanko<br/>(ml)<br/>(VB)</th>
										<th rowspan="2" class="text-center">Normalitas KMnO4<br/>(N)</th>
										<th rowspan="2" class="text-center">Volume Larutan Induk<br/>(ml)(O)</th>
										<th rowspan="2" class="text-center">Volume Alikuot<br/>(ml)(A)</th>
										<th rowspan="2" class="text-center">Nomor Atom Ca</th>
										<th rowspan="2" class="text-center">% Ca</th>
										<th colspan="2" class="text-center">Selesai Pengujian</th>
										<th rowspan="2" class="text-center">Ket</th>
								</tr>
								<tr>
										<th class="text-center">Tanggal</th>
										<th class="text-center">Pukul</th>
										<th class="text-center">Tgl</th>
										<th class="text-center">Pukul</th>
								</tr>
							</tr>
                		</thead>
	                	<tbody id="tabel_uji">
	                    <?php $z=1; foreach ($data as $r) { ?>
                     		<tr id="t2_<?php echo $r['id_sampel']; ?>" class="">
                        		<td class="text-center"><?php echo $this->uri->segment(5) + $z++ ?></td>
								<td>
									<div style="width:220px;" class="input-group">
										<input style="width:50px;" class="mulai_uji" onchange="klik_tgl('mulai_uji'+<?php echo $r['id_sampel'];?>);" type="date" id="mulai_uji<?php echo $r['id_sampel'];?>" name="mulai_uji[]" value="<?php echo explode(' ', $r['mulai_uji'])[0]; ?>">
										<input id="c_mulai_uji<?php echo $r['id_sampel'];?>" value="<?php echo toi_date03($r['mulai_uji']); ?>" readonly>
									</div>
								</td>
								<td style="width:110px;" class="input-group">
									<select name="jam_mulai[]">
										<?php for($x=0; $x<24; $x++) {  $x = strlen($x) < 2 ? '0'.$x : $x; ?>
											<option <?php echo date('H') == $x ? 'selected' : ''; ?> value="<?php echo $x;?>"><?php echo $x; ?></option>
										<?php } ?>
									</select> :
									<select name="menit_mulai[]">
										<?php for($x=0; $x<60; $x++) {  $x = strlen($x) < 2 ? '0'.$x : $x; ?>
											<option <?php echo date('i') == $x ? 'selected' : ''; ?> value="<?php echo $x;?>"><?php echo $x; ?></option>
										<?php } ?>
									</select>
								</td>
                        		<td>
                        			<input type="hidden" name="nomor_batch[]" value="<?php echo $r['nomor_batch'] ?>">
									<input type="hidden" name="kontrol_sampel2[]" value="<?php echo $r['kontrol_sampel'] ?>">
									<?php
									if(is_null($r["id_sampel"])){ ?>
										<input class="" type="text" onkeyup="persenCa(<?php echo $r['kontrol_sampel'] ?>)" id="kontrol_sampel" name="kontrol_sampel[]" placeholder="Kontrol Sampel" value="<?php echo $r["kontrol_sampel"]; ?>">
									<?php } else{?>
										<input type="hidden" name="kontrol_sampel[]" value="<?php echo ($r['kontrol_sampel'] ? $r['kontrol_sampel'] : 0) ?>">
										<?php echo nomor_id($r["id_sampel"]);
									} 
									?>
								</td>
                        		<td>
									<input class="winput" type="text" id="kode_krusibel<?php echo $r['id_sampel'] ?>" name="kode_krusibel[]" value="<?php echo $r["kode_krusibel"]; ?>">
								</td>
                        		<td>
									<input class="winput" type="text" onkeyup="persenCA(<?php echo $r['id_sampel'] ?>)" id="berat_sampel<?php echo $r['id_sampel'] ?>" name="berat_sampel[]" value="<?php echo $r["berat_sampel"]; ?>">
								</td>
								<td>
									<input class="winput" type="text" onkeyup="persenCA(<?php echo $r['id_sampel'] ?>)" id="volume_titrasi_sampel<?php echo $r['id_sampel'] ?>" name="volume_titrasi_sampel[]" value="<?php echo $r["volume_titrasi_sampel"]; ?>">
								</td>
		                        <td>
									<input class="winput auto_1" type="text" onkeyup="persenCA(<?php echo $r['id_sampel'] ?>)" id="volume_titrasi_blanko<?php echo $r['id_sampel'] ?>" name="volume_titrasi_blanko[]" value="<?php echo $r["volume_titrasi_blanko"]; ?>">
								</td>
		                        <td>
									<input class="winput auto_2" type="text" onkeyup="persenCA(<?php echo $r['id_sampel'] ?>)" id="normalitas_kmno4<?php echo $r['id_sampel'] ?>" name="normalitas_kmno4[]" value="<?php echo $r["normalitas_kmno4"]; ?>">
								</td>
		                        <td>
									<input class="winput auto_3" type="text" onkeyup="persenCA(<?php echo $r['id_sampel'] ?>)" id="volume_larutan_induk<?php echo $r['id_sampel'] ?>" name="volume_larutan_induk[]" value="<?php echo $r["volume_larutan_induk"]; ?>">
								</td>
                        		<td>
									<input class="winput auto_4" type="text" onkeyup="persenCA(<?php echo $r['id_sampel'] ?>)" id="volume_alikuot<?php echo $r['id_sampel'] ?>" name="volume_alikuot[]" value="<?php echo $r["volume_alikuot"]; ?>">
								</td>
                        		<td>
									<input class="winput auto_5" type="text" onkeyup="persenCA(<?php echo $r['id_sampel'] ?>)" id="nomor_atom_ca<?php echo $r['id_sampel'] ?>" name="nomor_atom_ca[]" value="<?php echo $r["nomor_atom_ca"]; ?>">
								</td>
                        		<td>
									<input class="winput" type="text" id="kadar_ca<?php echo $r['id_sampel'] ?>" name="kadar_ca[]" value="<?php echo $r["kadar_ca"]; ?>">
									<?php if(!is_null($r['id_sampel'])) { ?>
										<input type="hidden" name='c_range[]'>
									<?php } else { ?>
										<span style='font-size:10px;' class='text-center'><b>KS:</b></span>
										<input type='text' name='c_range[]' value="<?php echo $r['c_range'] ?>">
									<?php } ?>
								</td>
								<td>
									<div style="width:220px;" class="input-group">
										<input style="width:50px;" class="selesai_uji" onchange="klik_tgl('selesai_uji'+<?php echo $r['id_sampel'];?>);" type="date" id="selesai_uji<?php echo $r['id_sampel'];?>" name="selesai_uji[]" value="<?php echo explode(' ', $r['selesai_uji'])[0]; ?>">
										<input id="c_selesai_uji<?php echo $r['id_sampel'];?>" value="<?php echo toi_date03($r['selesai_uji']); ?>" readonly>
									</div>
								</td>
								<td style="width:110px;" class="input-group">
									<select name="jam_selesai[]">
										<?php for($x=0; $x<24; $x++) {  $x = strlen($x) < 2 ? '0'.$x : $x; ?>
											<option <?php echo date('H') == $x ? 'selected' : ''; ?> value="<?php echo $x;?>"><?php echo $x; ?></option>
										<?php } ?>
									</select> :
									<select name="menit_selesai[]">
										<?php for($x=0; $x<60; $x++) {  $x = strlen($x) < 2 ? '0'.$x : $x; ?>
											<option <?php echo date('i') == $x ? 'selected' : ''; ?> value="<?php echo $x;?>"><?php echo $x; ?></option>
										<?php } ?>
									</select>
								</td>
								<td>
									<input class="winput" type="text" id="keterangan<?php echo $r['id_sampel'] ?>" name="keterangan[]" value="<?php echo $r["keterangan"]; ?>">
								</td>

								<!-- ===ID SAMPEL (SYARAT MUTLAK HARUS ADA)=== -->
								<input type="hidden" id="id_sampel<?php echo $r['id_sampel'] ?>" name="id_sampel[]" value="<?php echo $r["id_sampel"]; ?>">
								<?php if(!empty($method)) { ?>
									<?php if(in_array($method, array('batch_sampel', 'kontrol_sampel'))) { ?>
										<input type="hidden" name="method_batch[]" value="<?php echo $method; ?>">
									<?php } ?>
									<?php if(in_array($method, array('kontrol_sampel'))) { ?>
										<input type="hidden" name="nomor_batch[]" value="<?php echo $nomor_batch; ?>">
									<?php } ?>
								<?php } ?>
								<!-- ========================================= -->
                      		</tr>
                    	<?php } ?>
                		</tbody>
                		<tfoot>

                		</tfoot>
                	</table>

            	</div> <!-- end panel-body -->
			</form> <!-- end form -->
			<div class="panel panel-footer">
			
			</div> <!-- end panel-footer -->

        </div><!-- end panel -->
    </div>
</div>

<div class="modal fade" id="modal-alert" style="display: none;" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                <div class="alert alert-danger m-b-0">
                    <p><b>Peringatan:</b> Apakah anda yakin akan membatalkan penyiapan sampel ini?</p>
                </div>
            </div>
            <div class="modal-footer">
                <a href="<?php //echo site_url('sampel/pengajuan/siapkan_sampel?id_uji='.$id_uji.'&status=0') ?>" class="btn width-100 btn-sm btn-primary">Ya</a>
                <a href="javascript:;" class="btn width-100 btn-sm btn-danger" data-dismiss="modal">Tidak</a>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
	function persenCA(x){
		var a1 = $('.auto_1').val();
		var a2 = $('.auto_2').val();
		var a3 = $('.auto_3').val();
		var a4 = $('.auto_4').val();
		var a5 = $('.auto_5').val();

		$('.auto_1').val(a1);
		$('.auto_2').val(a2);
		$('.auto_3').val(a3);
		$('.auto_4').val(a4);
		$('.auto_5').val(a5);


		var f = $('#berat_sampel'+x).val();
		var g = $('#volume_titrasi_sampel'+x).val();
		var i = $('#volume_titrasi_blanko'+x).val();
		var k = $('#normalitas_kmno4'+x).val();
		var l = $('#volume_larutan_induk'+x).val();
		var m = $('#volume_alikuot'+x).val();
		var n = $('#nomor_atom_ca'+x).val();

		if(f === '') f = 0;
		if(g === '') g = 0;
		if(i === '') i = 0;
		if(k === '') k = 0;
		if(l === '') l = 0;
		if(m === '') m = 0;
		if(n === '') n = 0;

		p = (parseFloat(g)-parseFloat(i))*parseFloat(k)*parseFloat(l)*parseFloat(n)*100;
		u = parseFloat(m)*parseFloat(f);

		var j = parseFloat(p)/parseFloat(u);

		if(isNaN(parseFloat(j))){
				$('#kadar_ca'+x).val(0);
		}else{
			j = j.toFixed(2);
			$('#kadar_ca'+x).val(j);
		}
	}

	function removeTd() {
		$('.mulai_uji').removeAttr('td');
	}

	function save_analis(){
		$('#form_edit').submit();
	}

	var c = 10000000;

	function add_kontrol_sampel(){
		var t1 = $("#t1").val();
		var t3 = $("#t3").val();
		var letak_id = $("#letak_id").val();
		var t = 0;

		var cek = "\n\
		<tr  class=''>\n\
    		<td class='text-center'></td>\n\
			<td>\n\
				<div style='width:220px;' class='input-group'>\n\
					<input style='width:50px;' class='mulai_uji' onchange=klik_tgl('mulai_uji"+c+"') type='date' id='mulai_uji"+c+"' name='mulai_uji[]' value='<?php echo explode(' ', $r['mulai_uji'])[0]; ?>'>\n\
					<input id='c_mulai_uji"+c+"' value='<?php echo toi_date03($r['mulai_uji']); ?>' readonly>\n\
				</div>\n\
			</td>\n\
			<td style='width:110px;' class='input-group'>\n\
				<select name='jam_mulai[]'>\n\
					<?php for($x=0; $x<24; $x++) {  $x = strlen($x) < 2 ? '0'.$x : $x; ?>
						<option <?php echo date('H') == $x ? 'selected' : ''; ?> value='<?php echo $x;?>'><?php echo $x; ?></option>\n\
					<?php } ?>
				</select> :\n\
				<select name='menit_mulai[]'>\n\
					<?php for($x=0; $x<60; $x++) {  $x = strlen($x) < 2 ? '0'.$x : $x; ?>
						<option <?php echo date('i') == $x ? 'selected' : ''; ?> value='<?php echo $x;?>'><?php echo $x; ?></option>\n\
					<?php } ?>
				</select>\n\
			</td>\n\
    		<td>\n\
				<input type='hidden' name='nomor_batch[]' value='<?php echo $data[0]['nomor_batch'] ?>'>\n\
				<input type='hidden' name='kontrol_sampel2[]'>\n\
				<input class='winput' type='text' id='kontrol_sampel' name='kontrol_sampel[]' placeholder='Kontrol Sampel'>\n\
			</td>\n\
    		<td>\n\
				<input class='winput' type='text' id='kode_krusibel"+c+"' name='kode_krusibel[]'>\n\
			</td>\n\
    		<td>\n\
				<input class='winput' type='text' onkeyup='persenCA("+c+")' id='berat_sampel"+c+"' name='berat_sampel[]'>\n\
			</td>\n\
			<td>\n\
				<input class='winput' type='text' onkeyup='persenCA("+c+")' id='volume_titrasi_sampel"+c+"' name='volume_titrasi_sampel[]'>\n\
			</td>\n\
            <td>\n\
				<input class='winput auto_1' type='text' onkeyup='persenCA("+c+")' id='volume_titrasi_blanko"+c+"' name='volume_titrasi_blanko[]'>\n\
			</td>\n\
            <td>\n\
				<input class='winput auto_2' type='text' onkeyup='persenCA("+c+")' id='normalitas_kmno4"+c+"' name='normalitas_kmno4[]'>\n\
			</td>\n\
            <td>\n\
				<input class='winput auto_3' type='text' onkeyup='persenCA("+c+")' id='volume_larutan_induk"+c+"' name='volume_larutan_induk[]'>\n\
			</td>\n\
    		<td>\n\
				<input class='winput auto_4' type='text' onkeyup='persenCA("+c+")' id='volume_alikuot"+c+"' name='volume_alikuot[]'>\n\
			</td>\n\
    		<td>\n\
				<input class='winput auto_5' type='text' onkeyup='persenCA("+c+")' id='nomor_atom_ca"+c+"' name='nomor_atom_ca[]'>\n\
			</td>\n\
    		<td>\n\
				<input class='winput' type='text' id='kadar_ca"+c+"' name='kadar_ca[]'>\n\
				<span style='font-size:10px;' class='text-center'><b>KS:</b></span>\n\
				<input class='' type='text' id='c_range"+c+"' name='c_range[]'>\n\
			</td>\n\
			<td>\n\
				<div style='width:220px;' class='input-group'>\n\
					<input style='width:50px;' class='selesai_uji' onchange=klik_tgl('selesai_uji"+c+"') type='date' id='selesai_uji"+c+"' name='selesai_uji[]' value='<?php echo explode(' ', $r['selesai_uji'])[0]; ?>'>\n\
					<input id='c_selesai_uji"+c+"' value='<?php echo toi_date03($r['selesai_uji']); ?>' readonly>\n\
				</div>\n\
			</td>\n\
			<td style='width:110px;' class='input-group'>\n\
				<select name='jam_selesai[]'>\n\
					<?php for($x=0; $x<24; $x++) {  $x = strlen($x) < 2 ? '0'.$x : $x; ?>
						<option <?php echo date('H') == $x ? 'selected' : ''; ?> value='<?php echo $x;?>'><?php echo $x; ?></option>\n\
					<?php } ?>
				</select> :\n\
				<select name='menit_selesai[]'>\n\
					<?php for($x=0; $x<60; $x++) {  $x = strlen($x) < 2 ? '0'.$x : $x; ?>
						<option <?php echo date('i') == $x ? 'selected' : ''; ?> value='<?php echo $x;?>'><?php echo $x; ?></option>\n\
					<?php } ?>
				</select>\n\
			</td>\n\
			<td>\n\
				<input class='winput' type='text' id='keterangan"+c+"' name='keterangan[]'>\n\
			</td>\n\
\n\
			<!-- ===ID SAMPEL (SYARAT MUTLAK HARUS ADA)=== -->\n\
			<input type='hidden' id='id_sampel"+c+"' name='id_sampel[]'>\n\
			<?php if(!empty($method)) { ?>
				<?php if(in_array($method, array('batch_sampel', 'kontrol_sampel'))) { ?>
					<input type='hidden' name='method_batch[]' value='<?php echo $method; ?>'>\n\
				<?php } ?>
				<?php if(in_array($method, array('kontrol_sampel'))) { ?>
					<input type='hidden' name='nomor_batch[]' value='<?php echo $nomor_batch; ?>'>\n\
				<?php } ?>
			<?php } ?>
			<!-- ========================================= -->\n\
  		</tr>";

		if ($("#t1").prop("checked")) {
			t = t1;
			$("#t2_"+letak_id).before(cek);
		}
		if ($("#t3").prop("checked")) {
			t = t3;
			$("#t2_"+letak_id).after(cek);
		}

		var tr = $('#tabel_uji tr');
        for (var i = 0; i < tr.length; i++) {
            var td = $(tr[i]).find('td').first().text(i+1);
        }

		c++;
	}

</script>
