<?php

namespace backend\controllers;

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

/**
 * ClientesController implements the CRUD actions for Clientes model.
 */
class ClientesController extends Controller {

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

    /**
     * Lists all Clientes models.
     * @return mixed
     */
    public function actionIndex() {
        $dataProvider = new ActiveDataProvider([
            'query' => \app\models\ClienteLineas::find(),
        ]);

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

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

    /**
     * Creates a new Clientes model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $cliente = new Clientes();
        $linea = new \app\models\Linea();
        $clineas = new \app\models\ClienteLineas();
        if ($cliente->load(Yii::$app->request->post()) && $linea->load(Yii::$app->request->post()) && $clineas->load(Yii::$app->request->post()) && \yii\base\Model::validateMultiple([$cliente, $linea])) {
            $cliente->telefono = $linea->numero;
            if ($cliente->save() && $linea->save()) {
                $clineas->clientes_id = $cliente->id;
                $clineas->linea_id = $linea->id;
                if ($clineas->save()) {
                    \Yii::$app->getSession()->setFlash(
                            'Registro de ventas guardado Correctamente', 'Cliente: ' . $cliente->nombre . ' ' . $cliente->apellidop . ' Folio ICC: ' . $linea->icc
                    );
                    return $this->redirect(['create']);
                }
            }

//            echo var_dump($cliente);
//            echo var_dump($linea);
//            echo var_dump($clineas);
            //return $this->redirect(['view', 'id' => $cliente->id]);
        } else {
            return $this->render('create', [
                        'cliente' => $cliente,
                        'linea' => $linea,
                        'clineas' => $clineas,
            ]);
        }
    }

    /**
     * Updates an existing Clientes model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $cliente = $this->findModel($id);
        
        if ($cliente->load(Yii::$app->request->post()) && $cliente->save()) {
            return $this->redirect(['view', 'id' => $cliente->id]);
        } else {
            return $this->render('update', [
                        'cliente' => $cliente,
                        'linea' => $linea,
                        'clineas' => $clineas,
            ]);
        }
    }

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

}
