<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class user_controller extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->model('backend/user_model');
        $this->load->helper('url');
        $this->load->library(array('form_validation','session'));
    }
 
    function index() {
        if($this->session->userdata('logged_in'))
        {
            $data['success'] = "";
            $data['error'] = "";
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
            $data['id'] = $session_data['id'];
            
            $where['id'] = '1';
            if($this->input->post()){
                $post = $this->input->post();
                foreach($post as $key => $value){
                    if($key != "submit" && $key != "password"){
                        $post_data[$key] = $value; 
                    }
                    if($key == "password"){
                        $post_data[$key] = md5($value); 
                    }
                }
                
                $result = $this->user_model->save($post_data,$where);
                
                if($result){
                    $data['success'] = 'The username and password successfully updated.';
                }
                else{
                    $data['error'] = $this->user_model->getErrorMessage();
                }  
            }
            
            $get = $this->user_model->find($where);
            if($get){
                foreach ($get as $row)
                {
                   $data['username'] = $row->username;
                   $data['password'] = $row->password;
                }
            }
            else{
                $data['username'] = "";
                $data['password'] = "";
            }
            
            
            $this->load->view('backend/header');
            $this->load->view('backend/navigation');
            $this->load->view('backend/change_password', $data);
            $this->load->view('backend/footer');
            
        } else {
        //If no session, redirect to login page
            redirect('/administrator/login', 'refresh');
        }
    }
 
}