<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request as Req;
use Request;

/**
 *
 */
class ManagerController extends Controller {
    /**
     * @return 信息管理
     */
    function index() {

        return view('admin.demo/index');
    }

    /**
     * 权限设定添加
     * [systemAdd description]
     * @return [type] [description]
     */
    function systemAdd(Req $req) {
        if (!$req->isMethod('post')) {
            return view('admin.Manager/systemAdd');
        } else {
            $model = new \App\Models\System_root();
            $data=$model->select('id')->orderBy('id','desc')->first();
            $id=!empty($data->id)?$data->id:0;
            $model->id=$id+1;
            $model->level = trim($req->input('level'));

            $model->name = trim($req->input('name'));

            $model->settingmoney = trim($req->input('settingmoney'));
          
            $model->save();
             $log = array(
                session('adminInfo.admin_name'),
                $model->level,
                $model->name, 
                $model->settingmoney
            );
            OperationLog(session('adminInfo.admin_id'), 'qxsystemAdd', $log);
            return responseToJson(1);
        }

    }

    //管理员信息
    function adminInfo() {
        $page = Request::has('page') ? Request::get('page') : 1;
        $limit = Request::has('limit') ? Request::get('limit') : 10;
        $field = Request::has('field') ? Request::get('field') : 5;
        $order = Request::has('order') ? Request::get('order') : 'desc';
        $id = Request::get('id') ? Request::get('id') : ''; //管理员ID，返回对应管理信息
        $adminlist = new \App\Models\System_user();
        if (empty($id)) {
            $loginname = Request::has('loginname') ? Request::get('loginname') : '';
            $name = Request::has('name') ? Request::get('name') : '';
            $status = Request::has('status') ? Request::get('status') : '';
            $star_time = Request::get('star_time') ? Request::get('star_time').' 00:00:00' : '';
            $end_time = Request::get('end_time') ? Request::get('end_time').' 23:59:59' : '';
            $sureblur = Request::has('sureblur') ? Request::get('sureblur') : '';
            $where = array();
            if (!empty($loginname)) {
                if (empty($sureblur) || $sureblur == '模糊') {
                    $where[] = array('loginname', 'like', '%' . $loginname . '%');
                } else {
                    $where[] = array('loginname', '=', $loginname);
                }
            }
            if (!empty($name)) {
                $where[] = array('name', 'like', '%' . $name . '%');
            }
            if (!empty($status)) {
                $where[] = array('status', '=', $status);
                if ($status != 1) {
                    $where[] = array('status', '<>', 1);
                }
            }
            if (!empty($star_time)) {
                $star_time = date('Y-m-d H:i:s', strtotime($star_time));
                $where[] = array('add_time', '>=', $star_time);
            }
            if (!empty($end_time)) {
                $end_time = date('Y-m-d H:i:s', strtotime($end_time));
                $where[] = array('add_time', '<=', $end_time);
            }

            $data = $adminlist->getAdminlist($where, 1, $limit, $field, $order, 'system_root');
            // if (!($data < 0)) {
            //  $adminstatus = trans('status.system_user.status');
            //  foreach ($data['data'] as $k => $v) {
            //      $data['data'][$k]['status'] = ($v['status'] == 0) ? $adminstatus[2] : $adminstatus[$v['status']];
            //  }
            // }
        } else {
            $data = $adminlist->getAdminInfo($id, 1, 'system_root', 'root_id', 'id');
        }
        $role_db=new \App\Models\Role;
        $datas = array();
        foreach ($data['data'] as $k=>$v){
            $datas[]=$v['id'];
//            $role=$role_db->getRoleBy($v['id']);
//            print_r($role);
        }
        $data_role=$role_db->getRoleIn($datas);
        foreach ($data['data'] as $k=>$v){
            $data['data'][$k]['role_name']='';
            foreach ($data_role as  $ka=>$va){
                if($va['admin_id']==$v['id']){
                    $data['data'][$k]['role_name'].=$va['role_name'].',';
                }
            }
        }
        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
    }

    //修改管理员信息
    function adminEdit() {
        $status = Request::has('status') ? Request::get('status') : 1;
        $id = Request::has('id') ? Request::get('id') : ''; //管理员ID
        $pwd = Request::has('password') ? Request::get('password') : ''; //管理员密码
        $levelid = Request::has('levelid') ? Request::get('levelid') : ''; //管理员权限ID
        $astatus = Request::has('status') ? Request::get('status') : ''; //管理员状态

        if (empty($id)) {
            return Rens(-7010000202); //未提交管理员ID
        }
        if (empty($pwd)) {
            return Rens(-7010000302); //未提交管理员密码
        }
        if (empty($levelid)) {
            return Rens(-7010000402); //未提交管理员权限ID
        }
        if (empty($astatus)) {
            return Rens(-7010000502); //未提交管理员状态
        }
        $password = GenPassword($pwd);
        $data = array
            (
            'password' => $password['password'],
            'root_id' => $levelid,
            'status' => $astatus,
            'encryption' => $password['encryption'],
        );
        $res = $status; //修改管理员信息
        if ($res == 1) {
            return Rens($res);
        }
        return Rens(-7010000602); //修改管理员信息失败
    }

    //管理员权限信息
    function adminLevel() {
        $page = Request::has('page') ? Request::get('page') : 1;
        $limit = Request::has('limit') ? Request::get('limit') : 10;
        $field = Request::has('field') ? Request::get('field') : 1;
        $order = Request::has('order') ? Request::get('order') : 'desc';
        $id = Request::has('id') ? Request::get('id') : ''; //管理员权限ID，返回对应管理员权限信息
        $rootdetails = new \App\Models\System_root();
        if (empty($id)) {
            $name = Request::has('name') ? Request::get('name') : '';
            $level = Request::has('level') ? Request::get('level') : '';
            $money_small = Request::has('money_small') ? Request::get('money_small') : '';
            $money_big = Request::has('money_big') ? Request::get('money_big') : '';
            $where = array();
            if (!empty($name)) {
                $where[] = array('name', 'like', '%' . $name . '%');
            }
            if (!empty($level)) {
                $where[] = array('level', '=', $level);
                if ($level == -1) {
                    $where[] = array('level', '=', '0');
                }
            }
            if (!empty($money_small)) {
                $where[] = array('settingmoney', '>=', $money_small);
            }
            if (!empty($money_big)) {
                $where[] = array('settingmoney', '<=', $money_big);
            }

            $data = $rootdetails->getRootlist($where, 1, $limit, $field, $order);
        } else {
            $rootdetails = new \App\Models\System_column_root();
            $data = $rootdetails->getRootmenulist($id, 2);
        }
        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
    }

    //权限菜单信息
    function rootMenu() {
        $status = Request::has('status') ? Request::get('status') : 1;
        $data = array
            (
            array(
                'id' => 1,
                'name' => '彩票注单管理',
                'parentid' => '',
                'haschild' => 1,
                'child' => array(
                    array(
                        'id' => 2,
                        'name' => '幸运28',
                        'parentid' => 1,
                        'haschild' => 2,
                    ),
                    array(
                        'id' => 3,
                        'name' => '加拿大28',
                        'parentid' => 1,
                        'haschild' => 2,
                    ),
                ),
            ),
            array(
                'id' => 13,
                'name' => '会员信息管理',
                'parentid' => '',
                'haschild' => 1,
                'child' => array(
                    array(
                        'id' => 14,
                        'name' => '会员列表',
                        'parentid' => 13,
                        'haschild' => 2,
                    ),
                    array(
                        'id' => 50,
                        'name' => '用户反馈意见',
                        'parentid' => 13,
                        'haschild' => 1,
                        'child' => array(
                            array(
                                'id' => 127,
                                'name' => '处理反馈信息',
                                'parentid' => 50,
                                'haschild' => 2,
                            ),
                            array(
                                'id' => 128,
                                'name' => '删除反馈信息',
                                'parentid' => 50,
                                'haschild' => 2,
                            ),
                        ),
                    ),
                ),
            ),
        );
        if (empty($data)) {
            return Rens(-7010000802); //没有数据
        }
        return Rens($data);
    }

    //修改权限信息
    function rootEdit() {
        $status = Request::has('status') ? Request::get('status') : 1; //1：成功 2：失败
        $rootid = Request::has('rootid') ? Request::get('rootid') : ''; //权限ID
        $level = Request::has('level') ? Request::get('level') : '';
        $name = Request::has('name') ? Request::get('name') : '';
        $maxmoney = Request::has('maxmoney') ? Request::get('maxmoney') : '';
        $menu = Request::has('menu') ? Request::get('menu') : '';
        if (empty($level)) {
            return Rens(-7010000802); //权限等级不能为空
        }
        if (empty($name)) {
            return Rens(-7010000902); //权限名不能为空
        }
        if (empty($maxmoney)) {
            return Rens(-7010001002); //最大操作金额不能为空
        }
        $res = $status; //修改权限信息
        $menulist = array('22', '21', '127');
        if (!empty($menu)) {
            $menu = json_decode($menu);
            $res = $this->addmenu($menu, $menulist, $rootid);
        }

        if ($res == 1) {
            return Rens($res);
        }
        return Rens(-7010001102); //设置权限失败

    }

    //删除管理员权限信息
    function delLevel() {
        $status = Request::has('status') ? Request::get('status') : 1;
        $id = Request::has('id') ? Request::get('id') : ''; //管理员权限ID，返回对应管理员权限信息
        if (empty($id)) {
            return Rens(-7010000802); //未提交管理权限ID
        }
        $res = $status; //删除管理员权限及其对应权限菜单关联信息
        if ($res == 1) {
            return Rens($res);
        }
        return Rens(-7010000902); //删除管理员权限信息失败
    }

    //操作日志
    function systemLog() {
        $page = Request::has('page') ? Request::get('page') : 1;
        $limit = Request::has('limit') ? Request::get('limit') : 10;
        $field = Request::has('field') ? Request::get('field') : 4;
        $order = Request::has('order') ? Request::get('order') : 'desc';

        $loginname = Request::has('loginname') ? Request::get('loginname') : '';
        $ip = Request::has('ip') ? Request::get('ip') : '';
        $operation = Request::has('operation') ? Request::get('operation') : '';
        $star_time = Request::get('star_time') ? Request::get('star_time').' 00:00:00' : '';
        $end_time = Request::get('end_time') ? Request::get('end_time').' 23:59:59' : '';
        $sureblur = Request::has('sureblur') ? Request::get('sureblur') : '';
        $where = array();
        if (!empty($loginname)) {
            if (empty($sureblur) || $sureblur == '模糊') {
                $where[] = array('loginname', 'like', '%' . $loginname . '%');
            } else {
                $where[] = array('loginname', '=', $loginname);
            }
        }
        if (!empty($ip)) {
            $where[] = array('ip', '=', $ip);
        }
        if (!empty($operation)) {
            $where[] = array('operation', 'like', '%' . $operation . '%');
        }
        if (!empty($star_time)) {
            $star_time = date('Y-m-d H:i:s', strtotime($star_time));
            $where[] = array('operation_time', '>=', $star_time);
        }
        if (!empty($end_time)) {
            $end_time = date('Y-m-d H:i:s', strtotime($end_time));
            $where[] = array('operation_time', '<=', $end_time);
        }

        $logdetails = new \App\Models\System_log();
        $data = $logdetails->getLoglist($where, 1, $limit, $field, $order);
        if (!($data < 0)) {
            foreach ($data['data'] as $k => $v) {
                $data['data'][$k]['ip'] = ($v['ip'] == null) ? '' : $v['ip'];
            }
        }

        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
    }

    //添加权限菜单
    private function addmenu($menu, $menulist, $rootid) {
        $data = array('root_id' => $rootid);
        foreach ($menu as $v) {
            if (!in_array($v['id'], $menulist)) {
                $data['menu_id'] = $v['id'];
                $res = 1; //新增权限菜单
            }
            if (!empty($v['child'])) {
                $getres = $this->addmenu($v['child'], $menulist, $rootid);
            }
        }
        return 1;
    }

}

?>