<?php

class Share extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        // if (!$this->maintenanceMode) {
        //     $totSegments = $this->uri->total_segments();
        //     if (!is_numeric($this->uri->segment($totSegments))) {
        //         $pageURL = $this->uri->segment($totSegments);
        //     } elseif (is_numeric($this->uri->segment($totSegments))) {
        //         $pageURL = $this->uri->segment($totSegments - 1);
        //     }
        //     if ($pageURL == "") {
        //         $pageURL = "home";
        //     }
        //     $this->data['page'] = $this->cmap_page_model->getPage($pageURL);
        //     $this->data['posts'] = getPagePostsByCategoryId(getChildrenByParentId(30000), 15);
        //     $this->data['reads'] = $this->cmap_page_model->getPagePostByParentCategoryId(30000, 32, 0, 'V');
        //     $this->data['likes'] = $this->cmap_page_model->getPagePostByParentCategoryId(30000, 32, 0, 'L');
        //     if ($this->data['page']['pageTemplate'] != "") {
        //         $this->data['header'] = $this->load->view('templates/header', $this->data, true);
        //         $this->data['footer'] = $this->load->view('templates/footer', '', true);
        //         $this->load->view('templates/' . $this->data['page']['pageTemplate'], $this->data);
        //     } else {
        //         $this->error();
        //     }
        // } else {
        //     $this->maintenance();
        // }
    }

    public function save_comment()
    {
        $this->load->model('comment_model');
        $this->comment_model->save([
            'postID' => $this->input->post('postId'),
            'userID' => $this->session->userdata('userID'),
            'comment' => $this->input->post('comment'),
            'datePosted' => date('Y-m-d H:i:s')
        ]);
    }

    public function reply_comment()
    {
        $this->load->model('comment_model');
        $this->comment_model->save([
            'postID' => $this->input->post('postId'),
            'userID' => $this->session->userdata('userID'),
            'parentID' => $this->input->post('parentId'),
            'comment' => $this->input->post('comment'),
            'datePosted' => date('Y-m-d H:i:s')
        ]);
    }

    public function rating_comment()
    {
        if (getSessionUser()->is) {
            $this->load->model('comment_model');
            $this->comment_model->rating([
                'userID' => getSessionUser()->userID,
                'commentID' => $this->input->post('commentID'),
                'title' => $this->input->post('like') ? 1 : 2
            ]);
        }else {
            $this->response(array('code' => 405, 'error' => '로그인 하세요'));
        }        
    }

    public function delete_comment()
    {
        $this->load->model('comment_model');
        if (getSessionUser()->userID == $this->comment_model->getOne($this->input->post('commentID'))->userID) {
            $this->comment_model->delete();
            $this->response(array('code' => 200));
        } else {
            $this->response(array('code' => 405, 'error' => '잘못된 요청입니다.'));
        }
    }

    public function update_comment()
    {
        $this->load->model('comment_model');
        if (getSessionUser()->userID == $this->comment_model->getOne($this->input->post('commentID'))->userID) {
            $this->comment_model->update([
                'commentID' => $this->input->post('commentID'),                
                'comment' => $this->input->post('comment')
            ]);
            $this->response(array('code' => 200));
        } else {
            $this->response(array('code' => 405, 'error' => '잘못된 요청입니다.'));
        }
        
    }

    public function pray()
    {
        $this->load->model('pray_model');
        $prayer = $this->input->post('prayer');
        if($insertId = $this->pray_model->save([
            'pray' => $prayer,
            'userID' => getSessionUser()->is ? getSessionUser()->userID : 0,
            'dateUpdated' => date('Y-m-d H:i:s')
        ])) {
            $this->response(array('code' => 200, 'commentId' => $insertId));
        }
    }
}
