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

class Findpassword_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");
        }


        /**
         * 找回密码
         * @author cxf
        */
        public function index()
        {

            $data['title'] = '用户找回密码'; 

            if($_POST){
            $telno = $this->input->post('telno');
            $password = $this->input->post('setNewPassword');
            $againpassword = $this->input->post('repetPassword');
            $validcode = $this->input->post('validcode');
            $kind = $this->input->post('kind');
			if($password==$againpassword)
			{				
				
				//查找全站个人用户中心有无此号码 xue add
			    $ucenterTel = $this->UserTest_model->ucenterCheckTelno($telno);
				//验证二手房有无此号码
				$result2 = $this->user_model->yztelno($telno);
				//全站个人会员中心和二手房都有此用户
                if($ucenterTel&&$result2>0)
				{
					$ucenterReturn = $this->UserTest_model->ucenterForgetpwd($telno,md5($password),$validcode);	     
					if($ucenterReturn['result']==1)
					{
						//修改二手房密码
						$result_pass = $this->user_model->updatepassowrd($telno,$password);
						if($result_pass>0)
						{
							//登录个人会员中心
							$ucenter_result = $this->UserTest_model->ucenterLogin($telno,md5($password));
							//登录二手房
						    $this->user_class->user_login($result2,md5($password),$telno);
							echo 1;exit();
						 }
					}else{
						 //修改密码失败
					     echo 3;
						 exit();
					 }
                 //全站有此用户，二手房没有
				}elseif($ucenterTel&&$result2==0)
				{
					$ucenterReturn = $this->UserTest_model->ucenterForgetpwd($telno,md5($password),$validcode);
					if($ucenterReturn['result']==1)
					{
						  //插入二手房数据
						  $passport_uid = $ucenterReturn['data']['passport_uid'];
						  $password = md5($password2);
						  $telno = $telno;
						  $city = $ucenterReturn['data']['city'];
						  $email = $ucenterReturn['data']['email'];
						  $username = $ucenterReturn['data']['username'];
						  $nickname = $ucenterReturn['data']['nickname'];
						  $avatar = $ucenterReturn['data']['avatar'];
						  $ip = $ucenterReturn['data']['ip'];
						  $insert_return = $this->UserTest_model->addUserFromUcenter($passport_uid,md5($password),$telno,$city,$email,$username,$nickname,$avatar,$ip);
						  if($insert_return>0)
						  {
								 //登录个人会员中心
								$ucenter_result = $this->UserTest_model->ucenterLogin($telno,md5($password2));
								//登录二手房
								$this->user_class->user_login($result2,md5($password),$telno);
						        echo 1;exit(); 
						  }
					
					}else{
						 //修改密码失败
					     echo 3;
						 exit();					
					}				    
				
				}elseif(!$ucenterTel&&$result2>0){
					//modify xue 2015-03-11
					//验证手机验证码
					$result = $this->user_model->yzusersms($validcode,$telno,$kind);		  
					if($result>0)
					{
					  $result_pass = $this->user_model->updatepassowrd($telno,$password);					   
					  if($result_pass>0)
					  {
						 $this->user_class->user_login($result2,md5($password),$telno);
						 echo 1;exit;
					  }else{
						 //更新密码失败
						echo 3;exit;
					  }
					}else{
					  //短信验证不通过
					  echo 2;exit;
					}

				    //（个人会员中心无此号码，二手房有这个号码）记录日志
					//$msg = date('Y-m-d H:i:s').":".$telno." \n";
					//@file_put_contents('./forgetpasswrod_err',$msg,FILE_APPEND);
					echo 4;exit();
				}elseif(!$ucenterTel&&$result2==0){
				    echo 4;exit();
				}



			
                
			


				
                

               
                //验证手机验证码
                /*$result = $this->user_model->yzusersms($validcode,$telno,$kind);
      
                  if($result>0)
                  {
                  $result_pass = $this->user_model->updatepassowrd($telno,$password);
                   
                    if($result_pass>0)
                    {
                     $this->user_class->user_login($result2,md5($password),$telno);
					 //更新个人用户中心密码

                     echo 1;exit;
                    }else{
                     //更新密码失败
                    echo 3;exit;
                    }
                  }else{
                  //短信验证不通过
                  echo 2;exit;
                  }*/
              }
          }
         $this->load->view('findpassword_test',$data);
        }

}
?>