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

class Frequency extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct() {
        parent::__construct();
        $this->load->model("Frequency_model", "freq");
    }
    public function index()
	{
        $res['view'] = 'pages/frequency';
        $res['data'] = array();
		$this->load->view('layout', $res);
    }

    public function get_data(){
        $res = $this->freq->get_all();
        header("Content-Type: application/json");
        echo json_encode($res["data"]);
    }

    public function add_row(){
        $duration = $this->input->post("duration");
        $row = array("created_date"=>date("Y-m-d H:i:s"), "duration" => $duration);
        $result = $this->freq->insert($row);
        $row["id"] = $result;
        header("Content-Type: application/json");
        echo json_encode($row);
    }

    public function delete_row(){
        $id = $this->input->post("id");
        $result = $this->freq->delete($id);
        echo json_encode(TRUE);
    }
}
