<?php

class App_recommand_setting 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('Language', 'language_model');
        $this->load->model('App_recommand_setting_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['objects'] = $this->App_recommand_setting_Model->get();

        $data['_view'] = 'app_recommand_setting';
        $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/landingmain/edit?save=1&id=' . $id);
            exit;
        }*/

        $request = $this->input->get();

        if (array_key_exists('id', $request) && array_key_exists('type', $request)) {
            $id = $request['id'];
            $type = $request['type'];
            switch ($type) {
                case "image":
                    $this->load->model("App_recommand_setting_image_Model");
                    $data['image'] = json_decode(json_encode($this->App_recommand_setting_image_Model->get_id($id)), true);
                    $data['_view'] = 'app_recommand_setting_image_edit';
                    break;
                case "title":
                    $this->load->model("App_recommand_setting_title_Model");
                    $data['title'] = json_decode(json_encode($this->App_recommand_setting_title_Model->get_id($id)), true);
                    $data['_view'] = 'app_recommand_setting_title_edit';
                    break;
                case "link":
                    $this->load->model("App_recommand_setting_link_Model");
                    $data['link'] = json_decode(json_encode($this->App_recommand_setting_link_Model->get_id($id)), true);
                    $data['_view'] = 'app_recommand_setting_link_edit';
                    break;
            }
        } else {
        }
        $data['save'] = array_key_exists('save', $request) ? $request['save'] : '';
        $data['_inactive_loading'] = true;
        $data['selectedmenu'] = "landingmain";
        //  $data['_view'] = '';
        $data['csrf'] = array(
            'name' => $this->security->get_csrf_token_name(),
            'hash' => $this->security->get_csrf_hash()
        );
        $this->load->view('backend/index', $data);
    }

    public function add_title()
    {
        $result = $this->db->select_max('displayorder')
            ->from('app_text')
            ->get()
            ->row_array();
        $data = [
            'title' => "Untitled",
            'content' => "Please enter the content",
            'displayorder' => $result['displayorder'] + 1,
            'display' => "0"
        ];
        $this->db->insert('app_text', $data);
    }

    public function add_image()
    {
        $result = $this->db->select_max('displayorder')
            ->from('app_text')
            ->get()
            ->row_array();
        $data = [
            'path' => "default",
            'content' => "Please enter the content",
            'displayorder' => $result['displayorder'] + 1,
            'display' => "0"
        ];
        $this->db->insert('app_image', $data);
    }

    public function add_link()
    {
        $result = $this->db->select_max('displayorder')
            ->from('app_text')
            ->get()
            ->row_array();
        $data = [
            'title' => "untitled",
            'path' => "Please upload image",
            'link' => "Please enter the URL",
            'displayorder' => $result['displayorder'] + 1,
            'display' => "0"
        ];
        $this->db->insert('app_link', $data);
    }



    function delete($id, $type)
    {
        switch ($type) {
            case "image":
                $this->load->model("App_recommand_setting_image_Model");
                $this->App_recommand_setting_image_Model->delete($id);
                break;
            case "text":
                $this->load->model("App_recommand_setting_title_Model");
                $this->App_recommand_setting_title_Model->delete($id);

                break;
            case "link":
                $this->load->model("App_recommand_setting_link_Model");
                $this->App_recommand_setting_link_Model->delete($id);
                break;
        }


        echo 'Deleted successfully.';
    }

    function update_rank()
    {
        $this->load->model("App_recommand_setting_image_Model");
        $this->load->model("App_recommand_setting_title_Model");
        $this->load->model("App_recommand_setting_link_Model");
        $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');

        if ($table_name == "app_image") {
            $params = array();
            parse_str($order, $searcharray);
            $rank = 1;
            foreach ($searcharray['listItem'] as $value) {
                $this->App_recommand_setting_image_Model->update_rank($value, $rank);
                $rank = $rank + 1;
            }
        }
        if ($table_name == "app_text") {
            $params = array();
            parse_str($order, $searcharray);
            $rank = 1;
            foreach ($searcharray['listItem'] as $value) {
                $this->App_recommand_setting_title_Model->update_rank($value, $rank);
                $rank = $rank + 1;
            }
        }
        if ($table_name == "app_link") {
            $params = array();
            parse_str($order, $searcharray);
            $rank = 1;
            foreach ($searcharray['listItem'] as $value) {
                $this->App_recommand_setting_link_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_image()
    {
        $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('app_image');
            header('location:' . base_url('backend/app_recommand_setting'));
        } else {
            $this->db->set('content', $post['content']);
            $this->db->set('path', $path['url']);
            $this->db->insert('image');
            header('location:' . base_url('backend/app_recommand_setting'));
        }
    }


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

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


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

    public function edit_link()
    {
        $post = $this->input->post();
        $get = $this->input->get('id');
        $path = $this->upload_picture();
        if ($post['id'] != "-1") {
          
            if ($path != '') {
                $this->db->set('path', $path['url']);
            }
            $this->db->set('title',$post['title']);
            $this->db->set("link",$post['link']);
            $this->db->set('display', $post['display']);
            $this->db->where('id', $post['id']);
            $this->db->update('app_link');
            header('location:' . base_url('backend/app_recommand_setting'));
        } else {
            $this->db->set('title',$post['title']);
            $this->db->set("link",$post['link']);
            $this->db->set('path', $path['url']);
            $this->db->insert('applink');
            header('location:' . base_url('backend/app_recommand_setting'));
        }
    }
}
