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

class Auth extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->model('User_model');
		$this->load->library('session');
	}

	public function login_petugas2()
	{
		$username = $this->input->post('username', TRUE);
		$password = $this->input->post('password', TRUE);

		$user = $this->User_model->get_by_username($username);

		// === verifikasi MD5 (sesuai DB kamu) ===
		if ($user && $user->password === md5($password)) {
			$this->session->set_userdata([
				'user_id'       => $user->user_id,
				'username'      => $user->username,
				'nama_lengkap'  => $user->nama_lengkap,
				'logged_in'     => TRUE
			]);
			redirect('dashboard');
		} else {
			// kirim error ke view
			$this->session->set_flashdata('login_error', 'Username atau password salah');
			redirect(base_url()); // balik ke landing page
		}
	}

	public function login_petugas()
	{
		$username = $this->input->post('username', TRUE);
		$password = $this->input->post('password', TRUE);

		$user = $this->User_model->get_by_username($username);

		if ($user && $user->password === md5($password)) {

			// JIKA TB_USER SUDAH ADA id_petugas

			$petugas = $this->db->get_where('tb_petugas', ['id_user' => $user->user_id])->row();


			$session_data = [
				'user_id'       => $user->user_id,
				'username'      => $user->username,
				'nama_lengkap'  => $user->nama_lengkap,
				'logged_in'     => TRUE
			];

			if ($petugas) {
				$session_data['id_petugas'] = $petugas->petugas_id;
				$session_data['nama_petugas'] = $petugas->nama_lengkap;
				$session_data['nip'] = $petugas->nip;
			}

			$this->session->set_userdata($session_data);
			redirect('dashboard');
		} else {
			$this->session->set_flashdata('login_error', 'Username atau password salah');
			redirect(base_url());
		}
	}

	public function login_petugas_ajax()
	{
		if (!$this->input->is_ajax_request()) {
			show_404();
		}

		$this->output->set_content_type('application/json');

		// Ambil input
		$u = trim($this->input->post('username', TRUE));
		$p = trim($this->input->post('password', TRUE));

		// Validasi sederhana
		$errors = [];
		if ($u === '') $errors['username'] = 'Username wajib diisi';
		if ($p === '') $errors['password'] = 'Password wajib diisi';

		if (!empty($errors)) {
			return $this->output->set_output(json_encode([
				'ok' => false,
				'errors' => $errors,
				'csrf' => $this->security->get_csrf_hash(), // kalau CSRF aktif
			]));
		}

		// Cek user (MD5 sesuai permintaan)
		$user = $this->User_model->get_by_username($u);
		if (!$user || $user->password !== md5($p)) {
			return $this->output->set_output(json_encode([
				'ok' => false,
				'errors' => ['password' => 'Username atau password salah'],
				'csrf' => $this->security->get_csrf_hash(),
			]));
		}

		$petugas = $this->db->get_where('tb_petugas', ['id_user' => $user->user_id])->row();


		$session_data = [
			'user_id'       => $user->user_id,
			'username'      => $user->username,
			'nama_lengkap'  => $user->nama_lengkap,
			'logged_in'     => TRUE
		];

		if ($petugas) {
			$session_data['id_petugas'] = $petugas->petugas_id;
			$session_data['nama_petugas'] = $petugas->nama_lengkap;
			$session_data['nip'] = $petugas->nip;
		}

		// Sukses: set session
		$this->session->set_userdata($session_data);
		// $this->session->set_userdata([
		// 	'user_id'       => $user->user_id,
		// 	'username'      => $user->username,
		// 	'nama_lengkap'  => $user->nama_lengkap,
		// 	'id_petugas'=>
		// 	'logged_in'     => TRUE
		// ]);

		return $this->output->set_output(json_encode([
			'ok' => true,
			'redirect' => site_url('dashboard'),
			'csrf' => $this->security->get_csrf_hash(),
		]));
	}



	public function logout()
	{
		$this->session->sess_destroy();
		redirect(base_url());
	}
}
