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

use Google\Service\CloudHealthcare\Result;
use Ngekoding\PhpHariLibur\Holiday;

class Login extends CI_Controller
{

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

    public function index($response = false)
    {

        $data_login = $this->db->get_where('sistem_login', [
            'email' => $this->session->userdata('email'),
            'status_akun' => 'AKTIF'
        ])->row_array();

        if ($data_login) {
            switch ($data_login['role_id']) {
                case 1:
                    redirect('owner');
                    break;
                case 2:
                    redirect('manager');
                    break;
                case 3:
                    redirect('keuangan');
                    break;
                case 4:
                    redirect('arisan');
                    break;
                case 5:
                    redirect('kasir');
                    break;
                case 6:
                    redirect('kabag_marketing');
                    break;
                case 7:
                    redirect('marketing');
                    break;
                default:
                    break;
            }
        } else {
            $result['message'] = $response;
            $this->load->view('loginpage', $result);
        }
    }

    public function authgoogle()
    {
        $profile        = $this->userlogin->oauth2_google()['profile'];
        $email          = $profile['email'];
        $picture        = $profile['picture'];
        $name_google    = $profile['name'];
        $result         = $this->Login_model->authgoogle($picture, $name_google, $email);
        if (isset($result->error)) :
            $this->session->set_flashdata('pesan_gagal', '<div class="alert alert-danger" id="pesan_gagal" role="alert">Email Tidak Terdaftar!</div>');
            redirect('/');
        else :
            $ip_address      = $this->input->ip_address();
            $data_login      = $this->session->userdata();
            $data_logs       = array(
                'login_id'   => $data_login['isloggedin'],
                'ip_address' => $ip_address,
                'sts_logs'   => 'IN'
            );
            $this->db->insert('logs_login', $data_logs);
            if ($data_login['level'] == 1) :
                redirect('owner');
            elseif ($data_login['level'] == 2) :
                redirect('manager');
            elseif ($data_login['level'] == 3) :
                redirect('keuangan');
            elseif ($data_login['level'] == 4) :
                redirect('arisan');
            elseif ($data_login['level'] == 5) :
                redirect('kasir');
            elseif ($data_login['level'] == 6) :
                redirect('kabag_marketing');
            elseif ($data_login['level'] == 7) :
                redirect('marketing');
            else :
                redirect('logout');
            endif;
        endif;
    }

    public function logout()
    {
        $ip_address      = $this->input->ip_address();
        $data_login      = $this->session->userdata();
        $data_logs       = array(
            'login_id'   => $data_login['isloggedin'],
            'ip_address' => $ip_address,
            'sts_logs'   => 'OUT'
        );
        $this->db->insert('logs_login', $data_logs);
        $this->session->sess_destroy();
        redirect(base_url());
    }
}