<?php
require( dirname(__DIR__).'/extensions/phpmailer/PHPMailerAutoload.php');
class AjaxStoFilterController extends Controller
{

    public function actionSend(){
        $text = Yii::app()->request->getParam('text');
        $name = Yii::app()->request->getParam('name');
        $phone = Yii::app()->request->getParam('phone');
        $email = Yii::app()->request->getParam('email');

        $to      = $email;
        $subject =  'Расчет стоимости ТО';
        $message = $text.'<br>Имя:'.$name.'<br>Телефон:'. $phone.'<br>Email:'. $email;
        $headers = 'From: webmaster@example.com' . "\r\n" .
            'Reply-To: webmaster@example.com' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

        mail($to, $subject, $message, $headers);

    }


    public function actionFilter(){

        $post = Yii::app()->request->getParam('StoFilter');

        if(!empty($post)){


            $form = new StoFilter();
            $form->attributes = Yii::app()->request->getParam('StoFilter');
            $filter['model'] = $form->searchFilter('model');
            $filter['engine'] = $form->searchFilter('engine');
            $filter['drive_unit'] = $form->searchFilter('drive_unit');
            $filter['transmission'] = $form->searchFilter('transmission');
            $filter['fuel_type'] = $form->searchFilter('fuel_type');




            if(!empty( $post['model']) && !empty( $post['engine']) && !empty( $post['drive_unit']) && !empty( $post['transmission'])&& !empty( $post['fuel_type'])){

                foreach($form->searchFilter('id') as $key => $val){

                    $dist =  new StoRequire();

                    $filter['mileage'] = $dist->searchFilter($key);;
                    $filter['id'] = $key;
                }


            }


            $json = json_encode($filter);
            die($json);

        }
    }


    public function actionRenderPriceList(){
        $id = Yii::app()->request->getParam('id');
        $dist = Yii::app()->request->getParam('dist');

        if(!empty($id) && !empty($dist)){



            $names = new StoRequireName();
            $items = new StoRequire();
            $optionalModel = new StoOptionalItems();

            $fields = Yii::app()->db->createCommand()
                ->select('*')
                ->from("{$names->tableName()} sn")
                ->rightJoin("{$items->tableName()} s", 'sn.id=s.sto_require_name_id')
                ->where('sn.sto_filter_id=:id AND s.dist=:dist', array(':id'=>$id,':dist' => $dist))->queryAll();

            $optional = Yii::app()->db->createCommand()
                ->select('*')
                ->from("{$optionalModel->tableName()} sn")
                ->where('sto_filter_id=:id', array(':id'=>$id))->queryAll();


            $this->renderPartial('/stoFilter/_priceList', array(
                'fields' => $fields,
                'optional' => $optional
            ));

        }
    }



}