<?php
class Auth extends CI_controller
{

	function __construct()
	{
		parent::__construct();
		$this->load->model('Model_operator');
	}

	function login()
	{
		if (isset($_POST['submit'])) {
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			$hasil	= $this->Model_operator->login($username, $password);
			$foto	= $this->Model_operator->ambil_foto($username, $password);
			if ($hasil == TRUE) {
				$this->db->where('username', $username);
				$this->db->update('operator', array('last_login' => date('Y-m-d')));
				$this->session->set_userdata(array(
					'id' => $hasil->row()->id_operator,
					'status_login' => 'oke',
					'username' => $hasil->row()->username,
					'nama' => $hasil->row()->nama_operator,
					'akses' => $hasil->row()->id_akses,
					'foto' => $foto->foto,
				));
				redirect('Dashboard');
			} else {
				$this->session->set_flashdata('message_name', 'Username atau Password salah!!');
				redirect('Auth/login');
			}
		} else {
			$this->load->view('loginbaru');
		}
	}

	function logout()
	{
		$this->session->sess_destroy();
		redirect('Auth/login');
	}

	function form_update(){
		$this->load->view('form_login');


	}
	public function update_password()
    {
        // Pastikan POST request terkirim
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            // Ambil data password dari form
            $password = $this->input->post('password');
            $confirm_password = $this->input->post('confirm_password');

            // Validasi bahwa kedua password sesuai
            if ($password == $confirm_password) {
                // Jika sesuai, update password di database
                $id_operator = $this->session->userdata('id_operator'); // Sesuaikan dengan session operator Anda
                $this->db->where('id_operator', $id_operator);
                $this->db->update('operator', ['password' => md5($password)]);

                // Redirect ke halaman lain atau tampilkan pesan sukses
                redirect('success_page');
            } else {
                // Jika password tidak cocok, tampilkan pesan kesalahan
                echo "Password and Confirm Password do not match!";
            }
        }
    }
}
