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

class Todo extends CI_Controller {
	public $filter = 'all';

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

		$this->load->helper('html');
		$this->load->helper('url');
		$this->load->helper('form');

		$this->load->model('model_todo');

		$this->filter = $this->input->get('filter') ?? 'all';
	}

	public function index()
	{
		$todos = $this->model_todo->getTodos($this->filter);
		$this->load->view('layout/header');
		$this->load->view('todos',['todos'=>$todos,'filter'=>$this->filter]);
		$this->load->view('layout/footer');
	}

	public function delete($id)
	{
		// TODO
	}

	public function toggle($id)
	{
		$this->model_todo->toggleTodo($id);
		redirect('/todo');
	}

	public function add()
	{
		$todo = ['text'=>$this->input->post('todo'),'done'=>0];
		$this->model_todo->addTodo($todo);
		$todos = $this->model_todo->getTodos();
		$this->index();
	}
	public function edit($id)
	{

		$this->load->library('form_validation');
		$this->load->model('model_todo');

		$todo = $this->model_todo->getTodo($id);
		$this->form_validation->set_rules('todo', 'Todo', 'required');
		if ($this->form_validation->run() === FALSE){
			$this->load->view('layout/header');
			$this->load->view('edit',['todo'=>$todo]);
			$this->load->view('layout/footer');


		}else{
			// TODO
		}
	}
}
