<?php

class Home_setting extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('Security', 'security_model');
        $this->load->model('Home_setting_Model');
        $this->security_model->admin($this->session->userdata('session_id'), $this->session->userdata('user_id'), $this->session->userdata('username'));
    }

    function index()
    {
        $data['_inactive_loading'] = true;
        $data['_view'] = "home_setting";
        $data['csrf'] = array(
            'name' => $this->security->get_csrf_token_name(),
            'hash' => $this->security->get_csrf_hash()
        );
        $data['objects'] = $this->Home_setting_Model->get();
        $this->load->view('backend/index', $data);
    }

    public function edit()
    {
        $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->Home_setting_Model->get_banner_id($id)), true);
                    $data['_view'] = 'home_setting_banner_edit';
                    break;
                case "title":
                    $this->load->model("App_recommand_setting_title_Model");
                    $data['title'] = json_decode(json_encode($this->Home_setting_Model->get_title_id($id)), true);
                    $data['_view'] = 'home_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('home_text')
            ->get()
            ->row_array();
        $data = [
            'title' => "Untitled",
            'content' => "Please enter the content",
            'displayorder' => $result['displayorder'] + 1,
            'display' => "0"
        ];
        $this->db->insert('home_text', $data);
    }

    public function add_image()
    {
        $result = $this->db->select_max('displayorder')
            ->from('banner')
            ->get()
            ->row_array();
        $data = [
            'path' => "default",
            'SD_path' => "default",

            'displayorder' => $result['displayorder'] + 1,
            'display' => "0"
        ];
        $this->db->insert('banner', $data);
    }

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

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

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

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

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

        $path = $this->upload_banner();
        $SD_path = $this->upload_SD_banner();
        if ($path != null) {
            $this->db->set('path', $path['url']);
        }
        if ($SD_path != null) {
            $this->db->set('SD_path', $SD_path['url']);
        }
        $this->db->set('display', $post['display'])
            ->where("id", $post['id'])
            ->update('banner');
        header('location:' . base_url('backend/home_setting'));
    }

    public function edit_title()
    {
        $post = $this->input->post();
        $get = $this->input->get();
        $this->db->set("content", $post['content'])
            ->set('title', $post['title'])
            ->set('display',$post['display'])
            ->where('id', $post['id'])
            ->update('home_text');
        header('location:' . base_url('backend/home_setting'));
    }

    function update_rank()
    {
        $this->load->model("Home_setting_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->Home_setting_Model->update_image_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->Home_setting_Model->update_text_rank($value, $rank);
                $rank = $rank + 1;
            }
        }
   


        return "";
    }

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

                break;
           
        }


        echo 'Deleted successfully.';
    }
    
}
