<?php if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class Home extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
    }


    public function index()
    {
        $this->load->model('post_model');
        $this->load->library('Cacher');
        $this->main();
    }

    private function main()
    {
        if ($this->isMobile()) {
            $this->mobile();
        } else {
            $this->data['header'] = $this->load->view('templates/others/header', $this->data, true);
            $this->data['footer'] = $this->load->view('templates/others/footer', '', true);
            $this->desktop();
        }
    }

    private function desktop()
    {
        $this->data['webtoons'] = $this->post_model->getPostsByCategoryId(getChildrenByParentId(20000), 6)->get()->result();
        $this->data['storyqt'] = $this->post_model->getPostsByPageIdAndCategoryId(30005, 30008, 6)->get()->result();
        $this->data['engMedi'] = $this->post_model->getPostsByPageIdAndCategoryId(30007, 30010, 6)->get()->result();
        $this->data['mchart'] = array_slice($this->cacher->melon(), 0, 7);
        $this->data['articles'] = getNewsArticleURL($this->post_model->getPostsByCategoryId(getChildrenByParentId(10000), 20)->get()->result());
        $this->data['dailyqt'] = $this->post_model->getPostsByPageId(30048, 2)->get()->result();
        $this->data['dailyword'] = $this->post_model->getPostsByCategoryId(37000, 1)->get()->row();
        $this->data['talkshow'] = $this->post_model->getPostsByPageId(30045, 7)->get()->result();
        $this->data['testimony'] = $this->post_model->getPostsByPageId(30044, 7)->get()->result();
        $this->data['sermon'] = $this->post_model->getPostsByPageId(30042, 7)->get()->result();
        $this->data['worship'] = $this->post_model->getPostsByPageId(30043, 7)->get()->result();
        $this->data['letter'] = $this->post_model->getPostsByPageId([30003, 30005], 27)->get()->result(); // 3, 5
        $this->data['pray'] = $this->post_model->getPostsByPageId(30009, 27)->get()->result(); // 9
        $this->data['book'] = $this->post_model->getPostsByPageId([30017, 30026], 27)->get()->result(); // 17, 26
        $this->data['meditate'] = $this->post_model->getPostsByPageId([30006, 30008], 27)->get()->result(); // 8, 6


        // ['webtoons','storyqt','engMedi','mchart','articles','dailyqt','dailyword','talkshow','testimony','sermon','worship','letter','pray', 'book', 'meditate']

        foreach($this->data as $k=>$v) {
           getPostsImageUrl($this->data[$k]);
        }

        $this->load->view('templates/others/home', $this->data);
    }

    private function mobile()
    {
        $this->data['storyqt'] = $this->post_model->getPostsByPageIdAndCategoryId(30005, 30008, 6)->get()->result();
        $this->data['featureToons'] = $this->post_model->getPostsRankingByRankTypeAndCategoryId('R', 20008, 4)
            ->join('cm_page_content co', 'pp.pageID=co.pageID', 'left')->get()->result();
        getResultWithAuthor($this->data['featureToons']);
        $this->data['talkshow'] = $this->post_model->getPostsByPageId(30045, 5)->get()->result();
        $this->data['testimony'] = $this->post_model->getPostsByPageId(30044, 5)->get()->result();
        $this->data['sermon'] = $this->post_model->getPostsByPageId(30042, 5)->get()->result();
        $this->data['worship'] = $this->post_model->getPostsByPageId(30043, 5)->get()->result();
        $this->data['articles'] = getNewsArticleURL($this->post_model->getPostsByCategoryId(getChildrenByParentId(10000), 8)->get()->result());
        $this->data['columns'] = getNewsArticleURL($this->post_model->getPostsByCategoryId(getChildrenByParentId(30011), 4)->get()->result());
        $this->load->view('mobile/home', $this->data);
    }
}
