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

class Login extends CI_Controller {
	public function index()
	{
		$this->load->view('login');
	}
	
// 	public function admin()
// 	{
// 		$this->load->view('admin/dashboard');
// 	}

	public function authLogin()
	{
		$this->load->model('M_login');
		$username = $this->input->post('email');
		$password = $this->input->post('password');

		$this->form_validation->set_rules('email','Email','required');
		$this->form_validation->set_rules('password','Password','required');

		if($this->form_validation->run() != FALSE){
			$data = [
				'email' => $username,
				'password' => md5($password),
				'status' => '1'
			];
			$cekLogin = $this->M_login->cekLogin($data);
		}else{
			$this->load->view('login');
		}
	}

	public function register()
	{
		$this->load->view('register');
	}

	public function daftar_alumni()
	{
		$this->load->model('M_alumni');
		$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'),
				'email' => $this->input->post('email'),
				'profesi' => $this->input->post('profesi'),
				'alamat_kantor' => $this->input->post('alamat_kantor'),
				'password' => md5($this->input->post('password')),
				'foto' => 'ikawigadflt.png',
			];
		}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'),
				'email' => $this->input->post('email'),
				'profesi' => $this->input->post('profesi'),
				'alamat_kantor' => $this->input->post('alamat_kantor'),
				'password' => md5($this->input->post('password')),
				'foto' => $foto['file_name'],
			];
		}

		$this->M_alumni->register_dataAlumni($data);
	}

	public function update_foto_alumni()
	{
		$config['upload_path']          = FCPATH.'assets/materi/foto_alumni';
		$config['allowed_types']        = 'jpg|jpeg|png';
		$config['overwrite']			= TRUE;
		// $config['max_size']             = 100;
		// $config['max_width']            = 1024;
		// $config['max_height']           = 768;

		$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 register_success()
	{
		$this->load->view('register_success');
	}

	public function logout()
	{
		$this->session->sess_destroy();
		redirect('login');
	}
}