<?php

namespace backend\controllers;

use common\models\SurveyNew;
use Yii;
use yii\data\ActiveDataProvider;
use yii\filters\VerbFilter;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use common\modules\config\models\Config;

/**
 * SurveyController implements the CRUD actions for SurveyNew model.
 */
class SurveyNewController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }
    /**
     * Lists all SurveyNew models.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => SurveyNew::find(),
        ]);

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

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

    /**
     * Creates a new SurveyNew model.
     * If creation is successful, the browser will be redirected to the 'view' SurveyNew.
     *
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new SurveyNew();
        $editor = request('editor') ? : config('page_editor_type');
        $model->markdown = $editor == 'markdown' ? 1 : 0;
        $type = Yii::$app->request->get('type','industry');
        $type = empty($type) ? 'industry' : $type;
        $model->type = $type;
        
        $typeDesc = '';
        switch ($type) {
            case 'industry':
                $typeDesc = '现代产业';
                break;
            case 'education':
                $typeDesc = '高等教育';
                break;
            case 'medical':
                $typeDesc = '医疗卫生';
                break;
            case 'circuit':
                $typeDesc = '集成电路';
                break;
            case 'special':
                $typeDesc = '专项政策';
                break;
        }

		$configModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => $type])->all();

        $cengciModels =  Config::find()->select(['name','value','extra','description','type'])->where(['group' => 'policy', 'name' => 'cengci'])->all();

		if (Yii::$app->request->isPost) {
			$data = Yii::$app->request->post();
			
			$arr = [];
			foreach ($data as $key=>$value) {
				if($key == '_csrfBackend' || $key == 'SurveyNew'){
					continue;
				}
				$arr[$key] = $value;
			}

			$model->load($data);
			$model->save();
			return $this->redirect(['index']);
		}
    
		return $this->render('create', [
			'model' => $model,
			'type'=>$typeDesc,
			'configModels' => $configModels,
			'cengciModels' => $cengciModels,
		]);

    }

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

        $typeDesc = '';
        switch ($model['type']) {
            case 'industry':
                $typeDesc = '现代产业';
                break;
            case 'education':
                $typeDesc = '高等教育';
                break;
            case 'medical':
                $typeDesc = '医疗卫生';
                break;
            case 'circuit':
                $typeDesc = '集成电路';
                break;
            case 'special':
                $typeDesc = '专项政策';
                break;
        }
        
		$configModels = Config::find()->select(['name','value','extra','description','type'])->where(['group' => $model['type']])->all();

		if (Yii::$app->request->isPost) {
			$data = Yii::$app->request->post();

			$arr = [];
			foreach ($data as $key=>$value) {
				if($key == '_csrfBackend' || $key == 'SurveyNew'){
					continue;
				}
				$arr[$key] = $value;
			}

			$model->load($data);
			$model->save();
			return $this->redirect(['index']);
		}

		return $this->render('update', [
			'model' => $model,
			'type'=>$typeDesc,
			'configModels' => $configModels,
		]);
    }

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

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

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