<?php

defined('BASEPATH') or exit('No direct script access allowed');

class stampanti_ws extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('stampanti_model');
        $this->load->model('utility_model');
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST');
        header("Access-Control-Allow-Headers: X-Requested-With");
    }

    /**
     * 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
     */
    //    public function index() {
    //        if (!$this->ion_auth->logged_in()) {
    //            redirect('auth/login');
    //        }
    //        $this->load->view('comune/header');
    //        $this->load->view('comune/navbar');
    //        $this->load->view('welcome_message');
    //        $this->load->view('comune/footer');
    //    }

    public function inserisciStampante()
    {
        try {

            $ip = $this->input->post('ip');
            $porta = $this->input->post('porta');
            $descrizione  = $this->input->post('descrizione');
            $modello  = $this->input->post('modello');
            $this->stampanti_model->inserisciStampante($ip, $porta, $descrizione, $modello);
        } catch (Exception $e) {
            $this->utility_model->restituisciErroreWS($e);
            exit;
        }
    }

    public function getStampanti()
    {
        try {

            echo $this->stampanti_model->getStampanti();
        } catch (Exception $e) {
            $this->utility_model->restituisciErroreWS($e);
            exit;
        }
    }

    public function aggiornaStampante()
    {
        try {

            $ip = $this->input->post('ip');
            $porta = $this->input->post('porta');
            $descrizione  = $this->input->post('descrizione');
            $modello  = $this->input->post('modello');
            $this->stampanti_model->aggiornaStampante($ip, $porta, $descrizione, $modello);
        } catch (Exception $e) {
            $this->utility_model->restituisciErroreWS($e);
            exit;
        }
    }

    public function eliminaStampante()
    {
        try {

            $ip = $this->input->post('ip');
            $this->stampanti_model->eliminaStampante($ip);
        } catch (Exception $e) {
            $this->utility_model->restituisciErroreWS($e);
            exit;
        }
    }

    public function ping()
    {
        try {
            $ip = $this->input->post('ip');
            echo $this->stampanti_model->ping($ip);
        } catch (Exception $e) {
            $this->utility_model->restituisciErroreWS($e);
            exit;
        }
    }
}
