<?php

namespace backend\controllers;

use backend\models\OrdersSearch;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\data\ActiveDataProvider;
use yii\web\HttpException;
use common\models\Orders;
use backend\models\OrdersProducts;
use common\modules\product\models\ProductVariant;
use yii\web\NotFoundHttpException;
use developeruz\db_rbac\behaviors\AccessBehavior;

class OrdersController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access'=>[
                'class' => AccessBehavior::className(),
                'rules' =>
                    ['site' =>
                        [
                            [
                                'actions' => ['login', 'error'],
                                'allow' => true,
                            ]
                        ]
                    ]
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
	
    public function actionIndex()
    {        
        $searchModel = new OrdersSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'dataProvider'=>$dataProvider,
            'searchModel'=>$searchModel,
            ]);
    }
	
    public function actionShow($id)
    {

		$model = $this->findModel((int)$id);
        $dataProvider = new ActiveDataProvider([
            'query' => OrdersProducts::find()->where(['order_id'=>(int)$id]),
            'pagination' => [
                'pageSize' => 20,
            ],
        ]);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        } else {
            $model_orderproducts = new OrdersProducts;


            return $this->renderAjax('show', [
                'model' => $model,
                'model_orderproducts'=>$model_orderproducts,
                'dataProvider' => $dataProvider,
            ]);
        }
    }
    
    public function actionLabelupdate(){
        $model = Orders::findOne($_POST['order_id']);
        $model->label = $_POST['label_id'];
        $model->save();
    }

    public function actionPayupdate(){
        $model = Orders::findOne($_POST['order_id']);
        $model->pay = $_POST['pay_id'];
        $model->save();
    }

	public function actionDelete(){
		$model = Orders::findOne($_GET['id']);
		$model->delete();
		return Yii::$app->response->redirect(['/orders/index']);
	}
        
        
        public function actionAdd(){
            $model = new OrdersProducts;
		if ($model->load(Yii::$app->request->post())) {

            if(!$modelMod = ProductVariant::find()->with(['product'])->where(['sku'=>$model->sku])->one())
                    throw new HttpException(404, 'Данного артикля не существует!');
            $model->product_name = $modelMod->product->name;
            $model->name = $modelMod->name;
            $model->sku = $modelMod->sku;
            $model->price = $modelMod->price;
            $model->sum_cost = $model->count*$modelMod->price;
            $model->mod_id = $modelMod->id;
            $model->save();
			//return Yii::$app->response->redirect(['/admin/orders/show','id'=>$_GET['order_id']]);
		}            
            
            //return $this->render('add',['model'=>$model]);
        }

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

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
        
	public function actionDelete_product(){
		$model = OrdersProducts::findOne($_GET['id']);
		$model->delete();
		return Yii::$app->response->redirect(['/admin/orders/show','id'=>$_GET['order_id']]);
	}

    protected function findModel($id)
    {
        if (($model = Orders::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }      
}
