<?php
class M_pelayanan extends CI_Model
{
	function __construct()
	{
		parent::__construct();
	}

	function kode_user()
	{
		return $this->session->userdata("kode_user");
	}

	public function data_pelayanan()
	{
		$sql = "SELECT p.*, jp.nama AS nama_jenis_pelayanan 
				FROM tb_pelayanan p 
				LEFT JOIN tb_jenis_pelayanan jp ON p.jenis_pelayanan_id = jp.id_jenis_pelayanan 
				WHERE p.is_deleted = 0";
		return $this->db->query($sql);
	}

	public function data_pelayanan_by_jenis_pelatihan($id_jenis_pelayanan = null)
	{
		$this->db->select("p.*, jp.nama AS nama_jenis_pelayanan");
		$this->db->from("tb_pelayanan p");
		$this->db->join("tb_jenis_pelayanan jp", "p.jenis_pelayanan_id = jp.id_jenis_pelayanan", "left");
		$this->db->where("p.is_deleted", 0);

		if ($id_jenis_pelayanan) {
			$this->db->where("p.jenis_pelayanan_id", $id_jenis_pelayanan);
		}

		return $this->db->get();
	}

	function simpan_pelayanan($data)
	{
		$kode_user = $this->kode_user();
		$sql = "INSERT INTO `tb_pelayanan`  (`nama`, `jenis_pelayanan_id`, `stipendium`, `stipendium_daring`, `keterangan`, `created_at`, `created_by`) 
               VALUES ('{$data['nama']}', '{$data['jenis_pelayanan_id']}', '{$data['stipendium']}', '{$data['stipendium_daring']}', '{$data['keterangan']}', NOW(), '$kode_user')";
		return $this->db->query($sql);
	}

	function ambil_pelayanan($data)
	{
		$sql = "SELECT * FROM tb_pelayanan WHERE id_pelayanan = '$data[id_pelayanan]'";
		return $this->db->query($sql);
	}

	public function update_pelayanan($data)
	{
		$kode_user = $this->kode_user();
		$sql = "UPDATE `tb_pelayanan` 
            SET `nama` = '$data[nama]', 
                `jenis_pelayanan_id` = '$data[jenis_pelayanan_id]', 
                `stipendium` = '$data[stipendium]', 
                `stipendium_daring` = '$data[stipendium_daring]', 
                `keterangan` = '$data[keterangan]', 
                `updated_at` = NOW(), 
                `updated_by` = '$kode_user' 
            WHERE `id_pelayanan` = '$data[id_pelayanan]'";
		return $this->db->query($sql);
	}

	function hapus_pelayanan($data)
	{
		$sql = "UPDATE `tb_pelayanan` SET `is_deleted`='1' WHERE id_pelayanan = '$data[id_pelayanan]'";
		return $this->db->query($sql);
	}
}
