<?php

class Users extends CI_Controller {
    
    function login(){
        $this->load->view('login');
    }
    
    function login_validation(){
        $this->load->model('Users_model');

        $this->form_validation->set_rules('username','Username', 'required');
        $this->form_validation->set_rules('password','Password', 'required');
    
        if($this->form_validation->run() == false )
        {
            $this->login();
        }else{
            
            $username = $this->input->post('username');
            $password = $this->input->post('password');
        
            if($this->Users_model->canLogin($username, $password)) {
                
                $this->session->set_userdata('username', $username);
        
                redirect(base_url().'index.php/Users/index');
                
            }else{
                $this->session->set_flashdata('error', 'Invalid username or password');
                
                redirect(base_url().'index.php/Users/login');
            }
            
        }
    }
    
    private function check_login() {
        if (!$this->session->has_userdata('username')) 
        {
            redirect(base_url().'index.php/Users/login');
            return;
        }
    }
    
    function logout(){
        
        $this->session->unset_userdata('username');

        redirect(base_url().'index.php/Users/login');
        
    }
    
    function editPortal(){
        
        $this->load->model('Users_model');
        $user = $this->Users_model->getPortal();
    
        $data = array();
        $data['url'] = $user;
        
        $this->form_validation->set_rules('url','url', 'required');
    
        if($this->form_validation->run() == false )
        {
            $this->load->view('editportal', $data);
        }else{
            
            $fromArray = array();
            $fromArray['url'] = $this->input->post('url');
            
            $this->Users_model->updatePortal($fromArray);
            
            $this->session->set_flashdata('success', 'Portal is updated successfully');
            redirect(base_url().'index.php/Users/index');
        }
        
    }

    //  function index(){

    //     if($this->session->userdata('username'))
    //     {
    //         $this->load->model('Users_model');
    //         $users = $this->Users_model->all();
            
    //         $data = array();
    //         $data['users'] = $users;
            
    //         $this->load->view('list', $data);  
            
    //     }else{
    //         redirect(base_url().'index.php/Users/login');
    //     }        
    // }
    
 function fetch()
 {
    $output = '';
    $query = '';
    $this->load->model('Ajaxsearch_model');
    
    if($this->input->post('query'))
    {
        $query = $this->input->post('query');
    }
        
    $data = $this->Ajaxsearch_model->fetch_data($query);
  
  //**************
  $output .='
    <div class="col-md-12">
    <font size="2" >
        <table class="table table-bordered table-hover">
            <tr class="thead-dark">
                <th>ID</th>
                <th>Mac ID</th>
                <th>Date</th>
                <th>Time</th>
                <th>Expiry</th>
                <th>Device Info</th>
                <th>Note</th>
                <th>Status</th>
                <th>Edit</th>
                <th>Delete</th>
            </tr>';
            
    if($data->num_rows() > 0) {
        foreach($data->result() as $row) {
            
            $output .= 
            '<tr>
            <td>'.$row->id.'</td>
            <td>'.$row->macid.'</td>
            <td>'.$row->date.'</td>
            <td>'.$row->time.'</td>
            <td>'.$row->expiry.'</td>
            <td>'.$row->deviceinfo.'</td>
            <td>'.$row->note.'</td>';
            
            if(strcmp($row->status, 'false') == 0){
                $output .= '<td><input type="checkbox" id="actiont" value="'.$row->id.'" disabled/></td>';
            }else{
                $output .= '<td><input type="checkbox" id="actiont" value="'.$row->id.'" checked disabled/></td>'; 
            }
    
            $output .= '<td><a href='.base_url().'index.php/Users/edit/'.$row->id. ' class="btn btn-primary">Edit</a></td>';
            
            $output .=
             '<td><a href='.base_url().'index.php/Users/delete/'.$row->id. ' class="btn btn-danger">Delete</a></td>
              </tr>';
         
        } } else {
            
                $output .= 
                    '<tr>
                    <td colspan="5">Records Not Found.</td>
                    </tr>';
            }
            
        $output .= '</table></div>';
        echo $output;
    }
    
    function index()
    {
        if($this->session->userdata('username'))
        {
            $this->load->view('list');
        }else{
            redirect(base_url().'index.php/Users/login');
        } 
    }

    function create(){

        $this->load->model('Users_model');

        $this->form_validation->set_rules('macid','Mac ID', 'required');
        $this->form_validation->set_rules('date','Date', 'required');
        $this->form_validation->set_rules('time','Time', 'required');
        #$this->form_validation->set_rules('expiry','Expiry', 'required');
        $this->form_validation->set_rules('deviceinfo','Device Info', 'required');
        $this->form_validation->set_rules('status','Status', 'required');

        if($this->form_validation->run() == false )
        {
            $this->load->view("create");
        }else{

            $fromArray = array();

            $fromArray['macid'] = $this->input->post('macid');
            $fromArray['date'] = $this->input->post('date');
            $fromArray['time'] = $this->input->post('time');
            #$fromArray['expiry'] = $this->input->post('expiry');
            $fromArray['deviceinfo'] = $this->input->post('deviceinfo');
            $fromArray['status'] = $this->input->post('status');
            
            $expiryVal = $this->input->post('exp');
            
             if($expiryVal == 1){
                
                $fromArray['expiry'] = date('d, F Y', strtotime($date. ' + 1 days'));
                
            }elseif($expiryVal == 3){
                
                $fromArray['expiry'] = date('d, F Y', strtotime($date. ' + 3 month'));
                
            }elseif($expiryVal == 6){
                
                $fromArray['expiry'] = date('d, F Y', strtotime($date. ' + 6 month'));
                
            }elseif($expiryVal == 12){
                
                $fromArray['expiry'] = date('d, F Y', strtotime($date. ' + 1 year'));
                
            }elseif($expiryVal == 24){
                
                $fromArray['expiry'] = date('d, F Y', strtotime($date. ' + 2 year'));
                
            }

            $this->Users_model->create($fromArray);

            $this->session->set_flashdata('success', 'User is added successfully');
            redirect(base_url().'index.php/Users/index');
        }
    }

    function edit($userid)
    {
        $this->load->model('Users_model');
        $user = $this->Users_model->getUser($userid);
        
        $data = array();
        $data['user'] = $user;

        $this->form_validation->set_rules('macid','Mac ID', 'required');
        $this->form_validation->set_rules('deviceinfo','Device Info', 'required');
        

        if($this->form_validation->run() == false )
        {
            $this->load->view('edit', $data);
        }else{
            $fromArray = array();

            $fromArray['macid'] = $this->input->post('macid');
            $fromArray['email'] = $this->input->post('email');
            $fromArray['phone'] = $this->input->post('phone');
            $fromArray['deviceinfo'] = $this->input->post('deviceinfo');
            $fromArray['note'] = $this->input->post('note');
            
            $fromArray['status'] = isset($_POST['status']) ? "true" : "false";
            
            $expiryVal = $this->input->post('exp');
            
             if($expiryVal == 1){
                
                $fromArray['expiry'] = date('d, F Y', strtotime($date. ' + 1 days'));
                
            }elseif($expiryVal == 3){
                
                $fromArray['expiry'] = date('d, F Y', strtotime($date. ' + 3 month'));
                
            }elseif($expiryVal == 6){
                
                $fromArray['expiry'] = date('d, F Y', strtotime($date. ' + 6 month'));
                
            }elseif($expiryVal == 12){
                
                $fromArray['expiry'] = date('d, F Y', strtotime($date. ' + 1 year'));
                
            }elseif($expiryVal == 24){
                
                $fromArray['expiry'] = date('d, F Y', strtotime($date. ' + 2 years'));
                
            }

            $this->Users_model->updateUser($userid, $fromArray);

            $this->session->set_flashdata('success', 'User is updated successfully');
            redirect(base_url().'index.php/Users/index');
        }

    }
    
    
    function delete($userid){

        $this->load->model('Users_model');

        $this->Users_model->deleteUser($userid);
        redirect(base_url().'index.php/Users/index');

    }
    
    public function saveEntryy(){

		$this->load->model('usersData');

        $macid = $this->input->post("macid");
        $date =  $this->input->post("date");
        $time =  $this->input->post("time");
        $deviceinfo =  $this->input->post("deviceinfo");

        $db_data = array(
			'macid' => $macid,
            'date' => $date,
            'time' => $time,
            'deviceinfo' => $deviceinfo
        );
    
        $result = $this->usersData->addUser($db_data);
		echo json_encode($result);

	//end of function
	}
	

}

?>