<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Model_uploadNilai extends CI_Model
{
	
    public function prosesUploadNilai($data)
    {

        $numrow = 0;
		$nilaiUpdate = [];
		$sudahVerifikasi = 0;
		$sudahTerdaftar =0;
        $jumlahUpload = 0;
        $operator = $this->db->get_where('user', ['email' => $this->session->userdata('email')])->row_array();
        foreach ($data as $value) {
            // if agar memulai dari baris ke 2
            if ($numrow > 1) {
					// definisikan nilai
					// akhir definisi nilaivar

					if($value['B']==null ||$value['B']=='' || $value['B']==null){
					continue;
					}
				
				// cek dulu apakah sudah di upload dan belum di daftarkan
				if(CekData::cekUpdateData('register',['nisn'=>$value['B'],'noOps'=>$operator['id'],'statusDaftar'=>1])==true){

					$dataNilai = MyNilai::getNilai($value, $operator['id']);
					// ambil data user
					$ambilDataUser = CekData::getData('register', ['nisn' => $value['B'], 'noOps' => $operator['id']]);
					
					// cek ke table nilai
					if(CekData::cekUpdateData('nilai',['nisn'=>$value['B'],'noOps'=>$operator['id']])==true){

						$this->db->set($dataNilai);
						$this->db->where(['id_pendaftar'=>$ambilDataUser['id'],'noOps'=>$operator['id']]);
						$this->db->update('nilai');
						$row[] = $value['C'];
						$nilaiUpdate[] = $row;
						continue;
					}else{
						$dataNilai['id_pendaftar'] = $ambilDataUser['id'];
						$this->db->insert('nilai', $dataNilai);
						$jumlahUpload++;
						continue;
					}
				}else if(CekData::cekUpdateData('register',['nisn'=>$value['B'],'noOps'=>$operator['id'],'statusDaftar'=>2])==true){
					$nama = CekData::getData('register',['nisn' => $value['B'], 'noOps' => $operator['id']]);
					$sudahTerdaftar++;
					continue;
				}else if(CekData::cekUpdateData('register',['nisn'=>$value['B'],'noOps'=>$operator['id'],'statusDaftar'=>3])==true){
					$nama = CekData::getData('register',['nisn' => $value['B'], 'noOps' => $operator['id']]);
					$sudahVerifikasi++;
					continue;
				} else if (CekData::cekUpdateData('nilai', ['nisn' => $value['B'], 'noOps' => $operator['id']]) == true){
				
					$dataNilai = MyNilai::getNilai($value, $operator['id']);
					$nisn = $dataNilai['nisn'];
					unset($dataNilai['nisn']);
					$this->db->set($dataNilai);
					$this->db->where(['nisn' => $nisn, 'noOps' => $operator['id']]);
					$this->db->update('nilai');
					$row[] = $value['C'];
					$nilaiUpdate[] = $row;

				}else{
					$dataNilai = MyNilai::getNilai($value, $operator['id']);
					$this->db->insert('nilai', $dataNilai);
					$jumlahUpload++;
				}
			
				
            }
            $numrow++;
        }
		// akhir dari loop

        $hasilUpload = [
            'upload' => $jumlahUpload,
            'pendaftarUpdate' => $nilaiUpdate,
            'sudahVerifikasi' => $sudahVerifikasi,
            'sudahTerdaftar' => $sudahTerdaftar
        ];
		

        return $hasilUpload;
    }

   
}
