<?php
namespace app\admin\controller;

/**
*	管理用户
*/
class User extends Admin{

    protected $User = null;
    protected function _initialize(){
        parent::_initialize();
        $this->User = model('User');
    }



    public function index(){
        $this->assign("meta_title","用户列表");
        return $this->fetch();
    }

    public function load(){
        $where = [];
        $rpid = input('param.rpid');
        if (!empty($rpid)) {
            $where['r.pid'] = $rpid;
        }else{
            $where['r.name'] = ['eq','user'];
        }
        $user = input('param.user');
        if (!empty($user)) {
            $where['u.id|u.name'] = $user;
        }
        $page = input('get.page');
        $limit = input('get.limit');
        $list = $this->User->field('u.*')->alias('u')
        ->join('Role r','r.id = u.role_id')
        ->where($where)->order('u.id desc')->paginate($limit,false,['page'=>$page]);
        $data = [];
        foreach ($list as $key => $value) {
            $data[$key]['id'] = $value['id'];
            $data[$key]['name'] = $value['name'];
            $data[$key]['state'] = $value['state'];
        }
        $this->output(0,'加载成功',$data,$list->total());
    }

    public function add(){
        if($this->request->isPost()){
            $name = input('post.name');
            $user = $this->User->where(['name'=>$name])->find();
            if ($user) {
                return json(['data'=>null,'code'=>1,'msg'=>'用户名已经存在']);
            }

            $role_id = input('post.role_id');
            $where['id'] = $role_id;
            $Role = model('Role');
            $role = $Role->where($where)->find();
            if (!$role) {
                return json(['data'=>null,'code'=>1,'msg'=>'请选择角色']);
            }

            $password = input('post.password');
            if (empty($password)) {
                return json(['data'=>null,'code'=>1,'msg'=>'密码不能为空']);
            }
            
            $this->User->name= $name;
            $salt = getNonceStr(4);
            $this->User->password = md5(md5($password).$salt);
            $this->User->salt = $salt;
            $this->User->role_id = $role_id;
            $result = $this->User->save();
            if (!$result) {
                output(1,'添加失败');
            }

            output(0,'添加成功');
        }else{
            $rpid = input('get.rpid');
            if (empty($rpid)) {
                $where['name'] = 'user';
            }else{
                $where['pid'] = $rpid;
            }
            $Role = model('Role');
            $roles = $Role->where($where)->select();
            $this->assign('roles',$roles);
            
            $this->assign('meta_title','添加用户');
            return $this->fetch();
        }
    }

    public function edit(){
        if ($this->request->isPost()) {
            $id = input('post.id');
            $name = input('post.name');
            if (!empty($name)) {
                $user = $this->User->where(['name'=>$name])->find();
                if ($user && $user['id'] != $id) {
                    return json(['data'=>null,'code'=>1,'msg'=>'用户名已经存在']);
                }
            }

            $user = $this->User->where(['id'=>$id])->find();
            if (!$user) {
                return json(['data'=>null,'code'=>1,'msg'=>'参数错误']);
            }

            $role_id = input('post.role_id');
            $where['id'] = $role_id;
            $Role = model('Role');
            $role = $Role->where($where)->find();
            if (!$role) {
                return json(['data'=>null,'code'=>1,'msg'=>'请选择角色']);
            }

            $password = input('post.password');
            if (!empty($password)) {
                $salt = $user['salt'];
                $user->password = md5(md5($password).$salt);
            }

            $oppwd = input('post.oppwd');
            if (!empty($oppwd)) {
                $user->oppwd = $oppwd;
            }

            $user->name= $name;
            $user->role_id = $role_id;
            $result = $user->save();
            if ($result) {
                return json(['data'=>null,'code'=>0,'msg'=>'编辑成功']);
            }
            return json(['data'=>null,'code'=>1,'msg'=>'编辑成功']);
        }else{
            $id = input('get.id');
            $user = $this->User->where(['id'=>$id])->find();
            $this->assign('user',$user);

            $role = $user['role'];
            if ($role['name'] == 'user') {
                $where['name'] = ['eq','user']; 
            }else{
                $where['name'] = ['not in',['user','admin']]; 
            }
            $Role = model('Role');
            $roles = $Role->where($where)->select();
            $this->assign('roles',$roles);

            $id = input('get.id');
            $user = $this->User->where(['id'=>$id])->find();
            $this->assign('user',$user);

            $this->assign('meta_title','编辑用户');
            return $this->fetch();
        }
    }

    public function center(){
        if ($this->request->isPost()) {
            $id = input('post.id');
            $user = $this->User->where(['id'=>$id])->find();
            if (!$user) {
                $this->output(1,'参数错误');
            }
            $center = input('post.center');
            $user->center = $center == 'true'?1:0;
            $result = $user->save();
            if ($result) {
                $this->output(0,'编辑成功');
            }
            $this->output(1,'编辑失败');
        }
    }

    public function active(){
        if ($this->request->isPost()) {
            $id = input('post.id');
            $user = $this->User->where(['id'=>$id])->find();
            if (!$user) {
                $this->output(1,'参数错误');
            }
            $active = input('post.active');
            $user->active = $active == 'true'?1:0;
            $result = $user->save();
            if ($result) {
                $this->output(0,'编辑成功');
            }
            $this->output(1,'编辑失败');
        }
    }

    public function state(){
        if ($this->request->isPost()) {
            $id = input('post.id');
            $user = $this->User->where(['id'=>$id])->find();
            if (!$user) {
                $this->output(1,'参数错误');
            }
            $state = input('post.state');
            $user->state = $state == 'true'?1:0;
            $result = $user->save();
            if ($result) {
                $this->output(0,'编辑成功');
            }
            $this->output(1,'编辑失败');
        }
    }

    public function level(){
        if ($this->request->isPost()) {
            $id = input('post.id');
            $user = $this->User->where(['id'=>$id])->find();
            if (!$user) {
                output(1,'参数错误');
            }
            $UserLevel = model('user.Level');
            $level = input('post.level');
            $data = [];
            for ($i=0; $i <= $level; $i++) { 
                $userlevel = $UserLevel->where(['user_id'=>$user['id'],'level'=>$i])->find();
                if (!$userlevel) {
                    $data[$i]['user_id'] = $user['id'];
                    $data[$i]['level'] = $i;
                }
            }
            $UserLevel->where(['user_id'=>$user['id'],'level'=>['gt',$level]])->delete();
            if (count($data) > 0) {
                $UserLevel->saveAll($data);
            }
            $user->level = $level;
            $result = $user->save();
            if ($result) {
                output(0,'编辑成功',['level_text'=>$user['level_text']]);
            }
            output(1,'编辑失败');
        }
    }

    public function balance(){
        if ($this->request->isPost()) {
            $id = input('post.id');
            $user = $this->User->where(['id'=>$id])->find();
            if (!$user) {
                output(1,'参数错误');
            }
            $balance = input('post.balance');
            $balance = floatval($balance);
            $this->User->startTrans();
            $user['balance'] += $balance;
            $result = $user->save();
            if (!$result) {
                $this->User->rollback();
                $this->output(1,'编辑失败');
            }

            $billdata = [];
            $i = 0;
            $billdata[$i]['user_id'] = $user['id'];
            $billdata[$i]['amount'] = $user['balance'];
            $billdata[$i]['value'] = $balance;
            $billdata[$i]['fee'] = 0;
            $billdata[$i]['type'] = 1;
            $billdata[$i]['account'] = 'balance';
            $Bill = model('Bill');
            $result = $Bill->saveAll($billdata);
            if (!$result) {
                $this->User->rollback();
                $this->output(1,'账单保存失败');
            }

            $this->User->commit();
            $this->output(0,'编辑成功',['balance'=>$user['balance']]);
            
        }
    }

    public function coin(){
        if ($this->request->isPost()) {
            $id = input('post.id');
            $user = $this->User->where(['id'=>$id])->find();
            if (!$user) {
                output(1,'参数错误');
            }
            $coin = input('post.coin');
            $coin = floatval($coin);
            $user['coin'] += $coin;
            $result = $user->save();
            if ($result) {
                output(0,'编辑成功',['coin'=>$user['coin']]);
            }
            output(1,'编辑失败');
        }
    }

    public function points(){
        if ($this->request->isPost()) {
            $id = input('post.id');
            $user = $this->User->where(['id'=>$id])->find();
            if (!$user) {
                $this->output(1,'参数错误');
            }
            $points = input('post.points');
            $points = floatval($points);
            $user['points'] += $points;
            $result = $user->save();
            if (!$result) {
                $this->User->rollback();
                $this->output(1,'编辑失败');
            }

            $billdata = [];
            $i = 0;
            $billdata[$i]['user_id'] = $user['id'];
            $billdata[$i]['amount'] = $user['points'];
            $billdata[$i]['value'] = $points;
            $billdata[$i]['fee'] = 0;
            $billdata[$i]['type'] = 1;
            $billdata[$i]['account'] = 'points';
            $Bill = model('Bill');
            $result = $Bill->saveAll($billdata);
            if (!$result) {
                $this->User->rollback();
                $this->output(1,'账单保存失败');
            }

            $this->User->commit();
            $this->output(0,'编辑成功',['points'=>$user['points']]);

            output(1,'编辑失败');
        }
    }

    public function remark(){
        if ($this->request->isPost()) {
            $id = input('post.id');
            $user = $this->User->where(['id'=>$id])->find();
            if (!$user) {
                output(1,'参数错误');
            }
            $value = input('post.value');
            $user->remark = $value;
            $result = $user->save();
            if ($result) {
                output(0,'编辑成功');
            }
            output(1,'编辑失败');
        }
    }

    public function forbid(){
        if ($this->request->isPost()) {
            $id = input('post.id');
            $user = $this->User->where(['id'=>$id])->find();
            if (!$user) {
                $this->output(1,'参数错误');
            }
            $options = input('options/a','');
            $user->forbid = $options;
            $result = $user->save();
            if (!$result) {
                $this->output(1,'保存失败');
            }
            $this->output(0,'保存成功');
        }else{
            $id = input('get.id');
            $user = $this->User->where(['id'=>$id])->find();
            if (!$user) {
                $this->error('参数错误');
            }
            $this->assign('user',$user);
            $this->assign('meta_title','禁止用户');
            return $this->fetch();
        }
    }

    public function delete(){
        if ($this->request->isPost()) {
            $id = input('post.id');
            $user = $this->User->where(['id'=>$id])->find();
            if (!$user) {
                $this->output(1,'参数错误');
            }
            $Tree = model('Tree');
            $tree = $Tree->where(['user_id'=>$user['id'],'layer'=>1])->find();
            if ($tree) {
                $Tree->where(['node_id'=>$user['id']])->update(['node_id'=>$tree['node_id']]);
            }
            $result = $user->delete();
            if (!$result) {
                $this->output(1,'删除失败');
            }
            $this->output(0,'删除成功');
        }
    }

    public function team(){
        $id = input('get.id');
        $user = $this->User->where(['id'=>$id])->find();
        if (!$user) {
            $this->error('参数错误');
        }
        $this->assign('user',$user);
        $this->assign('meta_title','团队');
        return $this->fetch();
    }

    public function loadusers(){;
        $pid = input('get.pid',0);
        $level = input('get.level',9999);

        $list = $this->User->tree($pid,$level);
        $this->output(0,'加载成功',$list);
    }

    public function fans(){
        $id = input('get.id');
        $user = $this->User->where(['id'=>$id])->find();
        if (!$user) {
            $this->error('参数错误');
        }
        $this->assign('user',$user);
        $this->assign('meta_title','粉丝团');
        return $this->fetch();
    }

    public function loadfans(){;
        $pid = input('get.pid',0);
        $level = input('get.level',9999);

        $list = $this->User->fans($pid,$level);
        $this->output(0,'加载成功',$list);
    }

    public function getinfobyname(){
        $name = input('post.name');
        $user = $this->User->where(['name'=>$name,'state'=>1])->find();
        if ($user) {
            return json(['data'=>$user,'code'=>0,'msg'=>'获取成功']);
        }
        return json(['data'=>null,'code'=>1,'msg'=>'获取失败']);
    }

    public function getinfobyno(){
        $no = input('post.no');
        $user = $this->User->where(['no'=>$no,'state'=>1])->find();
        if ($user) {
            $data['id'] = $user['id'];
            $data['no'] = $user['no'];

            $role = $user['role'];
            $data['role_name'] = $role['name'];

            $company = $user['company'];
            $data['company_cname'] = $company['cname'];
            return json(['data'=>$data,'code'=>0,'msg'=>'获取成功']);
        }
        return json(['data'=>null,'code'=>1,'msg'=>'获取失败']);
    }

    public function getinfobyid(){
        $id = input('post.id');
        $user = $this->User->where(['id'=>$id,'state'=>1])->find();
        if ($user) {
            return json(['data'=>$user,'code'=>0,'msg'=>'获取成功']);
        }
        return json(['data'=>null,'code'=>1,'msg'=>'获取失败']);
    }
}