<?php

	/***************************************************************\
	|																|
	| 开单大师（专业的房产ERP管理系统）								|
	|																|
	| © 2018 http://fs.ytsofts.cn All rights reserved.		|
	|																|
	| 本系统由淮南市银泰软件科技有限公司提供技术支持				|
	|																|
	| QQ号：984784483												|
	|																|
	\***************************************************************/

namespace Admin\Controller;
use Think\Controller;

class LoginController extends Controller {
	


	public function index(){
		if (IS_POST) {
			$verify = new \Think\Verify();
			$code= trim(I('code'));
			if($verify->check($code) === false){
				$this->error('验证码错误');
			}
        	$rules = array(
			     array('username','6,20','账号格式不正确',1,'length'),
			     array('password','checkPwd','密码格式不正确',1,'function'),
			);
			$db = M("yonghu"); // 实例化User对象
			if (!$db->validate($rules)->create()){
			     $this->error($db->getError());
			}
			$username = I('username');
            $password = I('password');
			/*根据账号密码查找用户信息*/
			$user_find = $db->where(array('dianhua' =>I('username')))->find();
			if (!$user_find || $user_find['mima'] != pass_salt(I('password'))) {
				$this->error('用户名或密码错误！');
			}
			if ($user_find['lock1']==1) {
				$this->error('该账号已被停用！');
			}
			if ($user_find['status']==0) {
				$this->error('用户名或密码错误！');
			}

			$company_find = M('company')->where(array('id'=>$user_find['gongsiid']))->find();

			if ($company_find['status'] == -1) {
				$this->error('公司已被停用!');
			}
			if ($company_find['status'] == 0) {
				$this->error('审核中...');
			}
			$ipdz=get_client_ip();
			$wldz = findCityByIp($ipdz);
			if ($wldz) {
				$zsdz=$wldz;
			} else {
				$zsdz='未知';
			}
			$data = array(
				'id'=>$user_find['id'],
				'logintime'=>time(),
				'loginip'=>$ipdz
			);
			$db->save($data);
			$data1 = array(
				'gongsiid'=>$user_find['gongsiid'],
				'shijian'=>time(),
				'ipdizhi'=>$ipdz,
				'zdbianhao'=>$zsdz,
				'yonghu'=>$user_find['ygmingcheng'],
				'uid'=>$user_find['id'],
				'bumen'=>$user_find['bumen'],
				'dlleibie'=>'pc',
			);
			M('dlrizhi')->add($data1);
			$role_id=M('role_user')->where(array('gongsiid'=>I('gongsiid'),'user_id' =>$user_find['id']))->getField('role_id');
			session('uid',$user_find['id']);
			session('role',$role_id);
			session('bumen',$user_find['bumen']);
			session('ygmingcheng',$user_find['ygmingcheng']);
			session('gongsiid',$user_find['gongsiid']);
			session('username',$user_find['dianhua']);
			session('companyName',$company_find['companyName']);

			session('groups',$user_find['groups']);
			session('user',$user_find);
			session('org',$company_find);
			$Rbac = new \Lib\Rbac\Rbac;
			
            /*登陆成功 相关信息写入Cookie Session*/
            cookie('username',$user_find['dianhua'],3600*24*7);
			if ($company_find['admin_id'] == $user_find['id']) {
				session('admin',1);
				/*if ($company_find['initialization'] == 0) {
					$Return['status'] = 1;
					$Return['data'] = array('url'=>U('initialization/index'));
					$Return['info'] = '登陆成功，跳转中。。。';
					$this->ajaxReturn($Return);
				} else {
					$Rbac->saveAccessList($user_find['id'],1);
				}*/
				$Rbac->saveAccessList($user_find['id'],1);
			} else {
				$Rbac->saveAccessList($user_find['id'],0);
			}
			$return = array('status'=>1,'info'=>'登陆成功，跳转中。。。','data'=>array('url'=>U('index/index')));
            $this->ajaxReturn($return);
        } else {
        	$this->assign('username',cookie('username'));
        	$this->display('index1');
        }
	}

	//公司注册
	public function register()
	{
		if (IS_POST) {
			$code = trim(I('code'));
			$companyName = trim(I('companyName'));
			$companyAddress = trim(I('companyAddress'));
			$nickname = trim(I('nickname'));
			$username = trim(I('username'));
			$password = trim(I('password'));
			$ppassword = trim(I('ppassword'));


			$verify = new \Think\Verify();
			if($verify->check($code) === false){
				$this->error('验证码错误!');
			}
			if ($password !== $ppassword) {
				$this->error('两次密码不匹配!');
			}

			$yonghu_find = M('yonghu')->where(array('dianhua'=>$username))->find();
			if ($yonghu_find) {
				$this->error('电话已被注册!');
			}
			
			
			$add_company_data['companyName'] = $companyName;
            $add_company_data['address'] = $companyAddress;
            $add_company_data['onlyCode'] = StrOrderOne();
            $add_company_data['status'] = 0;
            $add_company_data['addTime'] = time();
            $add_company_data['upTime'] = time();

            $add_user_data['ygmingcheng'] = $nickname;
            $add_user_data['dianhua'] = $username;
            $add_user_data['mima'] = pass_salt($password);
            $add_user_data['status'] = 1;
            $add_user_data['lock1'] = 0;

            $add_bumen_data['bmming'] = $companyName;
            $add_bumen_data['level'] = 1;

            $company_add_return = M('company')->add($add_company_data);
            $add_user_data['gongsiid'] = $company_add_return;
            $add_bumen_data['gongsiid'] = $company_add_return;
            $bumen_add_return = M('bumen')->add($add_bumen_data);
            $add_user_data['bumen'] = $bumen_add_return;
            $add_user_data['groups'] = $bumen_add_return;
            $user_add_return = M('yonghu')->add($add_user_data);
            M('company')->where(array('id'=>$company_add_return))->setField('admin_id',$user_add_return);
            $this->success('注册成功,请等待审核!');

		} else {
			$this->display();
		}
		
	}
	
	public function VerifyImg(){
		$verify = new \Think\Verify();
		//$verify->useZh = true;  //使用中文验证码
		$verify->fontSize = 18; //验证码字体大小
		$verify->useNoise = false;  //关闭验证码干扰项
		$verify->length = 2; 
		$verify->imageW = 146;
		$verify->imageH= 36;
		ob_end_clean();
		$verify->entry();
	}
	public function Logout(){
		session_unset();
		session_destroy();
		$this->redirect('Login/index');
	}
}