<?php

namespace backend\controllers;

use Yii;
use common\models\Production;
use yii\filters\AccessControl;
use yii\web\Controller;
use common\models\LoginForm;
use yii\filters\VerbFilter;
use common\models\Commande;
use common\models\CommandeProduit;
use common\models\PointVente;
use common\models\Produit;
use common\models\User;
use common\models\CommandeAutoForm ;
use common\models\ProductionProduit;
use yii\data\ActiveDataProvider;
use common\models\CommandeAuto ;
use common\models\CommandeAutoProduit ;
use yii\web\NotFoundHttpException;

class CommandeautoController extends BackendController {

    var $enableCsrfValidation = false;

    public function behaviors() 
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                        'matchCallback' => function ($rule, $action) {
                    return Yii::$app->user->identity->status == USER::STATUS_ADMIN 
                            || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
                }
                    ]
                ],
            ],
        ];
    }
    
    public function actionIndex() 
    {
        $dataProvider = new ActiveDataProvider([
            'query' => CommandeAuto::find()
                ->with('user', 'etablissement', 'pointVente','commandeAutoProduit')
                ->where(['id_etablissement'=>Yii::$app->user->identity->id_etablissement]),
            'pagination' => [
                'pageSize' => 1000,
            ],
        ]);
        
        return $this->render('index',[
            'dataProvider' => $dataProvider
        ]) ;
    }
    
    public function actionCreate() 
    {
        // form
        $model = new CommandeAutoForm ;
        $model->id_etablissement = Yii::$app->user->identity->id_etablissement ;
        
        // produits
        $produits = Produit::find()
            ->where(['id_etablissement' => $model->id_etablissement])
            ->orderBy('order ASC')
            ->all();
        
        if($model->load(Yii::$app->request->post()) && $model->validate() && $model->save())
        {
            
            $this->redirect(['commandeauto/index']) ;
        }
        
        return $this->render('create',[
            'model' => $model,
            'produits' => $produits
        ]) ;
    }
    
    public function actionUpdate($id)
    {
        // form
        $model = new CommandeAutoForm ;
        $commandeauto = CommandeAuto::findOne($id) ;
        if($commandeauto)
        {
            $model->id = $id ;
            $model->id_etablissement = $commandeauto->id_etablissement ;
            $model->id_user = $commandeauto->id_user ;
            $model->username = $commandeauto->username ;
            $model->id_point_vente = $commandeauto->id_point_vente ;
            $model->date_debut = date('d/m/Y',strtotime($commandeauto->date_debut)) ;
            if(strlen($model->date_fin))
                $model->date_fin = date('d/m/Y',strtotime($commandeauto->date_fin)) ;
            $model->lundi = $commandeauto->lundi ;
            $model->lundi = $commandeauto->lundi ;
            $model->mardi = $commandeauto->mardi ;
            $model->mercredi = $commandeauto->mercredi ;
            $model->jeudi = $commandeauto->jeudi ;
            $model->vendredi = $commandeauto->vendredi ;
            $model->samedi = $commandeauto->samedi ;
            $model->dimanche = $commandeauto->dimanche ;
            
            // produits
            $commandeauto_produits = CommandeAutoProduit::find()->where(['id_commande_auto' => $model->id])->all() ;
            foreach($commandeauto_produits as $commandeauto_produit)
            {
                $model->produits['produit_'.$commandeauto_produit->id_produit] = $commandeauto_produit->quantite ;
            }
        }
        else {
            throw new NotFoundHttpException('La commande automatique est introuvable.', 404);
        }
        
        // produits
        $produits = Produit::find()
            ->where(['id_etablissement' => $model->id_etablissement])
            ->orderBy('order ASC')
            ->all();

        if($model->load(Yii::$app->request->post()) && $model->validate() && $model->save())
        {
            $this->redirect(['commandeauto/index']) ;
        }
        
        return $this->render('update',[
            'model' => $model,
            'produits' => $produits
        ]) ;
    }
    
    public function actionDelete($id) 
    {
        CommandeAutoProduit::deleteAll(['id_commande_auto' => $id]) ;
        CommandeAuto::findOne($id)->delete() ;
        $this->redirect(['commandeauto/index']) ;
    }
}