<?php

class Landingsubmenu_Model extends Atapp_Model
{

    private $lang;

    public function __construct()
    {
        parent::__construct();
        $this->lang = get_contact_language();
    }

    function insert($arr)
    {
        $data = [
            'main_id' => $arr['main_id'],
            'title' => $arr['title'],
            'uri' => $arr['uri'],
            'display_in_menu' => $arr['display_in_menu'],
            'banner_desktop' => $arr['banner_desktop'],
            'banner_mobile' => $arr['banner_mobile'],
            'lang' => $this->config->item('system_default_language'),
        ];
        $this->db->insert('_landing_submenu', $data);
        $insertID = $this->db->insert_id();
        foreach ($this->language_supported as $lang) {
            if ($lang == $this->config->item('system_default_language')) {
                continue;
            }
            $data = [
                'main_id' => $arr['main_id'],
                'landing_submenu_id' => $insertID,
                'title' => $arr['title'],
                'banner_desktop' => $arr['banner_desktop'],
                'banner_mobile' => $arr['banner_mobile'],
                'lang' => $lang,
            ];
            $this->db->insert('_landing_submenu_lang', $data);
        }
        $this->load->model('at-admin/Page_settings_model', 'page_settings');
        $this->page_settings->update_page_settings($arr, $arr['main_id'], $insertID);
        return $insertID;

    }

    function update($id, $arr)
    {
        if ($this->lang == $this->config->item('system_default_language')) {
            $data = [
                'main_id' => $arr['main_id'],
                'title' => $arr['title'],
                'uri' => $arr['uri'],
                'display_in_menu' => $arr['display_in_menu'],
            ];
            if ($arr['banner_desktop'] != '') {
                $data['banner_desktop'] = $arr['banner_desktop'];
            }
            if ($arr['banner_mobile'] != '') {
                $data['banner_mobile'] = $arr['banner_mobile'];
            }
            $this->db->where('id', $id);
            $this->db->update('_landing_submenu', $data);
        }
        else {
            $data = [
                'main_id' => $arr['main_id'],
                'uri' => $arr['uri'],
                'display_in_menu' => $arr['display_in_menu'],
            ];
            $this->db->where('id', $id);
            $this->db->update('_landing_submenu', $data);
            $data = [
                'main_id' => $arr['main_id'],
                'title' => $arr['title'],
            ];
            if ($arr['banner_desktop'] != '') {
                $data['banner_desktop'] = $arr['banner_desktop'];
            }
            if ($arr['banner_mobile'] != '') {
                $data['banner_mobile'] = $arr['banner_mobile'];
            }
            $this->db->where('landing_submenu_id', $id);
            $this->db->where('lang', $this->lang);
            $this->db->update('_landing_submenu_lang', $data);
        }
        $this->load->model('at-admin/Page_settings_model', 'page_settings');
        $this->page_settings->update_page_settings($arr, $arr['main_id'], $id);
    }

    function get()
    {
        $this->db->select('ls.id,ls.main_id,lm.title as main_title,ls.title, ls.displayorder, ls.updated ');
        $this->db->order_by("main_id", "asc");
        $this->db->order_by("displayorder", "asc");
        $this->db->from('_landing_submenu ls');
        $this->db->where('submenu_id >', 0);
        $this->db->join('_landing_main lm', 'ls.main_id = lm.id');
        $result = $this->db->get()->result();
        if ($this->lang == $this->config->item('system_default_language')) {
            return $result;
        }
        $this->load->model('at-admin/Landingmain_Model', 'landingmain');
        foreach ($result as $idx => $row) {
            $main = $this->landingmain->get_id($row->main_id);
            if (! is_null($main)) {
                $result[$idx]->main_title = $main[0]->title;
            }
            $languages = $this->db->select('lang, title')
                ->from('_landing_submenu_lang')
                ->where('landing_submenu_id', $row->id)
                ->where('lang', $this->lang)
                ->get()->row_array();
            if (! is_null($languages)) {
                $result[$idx]->title = $languages['title'];
                $result[$idx]->banner_desktop = $languages['banner_desktop'];
                $result[$idx]->banner_mobile = $languages['banner_mobile'];
            }
        }
        return $result;
    }

    function getbymainid($id)
    {

        $this->db->select('ls.id,ls.main_id,lm.title as main_title,ls.title, ls.uri, ls.displayorder, ls.updated ');
        $this->db->where('main_id', $id);
        $this->db->order_by("main_id", "asc");
        $this->db->order_by("displayorder", "asc");
        $this->db->from('_landing_submenu ls');
        $this->db->join('_landing_main lm', 'ls.main_id = lm.id');
        $result = $this->db->get()->result();
        if ($this->lang == $this->config->item('system_default_language')) {
            return $result;
        }
        $this->load->model('at-admin/Landingmain_Model', 'landingmain');
        foreach ($result as $idx => $row) {
            $main = $this->landingmain->get_id($row->main_id);
            if (! is_null($main)) {
                $result[$idx]->main_title = $main[0]->title;
            }
            $languages = $this->db->from('_landing_submenu_lang')
                ->where('landing_submenu_id', $row->id)
                ->where('lang', $this->lang)
                ->get()->row_array();
            if (! is_null($languages)) {
                $result[$idx]->title = $languages['title'];
                $result[$idx]->banner_desktop = $languages['banner_desktop'];
                $result[$idx]->banner_mobile = $languages['banner_mobile'];
            }
        }
        return $result;
    }

    function get_id($id)
    {
        $this->db->where('id', $id);
        $result = $this->db->get('_landing_submenu')->result();
        if ($this->lang == $this->config->item('system_default_language')) {
            return $result;
        }
        $languages = $this->db->from('_landing_submenu_lang')
            ->where('landing_submenu_id', $result[0]->id)
            ->where('lang', $this->lang)
            ->get()->row_array();
        if (! is_null($languages)) {
            $result[0]->title = $languages['title'];
            $result[0]->banner_desktop = $languages['banner_desktop'];
            $result[0]->banner_mobile = $languages['banner_mobile'];
        }
        return $result;
    }

    function get_lang($lang)
    {

        $this->db->where('lang', $lang);
        $query = $this->db->get('_landing_submenu');
        return $query->result();

    }

    function search_by_uri($main_id, $action)
    {
        $this->db->where([
            'main_id' => $main_id,
            'uri' => $action,
        ]);
        $result = $this->db->get('_landing_submenu')->row_array();
        if ($this->lang == $this->config->item('system_default_language')) {
            return $result;
        }
        $languages = $this->db->from('_landing_submenu_lang')
            ->where('landing_submenu_id', $result['id'])
            ->where('lang', $this->lang)
            ->get()->row_array();
        if (! is_null($languages)) {
            $result['title'] = $languages['title'];
            $result['banner_desktop'] = $languages['banner_desktop'];
            $result['banner_mobile'] = $languages['banner_mobile'];
        }
        return $result;
    }

    function delete($id)
    {
        $this->db->delete('_landing_submenu', ['id' => $id]);
        $this->db->delete('_landing_submenu_lang', ['landing_submenu_id' => $id]);
    }

    function update_rank($id, $rank)
    {
        $data = array(
            'displayorder' => $rank,
        );

        $this->db->where('id', $id);
        $this->db->update('_landing_submenu', $data);

    }

    function findsubmenu($submenu)
    {
        //echo "search h1";
        //echo $page;
        $this->db->where('title', urldecode($submenu));
        $this->db->from('_landing_submenu');
        $query = $this->db->get();
        $ret = $query->row();
        return $ret->id;
    }

    public function deleteByMainId($main_id)
    {
        $ids = [];
        $submenu = $this->db->where('main_id', $main_id)
            ->get('_landing_submenu')->result_array();
        foreach ($submenu as $sub) {
            $ids[] = $sub['id'];
        }
        $this->load->model('at-admin/Page_settings_Model', 'page_settings');
        $this->load->model('at-admin/Page_objects_Model', 'page_objects');
        foreach ($ids as $id) {
            $this->page_settings->delete_page_settings($main_id, $id);
            $this->page_objects->delete_page_objects($main_id, $id);
            $this->delete($id);
        }
    }

    public function get_all_uploaded_pictures()
    {
        $pictures = [];
        $search = $this->picture_search_string;
        $landingmain = $this->db->like('banner_desktop', $search)
            ->or_like('banner_mobile', $search)
            ->get('_landing_submenu')->result_array();
        foreach ($landingmain as $row) {
            if (strpos($row['banner_desktop'], $search)) {
                $pictures[] = $row['banner_desktop'];
            }
            if (strpos($row['banner_mobile'], $search)) {
                $pictures[] = $row['banner_mobile'];
            }
        }
        $landingmain = $this->db->like('banner_desktop', $search)
            ->or_like('banner_mobile', $search)
            ->get('_landing_submenu_lang')->result_array();
        foreach ($landingmain as $row) {
            if (strpos($row['banner_desktop'], $search)) {
                $pictures[] = $row['banner_desktop'];
            }
            if (strpos($row['banner_mobile'], $search)) {
                $pictures[] = $row['banner_mobile'];
            }
        }
        return $pictures;
    }
}
