<?php

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

	namespace Admin\Controller;
	use Think\Controller;
	class FyxgjiluController extends CommonController {
		// 房源修改记录列表
		public function all(){
			if (I('is_ajax') && I('zy_type')) {
				$startUnix = strtotime(I('datetime1'));
				$endUnix = strtotime(I('datetime2'))+86399;
				$p = I('pageSize');
				$sn = I('sortName');
				$so = I('sortOrder');
				$zy_type = I('zy_type');
				if ($zy_type==1) {
					$sousuo=" where zy_type=1";
				}elseif ($zy_type==2) {
					$sousuo=" where zy_type=2";
				}
				if (I('datetime1') and I('datetime2')) {
					$sousuo.=" and addtime >= '".$startUnix."' and addtime<'".$endUnix."'";
				}elseif (I('datetime1')) {
					$sousuo.=" and addtime >= '".$startUnix."'";
				}elseif (I('datetime2')) {
					$sousuo.=" and addtime<'".$endUnix."'";
				}
				if (I('searchText')) {
					$sousuo1.= " where (e.pinyinjs like '%" .I('searchText'). "%') or (e.ygmingcheng like '%" .I('searchText'). "%') or (a.zy_bianhao like '%" .I('searchText'). "%')";
				}
				$Model = new \Think\Model();
				/*权限控制开始*/
				$map = "gongsiid=".session('gongsiid');
				$x = _rbac_lists_where(252,0);
				if ($x && $x != "all" && $x != "me") {
					$map .= " and bumen in ({$x})";
				}elseif ($x == "me") {
					$map .= " and id=".session('uid');
				}elseif ($x == false) {
					$map .= " and id=0";
				}
				/*权限控制结束*/
				$sqlcount = "select count(a.id) FROM (select * FROM jjrxt_rizhi ".$sousuo.") as a join (select id as e_id,ygmingcheng,pinyinjs from __YONGHU__ where {$map}) e on a.uid=e.e_id  ".$sousuo1;
				$countb=$Model->query($sqlcount);
				$count=$countb[0]['count(a.id)'];
				$Page = new \Think\Page($count,$p); //实例化分页类 传入总记录数和每页显示的记录数(15)
				$show = $Page->show();  //分页显示输出    
				$sql="select a.*,e.ygmingcheng,e.bumen FROM (select * FROM jjrxt_rizhi ".$sousuo.") as a join (select id as e_id,ygmingcheng,pinyinjs,bumen from __YONGHU__ where {$map}) e on a.uid=e.e_id ".$sousuo1." order by a.id DESC limit  ".$Page->firstRow.','.$Page->listRows;
				$arr=$Model->query($sql);
				foreach ($arr as $key => $value) {
					$arr[$key]['addtime1']=date('Y-m-d H:i:s',$value['addtime']);
					$arr[$key]['bmming']=bumen($value['bumen']);
					if ($value['zy_type'] == 1) {
						$es=M('fangyuan')->where(array('bianhao'=>$value['zy_bianhao']))->field('zhuangtai,weihurenid')->find();
						$arr[$key]['fyxqqx'] = _rbac_btn_show(46,$es['zhuangtai'],$es['weihurenid']) == true? '1': '0';
					} elseif ($value['zy_type'] == 2) {
						$es=M('keyuan')->where(array('xqbianhao'=>$value['zy_bianhao']))->field('zhuangtai,weihurenid')->find();
						$arr[$key]['kyxqqx'] = _rbac_btn_show(75,$es['zhuangtai'],$es['weihurenid']) == true? '1': '0';
					}
				}
				$data=array('data'=>$arr,'total'=>$count);
				header("Content-type: application/json");
				exit(json_encode($data));
			}else{
				$this->zy_type=I('zy_type');
				$this->display();
			}
		}
		//封开盘审核列表
		public function fkpsh(){
			$map .= " where gongsiid=".session('gongsiid');
			$x = _rbac_lists_where(841,0);
			if ($x && $x != "all" && $x != "me") {
				$map .= " and bumen in ({$x})";
			}elseif ($x == "me") {
				$map .= " and id=".session('uid');
			}elseif ($x == false) {
				$map .= " and id=0";
			}
			$condition = " where gongsiid=".session('gongsiid');
			$zylx=I('zylx');
			if ($zylx) {
				$condition.=" and zylx=".$zylx;
				$this->zylx=$zylx;
			} else {
				$condition.="";
				$this->zylx=0;
			}
			$czlx=I('czlx');
			if ($czlx) {
				$condition.=" and czlx=".$czlx;
				$this->czlx=$czlx;
				if ($czlx == 1) {
					$this->czxz="全部封盘";
					$this->lxxz="封盘类型";
					$this->dhxz="封电话";
					$this->dzxz="封地址";
					$this->djxz="封底价";
					$this->dqxz="封调取";
					$this->ckxz="封查看";
				} else if ($czlx == 2) {
					$this->czxz="全部开盘";
					$this->lxxz="开盘类型";
					$this->dhxz="开电话";
					$this->dzxz="开地址";
					$this->djxz="开底价";
					$this->dqxz="开调取";
					$this->ckxz="开查看";
				}
			} else {
				$condition.="";
				$this->czlx=0;
				$this->czxz="全部操作";
				$this->lxxz="操作类型";
				$this->dhxz="封/开电话";
				$this->dzxz="封/开地址";
				$this->djxz="封/开底价";
				$this->dqxz="封/开调取";
				$this->ckxz="封/开查看";
			}
			$type=I('type');
			if ($type) {
				$condition.=" and type=".$type;
				$this->type=$type;
			} else {
				$condition.="";
				$this->type=0;
			}
			$fkplx=I('fkplx');
			if ($fkplx) {
				$condition.=" and fkplx=".$fkplx;
				$this->fkplx=$fkplx;
			} else {
				$condition.="";
				$this->fkplx=0;
			}
			$shijian1=I('shijian1');
			$shijian2=I('shijian2');
			if ($shijian1) {
				$this->shijian1=$shijian1;
				$shijian3=strtotime($shijian1);
			}
			if ($shijian2) {
				$this->shijian2=$shijian2;
				$shijian4=strtotime($shijian2)+86400;
			}
			if ($shijian3 and $shijian4){
				if ($shijian3 > $shijian4){
					$condition.=" and addtime>".$shijian4." and addtime<".$shijian3;
					$this->shijian1=$shijian2;
					$this->shijian2=$shijian1;
				} else {
					$condition.=" and addtime>".$shijian3." and addtime<".$shijian4;
				}
			} elseif ($shijian3) {
				$condition.=" and addtime>".$shijian3;
			} elseif ($shijian4) {
				$condition.=" and addtime<".$shijian4;
			}
			$count=M('fpkp')->query("select count(*) from (select * from __FPKP__ {$condition}) a join (select id as b_id from __YONGHU__ {$map}) b on a.uid = b.b_id");
			$Page=new \Think\Page($count['0']['count(*)'],20);
			$show  = $Page->show();
			$list="select d.*,c.tjren,c.tjdh from (select a.*,b.fkpren,b.fkpdh,b.fkpbm from (select * from __FPKP__ {$condition}) a join (select id as b_id,ygmingcheng as fkpren,dianhua as fkpdh,bumen as fkpbm from __YONGHU__ {$map}) b on a.uid = b.b_id order by a.addtime DESC limit ".$Page->firstRow.','.$Page->listRows.") d left join (select id as c_id,ygmingcheng as tjren,dianhua as tjdh from __YONGHU__) c on d.adduid = c.c_id ";
			$fkp=M('fpkp')->query($list);
			$this->assign('firstRow',$Page->firstRow);
			$this->assign('fkp',$fkp);
			$this->assign('count',$count);
			$this->assign('page',$show);
			$this->display();
		}
		//封开盘审核
		public function fkpshenhe(){
			$is_rbac1 = _rbac_node_show(842);
			if (!$is_rbac1) {
				$this->redirect('Index/error');
			}
			$id=I('id');
			$fpkp=M('fpkp')->where(array('id'=>$id))->field('uid,zyid,type,fkplx,zylx,czlx')->find();
			if ($fpkp['zylx'] == 1) {
				$zyb=M('fangyuan');
				$jd=856;
			} else if ($fpkp['zylx'] == 2) {
				$zyb=M('keyuan');
				$jd=857;
			}
			$zyzt=$zyb->where(array('id'=>$fpkp['zyid']))->getField('zhuangtai');
			$is_rbac2 = _rbac_btn_show($jd,$zyzt,$fpkp['uid']);
			if (!$is_rbac2) {
				$this->redirect('Index/error');
			}
			if (IS_POST) {
				$type=I('type');
				if ($fpkp['type'] != 3){      
					$this->error('已审核！');
				}
				if ($type==""){
					$this->error('请选择审核意见！');
				}
				if ($type!=1 && $type!=2){
					$this->error('审核意见异常！');
				}
				$data['type']=$type;
				$data['remark']=I('remark');
				$data['shshijian']=time();
				$data['shuid']=session('uid');
				$isok=M('fpkp')->where(array('id'=>$id))->save($data);
				if ($isok){
					if ((($fpkp['czlx'] == 1) && ($type == 2)) || (($fpkp['czlx'] == 2) && ($type == 1))) {
						$data_ziyuan['fengpanrenid']="";
						if ($fpkp['fkplx'] == 1) {
							$data_ziyuan['fpdh']=2;
						}
						if ($fpkp['zylx'] == 1) {
							if ($fpkp['fkplx'] == 2) {
								$data_ziyuan['fpdz']=2;
							}
							if ($fpkp['fkplx'] == 3) {
								$data_ziyuan['fpdj']=2;
							}
							if ($fpkp['fkplx'] == 4) {
								$data_ziyuan['fpdq']=2;
							}
						}
						if ($fpkp['fkplx'] == 5) {
							$data_ziyuan['fpck']=2;
						}
						$zyb->where(array('id'=>$fpkp['zyid']))->save($data_ziyuan);
					}
					$this->success('审核成功');
				}else{
					$this->error('审核失败');
				}
			}else{
				$this->display();
			}
		}
		//封开盘日志
		public function fkprizhi(){
			$rzlx=I('rzlx');
			if ($rzlx == 1) {
				$map .= " where gongsiid=".session('gongsiid');
				$x = _rbac_lists_where(843,0);
				if ($x && $x != "all" && $x != "me") {
					$map .= " and bumen in ({$x})";
				}elseif ($x == "me") {
					$map .= " and id=".session('uid');
				}elseif ($x == false) {
					$map .= " and id=0";
				}
				$condition = " where gongsiid=".session('gongsiid');
				$zylx=I('zylx');
				if ($zylx) {
					$condition.=" and zylx=".$zylx;
					$this->zylx=$zylx;
				} else {
					$condition.="";
					$this->zylx=0;
				}
				$czlx=I('czlx');
				if ($czlx) {
					$condition.=" and czlx=".$czlx;
					$this->czlx=$czlx;
					if ($czlx == 1) {
						$this->czxz="全部封盘";
						$this->lxxz="封盘类型";
						$this->dhxz="封电话";
						$this->dzxz="封地址";
						$this->djxz="封底价";
						$this->dqxz="封调取";
						$this->ckxz="封查看";
					} else if ($czlx == 2) {
						$this->czxz="全部开盘";
						$this->lxxz="开盘类型";
						$this->dhxz="开电话";
						$this->dzxz="开地址";
						$this->djxz="开底价";
						$this->dqxz="开调取";
						$this->ckxz="开查看";
					}
				} else {
					$condition.="";
					$this->czlx=0;
					$this->czxz="全部操作";
					$this->lxxz="操作类型";
					$this->dhxz="封/开电话";
					$this->dzxz="封/开地址";
					$this->djxz="封/开底价";
					$this->dqxz="封/开调取";
					$this->ckxz="封/开查看";
				}
				$type=I('type');
				if ($type) {
					$condition.=" and type=".$type;
					$this->type=$type;
				} else {
					$condition.="";
					$this->type=0;
				}
				$fkplx=I('fkplx');
				if ($fkplx) {
					$condition.=" and fkplx=".$fkplx;
					$this->fkplx=$fkplx;
				} else {
					$condition.="";
					$this->fkplx=0;
				}
				$shijian1=I('shijian1');
				$shijian2=I('shijian2');
				if ($shijian1) {
					$this->shijian1=$shijian1;
					$shijian3=strtotime($shijian1);
				}
				if ($shijian2) {
					$this->shijian2=$shijian2;
					$shijian4=strtotime($shijian2)+86400;
				}
				if ($shijian3 and $shijian4){
					if ($shijian3 > $shijian4){
						$condition.=" and addtime>".$shijian4." and addtime<".$shijian3;
						$this->shijian1=$shijian2;
						$this->shijian2=$shijian1;
					} else {
						$condition.=" and addtime>".$shijian3." and addtime<".$shijian4;
					}
				} elseif ($shijian3) {
					$condition.=" and addtime>".$shijian3;
				} elseif ($shijian4) {
					$condition.=" and addtime<".$shijian4;
				}
				$count=M('fpkp')->query("select count(*) from (select * from __FPKP__ {$condition}) a join (select id as b_id from __YONGHU__ {$map}) b on a.uid = b.b_id");
				$Page=new \Think\Page($count['0']['count(*)'],20);
				$show  = $Page->show();
				$list="select e.*,c.tjren,c.tjdh,d.shren,d.shdh from (select a.*,b.fkpren,b.fkpdh,b.fkpbm from (select * from __FPKP__ {$condition}) a join (select id as b_id,ygmingcheng as fkpren,dianhua as fkpdh,bumen as fkpbm from __YONGHU__ {$map}) b on a.uid = b.b_id order by a.addtime DESC limit ".$Page->firstRow.','.$Page->listRows.") e left join (select id as c_id,ygmingcheng as tjren,dianhua as tjdh from __YONGHU__) c on e.adduid = c.c_id left join (select id as d_id,ygmingcheng as shren,dianhua as shdh from __YONGHU__) d on e.shuid = d.d_id ";
				$fkp=M('fpkp')->query($list);
				$this->assign('firstRow',$Page->firstRow);
				$this->assign('fkp',$fkp);
				$this->assign('count',$count);
				$this->assign('page',$show);
			} elseif ($rzlx == 2) {
				$fkp=M('fpkp')->where(array('id'=>I('id')))->field('shuid,shshijian,remark,type')->find();
				$this->shren=M('Yonghu')->where(array('id'=>$fkp['shuid']))->getField('ygmingcheng');
				$this->shdh=M('Yonghu')->where(array('id'=>$fkp['shuid']))->getField('dianhua');
				$this->fkp=$fkp;
			}
			$this->rzlx=$rzlx;
			$this->display();
		}
	}