<?php

namespace Home\Controller;
use Think\Controller;
class LoginController extends CommonController {
    public function index(){
        if(IS_POST){
            echo json_encode(D('Login')->login());
        }else{
            $mbcof=C('Member');
            foreach ($mbcof['register'] as $rek => $rev) {
                if($rek=='account'){$ltr='账号，';}
                if ($rek=='email') {$ltr.='邮箱，';}
                if ($rek=='mobile') {$ltr.='手机号';}
            }
            $this->ltr=$ltr;
        	$this->display();
        }
    }
    
    public function register(){
    	if(IS_POST){
            if (!check_verify(I('post.verify_code'),'',false)) {
                return array('status' => 0, 'info' => '验证码错了,重新输入吧');
                exit;
            }
            switch (I('post.registerType')) {
                case 'account':
                    echo json_encode(D('Login')->register());
                    break;
                case 'email':
                    echo json_encode(D('Login')->emailSendCode());
                    break;
                case 'mobile':
                    echo json_encode(D('Login')->noteSendCode());
                    break;
                
                default:
                    # code...
                    break;
            }
        }else{
            $mf = M('feedback');
            $mbcof=C('Member');
            $this->rtype = $mbcof['register'];
	    	$this->feedback = $mf->select();
            if(!$mbcof['register']['account']){
                $registerType=key($mbcof['register']);
            }else{
                $registerType=!I('get.registerType')?'account':I('get.registerType');
            }
            $this->registerType=$registerType;

	    	$this->display();
        }
    }
    
    public function emailRegister(){
        if(IS_POST){
            if (!check_verify(I('post.verify_code'),'',false)) {
                return array('status' => 0, 'info' => '验证码错了,重新输入吧');
                exit;
            }
            echo json_encode(D('Login')->register());
        }else{
            $thisMb = M('member')->field(array('uid','email'))->where(array('uid'=>I('get.rid')))->find();
            $this->thisMb=$thisMb;
            $mf = M('feedback');
            $this->feedback = $mf->select();
            $this->display('register');  
        }
        
    }
    
    public function mobileRegister(){
        if(IS_POST){
            if (!check_verify(I('post.verify_code'),'',false)) {
                return array('status' => 0, 'info' => '验证码错了,重新输入吧');
                exit;
            }
            echo json_encode(D('Login')->register());
        }else{
            $thisMb = M('member')->field(array('uid','mobile'))->where(array('uid'=>I('get.rid')))->find();
            $this->thisMb=$thisMb;
            $mf = M('feedback');
            $this->feedback = $mf->select();
            $this->display('register');  
        }
        
    }
    
    public function checkAccount(){
    	if(M('member')->where(array('account'=>I('post.account')))->count()!=0){
    		echo 'false';
		} else {
			echo 'true';
		}
    }
    
    public function checkVerify(){
    	if(check_verify(I('post.verify_code'),'',false)){
    		echo 'true';
		} else {
			echo 'false';
		}
    }
    
    public function checkMobile(){
    	$M = M('member');
    	if(M('member')->where(array('mobile'=>I('post.mobile')))->count()!=0){
    		echo 'false';
		} else {
			echo 'true';
		}
    }
    
    public function checkEmail(){
        $M = M('member');
        if(M('member')->where(array('email'=>I('post.email')))->count()!=0){
            echo 'false';
        } else {
            echo 'true';
        }
    }
    
    public function information(){
    	$userAgreement = include APP_PATH . 'Common/Conf/UserAgreement.php';
    	$this->userAgreement = htmlspecialchars_decode($userAgreement['USER_AGREEMENT']);
    	$this->display();
    }

    public function verify_code(){
        $Verify = new \Think\Verify();
        $Verify->fontSize = 17;
        $Verify->length   = 4;
        $Verify->codeSet = '0123456789';
        $Verify->entry();
    }

    Public function loginOut() {
        setcookie("$this->loginMarked", NULL, -3600, "/");
        unset($_SESSION["$this->loginMarked"], $_COOKIE["$this->loginMarked"]);
        if (isset($_SESSION[C('USER_AUTH_KEY')])) {
            unset($_SESSION[C('USER_AUTH_KEY')]);
            unset($_SESSION);
        }
        session_unset();
        session_destroy();
        $this->redirect("Index/index");
    }
}?>
