<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login_test extends MY_Controller
{

	function __construct()
	{
		parent::__construct();

		$this->load->model("user_model");
		$this->load->library("user_class");
		$this->load->helper("template_helper");
		$this->load->model("UserTest_model");
		$this->user_id = checklogin();
	}

    //登录页面
	function index()
	{
		$data['title'] = '用户登录';
		if($_POST)
		{
			$method	= $this->input->post('method');
			$telno	= $this->input->post('telno');
			$password	= $this->input->post('password');
			$telCode	= $this->input->post('telCode');
			$loginkind	= $this->input->post('loginkind');
			$password2	= $this->input->post('password2');
            $name 	= $this->input->post('name');
            $action	= $this->input->post('action');
            $refer	= $this->input->post('refer');
            $refer1	= $this->input->post('refer1');
            
            $ajaxyz	= $this->input->post('ajaxyz');

            //隐藏表单的提交
            if($this->input->post('telnotj'))
            {
              $telno = $this->input->post('telnotj');
            }
            if($this->input->post('passwordtj'))
            {
              $password2 = $this->input->post('passwordtj');
            }
            if($this->input->post('telcodetj'))
            {
              $telCode = $this->input->post('telcodetj');
            }
            if($this->input->post('loginkindtj'))
            {
              $loginkind = $this->input->post('loginkindtj');
            }

			if( $loginkind == 1 )
			{       
				    //验证全站个人会员中心有无此用户
					//$ucenterTel = $this->UserTest_model->ucenterCheckTelno($telno);
                    //如果全站有此用户，二手房有

					//如果全站有此用户，二手房没有（插入二手房一条）

                   //如果全站没此用户，二手房有（这种情况还需要考虑，问窦）
					
					 //如果全站没此用户，二手房也没有

					//验证是否有此手机用户
					$result = $this->user_model->yztelno($telno);
					

					if($result == 0)
					{
						echo 'N_USER';
					    exit;
					}
                   
					//动态密码登录
					$user_isset = $this->user_model->yzlogindtnew($telno,$telCode);
                    $result = $user_isset['id'];
                    $password = $user_isset['password'];

					if( $result == 0 )
					{
				        echo 'N_PASS';
					    exit;
					}elseif($ajaxyz==1 && $result>0){
                        
                        echo 'Y';
					    exit;   
                    }
				
			}
			else
			{
                
                if($ajaxyz==1)
                { 
                 $telno = trim(iconv('UTF-8', 'gbk', $telno));
                }

                
                $password = md5($password2);

                //检测个人会员中心有无此用户(不刷新处理)
				$ucenterTel = $this->UserTest_model->ucenterCheckTelno($telno);
				//验证二手房有无此用户
                $esfCheckTel = $this->user_model->yztelno($telno);
                //验证初始化
				$checkResult = False;

                if($ucenterTel&&$esfCheckTel!=0){
					//全站和二手房都有
					//考虑密码
					//验证全站密码和帐号（先）
                    $ucenterResult = "function问窦总要全站检测密码和帐号的接口（这个接口能返回登录接口详细的帐号信息非登录接口）";
					if($ucenterResult){
                         $checkResult = True;//全站验证通过
					
					}else{
						//验证二手房密码和帐号（如果全站不通过的时候二手房通过记录log日志）
                        $esfResult = $this->user_model->yzlogin($telno,$password);
						if($esfResult>0){
							 //log
                             //log("帐号--全站没有、二手房有密码");  这个问题找窦总，那还怎么登录他的状态?????
                           $checkResult = True;//二手房验证通过
						
						}else{
						   $checkResult == False;
						}
					    
					}

				}elseif($ucenterTel&&$esfCheckTel==0){
					//如果全站有此用户，二手房没有（插入二手房一条）
					 $ucenterResult = "function问窦总要全站检测密码和帐号的接口（这个接口能返回登录接口详细的帐号信息非登录接口）";
					 if($ucenterResult){
					     $checkResult = True;//全站验证通过
						 //把个人会员中心数据插入二手房						 
						 $this->UserTest_model->addUserFromUcenter($passport_uid,$password,$telno,$city,$email,$username,$nickname,$avatar,$ip);
					 }else{
					     $checkResult = False;
					 }

                      
				}elseif(!$ucenterTel&&$esfCheckTel!=0){
					//如果全站没此用户，二手房有（只管二手房，不管全站）					
                    $esfResult = $this->user_model->yzlogin($telno,$password);
					if($esfResult>0){
					   $checkResult = True;//二手房验证通过
					}else{
					   $checkResult = False;//二手房验证通过
					}

				}else{
				   //全站和二手房都没有此用户电话的（考虑二手房用户名登录）
                    //验证二手房用户名
                    $esfResult = $this->user_model->yzlogin($telno,$password);
					if($esfResult>0){
						//转换成手机号码
						$esfUserDetail = $this->user_model->getuserdetail($esfResult);
						$telno = $esfUserDetail['telno'];
						$checkResult = True;//二手房验证通过
						//log记个日志
					}else{
					    $checkResult = False;
					
					}

				}   
                
                $name = $telno;

				if(!$checkResult){
				   echo 'N_USER';
					exit;
				}elseif($ajaxyz==1 && $checkResult){
				    echo 'Y';
					exit;
				}
			}

			if( $result > 0 && $action!='loginyz' && $ajaxyz!='1')
			{
                
				 $this->user_model->updatelogintime($result);

				 $this->user_class->user_login($result,$password,$telno,$name);

                 //如果上一页是登陆页面，就直接跳首页
                 if((!empty($refer1) or !empty($refer)) && $refer!=USER_CENTER.'user/logout' && $refer1!=USER_CENTER.'user/logout')
                 {
                   //jumptotk(USER_CENTER.'user',"您已登录,进入会员中心首页!",3000,'tips');
                   if(!empty($refer)){
                   jumpto($refer);
                   }else{
                   jumpto($refer1);
                   }
                 }else{
                   //jumptotk($_SERVER['HTTP_REFERER'],"登录成功,页面将跳转到之前的页面。",3000,'tips');
                   jumpto(USER_CENTER.'user');
                 }
                 exit;
			}
		}else{
        if( $this->user_id > 0 )
		{
          //jumptotk(USER_CENTER.'user',"您已登录,进入会员中心首页!",3000,'tips');
          jumpto(USER_CENTER.'user');
		}
        }

		$this->load->view('login_test',$data);
	}

	//iframe_login_ajax
	function iframe_login_ajax()
	{
		$telno	= $this->input->get('telno');
		$password	= $this->input->get('password');
		$tel_code	= $this->input->get('tel_code');
		$is_code_login	= $this->input->get('is_code_login');
 
		$telno = trim(iconv('UTF-8', 'gbk', $telno));

		if( $is_code_login == 1 )
		{       
			//动态密码登录
			$user_isset = $this->user_model->yzlogindt($telno,$tel_code);
            $result = $user_isset['id'];
			$password = $user_isset['password'];
			if( $result )
			{
				$this->user_model->updatelogintime($result);
				$this->user_class->user_login($result,$password, $telno);

				//隐藏手机号部分数字
				$pattern = "/(1\d{1,2})\d\d(\d{0,3})/";
				$replacement = "\$1*****\$3";
				$telno2 = preg_replace($pattern, $replacement, trim($telno));
				$showname = $telno2 ? $telno2 : "" ;
				$esfmesnum =  $this->user_model->getmessagenumbyuidwd($result);
				$password_encrypt = passport_encrypt($password);

				$ajax_return['error'] = "0";
				$ajax_return['result'] = $result;
				$ajax_return['showname'] = $showname;
				$ajax_return['esfmesnum'] = $esfmesnum;
				$ajax_return['password_encrypt'] = $password_encrypt;

			}else{
				$ajax_return['error'] = "1";            
			}
		}else{
			$result	= $this->user_model->yzlogin($telno,$password);
			if( $result )
			{
				$this->user_model->updatelogintime($result);
				$this->user_class->user_login($result, md5($password), $telno);

				//隐藏手机号部分数字
				$pattern = "/(1\d{1,2})\d\d(\d{0,3})/";
				$replacement = "\$1*****\$3";
				$telno2 = preg_replace($pattern, $replacement, trim($telno));
				$showname = $telno2 ? $telno2 : "" ;
				$esfmesnum =  $this->user_model->getmessagenumbyuidwd($result);
				$password_encrypt = passport_encrypt(md5($password));

				$ajax_return['error'] = "0";
				$ajax_return['result'] = $result;
				$ajax_return['showname'] = $showname;
				$ajax_return['esfmesnum'] = $esfmesnum;
				$ajax_return['password_encrypt'] = $password_encrypt;

			}else{
				$ajax_return['error'] = "1";            
			}
		}
		
		echo serialize($ajax_return);
		exit;
	}

	/**
	* 用户登录接口
	* @author cxf
	*/
	function api_login()
	{

		$telno = $this->input->get('telno');
		$result = $this->user_model->yztelno($telno);

		if( $result > 0 )
		{
			$this->user_model->updatelogintime($result);
			$return	= $this->user_class->user_login($result,$password,$telno);
		}
		else
		{
			$password	= $this->user_model->getyzmpassword($telno);
			$result	= $this->user_model->adduser($telno,$paasword);
			$this->user_model->updatelogintime($result);
			$this->user_class->user_login($result,$password,$telno);
		}
	}

  

	
    /**
	* 用户链接判断
	* @author cxf
	*/
	function login_judge()
	{
      $this->load->view('login_judge');
	}
	
	/**
	* 用户链接判断
	* @author cxf
	*/
	function login_js()
	{
      $this->load->view('login_js');
	}

    /**
	* 用户链接判断
	* @author cxf
	*/
	function login_js_fenzhan()
	{
      $this->load->view('login_fenzhan_js');
	}

    /**
	* 外地站老头部
	* @author cxf
	*/
	function login_js_oldfenzhan()
	{
      $this->load->view('login_oldfenzhan_js');
	}

}
