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

class accounts extends Admin_Controller {

    function __construct() {
        parent::__construct();
	}

	public function index() {
	    
	    $accounts = $this->db->query("SELECT * FROM `accounts` WHERE `is_deleted` = 0 ")->result();
	    
	    $this->template
    		->set('view', 'admin/accounts/index')
    		->set('accounts', $accounts)
            ->render();
	}
	

    public function action(){
    
        $name = $this->input->post('name');
        $type = $this->input->post('type');
        
        // if(!empty($name) && !empty($type)){
            $account_data = array(
	            'name'            => $name,
                'type'            => $type,
                'dateCreation'    => date('Y-m-d H:i:s'),
	        );

            $add_row = $this->general_model->insertRow('accounts', $account_data);

                   redirect("admin/accounts", "refresh");
        
            
        
	    
        }

        public function view(){

            $id = intval($this->uri->segment(4));
            $accounts = $this->db->query("SELECT * FROM `accounts` WHERE `id` = $id ")->row();
	    
            $this->template
                ->set('view', 'admin/accounts/view')
                ->set('accounts', $accounts)
                ->render();
            }
	

            public function edit(){
            
                 
                $id  = $this->input->post('accounts_id');
                $name = $this->input->post('name');
                $type = $this->input->post('type');
            
                $update_data = array(
                    'name'            => $name,
                    'type'            => $type,
                );
    
                $edit_row = $this->general_model->updateRowById('accounts', 'id', $id, $update_data);
    
                       redirect("admin/accounts/view/$id", "refresh");
            
                }

}