<?php 
class Hasil_m extends CI_Model
{

  function __construct()
  {
    parent::__construct();
  }

  // Fungsi untuk menampilkan data bobot_preferensi dari tabel
  public function get_all_data()
	{ 
    $this->db->select('ps.id_perhitungan, ps.id_balita, ps.periode, ps.hasil, dps.id_alternatif, dps.id_kriteria, dps.id_bobot');
    $this->db->from('perhitungan_saw ps');
    $this->db->join('detail_perhitungan_saw dps', 'ps.id_perhitungan = dps.id_perhitungan');
    $this->db->join('balita bp', 'ps.id_balita = bp.id_balita');
    $query = $this->db->get();
		return $query->result();
	}
  public function get_all()
	{ 
    $this->db->select('*');
    $this->db->from('perhitungan_saw ps');
    $this->db->join('balita bp', 'ps.id_balita = bp.id_balita');
    $query = $this->db->get();
		return $query->result();
	}
  
  public function get_by_id($id) 
  {
    $this->db->select('ps.id_perhitungan, ps.id_balita, ps.periode, ps.hasil, dps.id_alternatif, dps.id_kriteria, dps.id_bobot');
    $this->db->from('perhitungan_saw ps');
    $this->db->join('detail_perhitungan_saw dps', 'ps.id_perhitungan = dps.id_perhitungan');
    $this->db->where('ps.id_perhitungan', $id);
    $query = $this->db->get();
    return $query->row();
  }
  
  public function cetak($id) 
  {
    $this->db->select('*');
    $this->db->from('perhitungan_saw ps');;
    $this->db->join('balita bp', 'ps.id_balita = bp.id_balita');
    $query = $this->db->get();
    return $query->row();
  }

  public function update($id, $data) {
    $this->db->where('id_perhitungan', $id);
    return $this->db->update('perhitungan_saw', $data);
}

  // Fungsi untuk menambahkan data bobot_preferensi ke dalam tabel
  public function insertperhitungan($data) {
    $this->db->insert('perhitungan_saw', $data);
    return $this->db->insert_id(); // Mengembalikan ID dari data yang baru ditambahkan
  }

  public function insertdetailperhitungan($data) {
    $this->db->insert('detail_perhitungan_saw', $data);
    return $this->db->insert_id(); // Mengembalikan ID dari data yang baru ditambahkan
  }
  
  public function get_nilai($id_alternatif, $id_kriteria,$id_perhitungan) {
    // Lakukan pengambilan nilai berdasarkan id alternatif dan id kriteria
    $this->db->select('bp.nilai');
    $this->db->select('ps.id_perhitungan, ps.id_balita, ps.periode, ps.hasil, dps.id_alternatif, dps.id_kriteria, dps.id_bobot, dk.nama_kriteria');
    $this->db->from('perhitungan_saw ps');
    $this->db->join('detail_perhitungan_saw dps', 'ps.id_perhitungan = dps.id_perhitungan');
    $this->db->join('kriteria dk', 'dps.id_kriteria = dk.id_kriteria');
    $this->db->join('alternatif al', 'dps.id_alternatif = al.id_alternatif');
    $this->db->join('bobot_preferensi bp', 'dps.id_bobot = bp.id_bobot');
    $this->db->where('dps.id_alternatif', $id_alternatif);
    $this->db->where('dps.id_kriteria', $id_kriteria);
    $this->db->where('ps.id_perhitungan', $id_perhitungan);
    $query = $this->db->get();
    if($query->num_rows() > 0) {
        return $query->row()->nilai;
    } else {
        return null;
    }
  }
  
  public function get_max_min($id_kriteria,$id_perhitungan)
	{
		$query = $this->db->query("SELECT max(c.nilai) as max, min(c.nilai) as min, c.nilai as nilai FROM `detail_perhitungan_saw` a
			JOIN kriteria b ON a.id_kriteria=b.id_kriteria 
			JOIN bobot_preferensi c ON a.id_bobot=c.id_bobot 
			WHERE a.id_kriteria='$id_kriteria' AND  a.id_perhitungan = $id_perhitungan");
		return $query->row_array();
	}
}
?>