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

class Login extends CI_Controller {
	public function __construct() {
        parent::__construct();
    }

	public function index(){
		if (strlen($this->session->userdata("iduser")) > 0) {
			redirect(app_url()."home");
		}
		$this->load->view("v_login");
	}

	public function process() {
		$this->load->helper("array");
		$this->load->helper("az_core");
		$username = azarr($_POST, "username");
		$password = azarr($_POST, "password");

		$this->db->select('username, iduser, user.name as user_name, user.idrole, role.name as role_name, idoutlet');
		$this->db->where("username", $username);
		$this->db->where("password", md5($password));
		$this->db->where('user.status', 1);
		$this->db->join('role', 'role.idrole = user.idrole', 'left');
		$data = $this->db->get("user");

		if ($data->num_rows() > 0) {
			$data_username = $data->row()->username;
			$data_id = $data->row()->iduser;
			$data_nama_user = $data->row()->user_name;
			$data_idrole = $data->row()->idrole;
			$data_role_name = $data->row()->role_name;
			$data_idoutlet = $data->row()->idoutlet;

			$this->session->set_userdata("username", $data_username);
			$this->session->set_userdata("iduser", $data_id);
			$this->session->set_userdata("name", $data_nama_user);
			$this->session->set_userdata('idrole', $data_idrole);
			$this->session->set_userdata('role_name', $data_role_name);
			$this->session->set_userdata('idoutlet', $data_idoutlet);

			redirect(app_url()."home");
		}		
		else {
			$this->session->set_flashdata("error_login", azlang('Wrong Username/Password'));
			redirect(app_url()."login");
		}	
	}

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

       	// ===== LOGIN OTOMATIS VIA DEVICE TERDAFTAR =====
   	public function auto_device() {
   		// 1) ambil "stiker" device dari cookie browser
   		$token = $this->input->cookie('az_device_token');
   		if (strlen($token) == 0) {
   			$this->session->set_flashdata('error_login', 'Device tidak dikenal. Silakan login manual.');
   			redirect(app_url().'login');
   			return;
   		}

   		// 2) cocokkan stiker dengan tabel device_registry
   		$this->db->where('device_token', $token);
   		$this->db->where('status', 1);
   		$device = $this->db->get('device_registry')->row();
   		if (!$device) {
   			$this->session->set_flashdata('error_login', 'Device tidak terdaftar/aktif. Silakan login manual.');
   			redirect(app_url().'login');
   			return;
   		}

   		// 3) device harus sudah dikaitkan dengan 1 user (mis. Kasir 1)
   		$iduser = isset($device->iduser) ? $device->iduser : '';
   		if (strlen($iduser) == 0) {
   			$this->session->set_flashdata('error_login', 'Device belum dikaitkan dengan user. Hubungi administrator.');
   			redirect(app_url().'login');
   			return;
   		}

   		// 4) ambil data user + role (cara sama persis seperti login manual)
   		$this->db->select('username, iduser, user.name as user_name, user.idrole, role.name as role_name, idoutlet');
   		$this->db->where('user.iduser', $iduser);
   		$this->db->where('user.status', 1);
   		$this->db->join('role', 'role.idrole = user.idrole', 'left');
   		$u = $this->db->get('user');
   		if ($u->num_rows() == 0) {
   			$this->session->set_flashdata('error_login', 'User pada device tidak aktif. Hubungi administrator.');
   			redirect(app_url().'login');
   			return;
   		}

   		// 5) buat session -> otomatis masuk
   		$this->session->set_userdata('username', $u->row()->username);
   		$this->session->set_userdata('iduser', $u->row()->iduser);
   		$this->session->set_userdata('name', $u->row()->user_name);
   		$this->session->set_userdata('idrole', $u->row()->idrole);
   		$this->session->set_userdata('role_name', $u->row()->role_name);
   		$this->session->set_userdata('idoutlet', $u->row()->idoutlet);

   		redirect(app_url().'home');
   	}
}