<?php

class categoryModel extends baseModel {

	public $tableName = 'categories';

	function get($id = 0) {
		if (!is_numeric($id)) {
			return -1;
		}
		$this->db->where('id = ?', $id);
		$category = $this->db->select('categories')[0];

		if ($category) {
			$this->db->where('category_id=?', $id);
			//$this->db->where('timestamp_end > NOW()');
			$this->db->where('is_active=?', 1);
			$this->db->where('project_type_id=?', 2);
			$category['count'] = $this->db->select('projects', 'COUNT(*) AS c')[0][c];
			//print_r($category['count']);
		}
		return $category;
	}
	// GET CATEGORY BY NAME
	function getbyname($cat_name = "") {
		if (!isset($cat_name)) {
			return -1;
		}
		$this->db->where('slug = ?', $cat_name);
		$category = $this->db->select('categories')[0];

		return $category;
	}

	function getList($cond = []) {
		
		if (!empty($cond['search']))
			$this->db->where("title LIKE '%{$cond['search']}%'");

		$this->db->where('active');
		$this->db->where('type=?', 'challenges');
		$this->db->order_by("sort ASC");
		$categories = $this->db->select('categories');
//		print_r($categories);
//		exit;

		foreach ($categories as $index => $category) {
    		// projects
			$this->db->where('project_type_id=?', 2);
			$this->db->where('category_id=?', $category['id']);
			//$this->db->where('cat_id=?', $category['id']);
			$this->db->where('is_active');
			
			//$this->db->where('sched=?', 1);
			//$this->db->where('type=?', 'challenges');
			//$this->db->where('timestamp_end > NOW()');
			$categories[$index]['count'] = $this->db->select('projects', 'COUNT(*) AS c')[0][c];
			//$categories[$index]['count'] = $this->db->select('api_challenges', 'COUNT(*) AS c')[0][c];

			// groups
			//$this->db->where('category_id=?', $category['id']);
			//$categories[$index]['count_groups'] = $this->db->select('groups', 'COUNT(*) AS c')[0][c];
		}

		return $categories;
	}

	/*
	function insert($data) {
		$this->db->insert('projects',
			array(
				'title' => $data['title'],
				'description' => $data['description'],
				'reviews_goal' => $data['reviews_goal'],
				'user_id' => $_SESSION['user_id']
			)
		);

		$id = $this->db->insert_id();

		return $id;
	}
	*/

	function getCategories() {
		$this->db->where('active');
		$this->db->where('type=?', 'challenges');
		return $this->db->select('categories');
	}

	function getCategory($id) {
		$this->db->where('id=?', $id);
		return $this->db->select('categories')[0];
	}

	function updateCategory($id, $data) {
		$this->db->where('id=?', $id);	
		$cur_image = $this->db->select_one("categories", ['image']);
		
		if(!empty($cur_image['image']) && isset($_FILES['image']) && !$_FILES['image']['error']) {
			@unlink($cur_image['image']);	
		}
		
			$file = $_FILES['image'];
				 
			if ($ext == 'jpg' || $ext == 'png' || $ext = 'jpeg' || $ext = 'gif' ) {	
				$imgname = strtolower(basename($_FILES["image"]["name"]));
				$destination = "static/uploads/categories/$imgname";
				move_uploaded_file($file['tmp_name'], $destination);
				$this->db->where('id=?', $id);
				$this->db->update('categories', ['image' => $destination]);					
			}			
		
		$this->db->where('id=?', $id);
		$this->db->update('categories', $data);
	}	
	
//	function updateCategory($id, $data) {
//		$this->db->where('id=?', $id);
//		$this->db->update('categories', $data);
//	}

//	function insertCategory($title) {
//		$this->db->insert('categories', ['title' => $title]);
//	}

	function insertCategory($data) {
		if (isset($_FILES['image']) && !$_FILES['image']['error']) {
			
			$file = $_FILES['image'];
			 
			$imgname = strtolower(basename($_FILES["image"]["name"]));
			if ($ext == 'jpg' || $ext == 'png' || $ext = 'jpeg' || $ext = 'gif') {				
				$destination = "static/uploads/categories/$imgname";
				move_uploaded_file($file['tmp_name'], $destination);
				$data['image'] = $destination;
			}
		}			
		$this->db->insert('categories', $data);
	}

	function deleteCategory($id) {
		$this->db->where('id=?', $id);
		$this->db->delete('categories');
	}

	function delete($id) {
		$this->db->where('id=?', $id);
		$this->db->delete('categories');
	}

	function getItem($id) {
		$this->db->where('id=?', $id);
		return $this->db->select('categories')[0];
	}
}