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

class Welcome extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
	}
	
	public function index()
	{
		if(isset($_SESSION['bot_admin_details'])){

            // $this->session->set_flashdata('info', '');
			redirect(site_url('admin/dashboard/'));
            // redirect(base_url());
            
        }
		
		if($this->input->post()){

			// echo "<pre>";
			// print_r($_POST);
			// exit;

			$data = $_POST;

			// get admin details 
			$admin = $this->cm->search_single_where(['id' => '1'], 'admin');
			if(empty($admin)){

				$this->session->set_flashdata('error', 'contact developer team, no admin found.');
				redirect(base_url());


				
			}else{
				
				if($admin['email'] == $data['email']){

					if($admin['password'] == md5($data['password'])){

						$admin_info['bot_admin_details'] = [
							'image' => $admin['image'],
							'email' => $admin['email']
						];
						$this->session->set_userdata($admin_info);

						$this->session->set_flashdata('success', 'Login success.');
						redirect(site_url('admin/dashboard/'));
						
					}else{

						$this->session->set_flashdata('error', 'Incorrect password.');
						redirect(base_url());
						
					}
					
				}else{
					
					$this->session->set_flashdata('error', 'Email address not found.');
					redirect(base_url());
					
				}
				
			}

			
		}else{

			$this->load->view('admin/auth/login');
		}
	}
	
	public function log_out(){
		
		unset($_SESSION['bot_admin_details']);
		
		$this->session->set_flashdata('success', 'Logout successfuly.');
		redirect(base_url());
		
	}
	
}
