<?php

class Improvement extends AtadminController {

    var $module = 'improvement';
    public function __construct()
    {
        // $this->load does not exist until after you call this
        parent::__construct(); // Construct CI's core so that you can use it

//        $this->load->database();
//        $this->load->helper('language');
//        $this->load->library('session');
        $this->load->model('at-admin/Security','security_model');
        $this->load->model('at-admin/Improvement_model','improvement');
//        $this->load->model('at-admin/Language','language_model');
         //require_once(APPPATH.'controllers/admin/Security.php'); //include controller
            //$security = new Security();  //create object 
            //$security->permission(); //call function    

        //check user have login or not
        $this->security_model->admin($this->session->userdata('session_id'),$this->session->userdata('user_id'),$this->session->userdata('username'));
        //end check user have login or not
    }

    function index()
    {
        $data['users'] = $this->db->get('_users')->num_rows();
        $data['selectedmenu']="";
        $data['_inactive_loading'] = true;
        $data['page'] = $this->module;
        $data['controller'] = $this->router->fetch_class();
        $data['improvement'] = $this->improvement->get(true);
        $data['_view'] = 'improvement';
        $data['csrf'] = array(
            'name' => $this->security->get_csrf_token_name(),
            'hash' => $this->security->get_csrf_hash()
        );
        $this->load->view('at-admin/index', $data);
    }

    function edit()
    {

        if($this->input->post('save')){
            //echo "post save";
            $request = $this->input->post();
            $arr['title'] = $request['title'];
            if (empty($request['id'])) {
                $arr['entered'] = date('Y-m-d H:i:s');
                $id = $this->improvement->insert($arr);
            } else {
                $this->improvement->update($request['id'], $arr);
                $id = $request['id'];
            }
            header('location:'.base_url().'at-admin/improvement/edit?save=1&id='.$id);
            exit;
        }

        $request = $this->input->get();
        if (array_key_exists('id', $request)) {
            $id = $request['id'];
//            $data['languages'] = $this->language_model->get_all();
            $data['improvement'] = $this->improvement->get_id($id);
        }
        else {
            $data['improvement'] = [];
        }
        $data['save'] = array_key_exists('save', $request) ? $request['save'] : '';
        $data['_inactive_loading'] = true;
        $data['selectedmenu']="improvement";
        $data['_view'] = 'improvement_edit';
        $data['csrf'] = array(
            'name' => $this->security->get_csrf_token_name(),
            'hash' => $this->security->get_csrf_hash()
        );
        $this->load->view('at-admin/index', $data);

    }

    function delete($id)
    {
       //$this->db->delete('items', array('id' => $id));
       $this->improvement->delete($id);
       echo 'Deleted successfully.';
    }

    function update_rank(){
        // $table = txt_only($_GET['sortList']);
        // $r_name = txt_only($_GET['rank_name']);
        // $id = txt_only($_GET['id_name']);
        // $item_name = $_GET['listItem'];

        $table_name = $this->input->post('table_name');
        $rank_name = $this->input->post('rank_name');
        $id_name = $this->input->post('id_name');
        $order = $this->input->post('order');
        // var_dump($order);
        $params = array();
        // parse_str($order, $params);
        parse_str($order, $searcharray);
        // print_r($searcharray);
        $rank=1;
        // var_dump($searcharray['listItem']);
        foreach ($searcharray['listItem'] as $value) {
            // echo "<br>";
            // echo 'value';
            // echo $value;
            $this->improvement->update_rank($value,$rank);
            $rank=$rank+1;
        }



        return "";


    }
    
}

?>
