<?php
require_once ("kontrolDB.php");
require_once ("kriteria_nilai.php");

class training
{
	private $db_handle;
	private $kriteria_nilai;
	
    public function __construct()
    {
       $this->db_handle = new kontrolDB();
       $this->kriteria_nilai=new kriteria_nilai();
    }
	
    public function tampil()
    {        
		$sql = "SELECT DISTINCT(kode_training) FROM knn_training ORDER BY kode_training ASC";
        $result = $this->db_handle->getDataAll($sql);
        return $result;
    }
    
    public function tampil_satu($kode, $kriteria)
    {        
		$query = "SELECT a.*, b.nilai, b.bobot FROM knn_training a, knn_kriteria_nilai b 
				  WHERE a.id_kriteria_nilai=b.id_kriteria_nilai AND a.kode_training=? AND a.id_kriteria=?";
        $paramType = "si";
        $paramValue = array(
            $kode, $kriteria
        );
        
        $result = $this->db_handle->getData($query, $paramType, $paramValue);
        return $result;
    }

    public function tampil_satu_b($kode, $kriteria)
    {        
		$query = "SELECT a.* FROM knn_training a 
				  WHERE a.kode_training=? AND a.id_kriteria=?";
        $paramType = "si";
        $paramValue = array(
            $kode, $kriteria
        );
        
        $result = $this->db_handle->getData($query, $paramType, $paramValue);
        return $result;
    }

    public function get_kode()
    {        
		$kode="";
		$sql = "SELECT DISTINCT(kode_training) FROM knn_training ORDER BY kode_training DESC LIMIT 0, 1";
        $result = $this->db_handle->getDataAll($sql);
		if($result){
			$kode=sprintf("%'.05d\n", (int)$result[0]['kode_training']+1);
		} else {
			$kode=sprintf("%'.05d\n", 1);
		}
        return $kode;
    }

	
    public function tampilByKode($kode)
    {        
		$query = "SELECT kode_training FROM knn_training WHERE kode_training=? LIMIT 0,1";
        $paramType = "s";
        $paramValue = array(
            $kode
        );
        
        $result = $this->db_handle->getData($query, $paramType, $paramValue);
        return $result;
    }
	
	private function cek_nilai($kode, $kriteria){
		$sql = "SELECT id_training FROM knn_training WHERE kode_training=$kode AND id_kriteria=$kriteria";
        $result = $this->db_handle->getDataAll($sql);
        if(count($result)>0){
			return true;
		} else {
			return false;
		}
	}
	
    public function tambah($data=array()) 
    {
		for($i=0; $i<count($data[1]); $i++){
			$kode=$data[0];
			$kriteria=$data[1][$i];
			$nilai=$data[2][$kriteria];
			$masukan=$data[3][$kriteria];

			$nilai1=0; $nilai2=0;
			if($masukan==1){
				$nilai1=$nilai;
				$nilai2=0;
			} elseif($masukan==2){
				$result=$this->kriteria_nilai->tampil($kriteria);
				if($result){
					foreach ($result as $key => $datax) {
						$arr_angka=explode("-",$datax['nilai']);
						if($nilai>=doubleval(@$arr_angka[0]) && $nilai<=doubleval(@$arr_angka[1])){
							$nilai1=$datax['id_kriteria_nilai'];
						}
					}
				}
				$nilai2=$nilai;
			} elseif($masukan==3){
				$nilai1=0;
				$nilai2=$nilai;
			} 
			
			if($this->cek_nilai($kode, $kriteria)==false) {
				$query = "INSERT INTO knn_training (kode_training,id_kriteria,id_kriteria_nilai,masukan)
											  VALUES (?, ?, ?, ?)";
				$paramType  = "siid";
				$paramValue = array($kode,$kriteria,$nilai1,$nilai2);
				$this->db_handle->insert($query, $paramType, $paramValue);
			} else {
				$query = "UPDATE knn_training SET id_kriteria_nilai=?, masukan=? WHERE kode_training=? AND id_kriteria=?";
				$paramType  = "idsi";
				$paramValue = array($nilai1,$nilai2,$kode,$kriteria);
				$this->db_handle->update($query, $paramType, $paramValue);
			}

		}
		return true;
    }
	
    public function ubah($data=array(), $id) 
    {
		for($i=0; $i<count($data[1]); $i++){
			$kode=$data[0];
			$kriteria=$data[1][$i];
			$nilai=$data[2][$kriteria];
			$masukan=$data[3][$kriteria];

			$nilai1=0; $nilai2=0;
			if($masukan==1){
				$nilai1=$nilai;
				$nilai2=0;
			} elseif($masukan==2){
				$result=$this->kriteria_nilai->tampil($kriteria);
				if($result){
					foreach ($result as $key => $datax) {
						$arr_angka=explode("-",$datax['nilai']);
						if($nilai>=doubleval(@$arr_angka[0]) && $nilai<=doubleval(@$arr_angka[1])){
							$nilai1=$datax['id_kriteria_nilai'];
						}
					}
				}
				$nilai2=$nilai;
			} elseif($masukan==3){
				$nilai1=0;
				$nilai2=$nilai;
			} 
			
			if($this->cek_nilai($kode, $kriteria)==false) {
				$query = "INSERT INTO knn_training (kode_training,id_kriteria,id_kriteria_nilai,masukan)
											  VALUES (?, ?, ?, ?)";
				$paramType  = "siid";
				$paramValue = array($kode,$kriteria,$nilai1,$nilai2);
				$this->db_handle->insert($query, $paramType, $paramValue);
			} else {
				$query = "UPDATE knn_training SET id_kriteria_nilai=?, masukan=? WHERE kode_training=? AND id_kriteria=?";
				$paramType  = "idsi";
				$paramValue = array($nilai1,$nilai2,$kode,$kriteria);
				$this->db_handle->update($query, $paramType, $paramValue);
			}

		}
		return true;
    }
		
    public function hapus($kode) {
        $query = "DELETE FROM knn_training WHERE kode_training= ?";
        $paramType = "s";
        $paramValue = array(
            $kode
        );
        $result = $this->db_handle->update($query, $paramType, $paramValue);
		return $result;
    }
	
	public function escapeString($value)
    {
        return $this->db_handle->escape_string($value);
    }
    
}
?>