<!-- <script src="<?php echo base_url();?>/assets/ckeditor.js"></script> -->
<script src="https://cdn.ckeditor.com/ckeditor5/12.3.1/classic/ckeditor.js"></script>

<style type="text/css">
	.width-td{width: 150px;}
	.hidden-h-td{height: 0px;}
</style>

<?php
	$p_array = [1=>'air','abu','protein_kasar','lemak_kasar','serat_kasar','kalsium','fosfor','betn','tdn','ur','af','andf','mbm'];
	$s_array = [1=>'Air','Abu','Protein Kasar','Lemak Kasar','Serat Kasar','Kalsium','Fosfor','BETN','TDN','Urea','Aflatoksin Total','aNDF','MBM'];
?>

<div class="row">
	<!-- begin col-8 -->
	<div class="col-sm-12">
		<!-- begin panel -->
		<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>
				</div>
				<?php if(in_array($id_group, ['1','5']) && !empty($is_penyelia)) : ?>
					<div class="btn-group" role="group" aria-label="Group Button Penyiapan Sampel">
						<a href="<?= site_url('disposisi') ?>" class="btn btn-primary m-b-5">
							<span class="fa fa-arrow-left"></span> Kembali
						</a>
						<a href="<?= site_url('verifikasi_penyelia/'.$id_uji.'/'.$id_status); ?>" class="btn btn-warning m-b-5 active">Verifikasi Penyelia</a>
						<a href="<?= site_url('tdn/'.$id_uji.'/'.$id_status); ?>" class="btn btn-warning m-b-5">TDN</a>
						<a href="<?= site_url('kadar_air_total/'.$id_uji.'/'.$id_status); ?>" class="btn btn-warning m-b-5">Kadar Air Total</a>
					</div>
				<?php else : ?>
					<a href='<?= site_url('verfikasi_penyelia/'.$id_uji.'/'.$id_status); ?>' class="btn btn-primary m-b-5">
						<span class="fa fa-arrow-left"></span> Kembali
					</a>
				<?php endif; ?>
				<hr>
			</div>
			<div class="panel-body table-responsive">
				<table class="table table-bordered table-hover table-striped table-td-valign-middle">
					<thead>
						<tr class="grey">
							<th rowspan="2" class="text-center">No.</th>
							<?php if(in_array($id_group, ['1','5']) && $id_status == 7) : ?>
								<th rowspan="2" class="text-center">Aksi</th>
							<?php endif; ?>
							<th rowspan="2" class="text-center">Nomor Sampel</th>
							<th colspan="<?= count($p_array); ?>" class="text-center">Hasil Pengujian (%)</th>
						</tr>
						<tr class="grey">
							<?php foreach($s_array as $s) : ?>
								<th class="text-center"><?= str_replace(' ','&nbsp;', $s); ?></th>
							<?php endforeach; ?>
						</tr>
					</thead>
					<tbody>
						<form method="post" action="<?php echo site_url("sampel/pengajuan/save_uji_sampel") ?>">
						<?php $z = 0; foreach ($data as $r) : ?>
							<tr> <?php $z++; ?>
								<td class="text-center"><?php echo $z; ?></td>
								<?php if(in_array($id_group, array('1','5')) && $id_status == 7) : ?>
									<td class="text-center">
									<?php echo aksi_button('start'); ?>
									<?php if(!empty($is_penyelia)) : ?>
										<li>
											<a href="<?php echo site_url('mulai_uji_analis/'.$id_uji.'/'.$id_status) ?>">Edit</a>
										</li>
									<?php endif; ?>
									<?php if(!empty($is_uji_analis)) : ?>
										<li>
											<button class="btn btn-link" type="submit">Simpan</button>
										</li>
									<?php endif; ?>
									<?php echo aksi_button('end') ?>
									</td>
								<?php endif; ?>
								<td class="text-center">
									<?php echo $r['nomor_sampel'];?>
								</td>

								<input type="hidden" name="id_uji" value="<?php echo $id_uji ?>">
								<input type="hidden" name="status" value="<?php echo $id_status ?>">
								<input type="hidden" name="id_sampel[]" value="<?php echo $r['id_sampel'] ?>">

								<?php if(!empty($is_penyelia) || !empty($is_lhu)) : ?>
									<?php foreach($p_array as $p) : ?>
										<th class="text-center"><?= $r['l_'.$p]; ?></th>
									<?php endforeach; ?>
								<?php endif; ?>

								<?php if(!empty($is_uji_analis)) : ?>
									<?php foreach($p_array as $p) : ?>
										<?php 
											if(in_array($r['l_kalsium'], [null, ''])){
												$r['l_kalsium'] = $r['l_ca'];
											}
										?>
										<td><input value="<?= $r['l_'.$p]; ?>" id="uji_<?= $p.$r['nomor_sampel']; ?>" class="width-td" name="<?= $p; ?>[]"></td>
									<?php endforeach; ?>
								<?php endif; ?>
							</tr>
						<?php endforeach; ?>
						<?php if(!empty($is_penyelia) || !empty($is_lhu)) : ?>
							<tr>
								<th rowspan="5"></th>
								<th colspan="<?php if(in_array($id_group, ['1','5']) && $id_status ==='7') { echo 2;} ?>" class="text-center">Metode</th>
								<td><?php echo $this->m_master->getMetodeUji(1)[0]['detail']; ?> </td>
								<td><?php echo $this->m_master->getMetodeUji(2)[0]['detail']; ?> </td>
								<td><?php echo $this->m_master->getMetodeUji(3)[0]['detail']; ?> </td>
								<td><?php echo $this->m_master->getMetodeUji(5)[0]['detail']; ?> </td>
								<td><?php echo $this->m_master->getMetodeUji(6)[0]['detail']; ?> </td>
								<td><?php echo $this->m_master->getMetodeUji(7)[0]['detail']; ?> </td>
								<td><?php echo $this->m_master->getMetodeUji(9)[0]['detail']; ?> </td>

								<td><?php echo $this->m_master->getMetodeUji(10)[0]['detail']; ?></td>
                                <td></td>
								<td><?php echo $this->m_master->getMetodeUji(11)[0]['detail']; ?></td>
								<td><?php echo $this->m_master->getMetodeUji(12)[0]['detail']; ?></td>
								<td><?php echo $this->m_master->getMetodeUji(13)[0]['detail']; ?></td>
								<td><?php echo $this->m_master->getMetodeUji(14)[0]['detail']; ?></td>
							</tr>
						<?php endif; ?>
						<?php if(!empty($is_uji_analis)) : ?>
							<tr>
								<th rowspan="6"></th>
								<th colspan="<?php if(in_array($id_group, ['1','5']) && $id_status ==='7') { echo 2;} ?>" class="text-center">Metode</th>
								<td><textarea name='metode_uji'><?php echo $this->m_master->getMetodeUji(1)[0]['detail']; ?></textarea> </td>
								<td><textarea name='metode_uji'><?php echo $this->m_master->getMetodeUji(2)[0]['detail']; ?></textarea> </td>
								<td><textarea name='metode_uji'><?php echo $this->m_master->getMetodeUji(3)[0]['detail']; ?></textarea> </td>
								<td><textarea name='metode_uji'><?php echo $this->m_master->getMetodeUji(5)[0]['detail']; ?></textarea> </td>
								<td><textarea name='metode_uji'><?php echo $this->m_master->getMetodeUji(6)[0]['detail']; ?></textarea> </td>
								<td><textarea name='metode_uji'><?php echo $this->m_master->getMetodeUji(7)[0]['detail']; ?></textarea> </td>
								<td><textarea name='metode_uji'><?php echo $this->m_master->getMetodeUji(9)[0]['detail']; ?></textarea> </td>
                                <td></td>
								<td><textarea name='metode_uji'><?php echo $this->m_master->getMetodeUji(10)[0]['detail']; ?></textarea></td>
								<td><textarea name='metode_uji'><?php echo $this->m_master->getMetodeUji(11)[0]['detail']; ?></textarea></td>
								<td><textarea name='metode_uji'><?php echo $this->m_master->getMetodeUji(12)[0]['detail']; ?></textarea></td>
								<td><textarea name='metode_uji'><?php echo $this->m_master->getMetodeUji(13)[0]['detail']; ?></textarea></td>
								<td><textarea name='metode_uji'><?php echo $this->m_master->getMetodeUji(14)[0]['detail']; ?></textarea></td>
							</tr>
						<?php endif; ?>
						<tr>
							<th class="text-center" colspan="<?php if(in_array($id_group, array('1','5')) && $id_status ==='7') { echo 2;} ?>">Tanggal&nbsp;Mulai&nbsp;Pengujian</th>
							<?php if(!empty($is_penyelia) || !empty($is_lhu)) : ?>
								<?php foreach($p_array as $p) : ?>
									<td class="text-center"><?= get_str_date2($data[0]['mulai_uji_'.$p]); ?></td>
								<?php endforeach; ?>
							<?php endif; ?>

							<?php if(!empty($is_uji_analis)) : ?>
								<?php $width= 160; ?>
								<?php foreach($p_array as $p) : ?>
									<td><?= toi_date_input('mulai_uji_'.$p, $data[0]['mulai_uji_'.$p], false, $width); ?></td>
								<?php endforeach; ?>
							<?php endif; ?>
						</tr>

						<tr>
							<th class="text-center" colspan="<?php if(in_array($id_group, array('1','5')) && $id_status ==='7') { echo 2;} ?>">Tanggal&nbsp;Selesai&nbsp;Pengujian</th>

							<?php if(!empty($is_penyelia) || !empty($is_lhu)) : ?>
								<?php foreach($p_array as $p) : ?>
									<td class="text-center"><?= get_str_date2($data[0]['selesai_uji_'.$p]); ?></td>
								<?php endforeach; ?>
							<?php endif; ?>

							<?php if(!empty($is_uji_analis)) : ?>
								<?php $width= 160; ?>
								<?php foreach($p_array as $p) : ?>
									<td><?= toi_date_input('selesai_uji_'.$p, $data[0]['selesai_uji_'.$p], false, $width); ?></td>
								<?php endforeach; ?>
							<?php endif; ?>
						</tr>
						<tr>
							<th colspan="<?php if(in_array($id_group, array('1','5')) && $id_status ==='7') { echo 2;} ?>">Catatan</th>
							<?php for($i =1; $i<=count($p_array); $i++) : ?>
								<td><?php echo $data[0]['ket_'.$p_array[$i]]; ?></td>
							<?php endfor; ?>
						</tr>
						
						<?php if(in_array($id_group, array('1', '2','5'))) : ?>
						<tr>
							<th colspan="<?php if(in_array($id_group, array('1','5')) && $id_status ==='7') { echo 2;} ?>">Keterangan</th>
							<td colspan="<?= count($p_array); ?>">
								<pre style="font-size:14px;"><?= str_replace('||', "\n", $data[0]['ket_lhu']); ?></pre>
							</td>
						</tr>
						<?php endif; ?>

						<tr>
							<th colspan="<?php if(in_array($id_group, array('1','5')) && $id_status ==='7') { echo 2;} ?>"></th>
							<?php for($i =1; $i<=count($p_array); $i++) : ?>
							<td class="text-center">
								<?php if(in_array($id_group, array('1','5')) && $id_status ==='7') : ?>
									<a href="#modal-alert2-<?php echo $p_array[$i] ?>" class="btn btn-danger" data-toggle="modal">Respon</a>
								<?php elseif(in_array($id_group, ['1','6']) && $id_status == 6 && !is_null($data[0]['ket_'.$p_array[$i]])) : ?>
									<!-- <a href="#?>" class="btn btn-danger">Uji Ulang</a> -->
                                    <button type="submit" class="btn btn-primary">Simpan</button>
								<?php endif; ?>
							</td>
						</td>
							<?php endfor; ?>
						</tr>
						<tr>
							<td class="" colspan="10" height="5px;"></td>
						</tr>

					</form>
				</tbody>
				<tfoot>

				</tfoot>
			</table>
		</div>
		<div class="panel-footer">
			<?php if($id_status == 6){ ?>
				<a href="<?= site_url('sampel/pengajuan/siapkan_sampel/'.$id_uji.'/7'); ?>" class="btn btn-primary btn-md">Selesai Analis</a>
			<?php } ?>
			<?php if($id_status == 7){ ?>
				<a href="#modal-alert3" class="btn btn-primary" data-toggle="modal">Verifikasi Analisis</a>
				<a href="<?= site_url('sampel/pengajuan/siapkan_sampel/'.$id_uji.'/6'); ?>" class="btn btn-danger btn-md">Kembalikan ke Analis</a>
			<?php } ?>
			<?php if($id_status == 8){ ?>
				<a href="<?= site_url('sampel/pengajuan/siapkan_sampel/'.$id_uji.'/9'); ?>" class="btn btn-primary btn-md">ACC LHU</a>
				<a href="<?= site_url('sampel/pengajuan/siapkan_sampel/'.$id_uji.'/7'); ?>" class="btn btn-warning btn-md">Kembalikan ke Penyelia</a>
			<?php } ?>
		</div>

	</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>

<?php foreach($p_array as $pkey => $pr) : ?> 
	<div class="modal fade" id="modal-alert2-<?php echo $pr; ?>" style="display: none;" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<form action="<?php echo site_url('sampel/pengajuan/ket_lhu'); ?>" method="get">
						<div class="modal-body">
							<div class="alert alert-info m-b-0">
								<b>Masukkan Alasan:</b>
								<input type="hidden" name="status" value="7">
								<input type="hidden" name="id_uji" value="<?php echo $id_uji;?>">
								<input type="hidden" name="nama_field" value="ket_<?php echo $pr;?>">
								<input type="hidden" name="id_sampel" value="<?php echo $data[0]['id_sampel']; ?>">
								<textarea class="form-control" name="ket"></textarea>
							</div>
						</div>
						<div class="modal-footer">
							<button type="submit" class="btn btn-info btn-sm">Simpan Alasan</button>
							<a href="javascript:;" class="btn width-100 btn-sm btn-danger" data-dismiss="modal">Batal</a>
						</div>
					</form>
				</div>
			</div>
	</div>
<?php endforeach; ?>

<div class="modal fade" id="modal-alert3" style="display: none;" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<form action="<?= site_url('sampel/pengajuan/siapkan_sampel/'.$id_uji.'/8'); ?>" method="post">
				<div class="modal-body">
					<div class="alert alert-info m-b-0">
						<b>Masukkan Keterangan:</b>
						<textarea style="height:100px;" class="form-control" id="editor1" name="ket_lhu"><?= str_replace("||", "\n", $data[0]['ket_lhu']); ?>
					</textarea>
					</div>
				</div>
				<div class="modal-footer">
					<button type="submit" class="btn btn-info btn-sm">Verifikasi Analisis</button>
					<a href="javascript:;" class="btn width-100 btn-sm btn-danger" data-dismiss="modal">Tutup</a>
				</div>
			</form>
		</div>
	</div>
</div>

<script>
	ClassicEditor
    .create( document.querySelector( '#editor' ) )
    .then( editor => {
        console.log( editor );
    } )
    .catch( error => {
        console.error( error );
    } );
</script>

<script>
	function runScript(e) {
		//See notes about 'which' and 'key'
		console.log(e.keyCode);
		
		var ket = $('#ket_lhu').val();
		if (e.keyCode == 13) {
			ket += '\\n';
			//var tb = document.getElementById("scriptBox");
			//eval(tb.value);
			//return false;
		}
		var ket = $('#ket_lhu').val(ket);
	}

	
</script>

