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

class Login extends CI_Controller 
{
	public function __construct() 
	{
        parent::__construct();
		$this->load->model('M_daftar');
		$this->load->model('M_gelombang');
    }
	
	public function index()
	{
		date_default_timezone_set('Asia/Jakarta');
		$today		= date('Y-m-d H:i:s');
		$ppdb 		= $this->M_gelombang->getGelombangOpen($today)->row();
		// print_r($ppdb);die();
		if($ppdb != NULL)
		{
			if($this->session->userdata('calonsiswa') == NULL )
			{
				$data['gelombang']		= $ppdb->gelombang;
				$data['tahun_ajaran']	= $ppdb->tahun_ajaran;
				
//--------------------------------------------------------------
                $target_date = '2025-06-28 12:00:00'; // Ganti dengan tanggal target Anda
                $now = new DateTime();
                $target = new DateTime($target_date);
                $diff = $target->diff($now);

                $data['days'] = $diff->format('%a');
                $data['hours'] = $diff->format('%h');
                $data['minutes'] = $diff->format('%i');
                $data['seconds'] = $diff->format('%s');
//----------------------------------end----------------------------
				
				$this->load->view('login',$data);
			}
			else
			{
				redirect('Pendaftaran');
			}
		}
		else
		{
			$this->load->view('tutup');
		}
	}
	
	public function login()
	{
		$nik			= $this->input->post('nik');
		$password		= md5($this->input->post('password'));
		$check			= $this->M_daftar->login($nik,$password)->row();
		// print_r($check);die();
		if($check != NULL)
		{
			$sess_array = array(
								'id'			=> $check->id_calon_siswa,
								'nama'			=> $check->nama,
								'id_status'		=> $check->id_status,
								'tempat_lahir'	=> $check->tempat_lahir,
								'no_kk'			=> $check->no_kk,
								'nisn'			=> $check->nisn,
								'id_jurusan'	=> $check->id_jurusan
								);		
			$this->session->set_userdata('calonsiswa', $sess_array);
			// echo "berhasil login";
			if($check->id_status == 1)
			{
				if($check->id_jurusan != NULL)
				{
					redirect('Pendaftaran/step6','refresh');
				}
				else if($check->nisn != NULL)
				{
					redirect('Pendaftaran/step4','refresh');
				}
				else if($check->no_kk != NULL)
				{
					redirect('Pendaftaran/step3','refresh');
				}
				else if($check->tempat_lahir != NULL)
				{
					redirect('Pendaftaran/step2','refresh');
				}
				else if($check->tempat_lahir == NULL)
				{
					redirect('Pendaftaran','refresh');
				}
			}
			else
			{
				redirect('Pendaftaran','refresh');
			}
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Maaf!</h4>
							<p>NIK atau password yang Anda masukkan salah. Silakan Coba lagi!</p>
						</center>
					</div>');	
			redirect('Login');		
		}
	}
	public function logout()
	{
		$this->session->unset_userdata('calonsiswa');
		$this->session->sess_destroy();
		redirect('Login', 'refresh');
	}
}
