<?php

/**



 * 会员



 */

namespace User\Controller;

use Common\Controller\AdminbaseController;



class IndexadminController extends AdminbaseController {



    function index(){

    	$users_model=M("Users");

        $user_login = I('user_login');

        $map['user_login'] = array('like', '%'.(string)$user_login.'%');

        $last_login_ip = I('last_login_ip');

        if($last_login_ip)$map['last_login_ip']  = $last_login_ip;

        $register_ip = I('register_ip');

        if($register_ip)$map['register_ip']  = $register_ip;
        $id = I('id');
        if($id)$map['id']  = $id;
        $user_status = I('user_status');

        if($user_status||$user_status=='0')$map['user_status']  = $user_status;

        $order= ' create_time DESC ';

        //echo $users_model->getLastSql();
        $total_money = I('total_money');
        if($total_money)$order= 'total_money DESC';


        $money = I('money');
        if($money)$order= 'money DESC';
        
        
        $map['user_type']  = 2;

        $count=$users_model->where($map)->count();

    	$page       = new \Think\Page($count,20);//实例化分页类 传入总记录数和每页显示的记录数

        /*条件*/

        $parameter['user_login'] = $user_login;

        $parameter['last_login_ip'] = $last_login_ip;

        $parameter['register_ip'] = $register_ip;

        $parameter['user_status'] = $user_status;

        $parameter['total_money'] = $total_money;

        $parameter['money'] = $money;

        foreach($parameter as $key=>$val){

            $page->parameter[$key]   =   urlencode($val);

        }

        $show  = $page->show();// 分页显示输出

        $lists = $users_model

        ->where($map)

        ->order($order)

        ->limit($page->firstRow . ',' . $page->listRows)

        ->select();

        if($_POST['export']){

           $arr=$users_model

           ->where($map)

           ->order($order)

           ->select();

           foreach ($arr as $key => $value) {

            if(M('oauth_user')->where(array('uid'=>$value['id']))->find()){
                    $arr[$key]['from']=M('oauth_user')->where(array('uid'=>$vo['id']))->getField('from');
            }else{
                    $arr[$key]['from']= '自然注册';
            }

        }

        $title="用户管理";

        $cell=array(
            array('id','ID'),
            array('user_login','用户名'),
            array('user_nicename','昵称'),
            array('money','账号剩余金额'),
            array('total_money','累计充值金额(元)'),
            array('user_email','E-mail'),
            array('create_time','注册时间'),
            array('register_ip','注册IP'),
             array('from','用户来源'),
            array('last_login_time','最后登录时间'),
            array('last_login_ip','最后登录ip'),
            );

        $excel=A('Asset/Excel');

        $excel->exportExcel($title,$cell,$arr);

    }

    $this->assign('lists', $lists);

    $this->assign("page", $show);

    $this->assign('user_login', $user_login);

    $this->assign('last_login_ip', $last_login_ip);

    $this->assign('register_ip', $register_ip);
    $this->assign('id', $id);
    $this->assign('user_status', $user_status);

    $this->assign('total_money', $total_money);

    $this->assign('money', $money);

    $this->display();

}



function ban(){

 $id=intval($_GET['id']);

 if ($id) {

  $rst = M("Users")->where(array("id"=>$id,"user_type"=>2))->setField('user_status','0');

  if ($rst) {

   $this->success("会员拉黑成功！", U("indexadmin/index"));

} else {

   $this->error('会员拉黑失败！');

}

} else {

  $this->error('数据传入失败！');

}

}







function cancelban(){

 $id=intval($_GET['id']);

 if ($id) {

  $rst = M("Users")->where(array("id"=>$id,"user_type"=>2))->setField('user_status','1');

  if ($rst) {

   $this->success("会员启用成功！", U("indexadmin/index"));

} else {

   $this->error('会员启用失败！');

}

} else {

  $this->error('数据传入失败！');

}

}



    /**

     * 重置密码

     * @author 顽皮蛋 <shf_l@163.com>

     */

    public function resetpwd(){

        if(IS_POST){

            $id=I('post.id');

            $password=I('post.password');

            $repassword=I('post.repassword');

            $realname=I('post.realname');

            $idcard=I('post.idcard');


            if($password==$repassword&&!empty($password)&&!empty($repassword)){

                $map['user_pass']=sp_password($password);

            }else{

               $this->error('请正确填写密码');

           }
           // if(!preg_match("/^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/", $idcard)){
           //  $this->error('身份证格式不正确');
        // }else{
            $map['id_card']=$idcard;
        // }
        $map['realname']=$realname;

        $email=I('post.email');

        if(empty($email)){

            $this->error('请输入邮箱');

        }else{

            $map['user_email']=$email;

        }
        $map['id']=$id;
        if(M('users')->save($map)){
            $ucenter_syn=C("UCENTER_ENABLED");
            if($ucenter_syn){
                $user_login=I('post.user_login');
                $domain  = explode('.',$_SERVER['HTTP_HOST']);
                include "data/uc_conf/".$domain['0']."/config.inc.php";
                include UC_CLIENT_ROOT."client.php";
                uc_user_edit($user_login,'',$repassword,$email,$ignoreoldpw = 1);
            }
            $this->success('修改成功！');
        }else{
            $this->error('修改失败！');
        }

    }else{

        $id  = I('id');

        $user_login=M('users')->where(array('id'=>$id))->find();

        $this->assign('id',$id);

        $this->assign('user_login',$user_login);

        $this->display();

    }




}

    /**

     * 查看用户信息

     * @author 顽皮蛋 <shf_l@163.com>

     */

    public function look(){

        $id = I('id');

        $info = M('users')->where(array('id'=>$id))->find();


        /*获取用户地址*/

        $users_other = M('users_other')->where(array('uid'=>$id))->find();

        

          $info['address']  = '未知';

      

      $this->assign('info', $info);

      $this->display();

  }

    

  

}



