<?php
    /**
     * Created by PhpStorm.
     * User: stes
     * Date: 24.05.18
     * Time: 18:49
     */
    
    namespace backend\controllers;
    
    use artbox\core\models\Language;
    use common\models\Price;
    use common\models\PriceLang;
    use common\models\Service;
    use yii\helpers\ArrayHelper;
    use yii\web\Controller;

    class PriceController extends Controller
    {
        public function actionIndex($service_id = null){
    
            if (\Yii::$app->request->post()){
                $price = \Yii::$app->request->post('Price');
                $price_lang = \Yii::$app->request->post('PriceLang');
                $transaction = \Yii::$app->db->beginTransaction();
                try {
                    Price::deleteAll(['service_id' => $service_id]);
                    foreach ($price as $key => $value){
                        $model = new Price();
                        $model->load($value, '');
                        $model->id = $key;
                        if ($model->save()){
                            $languages = [];
                            foreach ($price_lang[$key] as $k => $item){
                                $languages[] = [
                                    'language_id' => $k,
                                    'price_id'    => $model->id,
                                    'title'       => $item['title']
                                ];
                            }
                            \Yii::$app->db->createCommand()
                                          ->batchInsert(
                                              'price_lang',
                                              [
                                                  'language_id',
                                                  'price_id',
                                                  'title',
                                              ],
                                              $languages
                                          )
                                          ->execute();
                        }else{
                            continue;
                        }
                    }
                    $transaction->commit();
                    
                } catch (\Exception $e) {
                    $transaction->rollBack();
                    throw $e;
            
                } catch (\Throwable $e) {
                    $transaction->rollBack();
                    return false;
                }
        
            }
                $services = Service::find()->with('language')->all();
                $lastId = Price::find()->max('id');
                $prices = Price::find()->where(['service_id' => $service_id])->with('languages')->all();
                $data = ArrayHelper::map($services, 'id', 'title');
                
                return $this->render('index',[
                   'services' => $data,
                   'service_id' => $service_id,
                   'lastId' => $lastId,
                   'prices' => $prices
                ]);
                
        }
        
        public function actionNewPrice($lastId, $service_id = null){
            $languages = Language::getActive();
            $price = New Price();
            $price->service_id = $service_id;
            $price_lang = new PriceLang();
            return $this->renderAjax('_add_price',[
               'lastId' => $lastId,
               'price' => $price,
               'price_lang' => $price_lang,
               'service_id' => $service_id,
               'languages'  => $languages
            ]);
        }
    }