<?php

class Landingmain_Model extends Atapp_Model
{

    private $lang;

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

    private function _autofill($arr)
    {
        $title = '';
        $title_en = '';
        if (array_key_exists('title', $arr)) {
            $title = trim($arr['title']);
        }
        if (array_key_exists('title_en', $arr)) {
            $title_en = trim($arr['title_en']);
        }
        if (($title !== "") && ($title_en !== "")) {
            return [
                'zh' => $title,
                'en' => $title_en,
            ];
        }
        if (($title === "") && ($title_en === "")) {
            return [
                'zh' => $title,
                'en' => $title_en,
            ];
        }
        if ($title === "") {
            return [
                'zh' => $title_en,
                'en' => $title_en,
            ];
        }
        return [
            'zh' => $title,
            'en' => $title,
        ];
    }

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

    }

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

    function get($include_hidden = false)
    {
        $this->db->select('*');
        $this->db->order_by("displayorder", "asc");
        if (!$include_hidden) {
            $this->db->where('display_in_menu', 1);
        }
        $this->db->from('_landing_main');
        $result = $this->db->get()->result();
        if ($this->lang == $this->config->item('system_default_language')) {
            return $result;
        }
        foreach ($result as $idx => $row) {
            $languages = $this->db->from('_landing_main_lang')
                ->where(['landing_main_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_main')->result();
        if ($this->lang == $this->config->item('system_default_language')) {
            return $result;
        }
        $languages = $this->db->where('landing_main_id', $id)
            ->where(['lang' => $this->lang])
            ->get('_landing_main_lang')->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_main');
        return $query->result();
    }

    function search_by_uri($controller)
    {
        $this->db->where('uri', $controller);
        $result = $this->db->get('_landing_main')->row_array();
        if ($this->lang == $this->config->item('system_default_language')) {
            return $result;
        }
        $language = $this->db->where('landing_main_id', $result['id'])
            ->where('lang', $this->lang)
            ->get('_landing_main_lang')
            ->row_array();
        if (! is_null($language)) {
            $result['title'] = $language['title'];
            $result['banner_desktop'] = $language['banner_desktop'];
            $result['banner_mobile'] = $language['banner_mobile'];
        }
        return $result;
    }

    function delete($id)
    {
        $this->load->model('at-admin/Landingsubmenu_Model', 'landingsubmenu');
        $this->landingsubmenu->deleteByMainId($id);
        $this->db->delete('_landing_main', ['id' => $id]);
        $this->db->delete('_landing_main_lang', ['landing_main_id' => $id]);
    }

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

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

    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_main')->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_main_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;
    }
}
