<?php

namespace modules\cors\controllers;

use Yii;
use modules\cors\models\Licensedatabe;
use modules\cors\models\LicensedatabeSearch;
use modules\cors\models\Licensecatbe;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * LicensedataController implements the CRUD actions for licensedatabe model.
 */
class LicensedataController extends Controller
{
 	public function beforeAction($action) {
   		$id = yii::$app->request->get('id');
		\frontend\models\Audit::qcreate(['id' => yii::$app->request->get('id', null), 'controller' =>  Yii::$app->controller->id,  'action' => Yii::$app->controller->action->id,  'attributes' => null]);
		return parent::beforeAction($action);
	}
 

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

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
     /**
     * Lists Deleted licensedatabe models.
     * @return mixed
     */
    public function actionDeleted()
    {
        $searchModel = new licensedatabeSearch();
        $dataProvider = $searchModel->deleted(Yii::$app->request->queryParams);

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

    /**
     * Displays a single licensedatabe model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }
    /**
     * Creates a new licensedatabe model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new licensedatabe();
        $cats = \yii\helpers\ArrayHelper::map(\backend\models\Licensecatsbe::find()->where(['status' => 'Active'])->all(),'id','category');
        $tempid = Yii::$app->session->get('ldtempid');
        if (!$tempid) {	
			$tempid = \common\models\Base::tempId();
        	Yii::$app->session->set('ldtempid', $tempid);
			$r1 = new Licensecatbe();
			$r1->relatedid = $tempid;
			$r1->save();
		}
		$employees = \yii\helpers\ArrayHelper::map(\backend\models\User::find()->where(['is', 'blocked_at', null])->orderby(['username' => SORT_ASC])->all(), 'id', 'fullname');
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
        	$model = licensedatabe::find()->where(['=', 'created_at', $model->created_at])->andWhere(['=', 'created_by', $model->created_by])->one();
      		$linked = Licensecatbe::find()->where(['relatedid' => Yii::$app->session->get('ldtempid')])->all();
			foreach ($linked as $a => $link) {
				$link->relatedid = $model->id;
				$link->save();
			}
			Yii::$app->session->remove('tempid');
          	
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
            	'employees' => $employees,
            	'model' => $model,
            	'cats' => $cats,
            ]);
        }
    }

    /**
     * Updates an existing licensedatabe 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);
		$employees = \yii\helpers\ArrayHelper::map(\backend\models\User::find()->where(['is', 'blocked_at', null])->orderby(['username' => SORT_ASC])->all(), 'id', 'fullname');
		$cats = \yii\helpers\ArrayHelper::map(\backend\models\Licensecatsbe::find()->where(['status' => 'Active'])->all(),'id','category');
	        
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
            	'employees' => $employees,
                'model' => $model,
            	'cats' => $cats,
            ]);
        }
    }

    
    public function actionAddrow() {
		$r1 = new Licensecatbe();
    	$r1->relatedid = Yii::$app->request->post('id');
		if ($r1->save()) {
			return json_encode(['result' => 200, 'message' => 'Added']);
		} else {
			return json_encode(['result' => 100, 'message' => $r1->errors]);
		}    
	}
	
	public function actionDeleterow() {
		$row = Licensecatbe::find()->where(['id' => Yii::$app->request->post('id')])->one();
		$row->status = 'deleted';
		$row->deleted_at = strtotime('now');
		$row->deleted_by = Yii::$app->user->identity->id;
		if ($row->save()) {	
			return json_encode(['result' => 200, 'message' => 'Deleted']);
		} else {
			return json_encode(['result' => 100, 'message' => $row->errors]);
		}
	}
	public function actionSavefield() {
		$field = Yii::$app->request->post('field');
		$value = Yii::$app->request->post('value');
		$row = Licensecatbe::find()->where(['id' => Yii::$app->request->post('id')])->one();
		$row->$field = $value;
		if ($row->save()) {	
			return json_encode(['result' => 200, 'message' => 'Saved']);
		} else {
			return json_encode(['result' => 100, 'message' => $row->errors]);
		}
	}
    /**
     * Deletes an existing licensedatabe model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
		$model = $this->findModel($id);
		$model->status = 'Deleted';
		$model->deleted_at = strtotime('now');
		$model->save();
        return $this->redirect(['index']);
    }

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