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

class User extends CI_Controller 
{

 public function __construct()
 {
    parent::__construct();
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->load->library('password');
    $this->load->model('User_model');
    $this->load->library('userauth');
}

public function admin_user()
{
  $this->userauth->logged_admin();
  $data['all_users']=$this->User_model->get_user();
  $this->load->view('admin/admin-users',$data);
}

        //add user
public function add_user()
{
    $password = $this->password->encrypt_password($this->input->post('user_password'));
    $user_id = $this->input->post('user_id'); 
    $user= array('user_name'=>$this->input->post('user_name'),
        'user_password'=>$password);
    $action = $this->input->post('submit');
    $check_user=$this->User_model->check_user($user['user_name']);

    if($action == "Add")
    {
        if($check_user)
        {
            $user_data =$this->User_model->add_user($user);
            if($user_data)
            {
                $this->session->set_flashdata('success_msg','User Added Successfully');
                redirect('admin/admin-user');
            } else {
                $this->session->set_flashdata('error_msg','Error Occur');
                redirect('admin/admin-user'); 
            }
        }
        else {
            $this->session->set_flashdata('error_msg', 'User Already Exists.');
            redirect('admin/admin-user'); 
        }
    } 
    if($action == "Update")
    {
        $return_user = $this->User_model->return_user($user_id); 
        if($check_user)
        {
            $name = $this->input->post('user_name');
            $msg_type = "success_msg";
            $msg = "User Updated Successfully.";
        }
        else
        {
            $name = $return_user->user_name;
            if($this->input->post('user_name') == $return_user->user_name)
            {
              $msg_type = "success_msg";
              $msg = "User Updated Successfully.";  
          }
          else
          {
              $msg_type = "error_msg";
              $msg = "User Already Exists.";  
          }

      }
      $update_data= array('user_name'=>$name,
        'user_password'=>$password);
      $user_update = $this->User_model->update_user($update_data,$user_id);
      if($user_update)
      {
        $this->session->set_flashdata($msg_type,$msg);
        redirect('admin/admin-user');
    } else {
        $this->session->set_flashdata('error_msg','Error Occur');
        redirect('admin/admin-user'); 
    }
}

}
//end add user

        //return user

public function return_user($user_id)
{
  $this->userauth->logged_admin();
  $return_user = $this->User_model->return_user($user_id);
  $data['user_id']=$return_user->user_id;
  $data['user_name']=$return_user->user_name;
  $data['user_password'] = $this->password->decrypt_password($return_user->user_password);
  $data['all_users']=$this->User_model->get_user();
  $this->load->view('admin/admin-users',$data);
}

        //end return user

        //delete user

function delete_user($user_id)
{
    $delete_user = $this->User_model->delete_user($user_id);
    if($delete_user)
    {
        $this->session->set_flashdata('success_msg','User Deleted Successfully');
        redirect('admin/admin-user');
    } else {
        $this->session->set_flashdata('error_msg','Error Occur');
        redirect('admin/admin-user');   
    }
} 

        //end delete user

        //update status  

function update_status($user_id)
{
    $return_user = $this->User_model->return_user($user_id);
    $data['user_id']=$return_user->user_id;
    $data['user_status']=$return_user->user_status;  
    if($return_user->user_type !="admin")
    { 
        if($data['user_status']=="active")
        {
            $status ="inactive";
        }
        elseif($data['user_status']=="inactive")
        {
            $status ="active";   
        }
        $data['user_id']=$user_id;
        $this->User_model->update_status($status,$user_id);
    }
    else { $this->session->set_flashdata('error_msg',"Admin Can't Be Inactive."); }
    redirect('admin/admin-user');   
}

        //end update status 

}

?>
