<?php

class WorkController extends NodeSiteController
{
    /** @var WorkRoot */
    public $workRoot;

    /** @var WorkBrand[] */
    private $_brands;
    /** @var WorkModel[] */
    private $_models;
    /** @var FeedbackForm */
    public $feedbackForm;

    /** @return WorkBrand[] */
    public function getBrands()
    {
        if (!isset($this->_brands))
            $this->_brands = WorkBrand::model()->with('i18n')->findAll(array(
                'index'=>'id',
                'order'=>'rank asc',
                'condition'=>'not t.hidden and work_root_id = '.$this->getNode()->data_id,
            ));
        return $this->_brands;
    }

    /** @return WorkModel[] */
    public function getModels()
    {
        if (!isset($this->_models))
            $this->_models = WorkModel::model()->with('i18n')->findAll(array(
                'index'=>'id',
                'condition'=>'not t.hidden',
            ));
        return $this->_models;
    }

    public function init()
    {
        parent::init();
        /** @var $sectionData StaticData */
        $this->workRoot = WorkRoot::model()->with('i18n')->findByPk($this->getNode()->data_id);
        $this->headerGalleryId = $this->workRoot->header_gallery_id;

        $this->feedbackForm = new FeedbackForm();
        if (isset($_POST['FeedbackForm'])) {
            Yii::app()->request->validateCsrfToken(null);
            $this->feedbackForm->setAttributes($_POST['FeedbackForm']);
            if ($this->feedbackForm->validate()&&empty($this->feedbackForm->link)) {
                /** @var $mailer Swift_Mailer */
                $mailer = Yii::app()->swiftMailer->getMailer();
                $message = Swift_Message::newInstance();
                $message->setReplyTo($this->feedbackForm->email);
                $message->setFrom('site@auto-life.ua');
                $message->setTo($this->workRoot->feedback_email);
                $message->setSubject('Вопрос, c  раздела “наши работи” сайта Авто-лайф');
                $message->setBody(
                    "Имя: {$this->feedbackForm->name}\r\n" .
                        "E-Mail: {$this->feedbackForm->email}\r\n" .
                        "Контактный номер: {$this->feedbackForm->phone}\r\n" .
                        "Текст вопроса: {$this->feedbackForm->body}\r\n");

                $mailer->send($message);
                Yii::app()->user->setFlash('contact', Yii::t('site', 'Спасибо что написали. Мы свяжесся с вами как только сможем'));
                $this->refresh();
            }
        }
    }


    public function actionIndex()
    {
        $this->pageName = $this->workRoot->i18n->page_name;
        $this->setSEOParams($this->workRoot->i18n->title, $this->workRoot->i18n->keywords, $this->workRoot->i18n->description);


        $brands = WorkBrand::model()->with('workModels', 'i18n')->findAll(array(
            'index'=>'id',
            'order'=>'rank asc',
            'condition'=>'not t.hidden and work_root_id = '.$this->getNode()->data_id,
        ));
        $this->_brands = $brands;
        /** @var $page StaticPage */
        $this->setContacts(explode(',',$this->workRoot->contacts_data));

        $this->render('index', array(
            'brands' => $brands,
        ));
    }

    public function actionBrand($brand)
    {
        /** @var $currentBrand WorkBrand */
        $currentBrand = WorkBrand::model()->with('i18n')->findByAttributes(array(
            'link' => $brand,
        ));
        if (empty($currentBrand))
            throw new CHttpException(404);
        $this->sectionGalleryId = $currentBrand->gallery_id;

        $this->pageName = $currentBrand->i18n->page_name;
        $this->setSEOParams($currentBrand->i18n->title, $currentBrand->i18n->keywords, $currentBrand->i18n->description);

        $this->setContacts(explode(',',$currentBrand->contacts_data));
        $this->setContacts(explode(',',$this->workRoot->contacts_data));

//        $models = $currentBrand->workModels;
        $models= WorkModel::model()->findAll(array(
            'condition'=>'not t.hidden and work_brand_id = :brand_id',
            'params'=>array(
                ':brand_id'=>$currentBrand->id,
            )
        ));

        $this->render('brand', array(
            'currentBrand' => $currentBrand,
            'models' => $models,
        ));
    }

    public function actionModel($brand, $model)
    {
        /** @var $currentBrand WorkBrand */
        $currentBrand = WorkBrand::model()->with('i18n')->findByAttributes(array(
            'link' => $brand,
        ));
        if (empty($currentBrand))
            throw new CHttpException(404);
        $this->sectionGalleryId = $currentBrand->gallery_id;
        /** @var $currentModel WorkModel */
        $currentModel = WorkModel::model()->with('i18n')->findByAttributes(array(
            'link' => $model,
            'work_brand_id' => $currentBrand->id,
        ));
        if (empty($currentModel))
            throw new CHttpException(404);
        if($currentModel->headerGalleryBehavior->getGalleryPhotoCount()>0){
            $this->sectionGalleryId = $currentModel->header_gallery_id;
        }
        $this->pageName = $currentModel->i18n->page_name;
        $this->setSEOParams($currentModel->i18n->title, $currentModel->i18n->keywords, $currentModel->i18n->description);

        $this->setContacts(explode(',',$currentModel->contacts_data));
        $this->setContacts(explode(',',$currentBrand->contacts_data));
        $this->setContacts(explode(',',$this->workRoot->contacts_data));

//        $works = $currentModel->works;
        $works= Work::model()->findAll(array(
            'condition'=>'not t.hidden and work_model_id = :model_id',
            'order'=>'date desc',
            'params'=>array(
                ':model_id'=>$currentModel->id,
            )
        ));

        $this->render('model', array(
            'currentModel' => $currentModel,
            'currentBrand' => $currentBrand,
            'works' => $works,
        ));
    }

    public function actionView($brand, $model, $set)
    {
        /** @var $currentBrand WorkBrand */
        $currentBrand = WorkBrand::model()->with('i18n')->findByAttributes(array(
            'link' => $brand,
        ));
        if (empty($currentBrand))
            throw new CHttpException(404);
        $this->sectionGalleryId = $currentBrand->gallery_id;
        /** @var $currentModel WorkModel */
        $currentModel = WorkModel::model()->with('i18n')->findByAttributes(array(
            'link' => $model,
            'work_brand_id' => $currentBrand->id,
        ));
        if (empty($currentModel))
            throw new CHttpException(404);

        /** @var $currentWork Work */
        $currentWork = Work::model()->with('i18n')->findByAttributes(array(
            'link' => $set,
            'work_model_id' => $currentModel->id,
        ));
        if (empty($currentWork))
            throw new CHttpException(404);


        $this->pageName = $currentWork->i18n->page_name;
        $this->setSEOParams($currentWork->i18n->title, $currentWork->i18n->keywords, $currentWork->i18n->description);

        $this->setContacts(explode(',',$currentWork->contacts_data));
        $this->setContacts(explode(',',$currentModel->contacts_data));
        $this->setContacts(explode(',',$currentBrand->contacts_data));
        $this->setContacts(explode(',',$this->workRoot->contacts_data));

        $this->render('view', array(
            'currentWork' => $currentWork,
            'currentModel' => $currentModel,
            'currentBrand' => $currentBrand,
        ));
    }

    /** @return TuningService[] */
    public function getServices()
    {
        return array(); //TuningService::model()->with('i18n')->findAll(array('order' => 'rank asc'));
    }

}