<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/3/12
 * Time: 上午10:49
 * 资讯- 知识库
 */


namespace backend\controllers;
use backend\base\CommonController;
use backend\base\Help;
use common\models\NewKnowledgelabel;
use Yii;
use yii\helpers\ArrayHelper;
class DictionarynewsController extends CommonController
{
        //知识库
        public function actionLabel()
        {
            return $this->render('label');
        }

        public function actionLabelform()
        {
            $model = new NewKnowledgelabel();
            $input = Yii::$app->request->get();

            //搜索的情况下
            if(ArrayHelper::keyExists('id',$input))
            {
                $p = $model->FindById($input['id']);
                if(!empty($p))
                {
                    if($p['pid']  == 0)
                    {
                        $model->pid = $p['id'];
                    }

                    if($p['pid'] != 0)
                    {
                        $model->id = $p['id'];
                        $p  = $model->FindById($p['pid']);
                    }
                }
            }
            $query = $model->getList([],'id,name,pid,create_at,sort,is_show',2);
            if(!empty($query))
            {
                //重组数组,返回父类
                if(!empty($p))
                {
                    array_push($query,$p->toArray());
                }
                foreach ($query as &$val)
                {
                    $val['create_at'] = date('Y-m-d H:i',$val['create_at']);
                }
                return Help::JsonCode(Help::SUCCESS,Yii::t('app','aget_success'),$query);
            }
                return Help::JsonCode(Help::ERROR,Yii::t('app','get_error'),[]);
        }

        public function actionLabelcreate()
        {
            $model = new NewKnowledgelabel();
            $model->pid = 0;
            $rows = $model->getList([],'id,name,pid');
            return $this->render('labelcreate',['model'=>$rows]);
        }

        public function actionLabelcreateform()
        {
            $models = new NewKnowledgelabel();
            $models->scenario = 'add';
            $model = $models->Authenticator(Yii::$app->request->post('data'));
            if(!empty($model->pid))
            {
                $row = $models->FindById($model->pid);
                if(!empty($row))
                {
                    $model->path = $row->path.$row->id.',';
                }
            }
            if(is_object($model))
            {
                if($model->save() == true) return Help::JsonCode(Help::SUCCESS,Yii::t('app','add_success'));
            }
            return Help::JsonCode(Help::ERROR,Yii::t('app','add_error'),$model);
        }

        public function actionEditlabel()
        {
            $input = Yii::$app->request->get();
            if(!empty($input['id']))
            {
                $models = new NewKnowledgelabel();

                $models->pid = 0;
                $result  = $models->getList([],'id,name,pid');
                $row = $models->FindById($input['id']);
                return $this->render('editlabel',['model'=>$result,'row'=>$row]);

            }
        }

        public function actionEditlabelform()
        {
            $input = Yii::$app->request->post('data');
            $model = new NewKnowledgelabel();
            if(ArrayHelper::keyExists('pid',$input) && is_numeric($input['pid']))
            {
                $row = $model->FindById($input['pid']);
                if(!empty($row))
                {
                   $input['pid']   = $row['id'];
                   $input['path']  = $row['path'].$row->id.',';
                }
            }
            $res = $model->FindById($input['id']);
            //处理删除
            if(ArrayHelper::keyExists('del',$input) && is_numeric($input['del']))
            {
                    if($res['pid']== 0)
                    {
                        $model->pid = $res['id'];
                        $pidList = $model->getList([],'id');
                        if(!empty($pidList))  return Help::JsonCode(Help::ERROR,'检测到该父类下存在子类,请先删除子类在删除父类');
                    }
            }


            if($res->load($input,'') && $res->save())
            {
                return Help::JsonCode(Help::SUCCESS,Yii::t('app','edit_success'));
            }

                return Help::JsonCode(Help::ERROR,Yii::t('app','edit_error'));
        }

        public function actionPidlist()
        {
            $input = Yii::$app->request->post();
            $model = new NewKnowledgelabel();
            if(empty($input['pid']) || $input['pid'] == 0)
            {
                $model->pid = 0;
            }
            else
            {
                $model->pid = $input['pid'];
            }
            $result = $model->getList([],'id,name',1);
            if(!empty($result))   return Help::JsonCode(Help::SUCCESS,Yii::t('app','get_success'),$result);
            return Help::JsonCode(Help::ERROR,Yii::t('app','get_error'));
         }
}