<?php

class Footer_management extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model("Footer_management_Model");
    }

    public function index()
    {
        $this->load->model("Footer_management_Model");
        $data['objects'] = $this->Footer_management_Model->get();

        $data['_inactive_loading'] = true;
        $data['_view'] = "footer_management";
        $data['csrf'] = array(
            'name' => $this->security->get_csrf_token_name(),
            'hash' => $this->security->get_csrf_hash()
        );
        $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['link'] = json_decode(json_encode($this->Footer_management_Model->get_social_media_get_id($id)), true);
                    $data['_view'] = 'Footer_management_social_media_edit';
                    break;
                case "title":
                    $this->load->model("App_recommand_setting_title_Model");
                    $data['link'] = json_decode(json_encode($this->Footer_management_Model->get_hyperlink_list_get_id($id)), true);
                    $data['_view'] = 'Footer_management_hyperlink_list_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_hyperlink()
    {
        $result = $this->db->select_max('displayorder')
            ->from('footer_hyperlink_list')
            ->get()
            ->row_array();
        $data = [
            'title' => "untitled",
            'link' => "untitled",
            'displayorder' => $result['displayorder'] + 1,
            'display' => "0"
        ];
        $this->db->insert('footer_hyperlink_list', $data);
    }

    public function add_social_media()
    {
        $result = $this->db->select_max('displayorder')
            ->from("footer_social_media")
            ->get()
            ->row_array();
        $data = [
            'path' => "default",
            'link' => "untitled",
            'displayorder' => $result['displayorder'] + 1,
            'display' => "0"
        ];
        $this->db->insert('footer_social_media', $data);
    }


    
    public function upload_image()
    {
        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_social_media()
    {
        $post =  $this->input->post();
        $get = $this->input->get();
        $path = $this->upload_image();
        if ($path != null) {
            $this->db->set("path", $path['url']);
        }
        $this->db->set("link", $post['link']);
        $this->db->set('display', $post['display']);
        $this->db->where("id", $post['id']);
        $this->db->update('footer_social_media');
        header("location:" . base_url('backend/footer_management'));
    }

    public function edit_hyperlink_list()
    {
        $post =  $this->input->post();
        $get = $this->input->get();
        $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('footer_hyperlink_list');
        header("location:" . base_url('backend/footer_management'));
    }

    function update_rank()
    {   $this->load->model("Footer_management_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 == "footer_social_media") {
            $params = array();
            parse_str($order, $searcharray);
            $rank = 1;
            foreach ($searcharray['listItem'] as $value) {
                $this->Footer_management_Model->update_image_rank($value, $rank);
                $rank = $rank + 1;
            }
        }
        if ($table_name == "footer_hyperlink_list") {
            $params = array();
            parse_str($order, $searcharray);
            $rank = 1;
            foreach ($searcharray['listItem'] as $value) {
                $this->Footer_management_Model->update_text_rank($value, $rank);
                $rank = $rank + 1;
            }
        }
        


        return "";
    }

    public function delete($id ,$type)
    { echo $type;
        switch ($type) {
            case "image":
                $this->load->model("footer_management_Model");
                $this->footer_management_Model->delete_social($id);
                break;
            case "text":
                $this->load->model("footer_management_Model");
                $this->footer_management_Model->delete_hyperlink_list($id);

                break;
           
        }


        echo 'Deleted successfully.';
    }
}
