<?php
namespace App\Models;

use Core\Model;

class SksModel extends Model
{
    protected $table = 'tb_sks';

	public function updateOrInsert($nim, $tahun_akademik, $jml_sks, $status = 'Aktif')
	{
		// Cek apakah data sudah ada
		$sqlCheck = "SELECT * FROM tb_sks WHERE nim = :nim AND tahun_akademik = :tahun_akademik";
		$existing = $this->query($sqlCheck, ['nim' => $nim, 'tahun_akademik' => $tahun_akademik])->fetch();

		if ($existing) {
			// Update data jika ada
			$sqlUpdate = "UPDATE tb_sks SET jml_sks = :jml_sks, status = :status WHERE nim = :nim AND tahun_akademik = :tahun_akademik";
			return $this->query($sqlUpdate, [
				'jml_sks' => $jml_sks,
				'status' => $status,
				'nim' => $nim,
				'tahun_akademik' => $tahun_akademik,
			]);
		} else {
			// Insert data jika belum ada
			$sqlInsert = "INSERT INTO tb_sks (nim, tahun_akademik, jml_sks, status) VALUES (:nim, :tahun_akademik, :jml_sks, :status)";
			return $this->query($sqlInsert, [
				'nim' => $nim,
				'tahun_akademik' => $tahun_akademik,
				'jml_sks' => $jml_sks,
				'status' => $status,
			]);
		}
	}


}
