<?php

namespace backend\controllers;

use common\enums\StatusEnum;
use common\models\Industry;
use Yii;
use yii\data\ActiveDataProvider;
use yii\helpers\Url;
use yii\filters\VerbFilter;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\web\Response;

/**
 * CaetgoryController implements the CRUD actions for Industry model.
 */
class IndustryController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'ajax-update-field' => [
                'class' => 'common\\actions\\AjaxUpdateFieldAction',
                'allowFields' => ['status'],
                'findModel' => [$this, 'findModel']
            ],
            'switcher' => [
                'class' => 'backend\widgets\grid\SwitcherAction'
            ],
            'position' => [
                'class' => 'backend\\actions\\Position',
                'returnUrl' => Url::current()
            ]
        ];
    }
    /**
     * Lists all Industry models.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider(['query' => Industry::find()->active()->orderBy('sort asc'),  'pagination' => false]);
        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'pagination' => false
        ]);
    }
    /**
     * Displays a single Industry model.
     *
     * @param int $id
     *
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Industry model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionCreate($id = 0)
    {
        $model = new Industry();
        $model->loadDefaultValues();
        $model->pid = $id;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        } else {

            //二级分类id列表
            $childCateList = Industry::find()->where(['type' => 0])->andWhere(['!=', 'pid', 0])->select('id')->column();

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

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

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index']);
        } else {
            //二级分类id列表
            $childCateList = Industry::find()->where(['type' => 0])->andWhere(['!=', 'pid', 0])->select('id')->column();
            return $this->render('update', [
                'model' => $model,
                'childCateList' => $childCateList,
            ]);
        }
    }

    /**
     * Deletes an existing Industry model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     *
     * @param int $id
     *
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->softDelete();
        Yii::$app->cache->flush();
        Yii::$app->session->setFlash('success', '操作成功');

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

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

    /**
     * 产业链列表
     * @param $id
     * @return array
     * @author jiang
     */
    public function actionChain($id = null)
    {
        $where = [];
        if (!empty($id)) {
            //二级分类
            $ids = Industry::find()->where(['pid' => $id, 'type' => 0])->select('id')->column();
            $ids[] = $id;
            $where = ['in', 'pid', $ids];
        }

        $list = Industry::find()->where(['type' => 1])->active()->andWhere($where)->orderBy('sort asc')->select('title')->column();
        Yii::$app->response->format = Response::FORMAT_JSON;

        return ['data' => $list];
    }
}
