<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2018/2/24
 * Time: 下午12:48
 */
namespace backendApi\modules\v1\controllers;

use common\helpers\snowflake\PageSnowFake;
use common\models\DealType;
use common\models\Period;
use common\models\RegType;
use common\models\DecRole;
use common\models\OpenBank;
use common\models\UserSystem;
use Yii;
use backendApi\modules\v1\models\User;
use common\helpers\Cache;

class SiteController extends BaseController
{
    public $modelClass = User::class;
    public function behaviors() {
        $behaviors = parent::behaviors();
        //$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }


    public function actions()
    {
        $actions = parent::actions();
        $actions['captcha'] = [
            'class' => 'common\helpers\CaptchaAction',
            'width' => 120,
            'height' => 40,
            'padding' => 0,
            'minLength' => 4,
            'maxLength' => 4,
            'offset'=>8,        //设置字符偏移量 有效果
            'testLimit'=>1,
        ];

        return $actions;
    }

    /**
     * 请求服务器时间差
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionDaysDiff(){
        return static::notice(['daysDiff'=>Yii::$app->params['daysDiff']]);
    }

    /**
     * 请求页面数据
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionPageData() {
        $pageId = PageSnowFake::instance()->generateId();
        return static::notice(['pageId'=>$pageId]);
    }

    public function actionBaseInfo(){
        // 会员级别
        $decLevels = Cache::getDecLevelConfig();
        // 聘级
        $empLevels = Cache::getEmpLevelConfig();
        // 注册类型
        $regTypes = RegType::getTypes();
        // 类型
        $dealTypes = DealType::getAllTypes();
        // 体系
        $systems = UserSystem::getAllSystems();
        // 菜单
        $menu = require Yii::getAlias('@backendApi/config/menu.php');
        $menu = $this->_childMenu($menu);
        // 管理员角色
        $adminRoles = Cache::getAdminRole();
        // 超级管理员角色ID
        $superAdminRoleId = Yii::$app->params['superAdminRoleId'];
        // 时间差
        $daysDiff = Yii::$app->params['daysDiff'];
        // 钱包
        $shopWalletType = Yii::$app->params['shopWalletType'];
        // 全部报单中心角色
        $decRoles = DecRole::find()->where('1=1')->select('ID,ROLE_NAME')->indexBy('ID')->asArray()->all();
        // 全部子公司
        $subCompanies = [];
        // 全部会员状态
        $allStatus = Yii::$app->params['userStatus'];
        // 全部银行
        $allOpenBank = OpenBank::find()->where('1=1 AND STATUS=1')->select('BANK_NAME,BANK_CODE')->indexBy('BANK_CODE')->asArray()->all();
        // 钱包
        $allNation = Yii::$app->params['nation'];
        // 期数
        $period = Period::instance();
        $periodNum = $period->getNowPeriodNum();
        return [
            'decLevels' => $decLevels,
            'empLevels' => $empLevels,
            'regTypes' => $regTypes,
            'dealTypes' => $dealTypes,
            'systems' => $systems,
            'menu' => $menu,
            'adminRoles' => $adminRoles,
            'superAdminRoleId' => $superAdminRoleId,
            'daysDiff' => $daysDiff,
            'shopWalletType' => $shopWalletType,
            'decRoles' => $decRoles,
            'subCompanies' => $subCompanies,
            'allStatus' => $allStatus,
            'allOpenBank' => $allOpenBank,
            'allNation' => $allNation,
            'nowPeriodNum' => $periodNum,
        ];
    }

    private function _childMenu($parentArray){
        $menuResult = [];
        foreach($parentArray as $key => $parentMenu){
            // 菜单是否显示
            if(isset($parentMenu['show']) && !$parentMenu['show']){
                continue;
            }
            // 查看是否有该控制器的权限
            if(isset($parentMenu['controller']) && $parentMenu['controller']){
                if(!Yii::$app->user->validateAdminController($parentMenu['controller'])) continue;
            }
            // 查看是否有权限
            if(isset($parentMenu['action']) && $parentMenu['action']){
                if(!Yii::$app->user->validateAdminAction($parentMenu['controller'], $parentMenu['action'])) continue;
            }
            // 子菜单同样设置
            if(isset($parentMenu['child']) && !empty($parentMenu['child'])){
                $parentMenu['child'] = $this->_childMenu($parentMenu['child']);
            }
            // 如果在白名单的不显示菜单
            if(isset($parentMenu['controller']) && $parentMenu['controller']){
                if(Yii::$app->user->noCheckAdminController($parentMenu['controller'])) continue;
            }
            $menuResult[] = $parentMenu;
        }
        return $menuResult;
    }

    /**
     * 发送钉钉测试信息
     * @return mixed
     * @throws \yii\web\HttpException
     */
    public function actionSendNotice()
    {
        $data = [
            'code' => 400,
            'message' => 'autoSendDingTalk',
        ];
        return static::notice(['data' => $data['bug监控正常运行，没有发现异常.']]);
    }

    public function actionClearLoginFailedNum()
    {
        $adminName = \Yii::$app->request->get('adminName');

        \Yii::$app->redis->del('FAIL_NUMS:' . $adminName);

        return static::notice(['data' => '登陆失败次数清空']);
    }

    public function actionOpenBackendIpFilter()
    {
        \Yii::$app->redis->set('backend_ip_filter', 1);

        return static::notice(['data' => '开启后台IP过滤']);
    }

    public function actionCloseBackendIpFilter()
    {
        \Yii::$app->redis->del('backend_ip_filter');

        return static::notice(['data' => '关闭后台IP过滤']);
    }

    public function actionOpenMemberIpFilter()
    {
        \Yii::$app->redis->set('member_ip_filter', 1);

        return static::notice(['data' => '开启会员IP过滤']);
    }

    public function actionCloseMemberIpFilter()
    {
        \Yii::$app->redis->del('member_ip_filter');

        return static::notice(['data' => '关闭会员IP过滤']);
    }

}