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

class Login_model extends CI_Model
{

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


    // Authentikasi untuk login dengan username dan password
    // public function auth($username, $password)
    // {
    //     if ($username && $password) {
    //         $result = $this->Tbl_login_model->auth($username, $password);
    //         if ($result->num_rows() > 0) {
    //             $row = $result->row();
    //             $sesi = array(
    //                 'isloggedin' => $row->iduser
    //             );
    //             $this->session->set_userdata($sesi);
    //             $iduser = $sesi['isloggedin'];
    //             $data   = array(
    //                 'session_at'    => date('Y-m-d H:i:sa')
    //             );
    //             $this->Tbl_login_model->update_session($iduser, $data);
    //             return $result;
    //         } else {
    //             return to_obj([
    //                 "error" => "Gagal! username/password salah"
    //             ]);
    //         }
    //     }
    //     return false;
    // }

    // Authentikasi untuk login dengan google
    public function authgoogle($picture, $name_google, $email = false)
    {
        if ($email) {
            $result = $this->Tbl_login_model->authgoogle($email);
            if ($result->num_rows() > 0) {
                $row = $result->row();
                $sesi = array(
                    'isloggedin' => $row->id,
                    'level'      => $row->role_id,
                    'email'      => $row->email
                );
                $this->session->set_userdata($sesi);
                $id = $sesi['isloggedin'];
                $data   = array(
                    'gambar_profil' => $picture,
                    'session_at'    => date('Y-m-d H:i:sa')
                );
                $this->Tbl_login_model->update_session($id, $data);
                return $result;
            } else {
                return to_obj([
                    "error" => "Gagal! email belum terdaftar / Status Blokir"
                ]);
            }
        }
        return false;
    }

    function getDataRandomAlamat()
    {
        $query = $this->db->select('*');
        $query = $this->db->from('sistem_alamat');
        $query = $this->db->order_by('rand()', 1);
        $query = $this->db->get();
        return $query->row_array();
    }
    function getDataRandomAnggota()
    {
        $query = $this->db->select('*');
        $query = $this->db->from('anggota');
        $query = $this->db->order_by('rand()', 1);
        $query = $this->db->get();
        return $query->row_array();
    }
}