<?php

class Pageobjects extends CI_Controller {

    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->model('at-admin/Security','security_model');
        $this->load->model('at-admin/Page_objects_model','page_objects');
        $this->load->model('at-admin/Language','language_model');

        //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
    }

    public function edit()
    {
        if($this->input->post('save')){
            //echo "post save";
            $request = $this->input->post();
            switch ($request['page_object_type']) {
                case "TITLE":
                    $this->load->model('at-admin/Page_object_title_model', 'page_title');
                    $arr['title_type'] = $request['title_type'];
                    $arr['title'] = $request['title'];
                    $arr['summary'] = $request['summary'];
                    $this->page_title->update($arr, $request['id']);
                    break;
                case "PICTURES":
                    $this->load->model('at-admin/Page_object_picture_model', 'page_picture');
                    $arr['cols'] = $request['cols'];
                    $this->page_picture->update($arr, $request['id']);
                    break;
            }
            if ($request['submenu_id'] == 0) {
                header('location:' . base_url('at-admin/landingmain/edit?id=' . $request['main_id']));
            }
            else {
                header('location:' . base_url('at-admin/landingsubmenu/edit?id=' . $request['submenu_id']));
            }
            exit;
        }

        $request = $this->input->get();
        $id = $request['id'];
        $main_id = $request['main_id'];
        if (array_key_exists('submenu_id', $request)) {
            $submenu_id = $request['submenu_id'];
        }
        else {
            $submenu_id = 0;
        }
        $object = $this->page_objects->get($id);
        switch ($object['page_object_type']) {
            case "TITLE":
                $data['_view'] = 'pageobject_title_edit';
                break;
            case "PICTURES":
                $data['_view'] = 'pageobject_pictures_edit';
                break;
        }
        $data['save'] = array_key_exists('save', $request) ? $request['save'] : '';
        $data['_inactive_loading'] = true;
        $data['selectedmenu']="";
        $data['csrf'] = array(
            'name' => $this->security->get_csrf_token_name(),
            'hash' => $this->security->get_csrf_hash()
        );
        $data['objects'] = $object;
        $data['main_id'] = $main_id;
        $data['submenu_id'] = $submenu_id;
        $this->load->view('at-admin/index', $data);
    }

    public function add_title()
    {
        $request = $this->input->post();
        $this->add_object("T", $request['id'], $request['subid']);
    }

    public function add_picture()
    {
        $request = $this->input->post();
        $this->add_object("P", $request['id'], $request['subid']);
    }

    public function upload_picture()
    {
        if ($this->input->method() == 'post') {
            $request = $this->input->post();
            $this->load->model('at-admin/Upload_model', 'upload');
            if ($_FILES['picture']['error'] == 0) {
                $result = $this->upload->uploadfile('picture');
                if ($result['status'] == 'success') {
                    $max = $this->db->select_max('displayorder')
                        ->from('_page_object_pictures')
                        ->where(['picture_info_id' => $request['picture_info_id']])
                        ->get()->row_array();
                    $data = [
                        'picture_info_id' => $request['picture_info_id'],
                        'path' => $result['url'],
                        'displayorder' => $max['displayorder'] + 1,
                    ];
                    $this->load->model('at-admin/Page_object_picture_model', 'page_picture');
                    $this->page_picture->add_new_picture($data);
                }
            }
            if ($request['submenu_id'] == 0) {
                header('location: '. base_url('at-admin/pageobjects/edit?main_id=' . $request['main_id'] . '&id=' . $request['id']));
            }
            else {
                header('location: '. base_url('at-admin/pageobjects/edit?main_id=' . $request['main_id'] . '&submenu_id=' . $request['submenu_id'] . '&id=' . $request['id']));
            }
        }
    }

    private function add_object($object_type, $id, $subid)
    {
        $result = $this->db->select_max('displayorder')
            ->from('_page_objects')
            ->where(['main_id' => $id, 'submenu_id' => $subid])
            ->get()->row_array();
        switch ($object_type) {
            case "T":
                $data = [
                    'title_type' => 'M',
                    'title' => '[Untitled]',
                    'title_color' => '',
                    'summary' => '[Summary]',
                ];
                $this->db->insert('_page_object_title', $data);
                $insert_id = $this->db->insert_id();
                $data = [
                    'main_id' => $id,
                    'submenu_id' => $subid,
                    'displayorder' => $result['displayorder'] + 1,
                    'page_object_type' => 'TITLE',
                    'object_id' => $insert_id,
                ];
                break;
            case "P":
                $data = [
                    'cols' => 1,
                ];
                $this->db->insert('_page_object_picture_info', $data);
                $insert_id = $this->db->insert_id();
                $data = [
                    'main_id' => $id,
                    'submenu_id' => $subid,
                    'displayorder' => $result['displayorder'] + 1,
                    'page_object_type' => 'PICTURES',
                    'object_id' => $insert_id,
                ];
                break;
        }
        $this->db->insert('_page_objects', $data);
    }

    function update_rank(){

        $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');
        $params = array();
        parse_str($order, $searcharray);
        $rank = 1;
        foreach ($searcharray['listItem'] as $value) {
            $this->page_objects->update_rank($value,$rank);
            $rank=$rank+1;
        }
        return "";
    }

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

    function update_rank_picture(){

        $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');
        $params = array();
        parse_str($order, $searcharray);
        $rank = 1;
        $this->load->model('at-admin/Page_object_picture_model', 'page_picture');
        foreach ($searcharray['listItem'] as $value) {
            $this->page_picture->update_rank($value,$rank);
            $rank=$rank+1;
        }
        return "";
    }

    function delete_picture($id)
    {
        //$this->db->delete('items', array('id' => $id));
        $this->load->model('at-admin/Page_object_picture_model', 'page_picture');
        $this->page_picture->delete($id);
        echo 'Deleted successfully.';
    }

}
