<?php

class PlaceModel extends MY_Model
{

    private $currentPage = 1;
    private $perPage = 100;


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

    private function getParam($key) {
        return isset($this->config[$key]) ? $this->config[$key] : '';
    }

    public function getTable($config)
    {
        $this->config = $config;
        // $this->dateRange = [strtotime($this->getParam('dateRange')[0]), strtotime($this->getParam('dateRange')[1])];
        $this->perPage = $this->getParam('limit');
        if ($this->getParam('offset')) {
            $this->currentPage = $this->getParam('offset') / $this->perPage + 1;
        }
        $preBookingResult = $this->getTableResult();
        $preBookingResultsCount = $this->getTableResultsCount();
        return array('total' => $preBookingResultsCount, 'rows' => ($preBookingResultsCount ? $preBookingResult : []));
    }

    private function getTableResult()
    {
        $this->getTableClause();
        $this->db->offset(($this->currentPage - 1) * $this->perPage);
        $this->db->limit($this->perPage);
        $this->db->order_by('CreatedAt', 'desc');
        return $this->db->get('cm_naver_place')->result();
    }

    private function getTableClause()
    {
        if ($this->getParam('keyword')) {
            $this->db->like('Name', $this->getParam('keyword'), 'both');
        }

        if ($this->getParam('category') !== "") {
            $this->db->where('Category', $this->getParam('category'));
        }else {
            $this->db->where('Category', '개신교');
        }
        
    }

    private function getTableResultsCount()
    {
        $this->getTableClause();
        $this->db->from('cm_naver_place');
        return $this->db->count_all_results();
    }

    public function updateCategory($id, $category) {
        $this->db->where('Id', $id)->update('cm_naver_place', array(
            'Category' => $category
        ));
    }

    public function saveChurch($church) {
        $this->db->where('NId', $church['NId'])->update('cm_naver_place', array(
            'Name' => $church['Name'],
            'Phone' => $church['Phone'],
            'Address' => $church['Address']            
        ));
        $registeredChurchQuery = $this->db->where('NId', $church['NId'])->get('cm_place_church');
        if($registeredChurchQuery->num_rows()) {
            $this->db->where('NId', $church['NId'])->update('cm_place_church', array(
                'Pastor' => $church['Pastor'],
                'Homepage' => $church['Homepage'],
                'Twitter' => $church['Twitter'],
                'Facebook' => $church['Facebook'],
                'Kakao' => $church['Kakao'],
                'Youtube' => $church['Youtube'],
                'NaverCafe' => $church['NaverCafe'],
                'DaumCafe' => $church['DaumCafe'],
                'Instagram' => $church['Instagram']
            ));
            $registeredChurchId = $registeredChurchQuery->row()->Id;
        }else {
            $this->db->insert('cm_place_church', array(
                'NId' => $church['NId'],
                'Pastor' => $church['Pastor'],
                'Homepage' => $church['Homepage'],
                'Twitter' => $church['Twitter'],
                'Facebook' => $church['Facebook'],
                'Kakao' => $church['Kakao'],
                'Instagram' => $church['Instagram'],
                'NaverCafe' => $church['NaverCafe'],
                'DaumCafe' => $church['DaumCafe'],
                'Youtube' => $church['Youtube'],
                'CreatedAt' => date('Y-m-d H:i:s')
            ));
            $registeredChurchId = $this->db->insert_id();

        }

        $this->db->where('ChurchId', $registeredChurchId)->delete('cm_church_service');
        foreach($church['Services'] as $service) {
            $this->db->insert('cm_church_service', array(
                'ChurchId' => $registeredChurchId,
                'Service' => $service['Service'],
                'DayName' => $service['DayName'],
                'Chapter' => $service['Chapter'],
                'StartAt' => $service['StartAt']
            ));
        }
        

        
        return true;
    }
}
