<?php
namespace App\Models;

use Core\Database;
use PDO;

class SidangModel
{
    private $db;

    public function __construct()
    {
        $this->db = Database::getConnection();
    }

    public function getSidangByNIM($nim)
    {
        $sql = "SELECT * FROM tb_sidang_skripsi WHERE nim = :nim";
        $stmt = $this->db->prepare($sql);
        $stmt->bindParam(':nim', $nim);
        $stmt->execute();
        return $stmt->fetch();
    }

    public function getMahasiswaByNIM($nim)
    {
        $sql = "SELECT * FROM tb_mahasiswa WHERE nim = :nim";
        $stmt = $this->db->prepare($sql);
        $stmt->bindParam(':nim', $nim);
        $stmt->execute();
        return $stmt->fetch();
    }

    public function getAllDosen()
    {
        $sql = "SELECT nama FROM tb_dosen ORDER BY nama ASC";
        $stmt = $this->db->query($sql);
        return $stmt->fetchAll();
    }

    public function insertSidang($data)
    {
        $sql = "INSERT INTO tb_sidang_skripsi 
            (nim, nama, prodi, nik, ttl, alamat, judul, pembimbing1, pembimbing2) 
            VALUES (:nim, :nama, :prodi, :nik, :ttl, :alamat, :judul, :pembimbing1, :pembimbing2)";
        $stmt = $this->db->prepare($sql);
        return $stmt->execute([
            ':nim' => $data['nim'],
            ':nama' => $data['nama'],
            ':prodi' => $data['prodi'],
            ':nik' => $data['nik'],
            ':ttl' => $data['ttl'],
            ':alamat' => $data['alamat'],
            ':judul' => $data['judul'],
            ':pembimbing1' => $data['pembimbing1'],
            ':pembimbing2' => $data['pembimbing2'],
        ]);
    }

	public function updateSidang($data)
	{
		$sql = "UPDATE tb_sidang_skripsi 
				SET nik = :nik, ttl = :ttl, alamat = :alamat, 
					judul = :judul, pembimbing1 = :pembimbing1, pembimbing2 = :pembimbing2 
				WHERE nim = :nim";
		$stmt = $this->db->prepare($sql);
		return $stmt->execute([
			':nik' => $data['nik'],
			':ttl' => $data['ttl'],
			':alamat' => $data['alamat'],
			':judul' => $data['judul'],
			':pembimbing1' => $data['pembimbing1'],
			':pembimbing2' => $data['pembimbing2'],
			':nim' => $data['nim'],
		]);
	}

    public function getTokenData($token, $kegiatan)
    {
        $sql = "SELECT id_token FROM tb_token WHERE token = :token AND kegiatan = :kegiatan";
        $stmt = $this->db->prepare($sql);
        $stmt->execute([
            ':token' => $token,
            ':kegiatan' => $kegiatan,
        ]);
        return $stmt->fetch();
    }

    public function insertTokenSkripsi($data)
    {
        $sql = "INSERT INTO tb_token_s_skripsi (token, nim, tgl_input) VALUES (:token, :nim, :tgl_input)";
        $stmt = $this->db->prepare($sql);
        return $stmt->execute([
            ':token' => $data['token'],
            ':nim' => $data['nim'],
            ':tgl_input' => $data['tgl_input'],
        ]);
    }

    public function deleteToken($id_token)
    {
        $sql = "DELETE FROM tb_token WHERE id_token = :id_token";
        $stmt = $this->db->prepare($sql);
        return $stmt->execute([':id_token' => $id_token]);
    }

    public function getTokenSkripsiByNIM($nim)
    {
        $sql = "SELECT * FROM tb_token_s_skripsi WHERE nim = :nim";
        $stmt = $this->db->prepare($sql);
        $stmt->bindParam(':nim', $nim);
        $stmt->execute();
        return $stmt->fetch();
    }
	
	public function searchDosenByName($keyword)
	{
		$keyword = "%$keyword%";
		$sql = "SELECT nama FROM tb_dosen WHERE nama LIKE ? LIMIT 10";
		$stmt = $this->db->prepare($sql);
		$stmt->execute([$keyword]);
		return $stmt->fetchAll(\PDO::FETCH_ASSOC);
	}

}
