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

class Daftar 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)
		{
			$data['id_gelombang'] = $ppdb->id_gelombang;
			$this->load->view('daftar',$data);
		}
		else
		{
			$this->load->view('tutup');
		}
	}
	
	public function daftar()
	{
		date_default_timezone_set('Asia/Jakarta');
		$data['nama']			= strip_tags($this->input->post('nama'));
		$data['nik']			= strip_tags($this->input->post('nik'));
		$data['no_hp']			= strip_tags($this->input->post('no_hp'));
		$data['email']			= strip_tags($this->input->post('email'));
		$data['id_gelombang']	= $this->input->post('id_gelombang');
		$data['password']		= md5($this->input->post('password'));
		$data['id_status']		= 1;
		$data['tanggal_daftar']	= date('Y-m-d');
		$cekNIK					= $this->M_daftar->cekNIK($data['nik']);
		// print_r($cekNIK);die();
		if($cekNIK == NULL)
		{
			$insert			= $this->M_daftar->addCalonSiswa($data);
			if($insert)
			{
				$this->session->set_flashdata('msg', 
						'<div class="alert alert-success">
							<center>
								<h4>Pendaftaran berhasil!</h4>
								<p>Silakan login menggunakan akun yang baru saja Anda buat!</p>
							</center>
						</div>');	
				redirect('Login');
			}
			else
			{
				$this->session->set_flashdata('msg', 
						'<div class="alert alert-danger">
							<center>
								<h4>Pendafataran gagal!</h4>
								<p>Silakan Coba lagi!</p>
							</center>
						</div>');	
				redirect('Daftar');		
			}
		}
		else
		{
			$this->session->set_flashdata('msg', 
					'<div class="alert alert-danger">
						<center>
							<h4>Pendaftaran gagal!</h4>
							<p>NIK sudah pernah terdaftar! Silakan login!</p>
						</center>
					</div>');	
			redirect('Login');
		}
	}
}
