<?php

namespace app\controllers;
if(!defined('ACCESS_CONTROL')) {header($_SERVER['SERVER_PROTOCOL']. ' 404'); exit('404 not found');}
class Tasks extends ControllerSession {
    public function get_index()
    {
        $m_task = new \app\models\Task();
        $tasks = array();
        $tasks_tmp = $m_task->getTasks();
        $tasks_logs_errors = $m_task->getCountLogErrors();

        if($tasks_tmp)
        {
            foreach($tasks_tmp as $t)
            {
                if($t['local_file'] && $t['remote_file'])
                {
                    $log = sprintf('<a href="%s">log <span class="text-danger">%s</span>/<span class="text-success">%s</span></a>', '?do=tasks&action=logs&id='.$t['id'], isset($tasks_logs_errors[$t['id']]) ? $tasks_logs_errors[$t['id']]['err'] : 0, isset($tasks_logs_errors[$t['id']]) ? $tasks_logs_errors[$t['id']]['ok'] : 0 );
                    $t['file'] = sprintf('size: %s <br>update from: %s | %s',
                        \lib\Helpers::size_readable(filesize($t['local_file'])),
                        \lib\Helpers::cut_long_string(parse_url($t['remote_file'], PHP_URL_HOST), 45),
                        $log
                    );
                }
                elseif($t['local_file'])
                    $t['file'] = sprintf('size: %s', \lib\Helpers::size_readable(filesize($t['local_file'])));
                else
                    $t['file'] = sprintf('remote: %s', \lib\Helpers::cut_long_string(parse_url($t['remote_file'], PHP_URL_HOST), 45));
                $t['cut_countries'] = \lib\Helpers::cut_long_string($t['countries'], 15);
                array_push($tasks, $t);
            }
        }
        $this->_template = 'tasks/index';
        $this->render($this->_template, array('tasks'=>$tasks));
    }

    public function get_new(array $data=array())
    {
        $this->_template = 'tasks/new';
        $this->render($this->_template, array('form_action' => '?do=tasks&action=create', 'errors' => isset($data['errors']) ? $data['errors'] : array() ,'task'=>isset($data['task']) ? $data['task'] : array()));
    }

    public function post_create()
    {
        $m_task = new \app\models\Task();
        $m_task->set($this->params['post']['task']);
        $m_task->countries_inverse = $m_task->countries_inverse == 'on' ? 1 : 0;

        $m_task->insertTask();

        if(!$m_task->getErrors())
            $this->redirect("?do=tasks&");
        else {
            $this->get_new(array('errors'=>$m_task->getErrors(), 'task' => $this->params['post']['task']));
        }
    }

    public function get_edit()
    {
        $m_task = new \app\models\Task();
        $id = (int)$this->params['get']['id'];
        $task = $m_task->getTask($id);
        if($task)
        {
            $this->_template = 'tasks/edit';
            $this->render($this->_template, array('task'=>$task, 'errors' => isset($data['errors']) ? $data['errors'] : array(),'form_action'=>'?do=tasks&action=update&id='.$id));
        }else{
            $this->redirect('?do=tasks');
        }
    }
    public function post_update()
    {
        $m_task = new \app\models\Task();
        $id = (int)$this->params['get']['id'];

        $task = $m_task->getTask($id);
        if($task)
        {
            $m_task->id = $id;
            $m_task->set($this->params['post']['task']);
            $m_task->countries_inverse = $m_task->countries_inverse == 'on' ? 1 : 0;
            if($m_task->updateTask())
                $this->redirect('?do=tasks');
            else
            {
                $this->_template = 'tasks/edit';
                $this->render($this->_template, array('task'=>$this->params['post']['task'], 'errors' => $m_task->getErrors(),'form_action'=>'?do=tasks&action=update&id='.$id));
            }

        }else{
            $this->redirect('?do=tasks');
        }
    }

    public function put_toggle_status()
    {
        $id = (int)$this->params['get']['id'];

        header('Content-type: application/javascript');
        $m_task = new \app\models\Task();
        $m_task->toggleStatus($id);

        $this->render('layouts/page_reload', array(), array('layout'=>false, 'type'=>'js'));
    }

    public function delete_index()
    {
        $m_task = new \app\models\Task();
        $id = (int)$this->params['get']['id'];

        $m_task->delete($id);
        $this->render('tasks/delete', array('task_id'=>$id), array('layout'=>false, 'type'=>'js'));
    }

    public function delete_clearstats()
    {
        $m_task = new \app\models\Task();
        $id = (int)$this->params['get']['id'];

        $m_task->clear_stats($id);
        $this->render('layouts/page_reload', [], array('layout'=>false, 'type'=>'js'));
    }

    public function get_logs()
    {
        $m_task = new \app\models\Task();
        $id = (int)$this->params['get']['id'];

        $task = $m_task->getTask($id);
        if($task)
        {
            $m_logs = new \app\models\TaskLogs();
            $logs = $m_logs->logs_for($id);
            $this->render('tasks/logs', array('logs'=>$logs, 'task'=>$task));
        }else{
            $app->flash('error', 'Task not found    ');
            $app->redirect($app->urlFor('tasks'));
        }
    }
} 