<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Model_uploadBiodata extends CI_Model
{

	 // proses upload Biodata
    public function prosesUploadBiodata($data)
    {
        $numrow = 0;
        $nisnKosong = 0;
        $sudahVerifikasi = 0;
        $sudahDaftar = 0;
        $emailDuplikat = 0;
        $emailKosong = 0;
        $namaNisnDuplikast = [];
		$pendaftarDiupdate = [];
        $nisnLebih = [];
        $upload = 0;
        $operator = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
        // melakukan perulagan
        foreach ($data as $value) {
            // pilih baris yang ada datanya
            if ($numrow > 1) {
                $karakter = '123456789';
                $pin = substr(str_shuffle($karakter), 0, 5);
                $cekVerifikasi = CekData::cekUpdateData('register',['statusDaftar'=>3,'noOps'=>$operator['id'],'nisn'=>$value['B']]);
                $cekSudahDaftar = CekData::cekUpdateData('register',['statusDaftar'=>2,'noOps'=>$operator['id'],'nisn'=>$value['B']]);
				$cekBelumVerifikasi = CekData::cekUpdateData('register',['statusDaftar'=>1,'noOps'=>$operator['id'],'nisn'=>$value['B']]);

				    // prepare data untuk insert/update table register
                    $dataSiswa = [
                        'nama' =>  isset($value['C']) ? $value['C'] : "0",
                        'tempatLahir' => isset($value['D']) ? $value['D'] : "none",
                        'noOps' => $operator['id'],
                        'jk' => strtoupper(isset($value['E']) ? $value['E'] : "L"),
                        'agama' => isset($value['F']) ? $value['F'] : "Islam",
                        'anak_ke' => isset($value['G']) ? $value['G'] : "1",
                        'alamat' => isset($value['K']) ? $value['K'] : "none",
                        'tinggiBadan' => isset($value['I']) ? $value['I'] : "0",
                        'beratBadan' => isset($value['J']) ? $value['J'] : "0",
                        'noHp' => isset($value['H']) ? $value['H'] : "0",
                        'sekolahAsal' => $operator['asal_sekolah_ops'],
                        'npsn' => $operator['npsnOps'],
                        'rt' =>  isset($value['L']) ? $value['L'] : "0",
                        'rw' =>  isset($value['M']) ? $value['M'] : "0",
                        'longitude' => $value['P'],
                        'nisn' => $value['B'],
                        'kodePos' => isset($value['N']) ? $value['N'] : "000",
                        'nik' => isset($value['O']) ? $value['O'] : "000",
                        'kk' => isset($value['Q']) ? $value['Q'] : "327",
                        'email' =>  isset($value['R']) ? $value['R'] : "default" . $pin . "@ppdb.com",
                        'pin' =>  password_hash(($pin), PASSWORD_DEFAULT),
                        'roles_user' => 4,
                        'isAktif' => 1,
                        'statusDaftar' => 1,
                        'panto' =>  $pin,
                    ];
					// prepare untuk data ke table wali
                    $dataWali = [
                        'nisn' => $value['B'],
                        'noOps' =>  $operator['id'],
                        'ayah' => isset($value['T']) ? $value['T'] : "-",
                        'ibu' =>  isset($value['AA']) ? $value['AA'] : "-",
                        'nikAyah' =>  isset($value['U']) ? $value['U'] : "327",
                        'nikIbu' =>  isset($value['AB']) ? $value['AB'] : "327",
                        'TanggalLahirAyah' => isset($value['V']) ? $value['V'] : "00-00-000",
                        'TanggalLahirIbu' => isset($value['AC']) ? $value['AC'] : "00-00-000",
                        'pendidikanAyah' => isset($value['W']) ? $value['W'] : "None",
                        'pendidikanIbu' =>  isset($value['AD']) ? $value['AD'] : "None",
                        'pekerjaanAyah' => isset($value['X']) ? $value['X'] : "None",
                        'pekerjaanIbu' => isset($value['AE']) ? $value['AE'] : "None",
                        'penghasilanAyah' => isset($value['Y']) ? $value['Y'] : "Kurang Dari 1 jt",
                        'penghasilanIbu' => isset($value['AF']) ? $value['AF'] : "Kurang dari 1 jt",
                        'notelepon' =>  isset($value['AG']) ? $value['AG'] : "08",
                        'status' => 1,
                    ];

				// cek pendaftar sudah pernah di upload dan belum di daftarkan/di verifikasi maka update datanya
				if($cekBelumVerifikasi ==true){
					$this->db->trans_start();
                    $dataPendaftar = CekData::getData('register', ['nisn' => $value['B'], 'noOps' => $operator['id']]);

                    $dataWali['idRegister'] = $dataPendaftar['id'];
					unset($dataSiswa['nisn']);
					unset($dataWali['nisn']);

					$this->db->where(['nisn'=>$value['B'],'noOps'=>$operator['id']]);
					$this->db->update('register', $dataSiswa);
					// update wali
					$this->db->where(['nisn'=>$value['B'],'noOps'=>$operator['id']]);
					$this->db->update('wali', $dataWali);
					$this->db->trans_complete();
					
					if ($this->db->trans_status() === TRUE)
					{
						// record data nama yang di update
					$row[] = $value['C'];
					$pendaftarDiupdate[] = $row;
					}
				}else if($cekVerifikasi ==true || $cekSudahDaftar==true){
					$sudahVerifikasi++;
					continue;

				}else if($value['B'] == null || $value['B'] == ""){
                   

					$nisnKosong++;
                    continue;
				} else if(strlen($value['B']) > 10){
					// nisn lebih dari 10 digit
					 $row = $value['B'];
                    $nisnLebih[] = $row;
                    continue;
				}else{
					// jika aman simpan data nya
					$this->db->trans_start();
					$this->db->insert('register', $dataSiswa);
                    if($this->db->affected_rows()>0){
                        $user = CekData::getData('register', ['nisn' => $value['B'], 'noOps' => $operator['id']]);
                        $dataWali['idRegister'] = $user['id'];
                    }
                    $this->db->insert('wali', $dataWali);
					$this->db->trans_complete();
					if ($this->db->trans_status() === TRUE)
					{
						// record data nama yang di simpan
						$upload++;
					}else{
						
					}
				}
            }
            $numrow++;
        }
        $hasil = [
            'upload' => $upload,
            'sudahVerifikasi' =>  $sudahVerifikasi,
            'emailDuplikat' =>  $emailDuplikat,
            'pendaftarUpdate'=>$pendaftarDiupdate,
            'namaNisnDuplikast' =>  $namaNisnDuplikast,
            'nisnLebih' => $nisnLebih
        ];
        return $hasil;
    }

}
