<?php

namespace common\modules\support\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\web\NotFoundHttpException;

use common\models\Settings;
use common\modules\support\models\redmine\Issue;

/**
 * Default controller for the `support` module
 */
class SupportController extends Controller
{
	public $meta_title;
    public $meta_description;
    public $meta_keywords;
    
	public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'user' => 'support',
                'only' => ['index'],
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => ['index'],
                        'roles' => ['@'],
                    ],
                ],
            ],
        ];
    }

	public static function actionsTitles()
    {
        return [
            'Index'          => 'Техническая поддержка',
        ];
    }

    public function actionIndex()
    {
        $client = $this->getClient();

        $user = $client->user->getCurrentUser();

        $params = [
            'author_id' => $user['user']['id'],
        ];

        if(Yii::$app->request->get('s'))
        {
            $params = ArrayHelper::merge($params, [
                'f[]' => 'subject',
                'op[subject]' => '~',
                'v[subject][]' => Yii::$app->request->get('s'),
            ]);
        }

        $models = $client->issue->all($params);

        return $this->render('index', [
            'models' => $models,
        ]);
    }

    public function actionCreate()
    {
        $model = new Issue();

        $client = $this->getClient();

        $user = $client->user->getCurrentUser();

        if ($model->load(Yii::$app->request->post()) && $model->validate()) 
        {
            $client->issue->create([
                'subject' => $model->subject,
                'description' => $model->description,
                'project_id' => $model->project_id,
                'priority_id' => $model->priority_id,
                'status_id' => $model->status_id,
                'tracker_id' => $model->tracker_id,
                'author_id' => $user['user']['id']
            ]);

            return $this->redirect(['/support']);
        } 
        else 
        {
            return $this->render('create', [
                'model' => $model,
                'user' => $user
            ]);
        }
    }

    public function actionView($id)
    {
        $client = $this->getClient();

        $user = $client->user->getCurrentUser();

        $model = $client->issue->show($id, [
            'include' => [
                'attachments',
                'journals'
            ]
        ]);

        if(empty($model['issue']) || $model['issue']['author']['id'] != $user['user']['id'])
        {
            throw new NotFoundHttpException('Доступ запрещен!');
        }

        return $this->render('view', [
            'model' => $model['issue'],
        ]);
    }

    protected function getClient()
    {
        return $client = new \Redmine\Client(Settings::getValue('redmine-url'), Yii::$app->support->identity->redmine_key);
    }
}
