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

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Pesertadidik extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/pesertadidik
	 *	- or -
	 * 		http://example.com/index.php/pesertadidik/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/pesertadidik/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function __construct()
	{
		parent::__construct();
		$this->load->model(array('pesertadidik_m','master/master_m','pesertadidik_long_m'));
		$this->lang->load(array('pesertadidik','pesertadidik_long'));
		$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
	}

	public function index()
	{

		$this->data['subtitle'] = $this->lang->line('pesertadidik_heading');
		$this->parser->parse('pesertadidik_list',$this->data);
	}

	public function pesertadidik_ajax()
	{
		if (!$this->input->is_ajax_request()) {
		   exit('No direct script access allowed');
		}
		$pesertadidik = $this->pesertadidik_m->get_datatables();
		$data = array();

		foreach ($pesertadidik as $item) {
			$row   = array();
			$default_password = str_replace("-","" ,$item->tanggal_lahir);

			$account_data = array(
				'peserta_didik_id'=> $item->peserta_didik_id,
				'identity'=> $item->nisn,
				'password'=> $default_password,
				'password_confirm'=> $default_password,
				'email'=> $item->email,
				'first_name'=> $item->nama,
				'company'=>'-',
				'group'=>'6',
				'phone'=>$item->nomor_telepon_seluler,
			);

			$account_data = json_encode($account_data);

            $row[] = '<input type="checkbox" name="pesertadidik-id[]" class="styled" value="' . $item->peserta_didik_id . '">';
            $row[] = '<a href="'. site_url('pesertadidik/view/' . $item->peserta_didik_id) .'">'. title($item->nama) . '</a>';
            $row[] =  (($item->jenis_kelamin === 'L') ? 'Laki-laki' : 'Perempuan');
            $row[] =  $item->nisn ;
            $row[] =  $item->tempat_lahir;
            $row[] =  $item->tanggal_lahir;

			$action_btn = '<a href="'. site_url('pesertadidik/edit/' . $item->peserta_didik_id) . '" class="btn btn-primary" >' . lang('app_edit_btn') . '</a>&nbsp;<a href="'. site_url('pesertadidik/view/' . $item->peserta_didik_id) . '" class="btn btn-warning">' . lang('app_view_btn') . '</a>';

			if(!$item->registered)
			{
				$action_btn .= '&nbsp;<a href="'. site_url('pesertadidik/generate-account/' . $item->peserta_didik_id) . '" class="btn btn-info generate-account" data-account=\''. $account_data .'\'>' . lang('app_generate_account_btn') . '</a>';
			}

			$row[] = $action_btn;

            $data[] = $row;
        }

		$output = array(
                        "draw"            => $_POST['draw'],
                        "recordsTotal"    => $this->pesertadidik_m->count_all(),
                        "recordsFiltered" => $this->pesertadidik_m->count_filtered(),
                        "data"            => $data,
                );

		return $this->output
	        ->set_content_type('application/json')
	        ->set_status_header(200)
	        ->set_output(json_encode($output));

	}

	public function view($id = NULL)
	{

        $this->data['subtitle']        = $this->lang->line('pesertadidik_heading');
        $this->data['pesertadidik']    = $this->pesertadidik_m->get_data($id);

        $this->parser->parse('pesertadidik_detail',$this->data);
	}

	public function edit($id = NULL)
	{

		if ($id == NULL)
		{
			$this->data['subtitle']     = $this->lang->line('pesertadidik_create_heading');
			$pesertadidik               = $this->pesertadidik_m->get_field();
			$peserta_didik_longitudinal = $this->pesertadidik_long_m->get_field();
		}
		else
		{
			$this->data['subtitle']     = $this->lang->line('pesertadidik_edit_heading');
			$pesertadidik               = $this->pesertadidik_m->get($id);
			$peserta_didik_longitudinal = $this->pesertadidik_long_m->get_by(array('peserta_didik_id'=> $pesertadidik->peserta_didik_id, 'semester_id'=> $this->data['lainnya']->tahun_semester->semester_id), TRUE);

			if (empty( (array) $peserta_didik_longitudinal)) {
				$peserta_didik_longitudinal = $this->pesertadidik_long_m->get_field();
			}
		}


		$rules = $this->pesertadidik_m->_rules;

		$this->form_validation->set_rules($rules);

		if ($this->form_validation->run($this) == TRUE)
		{
			$data  = $this->pesertadidik_m->array_form_post(array('nama','jenis_kelamin','nisn','nik','tempat_lahir','tanggal_lahir','agama_id','kebutuhan_khusus_id','alamat_jalan','rt','rw','nama_dusun','desa_kelurahan','kode_wilayah','kode_pos','lintang','bujur','jenis_tinggal_id','alat_transportasi_id','ayah_id','ibu_id','anak_keberapa','nomor_telepon_rumah','nomor_telepon_seluler','email','penerima_kps','no_kps','layak_pip','penerima_kip','no_kip','nm_kip','no_kks','reg_akta_lahir','id_layak_pip','id_bank','rekening_bank','nama_kcp','rekening_atas_nama','kewarganegaraan','create_date','last_update','updater_id'));

			$data_periodik = $this->pesertadidik_long_m->array_form_post(array('semester_id','tinggi_badan','berat_badan','jarak_rumah_ke_sekolah','jarak_rumah_ke_sekolah_km','waktu_tempuh_ke_sekolah','menit_tempuh_ke_sekolah','jumlah_saudara_kandung','asrama_id','create_date','updater_id'));

			$peserta_didik_id = $this->pesertadidik_m->save($data, $id);

			if($peserta_didik_id)
			{
				if ($id === NULL)
				{
					$data_periodik['peserta_didik_id'] = $peserta_didik_id;
					$this->pesertadidik_long_m->save_data_periodik($data_periodik);
				}
				else
				{
					$this->pesertadidik_long_m->save_data_periodik($data_periodik, array('peserta_didik_id'=> $peserta_didik_id, 'semester_id'=> $this->data['lainnya']->tahun_semester->semester_id));
				}

				redirect('pesertadidik');
			}
		}

    	// Data Pribadi

    	$this->data['nama'] = array(
    		'name'  => 'nama',
    		'id'    => 'nama',
    		'type'  => 'text',
    		'required' => 'required',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_nama_placeholder'),
    		'value' => $this->form_validation->set_value('nama', $pesertadidik->nama)
    	);

    	// pass the user to the view
    	$genders = array('L'=>'Laki-laki', 'P'=>'Perempuan');
    	$this->data['gender_item']    = '';

    	foreach ($genders as $key => $value) {
    		$checked = null;
    		$item = null;

    		if ($key  == strtoupper($pesertadidik->jenis_kelamin)) {
    			$checked= ' checked="checked"';
    		}

    		$this->data['gender_item'] .= '<label class="radio-inline">' . htmlspecialchars($value,ENT_QUOTES,'UTF-8') . '<input type="radio" class="styled" name="jenis_kelamin" required value="'.$key.'" '.$checked.'></label>';
    	}


    	$this->data['nisn'] = array(
    		'name'  => 'nisn',
    		'id'    => 'nisn',
    		'type'  => 'text',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_nisn_placeholder'),
    		'value' => $this->form_validation->set_value('nisn', $pesertadidik->nisn)
    	);


    	$this->data['kewarganegaraan'] = array(
    		'name'        => 'kewarganegaraan',
    		'id'          => 'select-kewarganegaraan',
    		'required'    => 'required',
    		'data-master'    => 'negara',
    		'class'       => 'select2-master',
    		'data-placeholder'=> "Pilih " . $this->lang->line('pesertadidik_kewarganegaraan')
    	);

    	$this->data['kewarganegaraan_option'] = $this->master_m->get_master('negara','negara_id','nama', $pesertadidik->kewarganegaraan);
    	$this->data['kewarganegaraan_selected'] = $pesertadidik->kewarganegaraan;

    	$this->data['nik'] = array(
    		'name'  => 'nik',
    		'id'    => 'nik',
    		'type'  => 'text',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_nik_placeholder'),
    		'value' => $this->form_validation->set_value('nik', $pesertadidik->nik)
    	);

    	$this->data['tempat_lahir'] = array(
    		'name'  => 'tempat_lahir',
    		'id'    => 'tempat_lahir',
    		'type'  => 'text',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_tempat_lahir_placeholder'),
    		'value' => $this->form_validation->set_value('tempat_lahir', $pesertadidik->tempat_lahir)
    	);

    	$this->data['tanggal_lahir'] = array(
    		'name'  => 'tanggal_lahir',
    		'id'    => 'tanggal_lahir',
    		'type'  => 'date',
    		'required' => 'required',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_tanggal_lahir_placeholder'),
    		'value' => $this->form_validation->set_value('tanggal_lahir', $pesertadidik->tanggal_lahir)
    	);

    	$this->data['agama'] = array(
    		'name'        => 'agama_id',
    		'id'          => 'select-agama',
    		'required'    => 'required',
    		'data-master'    => 'agama',
    		'class'       => 'select2-master',
    		'data-placeholder'=> "Pilih " . $this->lang->line('pesertadidik_agama_id')
    	);

    	$this->data['agama_option'] = $this->master_m->get_master('agama','agama_id','nama', $pesertadidik->agama_id);
    	$this->data['agama_selected'] = $pesertadidik->agama_id;

    	$this->data['kebutuhan_khusus'] = array(
    		'name'        => 'kebutuhan_khusus_id',
    		'id'          => 'select-kebutuhan_khusus',
    		'data-master'    => 'kebutuhan_khusus',
    		'class'       => 'select2-master',
    		'data-placeholder'=> "Pilih " . $this->lang->line('pesertadidik_kebutuhan_khusus_id')
    	);

    	$this->data['kebutuhan_khusus_option'] = $this->master_m->get_master('kebutuhan_khusus','kebutuhan_khusus_id','kebutuhan_khusus', $pesertadidik->kebutuhan_khusus_id);
    	$this->data['kebutuhan_khusus_selected'] = $pesertadidik->kebutuhan_khusus_id;


    	$this->data['alamat_jalan'] = array(
    		'name'  => 'alamat_jalan',
    		'id'    => 'alamat_jalan',
    		'type'  => 'text',
    		'required' => 'required',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_alamat_jalan_placeholder'),
    		'value' => $this->form_validation->set_value('alamat_jalan', $pesertadidik->alamat_jalan)
    	);

    	$this->data['rt'] = array(
    		'name'  => 'rt',
    		'id'    => 'rt',
    		'type'  => 'text',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_rt_placeholder'),
    		'value' => $this->form_validation->set_value('rt', $pesertadidik->rt)
    	);

    	$this->data['rw'] = array(
    		'name'  => 'rw',
    		'id'    => 'rw',
    		'type'  => 'text',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_rw_placeholder'),
    		'value' => $this->form_validation->set_value('rw', $pesertadidik->rw)
    	);

    	$this->data['nama_dusun'] = array(
    		'name'  => 'nama_dusun',
    		'id'    => 'nama_dusun',
    		'type'  => 'text',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_nama_dusun_placeholder'),
    		'value' => $this->form_validation->set_value('nama_dusun', $pesertadidik->nama_dusun)
    	);

    	$this->data['desa_kelurahan'] = array(
    		'name'  => 'desa_kelurahan',
    		'id'    => 'desa_kelurahan',
    		'type'  => 'text',
    		'required' => 'required',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_desa_kelurahan_placeholder'),
    		'value' => $this->form_validation->set_value('desa_kelurahan', $pesertadidik->desa_kelurahan)
    	);

    	$this->data['kode_wilayah'] = array(
    		'name'        => 'kode_wilayah',
    		'id'          => 'select-kode_wilayah',
    		'required'    => 'required',
    		'data-master'    => 'mst_wilayah',
    		'class'       => 'select2-master',
    		'data-placeholder'=> "Pilih " . $this->lang->line('pesertadidik_kode_wilayah')
    	);

    	$this->data['kode_wilayah_option'] = $this->master_m->get_master('mst_wilayah','kode_wilayah','nama', $pesertadidik->kode_wilayah);
    	$this->data['kode_wilayah_selected'] = trim($pesertadidik->kode_wilayah);

    	$this->data['kode_pos'] = array(
    		'name'  => 'kode_pos',
    		'id'    => 'kode_pos',
    		'type'  => 'text',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_kode_pos_placeholder'),
    		'value' => $this->form_validation->set_value('kode_pos', $pesertadidik->kode_pos)
    	);

    	$this->data['lintang'] = array(
    		'name'  => 'lintang',
    		'id'    => 'lintang',
    		'type'  => 'text',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_lintang_placeholder'),
    		'value' => $this->form_validation->set_value('lintang', $pesertadidik->lintang)
    	);

    	$this->data['bujur'] = array(
    		'name'  => 'bujur',
    		'id'    => 'bujur',
    		'type'  => 'text',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_bujur_placeholder'),
    		'value' => $this->form_validation->set_value('bujur', $pesertadidik->bujur)
    	);

    	$this->data['jenis_tinggal_id'] = array(
    		'name'        => 'jenis_tinggal_id',
    		'id'          => 'select-jenis_tinggal_id',
    		'required'    => 'required',
    		'data-master'    => 'jenis_tinggal',
    		'class'       => 'select2-master',
    		'data-placeholder'=> "Pilih " . $this->lang->line('pesertadidik_jenis_tinggal_id')
    	);

    	$this->data['jenis_tinggal_id_option'] = $this->master_m->get_master('jenis_tinggal','jenis_tinggal_id','nama',$pesertadidik->jenis_tinggal_id);
    	$this->data['jenis_tinggal_id_selected'] = $pesertadidik->jenis_tinggal_id;


    	$this->data['alat_transportasi_id'] = array(
    		'name'        => 'alat_transportasi_id',
    		'id'          => 'select-alat_transportasi_id',
    		'required'    => 'required',
    		'data-master'    => 'alat_transportasi',
    		'class'       => 'select2-master',
    		'data-placeholder'=> "Pilih " . $this->lang->line('pesertadidik_alat_transportasi_id')
    	);

    	$this->data['alat_transportasi_id_option'] = $this->master_m->get_master('alat_transportasi','alat_transportasi_id','nama', $pesertadidik->alat_transportasi_id);
    	$this->data['alat_transportasi_id_selected'] = $pesertadidik->alat_transportasi_id;


    	$this->data['anak_keberapa'] = array(
    		'name'  => 'anak_keberapa',
    		'id'    => 'anak_keberapa',
    		'type'  => 'text',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_anak_keberapa_placeholder'),
    		'value' => $this->form_validation->set_value('anak_keberapa', $pesertadidik->anak_keberapa)
    	);

    	// pass the user to the view
    	$kps = array('1'=>'Ya', '0'=>'Tidak');
    	$this->data['penerima_kps_item']    = '';

    	foreach ($kps as $key => $value) {
    		$checked = null;
    		$item = null;

    		if ($key  == strtolower($pesertadidik->penerima_kps)) {
    			$checked= ' checked="checked"';
    		}

    		$this->data['penerima_kps_item'] .= '<label class="radio-inline">' . htmlspecialchars($value,ENT_QUOTES,'UTF-8') . '<input type="radio" class="styled" name="penerima_kps" value="'.$key.'" '.$checked.'></label>';
    	}

    	$this->data['no_kps'] = array(
    		'name'  => 'no_kps',
    		'id'    => 'no_kps',
    		'type'  => 'text',
    		'disabled'  => 'disabled',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_no_kps_placeholder'),
    		'value' => $this->form_validation->set_value('no_kps', $pesertadidik->no_kps)
    	);

    	// pass the user to the view
    	$layak_pip = array('1'=>'Ya', '0'=>'Tidak');
    	$this->data['layak_pip_item']    = '';

    	foreach ($layak_pip as $key => $value) {
    		$checked = null;
    		$item = null;

    		if ($key  == strtolower($pesertadidik->layak_pip)) {
    			$checked= ' checked="checked"';
    		}

    		$this->data['layak_pip_item'] .= '<label class="radio-inline">' . htmlspecialchars($value,ENT_QUOTES,'UTF-8') . '<input type="radio" class="styled" name="layak_pip" value="'.$key.'" '.$checked.'></label>';
    	}


    	$kip = array('1'=>'Ya', '0'=>'Tidak');
    	$this->data['penerima_kip_item']    = '';

    	foreach ($kip as $key => $value) {
    		$checked = null;
    		$item = null;

    		if ($key  == strtolower($pesertadidik->penerima_kip)) {
    			$checked= ' checked="checked"';
    		}

    		$this->data['penerima_kip_item'] .= '<label class="radio-inline">' . htmlspecialchars($value,ENT_QUOTES,'UTF-8') . '<input type="radio" class="styled" name="penerima_kip" value="'.$key.'" '.$checked.'></label>';
    	}


    	$this->data['no_kip'] = array(
    		'name'  => 'no_kip',
    		'id'    => 'no_kip',
    		'type'  => 'text',
    		'disabled'  => 'disabled',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_no_kip_placeholder'),
    		'value' => $this->form_validation->set_value('no_kip', $pesertadidik->no_kip)
    	);

    	$this->data['nm_kip'] = array(
    		'name'  => 'nm_kip',
    		'id'    => 'nm_kip',
    		'type'  => 'text',
    		'disabled'  => 'disabled',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_nm_kip_placeholder'),
    		'value' => $this->form_validation->set_value('nm_kip', $pesertadidik->nm_kip)
    	);

    	$this->data['no_kks'] = array(
    		'name'  => 'no_kks',
    		'id'    => 'no_kks',
    		'type'  => 'text',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_no_kks_placeholder'),
    		'value' => $this->form_validation->set_value('no_kks', $pesertadidik->no_kks)
    	);

    	$this->data['reg_akta_lahir'] = array(
    		'name'  => 'reg_akta_lahir',
    		'id'    => 'reg_akta_lahir',
    		'type'  => 'text',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_reg_akta_lahir_placeholder'),
    		'value' => $this->form_validation->set_value('reg_akta_lahir', $pesertadidik->reg_akta_lahir)
    	);


    	$this->data['id_layak_pip'] = array(
    		'name'        => 'id_layak_pip',
    		'id'          => 'select-id_layak_pip',
    		'class'       => 'select2-search',
    		'disabled'       => 'disabled',
    		'data-placeholder'=> "Pilih " . $this->lang->line('pesertadidik_id_layak_pip')
    	);

    	$this->data['id_layak_pip_option'] = $this->master_m->get_master('alasan_layak_pip','id_layak_pip','alasan_layak_pip');
    	$this->data['id_layak_pip_selected'] = $pesertadidik->id_layak_pip;

    	$this->data['ayah_id'] = array(
    		'name'        => 'ayah_id',
    		'id'          => 'select-ayah_id',
    		'data-master'       => 'orang_tua',
    		'data-where_field'       => 'jenis_kelamin',
    		'data-where_value'       => 'L',
    		'class'       => 'select2-master',
    		'data-placeholder'=> "Pilih " . $this->lang->line('pesertadidik_ayah_id')
    	);

    	$this->data['ayah_id_option'] = $this->master_m->get_master('orang_tua','orang_tua_id','nama');
    	$this->data['ayah_id_selected'] = $pesertadidik->ayah_id;

    	$this->data['ibu_id'] = array(
    		'name'        => 'ibu_id',
    		'data-master'       => 'orang_tua',
    		'data-where_field'       => 'jenis_kelamin',
    		'data-where_value'       => 'P',
    		'id'          => 'select-ibu_id',
    		'class'       => 'select2-master',
    		'data-placeholder'=> "Pilih " . $this->lang->line('pesertadidik_ibu_id')
    	);

    	$this->data['ibu_id_option'] = $this->master_m->get_master('orang_tua','orang_tua_id','nama');
    	$this->data['ibu_id_selected'] = $pesertadidik->ibu_id;

    	// Kontak

    	$this->data['nomor_telepon_rumah'] = array(
    		'name'  => 'nomor_telepon_rumah',
    		'id'    => 'nomor_telepon_rumah',
    		'type'  => 'text',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_nomor_telepon_rumah_placeholder'),
    		'value' => $this->form_validation->set_value('nomor_telepon_rumah', $pesertadidik->nomor_telepon_rumah)
    	);

    	$this->data['nomor_telepon_seluler'] = array(
    		'name'  => 'nomor_telepon_seluler',
    		'id'    => 'nomor_telepon_seluler',
    		'type'  => 'text',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_nomor_telepon_seluler_placeholder'),
    		'value' => $this->form_validation->set_value('nomor_telepon_seluler', $pesertadidik->nomor_telepon_seluler)
    	);

    	$this->data['email'] = array(
    		'name'  => 'email',
    		'id'    => 'email',
    		'type'  => 'text',
    		'required' => 'required',
    		'class' => 'form-control',
    		'placeholder'=> $this->lang->line('pesertadidik_email_placeholder'),
    		'value' => $this->form_validation->set_value('email', $pesertadidik->email)
    	);

    	// Data Pelengkap

		$this->data['tinggi_badan'] = array(
			'name'  => 'tinggi_badan',
			'id'    => 'tinggi_badan',
			'type'  => 'text',
			'class' => 'form-control',
			'required' => 'required',
			'placeholder'=> $this->lang->line('peserta_didik_longitudinal_tinggi_badan_placeholder'),
			'value' => $this->form_validation->set_value('tinggi_badan', $peserta_didik_longitudinal->tinggi_badan)
		);

		$this->data['berat_badan'] = array(
			'name'  => 'berat_badan',
			'id'    => 'berat_badan',
			'type'  => 'text',
			'class' => 'form-control',
			'required' => 'required',
			'placeholder'=> $this->lang->line('peserta_didik_longitudinal_berat_badan_placeholder'),
			'value' => $this->form_validation->set_value('berat_badan', $peserta_didik_longitudinal->berat_badan)
		);

		// pass the user to the view
		$jarak = array('1'=>'Kurang dari 1 KM', '2'=>'Lebih dari 1 KM');
		$this->data['jarak_item']    = '';

		foreach ($jarak as $key => $value) {
			$checked = null;
			$item = null;

			if ($key  == strtolower($peserta_didik_longitudinal->jarak_rumah_ke_sekolah)) {
				$checked= ' checked="checked"';
			}

			$this->data['jarak_item'] .= '<label class="radio-inline">' . htmlspecialchars($value,ENT_QUOTES,'UTF-8') . '<input type="radio" class="styled" name="jarak_rumah_ke_sekolah" value="'.$key.'" '.$checked.'></label>';
		}

		$this->data['jarak_rumah_ke_sekolah_km'] = array(
			'name'  => 'jarak_rumah_ke_sekolah_km',
			'id'    => 'jarak_rumah_ke_sekolah_km',
			'disabled'  => 'disabled',
			'type'  => 'text',
			'class' => 'form-control',
			'placeholder'=> $this->lang->line('peserta_didik_longitudinal_jarak_rumah_ke_sekolah_km_placeholder'),
			'value' => $this->form_validation->set_value('jarak_rumah_ke_sekolah_km', $peserta_didik_longitudinal->jarak_rumah_ke_sekolah_km)
		);

		$this->data['waktu_tempuh_ke_sekolah'] = array(
			'name'  => 'waktu_tempuh_ke_sekolah',
			'id'    => 'waktu_tempuh_ke_sekolah',
			'type'  => 'text',
			'class' => 'form-control',
			'placeholder'=> $this->lang->line('peserta_didik_longitudinal_waktu_tempuh_ke_sekolah_jam'),
			'value' => $this->form_validation->set_value('waktu_tempuh_ke_sekolah', $peserta_didik_longitudinal->waktu_tempuh_ke_sekolah)
		);

		$this->data['menit_tempuh_ke_sekolah'] = array(
			'name'  => 'menit_tempuh_ke_sekolah',
			'id'    => 'menit_tempuh_ke_sekolah',
			'type'  => 'text',
			'class' => 'form-control',
			'placeholder'=> $this->lang->line('peserta_didik_longitudinal_waktu_tempuh_ke_sekolah_menit'),
			'value' => $this->form_validation->set_value('menit_tempuh_ke_sekolah', $peserta_didik_longitudinal->menit_tempuh_ke_sekolah)
		);

		$this->data['jumlah_saudara_kandung'] = array(
			'name'  => 'jumlah_saudara_kandung',
			'id'    => 'jumlah_saudara_kandung',
			'type'  => 'text',
			'class' => 'form-control',
			'placeholder'=> $this->lang->line('peserta_didik_longitudinal_jumlah_saudara_kandung_placeholder'),
			'value' => $this->form_validation->set_value('jumlah_saudara_kandung', $peserta_didik_longitudinal->jumlah_saudara_kandung)
		);

		$this->data['asrama_id'] = array(
			'name'        => 'asrama_id',
			'id'          => 'select-asrama_id',
			'required'    => 'required',
			'data-master'    => 'prasarana',
			'data-filter'    => 'jenis_prasarana_id-37',
			'class'       => 'select2-master',
			'data-placeholder'=> "Pilih " . $this->lang->line('peserta_didik_longitudinal_asrama_id_placeholder')
		);

		$this->data['asrama_id_option'] = $this->master_m->get_master('prasarana','prasarana_id','nama', $peserta_didik_longitudinal->asrama_id, array('jenis_prasarana_id'=>'37'));
		$this->data['asrama_id_selected'] = trim($peserta_didik_longitudinal->asrama_id);

    	$this->data['pesertadidik_meta'] = array(
    		'create_date' => ((!empty($pesertadidik->create_date)) ? $pesertadidik->create_date : now()),
    		'last_update' => ((!empty($pesertadidik->create_date)) ?  now() : $pesertadidik->last_update),
    		'updater_id'  => ((!empty($pesertadidik->updater_id)) ? $pesertadidik->updater_id : $this->data['app_session']->id ),
    		'semester_id' => ((!empty($peserta_didik_longitudinal->semester_id)) ? $peserta_didik_longitudinal->semester_id : $this->data['lainnya']->tahun_semester->semester_id)
    	);

    	$this->data['orangtua_meta'] = array(
    		'created'    =>  now(),
    		'createdby'  => $this->data['app_session']->id
    	);

    	$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
    	$this->parser->parse('pesertadidik_edit', $this->data);
	}

	public function add_orangtua()
	{
		if (!$this->input->is_ajax_request()) {
			exit('No direct script access allowed');
		 }

		$response = array();

		$this->load->model('orangtua/orangtua_m');
		$data  = $this->orangtua_m->array_form_post(array('nama','agama_id','jenis_kelamin','tempat_lahir','tanggal_lahir','jenjang_pendidikan_id','status_kekerabatan_id','penghasilan_id','pekerjaan_id','kewarganegaraan','email','nomor_telepon_seluler','nomor_telepon_rumah','alamat','desa_kelurahan','kode_wilayah','created','createdby','modified','modifiedby'));

		if (isset($_POST['nama']) && !empty($_POST['nama']))
		{
			if($this->orangtua_m->save($data, $id))
			{
				$response = array('status' => 'success', 'message'=>'Orang tua berhasil ditambahkan');
			}
			else
			{
				$response = array('status' => 'failed', 'message'=>'Orang tua gagal ditambahkan');
			}
		}
		else{
			$response = array('status' => 'error', 'message'=>'Field nama kosong, atau permintaan tidak valid!');

		}


		return $this->output
					->set_content_type('application/json')
					->set_status_header(200)
					->set_output(json_encode($response));
	}

    public function pdbaru()
    {
        $this->data['subtitle'] = 'Peserta Didik Baru';
        $this->parser->parse('pdb_list.tpl',$this->data);
    }

    public function pdbdetail($id = NULL)
    {
        $this->load->model('pdb_m');
        $this->lang->load('pdb');

        $this->data['subtitle'] = 'Detail Peserta Didik Baru';
        $this->data['pdb']      = $this->pdb_m->get_data($id);
        $this->data['pdb_nilai_rapor'] = json_decode($this->data['pdb']->nilai_rapor);

        $this->parser->parse('pdb_detail',$this->data);
    }

    public function pdb_ajax()
    {
        if (!$this->input->is_ajax_request()) {
           exit('No direct script access allowed');
        }
        $this->load->model('pdb_m');
        $pdb  = $this->pdb_m->get_datatables();
        $data = array();

        foreach ($pdb as $item) {
            $row   = array();

            $row[] = '<input type="checkbox" name="pdb-id[]" class="styled" value="' . $item->pdb_id . '">';
            $row[] = '<a href="'. site_url('pesertadidik/pdbdetail/' . $item->pdb_id) .'">'. title($item->nama_lengkap) . '</a>';
            $row[] =  (($item->jenis_kelamin === 'L') ? 'Laki-laki' : 'Perempuan');
            $row[] =  $item->nisn;
            $row[] =  $item->tempat_lahir;
            $row[] =  $item->bentuk_pendidikan;
            $row[] =  $item->tanggal_lahir;
            $row[] =  $item->tanggal_daftar;
            $row[] =  '<a href="'. site_url('pesertadidik/pdbdetail/' . $item->pdb_id) . '" class="btn btn-info">' . lang('app_view_btn') . '</a> &nbsp;&nbsp; <a href="'. site_url('pesertadidik/delete_pdb/' . $item->pdb_id) . '" data-trigger="delete" class="btn btn-danger">' . lang('app_delete_btn') . '</a>';

            $data[] = $row;
        }

        $response = array(
                        "draw"            => $_POST['draw'],
                        "recordsTotal"    => $this->pdb_m->count_all(),
                        "recordsFiltered" => $this->pdb_m->count_filtered(),
                        "data"            => $data,
                );

        return $this->output
            ->set_content_type('application/json')
            ->set_status_header(200)
            ->set_output(json_encode($response));

    }

    public function delete_pdb($id = NULL)
    {
        $this->load->model('pdb_m');

        if($this->pdb_m->delete($id))
        {
            redirect('pesertadidik/pdbaru');
        }
    }

    public function pdb_export()
    {
    $this->load->model('pdb_m');
    $this->load->helper('download');
    // Create new Spreadsheet object
      $spreadsheet = new Spreadsheet();
      $sheet = $spreadsheet->getActiveSheet();
    // Set document properties
        $spreadsheet->getProperties()->setCreator('alazka.sch.id')
          ->setLastModifiedBy('Alazka')
          ->setTitle('Peserta Didik Baru Tahun Ajaran 2019/2020')
          ->setSubject('Peserta Didik Baru TK/SD/SMP/SMA Alazka Tahun Ajaran 2019/2020')
          ->setDescription('Data Export Website Peserta Didik Baru TK/SD/SMP/SMA Alazka Tahun Ajaran 2019/2020');
    // add style to the header
        $styleArray = array(
          'font' => array(
            'bold' => true,
          ),
          'alignment' => array(
            'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
            'vertical'   => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
          ),
          'borders' => array(
              'bottom' => array(
                  'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
                  'color' => array('rgb' => '333333'),
              ),
          ),
          'fill' => array(
            'type'       => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
            'rotation'   => 90,
            'startcolor' => array('rgb' => '0d0d0d'),
            'endColor'   => array('rgb' => 'f2f2f2'),
          ),
        );
        $spreadsheet->getActiveSheet()->getStyle('A1:AD1')->applyFromArray($styleArray);
        // auto fit column to content
        foreach(range('A', 'AD1') as $columnID) {
          $spreadsheet->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
        }
        // set the names of header cells
          $sheet->setCellValue('A1', 'ID');
          $sheet->setCellValue('B1', 'Nama Lengkap');
          $sheet->setCellValue('C1', 'Nama Panggilan');
          $sheet->setCellValue('D1', 'Jenis Kelamin');
          $sheet->setCellValue('E1', 'NISN');
          $sheet->setCellValue('F1', 'Tempat Lahir');
          $sheet->setCellValue('G1', 'Tanggal lahir');
          $sheet->setCellValue('H1', 'Agama');
          $sheet->setCellValue('I1', 'Anak Ke');
          $sheet->setCellValue('J1', 'Status dikeluarga');
          $sheet->setCellValue('K1', 'Alamat Jalan');
          $sheet->setCellValue('L1', 'RT');
          $sheet->setCellValue('M1', 'RW');
          $sheet->setCellValue('N1', 'Nama Dusun');
          $sheet->setCellValue('O1', 'Desa Keluarahan');
          $sheet->setCellValue('P1', 'Kecamatan');
          $sheet->setCellValue('Q1', 'Kota/Kabupaten');
          $sheet->setCellValue('R1', 'No. Telepon Rumah');
          $sheet->setCellValue('S1', 'No. Telepon Seluler');
          $sheet->setCellValue('T1', 'Email');
          $sheet->setCellValue('U1', 'Nama Ayah');
          $sheet->setCellValue('V1', 'Pekerjaan Ayah');
          $sheet->setCellValue('W1', 'Nama Ibu Kandung');
          $sheet->setCellValue('X1', 'Pekerjaan Ibu');
          $sheet->setCellValue('Y1', 'Telp orangtua');
          $sheet->setCellValue('Z1', 'Sekolah asal');
          $sheet->setCellValue('AA1', 'Alamat sekolah asal');
          $sheet->setCellValue('AB1', 'Tahun ajaran');
          $sheet->setCellValue('AC1', 'Unit');
          $sheet->setCellValue('AD1', 'Tanggal Pendaftaran');

        $getdata = $this->pdb_m->get_data();

        // Add some data
        $x = 2;
        foreach($getdata as $get){
          $sheet->setCellValue('A' . $x, $x-1);
          $sheet->setCellValue('B' . $x, $get->nama_lengkap);
          $sheet->setCellValue('C' . $x, $get->nama_panggilan);
          $sheet->setCellValue('D' . $x, $get->jenis_kelamin);
          $sheet->setCellValue('E' . $x, $get->nisn);
          $sheet->setCellValue('F' . $x, $get->tempat_lahir);
          $sheet->setCellValue('G' . $x, $get->tanggal_lahir);
          $sheet->setCellValue('H' . $x, $get->agama);
          $sheet->setCellValue('I' . $x, $get->anak_keberapa);
          $sheet->setCellValue('J' . $x, $get->status_dikeluarga);
          $sheet->setCellValue('K' . $x, $get->alamat_jalan);
          $sheet->setCellValue('L' . $x, $get->rt);
          $sheet->setCellValue('M' . $x, $get->rw);
          $sheet->setCellValue('N' . $x, $get->nama_dusun);
          $sheet->setCellValue('O' . $x, $get->desa_kelurahan);
          $sheet->setCellValue('P' . $x, $get->kecamatan);
          $sheet->setCellValue('Q' . $x, $get->kabupaten);
          $sheet->setCellValue('R' . $x, $get->nomor_telepon_rumah);
          $sheet->setCellValue('S' . $x, $get->nomor_telepon_seluler);
          $sheet->setCellValue('T' . $x, $get->email);
          $sheet->setCellValue('U' . $x, $get->nama_ayah);
          $sheet->setCellValue('V' . $x, $get->pekerjaan_ayah);
          $sheet->setCellValue('W' . $x, $get->nama_ibu_kandung);
          $sheet->setCellValue('X' . $x, $get->pekerjaan_ibu);
          $sheet->setCellValue('Y' . $x, $get->telp_orangtua);
          $sheet->setCellValue('Z' . $x, $get->sekolah_asal);
          $sheet->setCellValue('AA' . $x, $get->alamat_sekolah_asal);
          $sheet->setCellValue('AB' . $x, $get->tahun_ajaran);
          $sheet->setCellValue('AC' . $x, $get->bentuk_pendidikan);
          $sheet->setCellValue('AD' . $x, $get->create_date);
          $x++;
        }

    //Create file excel.xlsx
      $writer = new Xlsx($spreadsheet);

      $folder = FCPATH . '/themes/default/uploads/pdb/';
      $filename = $folder . "Data Export PDB Website Alazka " . date('Y-m-d')  . '.xlsx';
      $writer->save($filename);
      if (file_exists($filename)) {
        force_download($filename, NULL);
      }
      else
      {
        exit('file not found');
      }
    //End Function index
    }

}
