<?php

class Success_cases extends CI_Controller
{

    var $module = 'landingmain';
    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('Security', 'security_model');
        $this->load->model('Landingmain_Model', 'landingmain_model');
        $this->load->model("Success_cases_Model");
        $this->load->model('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['landingmain'] = $this->landingmain_model->get(true);
        $data['cases'] = $this->Success_cases_Model->get(true);

        $data['_view'] = 'success_cases';
        $data['csrf'] = array(
            'name' => $this->security->get_csrf_token_name(),
            'hash' => $this->security->get_csrf_hash()
        );
        $this->load->view('backend/index', $data);
    }


    function edit()
    {

        /*    if ($this->input->post('save')) {
            //echo "post save";
            $request = $this->input->post();
            //            $id = $this->input->post('id');

            // $arr['main_id'] = $this->input->post('main_id');
            $arr['title'] = $this->input->post('title');
            $arr['title_en'] = $this->input->post('title_en');
            $arr['uri'] = $this->input->post('uri');
            $arr['displayorder'] = $this->input->post('displayorder');
            // $arr['keywords'] = $this->input->post('keywords');
            // $arr['lang'] = $this->input->post('lang');

            if (empty($request['id'])) {
                $arr['entered'] = date('Y-m-d H:i:s');
                $id = $this->landingmain_model->insert($arr);
            } else {
                $this->landingmain_model->update($request['id'], $arr);
            }
            header('location:' . base_url() . 'backend/success_cases/edit?save=1&id=' . $id);
            exit;
        }*/

        $request = $this->input->get();
        if (array_key_exists('id', $request)) {
            $id = $request['id'];
            $this->load->model('Success_cases_Model');
            $data['cases'] = json_decode(json_encode($this->Success_cases_Model->get_id($id)), true);
            } else {
            $data['cases'][0]['id'] = -1;
            $data['cases'][0]['content'] = '';
        }
        $data['save'] = array_key_exists('save', $request) ? $request['save'] : '';
        $data['_inactive_loading'] = true;
        $data['selectedmenu'] = "landingmain";
        $data['_view'] = 'success_cases_edit';
        $data['csrf'] = array(
            'name' => $this->security->get_csrf_token_name(),
            'hash' => $this->security->get_csrf_hash()
        );
        $this->load->view('backend/index', $data);
    }


    function delete($id)
    {
        //$this->db->delete('items', array('id' => $id));
        $this->Success_cases_Model->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->Success_cases_Model->update_rank($value, $rank);
            $rank = $rank + 1;
        }



        return "";
    }

    public function upload_picture()
    {
        if ($this->input->method() == 'post') {
            $request = $this->input->post();

            $this->load->model('Upload_model', 'upload');
            if ($_FILES['picture']['error'] == 0) {
                $result = $this->upload->uploadfile('picture');
                if ($result['status'] == 'success') {
                    return $result;
                }
            }
           
        }
    }


    public function edit_succes_cases()
    {
        $post = $this->input->post();

        $get = $this->input->get('id');

        $path = $this->upload_picture();
        if ($post['id'] != "-1") {
            $this->db->set('content', $post['content']);
            if ($path != '') {
                $this->db->set('path', $path['url']);
            }
            $this->db->set('display', $post['display']);
            $this->db->where('id', $post['id']);
            $this->db->update('success_cases');
            header('location:' . base_url('backend/success_cases'));
        } else {
            $this->db->set('content', $post['content']);
            $this->db->set('path', $path['url']);
            $this->db->insert('success_cases');
            header('location:' . base_url('backend/success_cases'));
        }
    }
}
