<?php

namespace backend\controllers;

use Yii;
use app\models\Refieren;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * RefierensController implements the CRUD actions for Refieren model.
 */
class RefierensController extends Controller {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Refieren models.
     * @return mixed
     */
    public function actionIndex() {
        $searchModel = new \app\models\RefierensSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
                    'dataProvider' => $dataProvider,
                    'searchModel' => $searchModel,
        ]);
    }

    /**
     * Displays a single Refieren model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id) {
        return $this->render('view', [
                    'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Refieren model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $refiere = new Refieren();
        $referido = new \app\models\Referidos();
        if ($refiere->load(Yii::$app->request->post()) && $referido->load(Yii::$app->request->post())) {
            //echo var_dump(Yii::$app->request->post());
            $refiere->pdv_id = Yii::$app->user->identity->pdv_id;
            if ($refiere->bono_id == 0) {
                $bono = new \app\models\Bono();
                $bono->bono = $refiere->bono_otro;
                $bono->monto = $refiere->bono_otro_monto;
                $bono->save();
                $refiere->bono_id = $bono->id;
            }
            if ($refiere->save()) {
                $referido->refieren_id = $refiere->id;
                if ($referido->save()) {
                    \Yii::$app->getSession()->setFlash(
                            'Referido guardado Correctamente', ''
                            . 'Refiere: ' . $refiere->nombre . ' | Telefono: ' . $refiere->telefono
                            . '<br /> Referido: ' . $referido->nombre . ' | Telefono: ' . $referido->telefono
                    );
                    return $this->redirect(['create']);
                }
            }
            //echo var_dump($refiere);
            //echo var_dump($referido);
            //return $this->redirect(['index']);
        } else {
            return $this->render('create', [
                        'refiere' => $refiere,
                        'referido' => $referido,
            ]);
        }
    }

    /**
     * Updates an existing Refieren model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Refieren model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id) {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Refieren model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return Refieren the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = Refieren::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

}
