<?php if(!empty($is_grafik)) : ?>
<script src="<?= base_url();?>/assets/plugins/chart-js/Chart.min.js"></script>
<script src="<?= base_url();?>/assets/plugins/chart-js/chartjs-plugin-datalabels.min.js"></script>
<script src="<?= base_url();?>/assets/plugins/gritter/js/jquery.gritter.js"></script>
<script src="<?= base_url();?>/assets/js/page-index-v3.demo.min.js"></script>

<div class="panel panel-lime">
<div class="panel no-rounded-corner bg-default text-white wrapper m-b-0">
<div class="chartjs-size-monitor" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px; overflow: hidden; pointer-events: none; visibility: hidden; z-index: -1;">
<div class="chartjs-size-monitor-expand" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;">
<div style="position:absolute;width:1000000px;height:1000000px;left:0;top:0">
</div></div><div class="chartjs-size-monitor-shrink" style="position:absolute;left:0;top:0;right:0;bottom:0;overflow:hidden;pointer-events:none;visibility:hidden;z-index:-1;"><div style="position:absolute;width:200%;height:200%;left:0; top:0"></div></div></div>
                            <!-- end btn-group -->
                            <!-- begin title -->
                            <h4 class=" m-t-0 m-b-10">
                                <i class="fa fa-snowflake-o text-success-light"></i> Persamaan Kurva Kalibrasi/Standar
                                <small class="text-muted m-l-5"><?= date('Y'); ?></small>
                            </h4>
                            <!-- end title -->
                            <!-- begin chart -->
                        <canvas id="line-chart" style="display: block; width: 500px; height: 166px;" height="166" width="500"></canvas>
                        </div>
                        <div class="col-sm-12" style="margin-left: 40px;">
                          <h4>Rumus Persamaan</h4>
                          <label> y = <?= $grafik[0]['slope'].'x '.(strpos('|'.$grafik[0]['intercept'], '-')  == false ? '+' : '').$grafik[0]['intercept']; ?></label>
                          <br>
                          <label> r = <?= round($grafik[0]['d_range'], 4);?></label>
                        </div>
</div>
<?php endif; ?>
<?php $this->load->view('uji_analis/header_input01'); ?>
					
	                <table class="table table-bordered table-hover table-striped table-td-valign-middle">
						<thead>
							<tr>
						  		<th rowspan="2" class="text-center">No.</th>
						  		<th rowspan="2" class="text-center">Nomor Sampel</th>
								<th rowspan="2" class="text-center">Kode Krusibel Porselen</th>
								<th rowspan="2" class="text-center">Berat Sampel (gram)</th>
								<th rowspan="2" class="text-center">V Indukan (mL)</th>
								<th rowspan="2" class="text-center">V Labu (mL)</th>
								<th rowspan="2" class="text-center">V Pipet (mL)</th>
								<th rowspan="2" class="text-center">Abs (Y)</th>
								<th rowspan="2" class="text-center">Intercept (a)</th>
								<th rowspan="2" class="text-center">Slope (b)</th>
								<th rowspan="2" class="text-center">Konsentrasi Sampel (ppm)</th>
								<th rowspan="2" class="text-center">% Ca</th>
						  	</tr>
	                	</thead>
	                	<tbody id="tabel_uji">

	                    <?php $z=1; foreach ($data as $r) { ?>
	                      	<tr id="t2_<?php echo $r['id']; ?>" class="">
		                        <td id="nama_element_induk" class="text-center"><?php echo $this->uri->segment(5) + $z++ ?></td>
														
								<td id="td1_<?php echo $r['id'];?>">
									<input type="hidden" name="nomor_batch[]" value="<?php echo $nomor_batch; ?>">
									<?php if(!empty($is_edit)) : ?>
										<input type='hidden' name='id[]' value="<?= $r['id']; ?>">
									<?php endif; ?>
									<?php if($r["id_sampel"] == 0 ) : ?>
										<input class="wth-form text-center" id="kontrol_sampel" name="kontrol_sampel[]" placeholder="Kontrol Sampel" value="<?= val_ks($r["kontrol_sampel"]); ?>">
									<?php else : ?>
										<input type="hidden" name="kontrol_sampel[]" value="<?= ($r['kontrol_sampel'] ? $r['kontrol_sampel'] : 0) ?>">
										<?= $r["nomor_sampel"]; ?>
									<?php endif; ?>
								</td>
								<td>
									<input class="winput wth-form" type="text" onkeyup="persenCa(<?= $r['id']; ?>)" id="kode_krusibel_porselen<?= $r['id']; ?>" name="kode_krusibel_porselen[]" value="<?= ($r["kode_krusibel_porselen"]) ? $r["kode_krusibel_porselen"] : $r["abu_kb"]; ?>">
								</td>
								<td>
									<input class="winput wth-form" type="text" onkeyup="persenCa(<?= $r['id']; ?>)" id="berat_sampel<?= $r['id']; ?>" name="berat_sampel[]" value="<?= ($r["berat_sampel"]) ? $r["berat_sampel"] : $r["abu_bs"]; ?>">
								</td>
								<td>
									<input class="winput wth-form" type="text" onkeyup="persenCa(<?= $r['id']; ?>)" id="v_indukan<?= $r['id']; ?>" name="v_indukan[]" value="<?php echo ($r["v_indukan"]) ? $r["v_indukan"] : 250; ?>">
								</td>
								<td>
									<input class="winput wth-form" type="text" onkeyup="persenCa(<?= $r['id']; ?>)" id="v_labu<?= $r['id']; ?>" name="v_labu[]" value="<?php echo ($r["v_labu"]) ? $r["v_labu"] : 25; ?>">
								</td>
								<td>
									<input class="winput wth-form" type="text" onkeyup="persenCa(<?= $r['id']; ?>)" id="v_pipet<?= $r['id']; ?>" name="v_pipet[]" value="<?php echo $r["v_pipet"]; ?>">
								</td>
								<td>
									<input class="winput wth-form" type="text" onkeyup="persenCa(<?= $r['id']; ?>)" id="abs<?= $r['id']; ?>" name="abs[]" value="<?php echo $r["abs"]; ?>">
								</td>
								<td>
									<input class="winput wth-form" type="text" onkeyup="persenCa(<?= $r['id']; ?>)" id="intercept<?= $r['id']; ?>" name="intercept[]" value="<?php echo (!is_null($data[0]["intercept"])) ? $data[0]["intercept"] : $intercept; ?>">
								</td>
								<td>
									<input class="winput wth-form" type="text" onkeyup="persenCa(<?= $r['id']; ?>)" id="slope<?= $r['id']; ?>" name="slope[]" value="<?php echo (!is_null($data[0]["slope"])) ? $data[0]["slope"] : $slope; ?>">
								</td>
								<td>
									<input class="winput wth-form" type="text" onkeyup="persenCa(<?= $r['id']; ?>)" id="konsentrasi_sampel<?= $r['id']; ?>" name="konsentrasi_sampel[]" value="<?php echo $r["konsentrasi_sampel"]; ?>">
								</td>
								<td>
									<input class="wth-form" type="text" id="kadar_ca<?= $r['id']; ?>" name="kadar_ca[]" value="<?php echo $r["kadar_ca"]; ?>">
									
									<?php if(!empty($is_edit) || $r['id_sampel'] == 0) : ?>
										<br><b>KS:</b><br>
										<input type='text' id="c_range<?= $r['id']; ?>" name='c_range[]' value="<?= $r['c_range']; ?>">
									<?php elseif($r['id_sampel'] != 0) : ?>
										<input type="hidden" name='c_range[]'>
									<?php else : ?>
										<input type="hidden" name='c_range[]'>
									<?php endif; ?>
								</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"]; ?>">
								<!-- ========================================= -->

	                        </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> <!-- end col-12 -->
</div> <!-- end row -->

<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="javascript:;" 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>
    function persenCa(x){
		var f = $('#berat_sampel'+x).val();
		var g = $('#v_indukan'+x).val();
		var h = $('#v_labu'+x).val();
		var i = $('#v_pipet'+x).val();
		var j = $('#abs'+x).val();
		var k = $('#intercept'+x).val();
		var l = $('#slope'+x).val();
		var m = 0; 

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

		m = (parseFloat(j)-parseFloat(k))/parseFloat(l);
		var o = (parseFloat(g)*parseFloat(h)*parseFloat(m))/(parseFloat(f)*parseFloat(i)*10000);
		console.log(o);

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

		if(isNaN(parseFloat(m))){
		  	$('#konsentrasi_sampel'+x).val(0);
		}else{
		  	m = m.toFixed(4);
		  	$('#konsentrasi_sampel'+x).val(m);
		}
    }

    var no_urut = 1;
    $('#nama_element_induk').find('tr').first().text(no_urut++);

    var c = 10000000;

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

		var cek = "<tr>\n\
                        <td id='nama_element_induk' class='text-center'>"+"</td> \n\
						\n\
						<td>\n\
							<?php if(!empty($is_edit)) : ?>\n\
								<input type=hidden name='id[]' value='0'>\n\
							<?php endif; ?>\n\
							<input type='hidden' name='nomor_batch[]' value='<?= $nomor_batch; ?>'>\n\
							<input class='wth-form' type='text' id='kontrol_sampel' name='kontrol_sampel[]' placeholder='Kontrol Sampel'>\n\
						</td>\n\
						<td>\n\
							<input class='winput wth-form' type='text' onkeyup='persenCa("+c+")' id='kode_krusibel_porselen"+c+"' name='kode_krusibel_porselen[]'>\n\
						</td>\n\
						<td>\n\
							<input class='winput wth-form' type='text' onkeyup='persenCa("+c+")' id='berat_sampel"+c+"' name='berat_sampel[]'>\n\
						</td>\n\
						<td>\n\
							<input class='winput wth-form' type='text' onkeyup='persenCa("+c+")' id='v_indukan"+c+"' name='v_indukan[]' value='250'>\n\
						</td>\n\
						<td>\n\
							<input class='winput wth-form' type='text' onkeyup='persenCa("+c+")' id='v_labu"+c+"' name='v_labu[]' value='25'>\n\
						</td>\n\
						<td>\n\
							<input class='winput wth-form' type='text' onkeyup='persenCa("+c+")' id='v_pipet"+c+"' name='v_pipet[]'>\n\
						</td>\n\
						<td>\n\
							<input class='winput wth-form' type='text' onkeyup='persenCa("+c+")' id='abs"+c+"' name='abs[]'>\n\
						</td>\n\
						<td>\n\
							<input class='winput wth-form' type='text' onkeyup='persenCa("+c+")' id='intercept"+c+"' name='intercept[]' value='<?php echo $intercept; ?>'>\n\
						</td>\n\
						<td>\n\
							<input class='winput wth-form' type='text' onkeyup='persenCa("+c+")' id='slope"+c+"' name='slope[]' value='<?php echo $slope; ?>'>\n\
						</td>\n\
						<td>\n\
							<input class='winput wth-form' type='text' onkeyup='persenCa("+c+")' id='konsentrasi_sampel"+c+"' name='konsentrasi_sampel[]'>\n\
						</td>\n\
						<td>\n\
							<input class='wth-form' 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='wth-form' type='text' id='c_range"+c+"' name='c_range[]'>\n\
						</td>\n\
						\n\
\n\
						<!-- ===ID SAMPEL (SYARAT MUTLAK HARUS ADA)=== -->\n\
							<input type='hidden' id='id_sampel"+c+"' name='id_sampel[]'>\n\
						<!-- ========================================= -->\n\
\n\
                      </tr>";

		if ($("#t1").prop("checked")) {
			t = t1;
			//$("#t2_"+letak_id).before("<tr>"+$("#t2_"+letak_id).html()+"</tr>");
			$("#t2_"+letak_id).before(cek);
		}
		if ($("#t3").prop("checked")) {
			t = t3;
			//$("#t2_"+letak_id).after("<tr>"+$("#t2_"+letak_id).html()+"</tr>");
			//$("#t2_"+letak_id).html('');
			$("#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++;

		//$("#"+t+letak_id).append($("#t2_0").html());
		//alert(t);		
	}

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

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

</script>

<?php if(!empty($is_grafik)){ ?>
<?php $a=array(); foreach($grafik as $r) {$a[] = $r['deret_std_ca'];}?>
<?php $b=array(); foreach($grafik as $r) {$b[] = $r['abs'];}?>
<?php 
  $n = '';
  for($i=0;$i<=(int)end($a);$i+=2){
    if(in_array($i,$a)){
      for($c=0;$c<count($a);$c++){
        if($i == $a[$c]){
          $n .= $b[$c].',';
        }
      }
    }else {
      $n .= 'null,';
    }
  }
?>

<script>
new Chart(document.getElementById("line-chart"), {
  type: 'line',
  data: {
    labels: [<?php for($i=0; $i<=(int)end($a);$i+=2) {echo $i.',';}?>],
    datasets: [{
        data: [<?= $n; ?>],
        label: '',//"Data Uji Sampel",
		borderColor: "#00e6e6",
        fill: true
      }
    ]
  },
  options: {
    spanGaps: true,
    elements: {
      line: {
          tension: 0
      }
    },
  	legend: { display: false },
    title: {
      display: false,
    //   text: 'World population per region (in millions)'
    },
    plugins: {
      datalabels: {
          anchor :'end',
          align :'top',
      }
    },
    scales: {
        xAxes: [{
            ticks: {
                autoSkip: false,
                //maxRotation: 90
            },
					display: true,
					scaleLabel: {
							display: true,
							labelString: ''//'Bulan'
					}
                }],
                yAxes: [{
						display: true,
						scaleLabel: {
							display: true,
							labelString: ''//'Jumlah Sampel'
						},
						ticks: {
							min: 0.000,
							max: 1.000,

							// forces step size to be 5 units
							stepSize: 0.200
						}
					}]
      }
  }
});
</script>
<?php } ?>
