<?php

class M_alumni extends CI_Model {
	public function tampil_data()
	{
		return $this->db->get('data_alumni')->result_array();
	}

	public function tampil_data_approved()
	{
		return $this->db->get_where('data_alumni', array('status' => '1'))->result_array();
	}

	public function detailAlumni($username)
	{
		$query = $this->db->get_where('data_alumni', array('email' => $username))->row();
		return $query;
	}

	public function update_dataAlumni($where,$data)
	{
		$this->db->where($where);
		$this->db->update('data_alumni', $data);
	}

	public function update_statusAlumni($email,$data)
	{
		//$a = $this->db->where('email',$email);
		if($data['status'] == 1){
			$this->db->where('email',$email);
			$this->db->update('data_alumni', $data);
			//echo "<script>console.log('appoved');</script>";

		}else if($data['status'] == 2){
			$this->db->where('email',$email);
			$this->db->update('data_alumni', $data);
 			//echo "<script>console.log('rejected');</script>";
			 //redirect('admin');
			
		}else{
			echo "perubahan status gagal";
			//redirect('admin');
		}
		
	}

	public function update_password_dataAlumni($passwwordBaru,$data)
	{
		$cekUser = $this->db->get_where('data_alumni',$data)->num_rows();
        if($cekUser > 0){
			$email =  $data['email'];
			$where = array(
				'email' => $email
			);
			$dataUpdate = array(
				"password" => md5($passwwordBaru)
			);
			$this->db->where($where);
			$this->db->update('data_alumni', $dataUpdate);
			redirect('alumni');
            //echo "data oke";
        }else{
			echo '<script> alert("Password lama salah. Ganti Password baru gagal.");
				window.location.assign("index")</script>'; 
			//redirect('alumni');
        }
	} 

	public function register_dataAlumni($data)
	{	
		$where = array(
			'email' => $data['email'],
		);
		$cekRegis = $this->db->get_where('data_alumni',$where)->num_rows();
		if($cekRegis > 0){
			echo '<script>alert("Data anda sudah terdaftar. Silahkan hubungi Admin untuk mengkonfirmasi data anda.");
                window.location.assign("register")</script>'; 
		}else{
			//echo $cekRegis;
			if($data['th_lulus'] < $data['th_masuk']){
				echo '<script>alert("Tahun lulus anda lebih kecil dari tahun masuk anda.");
                window.location.assign("register")</script>'; 
			}else{
				//echo "bener";
				$this->db->insert('data_alumni', $data);
				redirect('login/register_success');
			}
		}
	}

	public function update_revisiAlumni($where,$datarev)
	{
		$cekRev = $this->db->get_where('data_alumni',$where)->num_rows();
		echo "<script>console.log('$cekRev');</script>";
		if($cekRev <= 1){
			$this->db->where($where);
			$this->db->update('data_alumni', $datarev);
			//echo "<script>console.log('berhasil');</script>";
		}else{
			echo '<script type="text/javascript">alert("Ubah Email gagal. Nomor telepon anda telah terdaftar.");
           	window.location.assign("admin")</script>'; 
		}
	}
}

?>