<?php

class Setmain extends MY_Controller
{

    private $parentCategoryName = 'grace-category';

    private $lifeCategories = array('ids' => array());

    public function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->library('post');
        $this->load->model('category_model');
        $this->load->model('editor_model');
        $this->load->model('life_model');
        $this->lifeCategories['result'] = $this->category_model->findById()->result();
        foreach ($this->lifeCategories['result'] as $c) {
            array_push($this->lifeCategories['ids'], $c->idx);
        }
    }

    public function index()
    {
    }

    public function homenews()
    {
        $configType = 'home';
        $configPage = 'hometopnews';
        $this->load->model('config_model');
        if ($this->input->post('title')) {
            $this->db->where('Page', $configPage)->delete('cm_config');
            foreach ($this->input->post('title') as $k => $title) {
                $this->db->insert($this->config_model->table, array(
                    'PostId' => $this->input->post('postid')[$k],
                    'Type' => $configType,
                    'Page' => $configPage,
                    'OrderNum' => $this->input->post('display_sort')[$k],
                ));
            }
            go($this->input->post('redirect'), 'Success');
        } else {

            $config = $this->config_model->findByTypeAndPage($configType, $configPage)->order_by('OrderNum')->get()->result();
            $data = array($configPage => null, 'posts' => array(null));
            if ($config) {
                $postIds = array();
                foreach ($config as $c) {
                    $postIds[] = $c->PostId;
                }
                $postIds = implode(',', $postIds);
                $query = $this->db->query("SELECT
                    `l`.*
                FROM
                    `news` `l`
                WHERE
                    `l`.`idx` IN ($postIds)");

                $post = $query->result();
                foreach ($post as $k => $p) {
                    foreach ($config as $c) {
                        if ($p->idx == $c->PostId) {
                            $post[$k]->display_sort = $c->OrderNum;
                            if (!$c->OrderNum) {
                                $post[$k]->display_sort = $k + 1;
                            }

                        }
                    }
                }
                $data = array($configPage => $config, 'posts' => $post);
            }

            $data['newsCategories'] = $this->db->where('is_del', 'N')->get('news_section')->result();

            $this->load->view('adm/pageset/main/topnews', $data);
        }

    }

    public function dailyqt()
    {
        $configType = 'home';
        $configPage = 'homedailyqt';
        $this->load->model('config_model');
        if ($this->input->post('title')) {
            $this->db->where('Page', $configPage)->delete('cm_config');
            foreach ($this->input->post('title') as $k => $title) {
                $this->db->insert($this->config_model->table, array(
                    'PostId' => $this->input->post('postid')[$k],
                    'Type' => $configType,
                    'Page' => $configPage,
                    'DpTime' => $this->input->post('dptime')[$k],
                ));
            }
            go($this->input->post('redirect'), 'Success');
        } else {
            $config = $this->config_model->findByTypeAndPage($configType, $configPage)->order_by('OrderNum')->get()->row();
            $data = array('dailyqt' => null, 'posts' => array(null));
            if ($config) {
                $query = $this->db->query("SELECT
                    `l`.*, c.DpTime
                FROM
                    `life` `l` inner join cm_config c on l.idx = c.PostId
                WHERE c.Type='$configType' and c.Page='$configPage'");

                $post = $query->result();
                $data = array('dailyqt' => $config, 'posts' => $post);
            }

            $data['lifeCategories'] = $this->lifeCategories['result'];
            $data['graceEditors'] = $this->editor_model->findByCategoryId($this->lifeCategories['ids'])->get()->result();

            $this->load->view('adm/pageset/main/dailyqt', $data);
        }

    }

    public function todayword()
    {
        $configType = 'home';
        $configPage = 'hometodayword';
        $this->load->model('config_model');
        if ($this->input->post('title')) {
            $this->db->where('Page', $configPage)->delete('cm_config');
            foreach ($this->input->post('title') as $k => $title) {
                $this->db->insert($this->config_model->table, array(
                    'PostId' => 0,
                    'Type' => $configType,
                    'Page' => $configPage,
                    'Title' => $this->input->post('title')[$k],
                    'Headline' => $this->input->post('headline')[$k],
                    'DpTime' => $this->input->post('dptime')[$k],
                ));
            }
            go($this->input->post('redirect'), 'Success');
        } else {
            $config = $this->config_model->findByTypeAndPage($configType, $configPage)->order_by('OrderNum')->get()->result();
            $data = array('posts' => array(null));
            if ($config) {
                // $ccc = array();
                // foreach($config as $c) $ccc[] = $c->PostId;
                // $query = $this->db->query("SELECT
                //     `l`.*
                // FROM
                //     `life` `l`
                // WHERE
                //     `l`.`idx` IN (".implode(',', $ccc).")");

                // $post = $query->result();
                $data = array('posts' => $config);
            }

            $data['lifeCategories'] = $this->lifeCategories['result'];
            $data['graceEditors'] = $this->editor_model->findByCategoryId($this->lifeCategories['ids'])->get()->result();

            $this->load->view('adm/pageset/main/todayword', $data);
        }

    }
}
