<?php

namespace backend\modules\sikk\controllers;

use Yii;
use common\models\DimxDim;
use common\models\SikkLog;
use common\models\search\SikkLogSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

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

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

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

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

    /**
     * Creates a new SikkLog model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new \common\models\DimxDim();
        $modelsikkLog = new \common\models\SikkLog();
        if ($model->load(Yii::$app->request->post())) {
            $cari = \common\models\DimxDim::findOne(['nim'=>$model->nim]);
            $modelsikkLog->mhs_id=$cari->dim_id;

            $sql='SELECT log_id FROM sikk_log WHERE mhs_id='.$cari->dim_id.' AND time_out IS NULL';
            $hasilQuery = Yii::$app->db->createCommand($sql)->queryAll();
            if($hasilQuery==NULL){
                Yii::$app->session->setFlash('success', '<b>Anda diizinkan untuk masuk!<b>');
                date_default_timezone_set('Asia/Jakarta');
                $date = date('Y-m-d H:i:s');
                $modelsikkLog->time_in=date_format($date, 'd-m-Y H:i:s');
                $modelsikkLog->created_at=date('Y-m-d H:i:s');
                if($modelsikkLog->save()){
                    return $this->redirect(['dimx-dim/view', 'id' => $cari->dim_id]);
                }
            }
            if($hasilQuery!=NULL){
                
            date_default_timezone_set('Asia/Jakarta');
            $date = date_create(date('Y-m-d H:i:s'));
                $date1=date('Y-m-d H:i:s');
                $tanggal="'".$date1."'";
                Yii::$app->session->setFlash('success', '<b>Anda diizinkan untuk keluar!<b>');
                $sql1='UPDATE sikk_log SET time_out='.$tanggal.' WHERE mhs_id='.$cari->dim_id.'';
                Yii::$app->db->createCommand($sql1)->execute();
                    return $this->redirect(['dimx-dim/view', 'id' => $cari->dim_id]);
                
            }
           
           
           
            
//            return $this->redirect(['view', 'id' => $model->log_id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing SikkLog model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $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->log_id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

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

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

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