<?php

namespace app\controllers;

use Yii;
use app\models\MVoting;
use app\models\MVotingSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;


/**
 * MVotingController implements the CRUD actions for MVoting model.
 */
class VotingController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all MVoting models.
     * @return mixed
     */
    public function actionIndex()
    {
        //$model_voting = MVoting::find()->all();//"Select * from m_voting";
        $searchModel = new MVotingSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

    /**
     * Displays a single MVoting model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->renderAjax('view', [
            'model' => $this->findModel($id),
        ]);
    }

    public function actionViewdata(){

        return $this->render('view_data', [

        
        ]);

    }

    /**
     * Creates a new MVoting model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new MVoting();
        
        

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $email = \Yii::$app->mailer->compose()
            ->setFrom(['gtconlinesystem@gmail.com'=>'Online Voting System'])
            ->setTo('goklasif10029@gmail.com')
            ->setSubject('Informasi Pemberitahuan Voting')
            ->setHtmlBody('
                <b>Terima Kasih Buat Partisipasi Anda Telah Melakukan Voting</b>
                <br><br>
                <b>Hasil Voting Akan Ditunjukkan Jika Periode Voting Telah Berakhir</b>')
            ->send();

            if($email){
                Yii::$app->session->setFlash('success', "Voting Berhasil Dilakukan, Informasi bahwa anda telah berhasil melakukan Voting dikirim ke Email Anda");
            }
            else{
                Yii::$app->session->setFlash('warning', "Voting Gagal Dilakukan, Silahkan Coba Kembali");
            }

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

        return $this->renderAjax('create', [
            'model' => $model,
        ]);
    }

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

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        }

        return $this->renderAjax('update', [
            'model' => $model,
        ]);
    }

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

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

    /**
     * Finds the MVoting model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return MVoting the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = MVoting::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    public function actionIndexHasilVoting()
    {
        $searchModel = new MVotingSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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

    public function actionIndexPenggunaVoting()
    {
        $searchModel = new MVotingSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

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


}
