<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\entity\Tasktypes;
use app\models\entity\Tasks;
use app\models\entity\Tasknotes;
use app\models\entity\Accounts;
use app\models\entity\AccountsJobtypes;
use app\models\entity\Sections;
use app\models\entity\Companies;
use app\models\entity\Jobtypes;
use app\models\entity\ProjectsLocotech;
use app\models\entity\Projecttypes;

class AnalyticsController extends MainController
{
    private $threeWeeksAgo;
    
    public function beforeAction($action)
    {
        parent::beforeAction($action);
        $date = strtotime('-3 weeks');
        $this->threeWeeksAgo = date('Y-m-d', $date);
        $key = Yii::$app->request->headers->get('uuid-key');
        $this->checkAuth($key);
        $headerContent = Yii::$app->request->headers->get('content-type');
        \Yii::$app->response->format = \yii\web\Response::FORMAT_XML;
        if ( 'application/json' == $headerContent ) {
            \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        }
        return true;
    }
    
    public function actionCompanies()
    {
        $JobtypesAccountsEntity = Companies::find()->all();
        foreach( $JobtypesAccountsEntity as $jobtypeAccount ) {
            $value = [];
            $value['id'] = $jobtypeAccount->id;
            $value['name'] = $jobtypeAccount->name; 
            $value['address'] = $jobtypeAccount->address; 
            $arResult[] = $value;
        }
        return $arResult;
    }
    
    public function actionComands()
    {
        $tasksEntity = Tasks::find()->all();
        $arResult = [];
        foreach($tasksEntity as $task) {
            $value = [];
            $value['action'] = $task->project->action;
            $value['loco_type'] = $task->project->loco_type;
            $value['status'] = $task->project->status;
            $value['loco_number'] = $task->project->loco_number;
            $value['depo'] = $task->project->depo;
            $value['service'] = $task->project->depo_service;
            $value['created'] = $task->project->created;
            $value['section_id'] = $task->project->section_id;
            $value['id'] = $task->project->id;
            $value['created'] = $task->created;
            $value['finished_time'] = $task->finished_time;
            $value['accepted_time'] = $task->accepted_time;
            $value['expires'] = $task->expires;
            $value['type'] = $task->type;
            $value['account_id'] = $task->accounts->id;
            $value['accounts_name'] = $task->accounts->name;
            $value['task_status'] = $task->status;
            $value['task_id'] = $task->id;
            $arResult[] = $value;
        }
        return $arResult;
    }
    
    public function actionProjecttypes()
    {
        $projecttypesEntity = Projecttypes::find()->all();
        $arResult = [];
        foreach($projecttypesEntity as $projecttype){
            $value = [];
            $value['id'] = $projecttype->id;
            $value['name'] = $projecttype->name;
            $value['company'] = $projecttype->company;
            $arResult[] = $value;
         }
        return $arResult;
    }
    
    public function actionProjects()
    {
        $projectsEntity = ProjectsLocotech::find()
//            ->where(['>', 'created', $this->threeWeeksAgo])
            ->all();
        $arResult = [];
        foreach($projectsEntity as $projecttype) {
            $value['id'] = $projecttype->id;
            $value['action'] = $projecttype->action;
            $value['company'] = $projecttype->company;
            $value['status'] = $projecttype->status;
            $value['loco_type'] = $projecttype->loco_type;
            $value['loco_number'] = $projecttype->loco_number;
            $value['created'] = $projecttype->created;
            $value['tasks'] = $projecttype->tasks;
            $arResult[] = $value;
         }
        return $arResult;
    }
    
    public function actionTasktypes()
    {
        $arResult = [];
        $arrTaskstypes = Tasktypes::find()->all();
        foreach($arrTaskstypes as $taskstype){
            $value = [];
            $value['id'] = $taskstype->id;
            $value['name'] = $taskstype->name;
            $value['company'] = $taskstype->company;            
            $value['main_task'] = $taskstype->main_task;
            $value['time_to_complete_minutes'] = $taskstype->time_to_complete_minutes;
            $arResult[] = $value;
         }
        return $arResult;
    }
     
    public function actionTasks()
    {
        $arResult = [];
        $arrTaskstypes = Tasks::find()
//            ->where(['>', 'created', $this->threeWeeksAgo])
            ->all();
        foreach($arrTaskstypes as $taskstype) {
            $value['id'] = $taskstype->id;
            $value['status'] = $taskstype->status;
            $value['parent_id'] = $taskstype->parent_id;
            $value['type'] = $taskstype->type;
            $value['assignees_arr'] = $taskstype->assignees_arr;
            $value['input_id'] = $taskstype->input_id;
            $value['created'] = $taskstype->created;
            $value['accepted_time'] = $taskstype->accepted_time;
            $value['finished_time'] = $taskstype->finished_time;
            $value['assigned'] = $taskstype->assigned;
            $value['text'] = $taskstype->text;
            $arResult[] = $value;
         }
        return $arResult;
    } 
     
    public function actionAccounts()
    {
        $arrAccounts = Accounts::find()->all();
        foreach($arrAccounts as $accounts){
            $value = [];
            $value['id'] = $accounts->id;
            $value['name'] = $accounts->name;
            $value['cmdlevel'] = $accounts->cmdlevel;
            $value['company'] = $accounts->company;
            $value['last_seen_mobile'] = $accounts->last_seen_mobile;
            $value['overplan_mode'] = $accounts->overplan_mode;
            $arResult[] = $value;
         }
        return $arResult;
    }
    
    public function actionJobtypes()
    {
        $jobtypesModel = Jobtypes::find()->all();
        foreach($jobtypesModel as $jobtype){
            $value = [];
            $value['id'] = $jobtype->id;
            $value['name'] = $jobtype->name;
            $value['company'] = $jobtype->company;
            $arResult[] = $value;
        }
        return $arResult;
    }
    
    public function actionAccountsjobtypes()
    {
         $JobtypesAccountsEntity = AccountsJobtypes::find()->all();
        foreach( $JobtypesAccountsEntity as $jobtypeAccount ) {
            $value = [];
            $value['account_id'] = $jobtypeAccount->account_id;
            $value['jobtype_id'] = $jobtypeAccount->jobtype_id; 
            $value['priority'] = $jobtypeAccount->priority; 
            $arResult[] = $value;
        }
        return $arResult;
    }
    
    
    public function actionTasknotes()
    {
         $tasknotesEntity = Tasknotes::find()->all();
        foreach( $tasknotesEntity as $jobtypeAccount ) {
            $value = [];
            $value['id'] = $jobtypeAccount->id;
            $value['task_id'] = $jobtypeAccount->task_id;
            $value['checkpoint_id'] = $jobtypeAccount->checkpoint_id; 
            $value['text'] = $jobtypeAccount->text; 
            $value['date'] = $jobtypeAccount->date; 
            $arResult[] = $value;
        }
        return $arResult;
    }
    
    public function actionSections()
    {
        $SectionsEntity = Sections::find()->all();
        foreach($SectionsEntity as $section){
            $value = [];
            $value['id'] = $section->id;
            $value['locomotive_series'] = $section->locomotive_series;
            $value['section_number'] = $section->section_number;
            $value['section_subnumber'] = $section->section_subnumber;        
            $arResult[] = $value;
         }
        return $arResult;
    }
    
}