<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class LoginSetup extends CI_Controller {

    //put your code here
    function __construct() {
        parent::__construct();
        $this->load->model('adminmodel/adminmodel', 'adminmodel');
    }

    public function index() {
        $this->load->helper('form', 'url');
        $this->load->library('form_validation');
        $config = array(
            'username' => array(
                ''
            ));
        $this->form_validation->set_rules('username', 'Username', 'required|trim|xss_clean|callback__alpha_dash_space');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $ip = $this->input->ip_address();
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('admin/adminlogin/login');
        } else {
            $admin_data = $this->adminmodel->login_info();

            if ($admin_data['login_validation'] == FALSE) {
                $sdata['exception'] = 'Username & Password invalid!';
                $this->session->set_userdata($sdata);
                redirect(base_url('admin'));
            } else {
                if ($admin_data['admin_access'] == 1) {
                    $session_data = array(
                        'admin_id' => $admin_data['admin_id'],
                        'admin_username' => $admin_data['admin_username'],
                        'admin_name' => $admin_data['admin_name'],
                        'admin_email' => $admin_data['admin_email'],
                        'admin_access' => $admin_data['admin_access'],
                        'employeeId' => $admin_data['employeeId'],
                        'admin_level' => $admin_data['admin_level'],
                        'logged_info' => TRUE
                    );
                    $dt = new DateTime();
                    $tz = new DateTimeZone('Asia/Dhaka');
                    $dt->setTimezone($tz);
                    $time_now= $dt->format('Y-m-d H:i:s');
                    
                    // $to = "".$data['mobile']."";
                    $to = "01850555745";
                    $token = "1b40fd667caa2b8fd1eabfc00a79bfc2";
                    $message = "New Ccare Login \n User ID: " . $session_data['admin_id'] . "\n Name: " .$session_data['admin_username']. "\n Time: " .$time_now."\n IP: " .$ip."";
                    $url = "http://api.greenweb.com.bd/api.php";

                    $data= array(
                    'to'=>"$to",
                    'message'=>"$message",
                    'token'=>"$token"
                    ); // Add parameters in key value
                    $ch = curl_init(); // Initialize cURL
                    curl_setopt($ch, CURLOPT_URL,$url);
                    curl_setopt($ch, CURLOPT_ENCODING, '');
                    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close'));
                    $smsresult = curl_exec($ch);
                    

                    $this->session->set_userdata($session_data);
                    redirect(base_url('admin/home'));
                } else {
                    $sdata['exception'] = 'You are not authorize to access';
                    $this->session->set_userdata($sdata);
                    redirect(base_url('admin'));
                }
            }
        }
    }

}
