<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Alumni extends CI_Controller {
	public function index()
	{
		$this->load->model('M_alumni');
		if(isset($_SESSION['username'])){
			$username = $_SESSION['username'];
			//$this->load->model('M_alumni');
			//Load Detail Profil
			$detail = $this->M_alumni->detailAlumni($username);
			$data['detail'] = $detail;

			//Load Para Alumni
			$this->load->model('M_alumni');
			$data['data_alumni'] = $this->M_alumni->tampil_data_approved();

			//Load View Detail Alumni
			$this->load->model('M_alumni');
			$detail = $this->M_alumni->detailAlumni($username);
			$viewDetail['detail'] = $detail;

			//Load View
			$this->load->view('alumni/header',$data);
			$this->load->view('alumni/dashboard',$data);
			//$this->load->view('detail',$data);
			$this->load->view('alumni/footer');
		}else{
			//echo "failed from alumni";
			redirect('login');
		}
		
	}

	public function detail_alumni($username)
	{
		$this->load->model('M_alumni');
		// $this->M_alumni->detailAlumni($username);
		echo json_encode($this->M_alumni->detailAlumni(urldecode($username)));
	}

	public function update_alumni()
	{
		$this->load->model('M_alumni');
		$username = $_SESSION['username'];
		$where = array(
			'email' => $username
		);
		
		$foto = $this->update_foto_alumni();

		if($foto['file_name'] == NULL){
			$data = [
				'nama' => $this->input->post('nama'),
				'th_masuk' => $this->input->post('th_masuk'),
				'th_lulus' => $this->input->post('th_lulus'),
				'jurusan' => $this->input->post('jurusan'),
				'alamat' => $this->input->post('alamat'),
				'telp' => $this->input->post('telp'),
				'profesi' => $this->input->post('profesi'),
				'alamat_kantor' => $this->input->post('alamat_kantor'),
				'motto' => $this->input->post('motto'),
			];
		}else{
			$data = [
				'nama' => $this->input->post('nama'),
				'th_masuk' => $this->input->post('th_masuk'),
				'th_lulus' => $this->input->post('th_lulus'),
				'jurusan' => $this->input->post('jurusan'),
				'alamat' => $this->input->post('alamat'),
				'telp' => $this->input->post('telp'),
				'profesi' => $this->input->post('profesi'),
				'alamat_kantor' => $this->input->post('alamat_kantor'),
				'motto' => $this->input->post('motto'),
				'foto' => $foto['file_name'],
			];
		}

		$this->M_alumni->update_dataAlumni($where, $data);
		redirect('alumni');
	}

	public function update_foto_alumni()
	{
		$username = $_SESSION['username'];
		$namafoto = explode('.',$username);
		//echo print_r($namafoto);

		$config['file_name'] 		= $namafoto[0];
		$config['upload_path']      = FCPATH.'assets/materi/foto_alumni';
		$config['allowed_types']	= 'jpg|jpeg|png';
		$config['overwrite']		= TRUE;

		$this->load->library('upload', $config);

		if (!$this->upload->do_upload('foto'))
		{		
			echo "error";
			//echo $config['file_name'];
		}
		else
		{
			return $this->upload->data(); 
			//echo "foto berhasil";
			//echo $config['file_name'];
		}
	}

	public function update_password_alumni()
	{
		$this->load->model('M_alumni');
		$password = $this->input->post('password');
		$password_baru = $this->input->post('password_baru');

		$username = $_SESSION['username'];

		$data = array(
			'email' =>$username,
			'password' => md5($password),
		);

		$this->M_alumni->update_password_dataAlumni($password_baru, $data);
	}


	public function lihat_para_alumni()
	{
		$this->load->model('M_alumni');
		$data['data_alumni'] = $this->M_alumni->tampil_data();

		
	}

	

}
