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

class Model_todo extends CI_Model {
	public function __construct()
	{
		$this->load->database();
	}
	public function deleteTodo($id)
	{
		$this->db->delete('todo',['id'=>$id]);
	}

	public function getTodos($filter='all')
	{
		$where_filter = ["done" => 1, "active" => 0, "all" => "%"];
		return $this
			->db
			->query("SELECT * FROM todo WHERE done LIKE ?",[$where_filter[$filter]])
			->result();
	}

	public function toggleTodo($id){

		/*	en utilisant Query Builder class 
		 *	$this
			 ->db
			 ->set('done','1-done',false)
			 ->where('id',$id)
			 ->update('todo');
		 */

		$sql = "UPDATE todo SET done = 1 - done WHERE id = ?";
		$this
			->db
			->query($sql,[$id]);
	}

	public function editTodo($id,$text)
	{
		// TODO
	}

	public function getTodo($id){
		$sql = "SELECT * FROM todo where id = ?";
		return  $this
			->db
			->query($sql,[$id])->row();
	}

	public function addTodo($todo)
	{
		$this->db->insert('todo', $todo);
		return $this
			->db
			->insert_id();
	}
}
