<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\InvalidT;
use common\models\UserT;
use Yii;
use yii\web\Response;

class InvalidController extends \yii\web\Controller
{
    public $my = null;
    public $enableCsrfValidation = false;
    public $layout = 'blue-main';

    public function init()
    {
        parent::init();

        $cookie = Yii::$app->request->cookies;
        $user_id = MyLib::encrypt($cookie->get('aid'),'DECODE');

        if($user_id != 0)
        {
            $this->my = UserT::findOne(['id'=>$user_id]);
        } else {
            Yii::$app->response->redirect('/common/login')->send();
            exit;
        }
    }

    public function actionIndex()
    {
        $items = InvalidT::getTree();
        return $this->render('index',[
            'items'=>$items
        ]);
    }

    public function actionIndexJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $items = InvalidT::getTree();;

        $data = [];
        $data['total'] = count($items);
        $data['rows'] = [];
        foreach($items as $item) {
            $row = $item->toArray();
            $row['type'] = $item->getShowType();
            $data['rows'][] = $row;
        }

        return $data;
    }

    public function actionEdit()
    {
        $request = Yii::$app->request;
        $id = $request->get('id',0);
        if($id > 0) {
            $info = InvalidT::findOne(['id'=>$id]);
        } else {
            $info = new InvalidT();
        }

        $parent_items = InvalidT::getTree();

        return $this->render('edit',[
            'info' => $info,
            'parent_items' => $parent_items
        ]);
    }

    public function actionSave()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '保存失败';

        if($request->isPost) {
            $id = $request->post('id',0);
            $name = $request->post('name');
            $parent_id = (int)$request->post('parent_id');
            $difc = (int)$request->post('difc');
            $paixu_id = (int)$request->post('paixu_id');

            if($name == '') {
                $result['msg'] = '请输入名称!';
                return $result;
            }
            if($id > 0 && $parent_id == $id) {
                $result['msg'] = '上级不能选择自己!';
                return $result;
            }

            $tran = InvalidT::getDb()->beginTransaction();

            try {
                $row = null;
                if($id > 0) {
                    $row = InvalidT::findOne(['id'=>$id]);
                    if($row->hasChild($parent_id)) {
                        $result['msg'] = '上级不能选择自己下级!';
                        return $result;
                    }
                } else {
                    $row = new InvalidT();
                }
                $row->name = $name;
                $row->parent_id = $parent_id;
                $row->difc = $difc;
                $row->paixu_id = $paixu_id;
                $row->save();

                $tran->commit();
            } catch(\Exception $e) {
                $tran->rollBack();
                throw $e;
            }

            $result['success'] = true;
            $result['msg'] = '保存成功';
        }
        return $result;
    }

    public function actionDelete()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '删除失败';

        if($request->isPost) {
            $id = $request->post('id',0);

            if($id > 0) {
                $tran = InvalidT::getDb()->beginTransaction();
                try {
                    $row = InvalidT::find()
                        ->where('id='.$id)
                        ->one();
                    $children = $row->invalids;
                    if(count($children) > 0) {
                        $result['msg'] = '请先删除下级';
                        return $result;
                    }
                    $row->delete();

                    $result['success'] = true;
                    $result['msg'] = '删除成功';

                    $tran->commit();
                } catch(\Exception $e) {
                    $tran->rollBack();
                    throw $e;
                }
            }
        }
        return $result;
    }
}