<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/3/5
 * Time: 下午5:19
 */

namespace backend\controllers;

use backend\base\CommonController;
use backend\server\Menus;
use backend\base\Help;
use yii\helpers\ArrayHelper;
use common\models\SysmenuQ;
use Yii;

class MenuController extends CommonController
{
    public $enableCsrfValidation = false;

    /*
     * 菜单数据显示
     * */
    public function actionIndex()
    {
        return $this->render('index');
    }

    /*
     * 获取菜单数据
     * */
    public function actionIndexfrom()
    {
        $model = new Menus();
        $rows = $model->MenuListSorting();
        return Help::JsonData(0, '成功', $rows['count'], $rows['data']);
    }

    /*
     * 获取2级和3级菜单数据
     * */
    public function actionNavlist()
    {
        $model = new Menus();
        $result = $model->getchildren();
        if ($result != false) {
            return Help::JsonCode(Help::SUCCESS, '查询成功', $result);
        }
        return Help::JsonCode(Help::ERROR, '查询失败');
    }

    /*
     * 菜单页面
     * */
    public function actionCreate()
    {
        $model = new \backend\server\Menus();
        $rows = $model->MenuListSorting();
        $arr = [];
        //清楚3级目录,
        foreach ($rows['data'] as $key => $val) {
            if ($val['level'] != 3 && !empty($val['level'])) {
                $arr[$key]['name'] = $val['name'];
                $arr[$key]['id'] = $val['id'];
            }
        }
        return $this->render('create', ['model' => $arr]);
    }

    /*
     *添加菜单
     * */
    public function actionCreatefrom()
    {

//            p(Yii::$app->request->post());
        $model = new Menus();
        $result = $model->Create();
        if ($result === true) {
            return Help::JsonCode(Help::SUCCESS, '菜单添加成功');
        }
        return Help::JsonCode(Help::ERROR, '菜单添加失败', $result);
    }

    /*
     * 删除菜单
     * */
    public function actionDelmenu()
    {
        $model = new Menus();
        $result = $model->Del();
        if ($result === true) {
            return Help::JsonCode(Help::SUCCESS, '删除成功');
        }
        return Help::JsonCode(Help::ERROR, $result);
    }

    /*
     * 修改菜单
     * */
    public function actionEditfrom()
    {

        $model = new Menus();
        $result = $model->Edit();
        if ($result === true) {
            return Help::JsonCode(Help::SUCCESS, '菜单添加成功');
        }
        return Help::JsonCode(Help::ERROR, '菜单添加失败', $result);
    }

    /*
     * 修改菜单页面
     * */
    public function actionEdit()
    {
        $model = new \common\models\Sysmenu();
        $model->id = Yii::$app->request->get('id');
        $result = $model->FindById();
        return $this->render('edit', ['model' => $result]);
    }


    //====================前台菜单======================

    /*
     * 前端菜单显示页面
     * */
    public function actionFronthome()
    {
        return $this->render('fronthome');
    }

    /*
     * 前端菜单-数据
     * */
    public function actionFronthomeform()
    {
        $model = new SysmenuQ();
        $rows = $model->getList(Yii::$app->request->post());
        if ($rows != null) {
            return Help::JsonData(0, '成功', $model->Total(), $rows);
        }
        return Help::JsonCode(Help::ERROR, '暂无数据');
    }


    /*
     * 前端菜单-添加页面
     * */
    public function actionFrontadd()
    {
        return $this->render('frontadd');
    }

    /*
     * 前端菜单-添加数据
     * */
    public function actionFrontaddform()
    {
        $input = Yii::$app->request->post('data');
        $model = new SysmenuQ();
        $model->scenario = 'add';
        $Auth = $model->Authenticator($input);
        if (is_object($Auth)) {
            if ($Auth->save() == true) return Help::JsonCode(Help::SUCCESS, '添加成功');
        }
        return Help::JsonCode(Help::ERROR, '添加失败', $Auth);
    }

    /*
     *  前端菜单-修改页面
     * */
    public function actionFrontedit()
    {
        $model = new SysmenuQ();
        $row = $model->FindById(Yii::$app->request->get('id'));
        if ($row != null) {
            return $this->render('frontedit', ['model' => $row]);
        }
    }

    /*
     * 前端菜单-修改数据
     * */
    public function actionFronteditform()
    {
        $model = new SysmenuQ();
        $input = Yii::$app->request->post('data');
        $model->Authenticator($input);
        $row = $model->FindById($input['id']);
        if ($row != null) {
            $setAttr = Help::SetAttr($input, $model, $row);
            if ($setAttr->update() == true) return Help::JsonCode(Help::SUCCESS, '修改成功');
        }
        return Help::JsonCode(Help::ERROR, '修改失败');
    }

    /*
     * 前端菜单-删除and禁用数据
     * */
    public function actionFrontstateform()
    {
        $model = new SysmenuQ();
        $input = Yii::$app->request->post();
        $row = $model->FindById($input['id']);
        if ($row != null) {
            switch ($input['type']) {
                case '1':
                    $row->del = 2;
                    break;
                case '2':
                    if ($row->status == 2) {
                        $row->status = 1;
                    } else if ($row->status == 1) {
                        $row->status = 2;
                    }
                    break;
            }
            if ($row->update() == true) return Help::JsonCode(Help::SUCCESS, '操作成功');

        }
        return Help::JsonCode(Help::ERROR, '操作失败');
    }

    /**
     * 修改菜单状态
     * @author lyy
     **/
    public function actionMenustatus()
    {
        $model = new Menus();
        $result = $model->EditMenuStatus();
        return Help::JsonCode(Help::SUCCESS, '操作成功', $result);
    }

    /*
* 修改排序
* */
    public function actionMenusortedit()
    {
        $model = new Menus();
        $state = $model->SetSortH();
        if($state) return Help::JsonCode(Help::SUCCESS, '操作成功');
        return Help::JsonCode(Help::ERROR, '操作失败');
//        $input = Yii::$app->request->post();
//        $find = $model->GetFindById($input['id']);
//        if ($find != null) {
//            if (!empty($input['sort'])) {
//                $find->sort = Yii::$app->request->post('sort');
//            }
//
//            if (!empty($input['examineid'])) {
//                switch ($find->status) {
//                    case 1:
//                        $find->status = 2;
//                        break;
//                    case 2:
//                        $find->status = 1;
//                        break;
//                }
//            }
//
//            if ($find->update(false) == true) return Help::JsonCode(Help::SUCCESS, '操作成功');
//        }
//        return Help::JsonCode(Help::ERROR, '操作失败');
    }
}