<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class m_maptoon extends CI_Model{

	var $tb_name_category = 'mt_categories';
	var $tb_name_episode = 'mt_episodes';
	var $tb_name_maptoon_list = 'mt_list';
	var $tb_name_notice = 'mt_notices';
	var $tb_name_cartoonist = 'mt_cartoonists';
	var $tb_name_ep_like ='';

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

        $this->load->database();
        $this->load->library('session');
	}

	/*
	return list of maptoon
	@param (array) search keywords keys & values
	@return array or FALSE
	 */
	public function get_maptoon_list($arr='') {
		if(is_array($arr)==TRUE && count($arr)>0) {
			$query = $this->db->get_where($this->tb_name_maptoon_list, $arr);
		} else {
			$query = $this->db->get($this->tb_name_maptoon_list);
		}
		$row = $query->result_array();
		return $row;
	}

	/*
	 return episode data by maptoon id and idx
	 @param array (mt_idx, episode_idx)
	 @return array or FALSE 
	 */
	public function get_episode_by_mt_idx_episode_idx($arr){
		$query = $this->db->get_where($this->tb_name_episode,$arr);
		$result= $query -> row();
		$data = (array)$result;
		if(count($data)==0){
			return false;
		}else{
			return $data;
		}
	}

	/*
	return num of episodes by maptoon idx
	@param (numeric) idx of mt_list table
	@return (numeric)
	 */
	public function get_display_episodes($req_data) {
		$mt_idx            = $req_data['idx'];
		$offset            = $req_data['offset'];
		$limit             = $req_data['limit'];
		
		$this->db->limit($limit, $offset);
		$_where=['mt_list_idx' => $mt_idx,
				'is_display' => 1,
				'DATE(display_date)<=' => date("Y-m-d H:i:s")];
		$this->db->order_by("idx","DESC");
		$query = $this->db->get_where($this->tb_name_episode,$_where);

		return $query->result_array();
	}

	/*
	return toal count of episodes by maptoon idx
	@param (numeric) idx of mt_list table
	@return (numeric)
	*/
	public function get_totalcount_episodes($mt_idx){   
		$_where=['mt_list_idx' => $mt_idx,
				'is_display' => 1,
				'DATE(display_date)<=' => date("Y-m-d H:i:s")];

		$query = $this->db->get_where($this->tb_name_episode,$_where);
		return $query->num_rows();
	}

	/*
	get announcement list
	@return array or FALSE
	*/
	public function get_announcement_list($mt_idx){
		$this->db->order_by($this->tb_name_notice.'.reg_date','DESC');
		$this->db->select($this->tb_name_notice.'.*');
		$this->db->select($this->tb_name_maptoon_list.'.name');
		$this->db->from($this->tb_name_notice);
        $this->db->join($this->tb_name_maptoon_list, $this->tb_name_maptoon_list.'.idx = '.$this->tb_name_notice.'.mt_list_idx');
        if($mt_idx!=0)$this->db->where($this->tb_name_notice.'.mt_list_idx',$mt_idx);
		
		$query = $this->db->get();
		return $query->result_array();
	}

	/*
	find like data and insert or delete
	return String message
	*/
	public function clcik_like($req_data){
		$query = $this->db->get($this->tb_name_ep_like, $req_data);
		$num=$query->num_rows();
		$message;
		if($num==0){
			//Insert
			$this->db->insert($this->tb_name_ep_like, $req_data);
			$message="좋아하기 버튼을 클릭했습니다.";
			$state=1;
		}else{
			//Delete
			$this->db->delete($this->tb_name_ep_like, $req_data);
			$message="좋아하기를 취소했습니다.";
			$state=3;
		}
		$return_data=array('message'=>$message,'state'=>$state);
		return $return_data;
	}

	public function grade_($req_data){
		$mb_idx = $req_data['mb_idx'];
		$ep_idx = $req_data['ep_idx'];
		$grade = $req_data['grade'];
	}
}