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

class m_maptoon_test 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';

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

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

	/*
	return maptoon category list
	@param (numeric) primary key of category table
	@return array or FALSE
	 */
	public function get_category($idx='') {
		$idx = trim($idx);
		if(is_numeric($idx)==TRUE && $idx>0) {
			$query = $this->db->get_where($this->tb_name_category,['idx' => $idx]);
			return $query->row_array();
		} else {
			return FALSE;
		}
	}

	/*
	return episodes data by category id
	@param (numeric) primary key of category table
	@return array or FALSE
	 */
	public function get_episodes_by_category($cid) {
		if(is_numeric($cid)==TRUE && $cid>0) {
			$query = $this->db->get_where(
				$this->tb_name_episode,
				['mt_categories_idx' => $cid]);
			return $query->result_array();
		} else {
			return FALSE;
		}
	}

	/*
	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) {
			$query = $this->db->get_where($this->tb_name_maptoon_list, $arr);
			$row = $query->result_array();
			return $row;
		} else {
			return FALSE;
		}
	}

	/*
	insert maptoon data
	@return (boolean)
	 */
	public function insert_maptoon($arr) {
		if(is_array($arr)==TRUE) {
			return $this->db->insert($this->tb_name_maptoon_list,$arr);
		} else {
			return FALSE;
		}
	}

	public function get_cartoonist_list() {
		$this->db->order_by('name','ASC');
		$query = $this->db->get($this->tb_name_cartoonist);
		return $query->result_array();
	}

	public function get_cartoonist_idx_by_name($name) { //test
		$this->db->select("idx");
		$this->db->where('name',$name);
		$query = $this->db->get($this->tb_name_cartoonist);
		return $query->result_array();
	}

	/*
	get announcement list
	*/
	public function get_announcement_list(){
		$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');
		$query = $this->db->get();
		return $query->result_array();

	}

	public function get_mt_title(){
		$this->db->order_by("name","ASC");
		$this->db->select("idx");
		$this->db->select("name");
		$query = $this->db->get($this->tb_name_maptoon_list);
		return $query->result_array();
	}

	public function get_episode_list($req_data){
		$sort_list                = $req_data['sort_list']; // 최신 조회
		$date_reg                 = $req_data['date_reg'];  // 등록 날짜별
		$mt_categories_idx        = $req_data['category'];  // 카테고리
		$mt_list_idx              = $req_data['mt_name'];   // 맵툰 이름
		$title                    = $req_data['ep_title'];  // 맵툰 제목
		$sdate                    = $req_data['sdate'];     // 시작일
		$edate                    = $req_data['edate'];     // 종료일
		$author                   = $req_data['author'];    // 작가
		$offset                   = $req_data['offset'];
        $limit                    = $req_data['limit'];
		
		$this->db->select('a.*');
		$this->db->select('b.mt_cartoonists_idx');
		$this->db->from($this->tb_name_episode.' AS a');
		$this->db->join($this->tb_name_maptoon_list.' AS b', 'b.idx = a.mt_list_idx');
		if($mt_categories_idx!='') $this->db->where('mt_categories_idx',$mt_categories_idx);
		if($mt_list_idx!='') $this->db->where('mt_list_idx',$mt_list_idx);
		if($title!='')$this->db->where('title',$title);

		//작가 번호로 조회
		if($author!=''){
		for($i=0;$i<=count($author);$i++){
			$i==0? $this->db->where('reg_date',$author[$i]['idx']) : $this->db->or_where('reg_date',$author[$i]['idx']);
		}}

		//등록날짜별 조회
		if(isset($date_reg)){
		switch ($date_reg) {
			case 'td'  : $this->db->where('DATE(reg_date) =',date("Y-m-d")); break;
			case 'yd'  : $this->db->where('DATE(reg_date) >=',date("Y-m-d", strtotime("-1 days"))); break;
			case 'w'  : $this->db->where('DATE(reg_date) >=',date("Y-m-d", strtotime("-7 days"))); break;
			case '30d'  : $this->db->where('DATE(reg_date) >=',date("Y-m-d", strtotime("-30 days"))); break;
			case 'lm'  : $this->db->where('DATE(reg_date) >=',date("Y-m-d", strtotime("-1 months"))); break;
			case 'sel'  : $this->db->where('DATE(reg_date) <=',$sdate); $this->db->where('DATE(reg_date) >=',$edate); break;
		}}

		$this->db->limit($limit, $offset);
		if($sort_list=="up") $this->db->order_by("reg_date","DESC");
		if($sort_list=="vi") $this->db->order_by("view_count","DESC");		

		$res = $this->db->get();
		$row = $res->result_array();
		return $row;
		
	}

}