<?php

class Comment extends MY_Controller
{
    private $menuCategories = array();

    public function __construct()
    {
        parent::__construct();        
        $this->load->model('editor_model');
        $this->load->model('comment_model');
        $this->menuCategories['은혜충전'] = $this->getSubCategoryId('grace-category');
        $this->menuCategories['크로스맵TV'] = $this->getSubCategoryId('crossmaptv-category');        
    }

    public function index()
    {
        $data = array(
            'menuCategories' => array_keys($this->menuCategories),
            'graceCategories' => $this->category_model->findById($this->categories['grace'])->result(),
            'graceEditors' => $this->editor_model->findByCategoryId($this->categories['grace'])->get()->result(),
        );
        $this->load->view($this->adminViewFolder . 'comment', $data);
    }

    private function tableClause()
    {
        if ($this->param('term')) {
            $this->db->like('l.title', $this->param('term'));
        }
        if ($this->param('category')) {
            $this->db->like('l.category_idx', $this->param('category'));
        }
        if ($this->param('editor')) {
            $this->db->like('p.idx', $this->param('editor'));
        }
    }

    public function table()
    {
        $request = $this->getRequest();
        $sort = $this->param('sort');
        $sortColumns = array('comments' => 'comment_count', 'reg_date' => 'reg_date');

        $this->comment_model->find()->limit($this->param('limit'))->offset($this->param('offset'))
        ->join('life f', 'cmt.content_idx=f.idx', 'left')
        ->join('category c', 'f.category_idx=c.idx', 'left')
        ->join('partner p', 'f.partner_idx=p.idx', 'left')
        ->where('cmt.content_type', '3')->where('f.is_del', 'N')->where('cmt.is_del', 'N');
        if($this->param('term')) {
            $this->db->group_start()->like('cmt.comment', $this->param('term'), 'both')
            ->or_like('f.title', $this->param('term'), 'both')->group_end();
        }
        $comments = $this->db->select('cmt.idx commentId, f.title, c.category_name, p.partner_name, cmt.comment, f.idx, cmt.reg_date, 0 likes, 0 bads')
        ->get()->result();
        $commentId = array();
        foreach($comments as $c) {
            array_push($commentId, $c->idx);
        }

        if(count($comments)) {
            $likes = $this->db->where_in('comment_idx', $commentId)
            ->select('count(idx) cnt, type_name, comment_idx')
            ->group_by('comment_idx, type_name')->get('comment_like_log')->result();
            foreach($comments as $k => $c) {                
                foreach($likes as $l) {
                    if($c->idx == $l->comment_idx) {
                        if($l->type_name == 'like_count') {
                            $comments[$k]->likes += $l->cnt;
                        }else {
                            $comments[$k]->bads += $l->cnt;
                        }
                    }
                }
            }
        }        

        if($this->param('term')) {
            $this->db->group_start()->like('cmt.comment', $this->param('term'), 'both')
            ->or_like('f.title', $this->param('term'), 'both')->group_end();
        }
        $commentTotalCount = $this->comment_model->find()
        ->join('life f', 'cmt.content_idx=f.idx', 'left')
        ->where('cmt.content_type', '3')->where('f.is_del', 'N')->where('cmt.is_del', 'N')
        ->count_all_results();

        $this->output
            ->set_content_type('application/json')
            ->set_output(json_encode(array('total' => $commentTotalCount,
                'rows' => $comments)));
    }



    public function content_delete() {
        $this->db->where('idx', $this->param('contentId'))->update($this->comment_model->table, array('is_del' => 'Y'));
        if ($deleted = $this->db->affected_rows()) {
            $this->response(array('deleted' => $deleted));
        }
    }

}
