<?php

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

	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']=zym($value['bumen'],5);
					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(){
			if (I('is_ajax')) {
				/*权限控制开始*/
				$condition = "e.gongsiid=".session('gongsiid');
				$x = _rbac_lists_where(841,0);
				if ($x && $x != "all" && $x != "me") {
					$condition .= " and e.bumen in ({$x})";
				} elseif ($x == "me") {
					$condition .= " and e.id=".session('uid');
				} elseif ($x == false) {
					$condition .= " and e.id=0";
				}
				/*权限控制结束*/
				$zylx=I('zylx');
				$czlx=I('czlx');
				$type=I('type');
				$fkplx=I('fkplx');
				$sousuo = I('sousuo');
				$shijian = I('shijian');
				if ($zylx) {
					$condition.=" and a.zylx=".$zylx;
				}
				if ($czlx) {
					$condition.=" and a.czlx=".$czlx;
				}
				if ($type) {
					$condition.=" and a.type=".$type;
				}
				if ($fkplx) {
					$condition.=" and a.fkplx=".$fkplx;
				}
				if ($sousuo) {
					$condition .= " and (a.zybh like '%$sousuo%' or e.ygmingcheng like '%$sousuo%' or e.pinyinjs like '%$sousuo%' or e.dianhua like '%$sousuo%')";
				}
				if ($shijian) {
					$time_s = explode(' - ',$shijian);
					$condition .= " and a.addtime>=".strtotime($time_s[0])." and a.addtime<".strtotime($time_s[0])+86400;
				}
				$field = I('field'); //排序字段名
				$order = I('order'); //排序方式
				$limit = I('limit',30,'int'); //每页条数
				$limit = $limit > 150 ? 150 : $limit;
				if ($field && $order) {
					$dlpx = "order by a.".$field." ".$order;
				} else {
					$dlpx = "order by a.id desc";
				}
				$Model = new \Think\Model();
				$count = $Model->query("select count(*) from __FPKP__ a join __YONGHU__ e on a.uid=e.id where {$condition}");
				$Page = new \Think\Page($count['0']['count(*)'],$limit);
				$list = "select a.*,e.ygmingcheng,e.bumen,e.dianhua from __FPKP__ a join __YONGHU__ e on a.uid=e.id where {$condition} ".$dlpx." limit ".$Page->firstRow.','.$Page->listRows;
				$data = $Model->query($list);
				foreach ($data as $key => $value) {
					$data[$key]['czrxx'] = $value['ygmingcheng']."/".zym($value['bumen'],6)."（".$value['dianhua']."）";
					$data[$key]['tjrxx'] = zym($value['adduid'],5)."（".zym($value['adduid'],14)."）";
					$data[$key]['addtime'] = $value['addtime'] ? date('Y-m-d H:i:s',$value['addtime']) : "——";
					$data[$key]['endunix'] = $value['endunix'] ? date('Y-m-d',$value['endunix']) : "——";
					switch ($value['type']) {
						case '1':
							$shzt = "已通过";
							break;
						case '2':
							$shzt = "已驳回";
							break;
						case '3':
							$shzt = "审核中";
							break;
						default:
							break;
					}
					switch ($value['czlx']) {
						case '1':
							$caozuo = "封盘";
							switch ($value['fkplx']) {
								case '1':
									$fkpcz = "封电话";
									break;
								case '2':
									if ($value['zylx'] == 2) {$fkpcz = "封地址";}
									break;
								case '3':
									if ($value['zylx'] == 2) {$fkpcz = "封底价";}
									break;
								case '4':
									if ($value['zylx'] == 2) {$fkpcz = "封调取";}
									break;
								case '5':
									$fkpcz = "封查看";
									break;
								default:
									break;
							}
							break;
						case '2':
							$caozuo = "开盘";
							switch ($value['fkplx']) {
								case '1':
									$fkpcz = "开电话";
									break;
								case '2':
									if ($value['zylx'] == 2) {$fkpcz = "开地址";}
									break;
								case '3':
									if ($value['zylx'] == 2) {$fkpcz = "开底价";}
									break;
								case '4':
									if ($value['zylx'] == 2) {$fkpcz = "开调取";}
									break;
								case '5':
									$fkpcz = "开查看";
									break;
								default:
									break;
							}
							break;
						default:
							break;
					}
					switch ($value['zylx']) {
						case '1':
							$ziyuan = "房源";
							break;
						case '2':
							$ziyuan = "客源";
							break;
						default:
							break;
					}
					$data[$key]['shzt'] = $shzt ? $shzt : "——";
					$data[$key]['fkpcz'] = $fkpcz ? $fkpcz : "——";
					$data[$key]['caozuo'] = $caozuo ? $caozuo : "——";
					$data[$key]['ziyuan'] = $ziyuan ? $ziyuan : "——";
					if ($value['type'] == 3) {
						if ($value['zylx'] == 1) {
							$zyb=M('fangyuan');
							$jd=856;
						} else if ($value['zylx'] == 2) {
							$zyb=M('keyuan');
							$jd=857;
						}
						$zyzt=$zyb->where(array('id'=>$value['zyid']))->getField('zhuangtai');
						$is_rbacsh = _rbac_btn_show($jd,$zyzt,$value['uid']);
						$data[$key]['rbacsh'] = $is_rbacsh ? 1 : 0;
					} else {
						$is_rbacrz = _rbac_btn_show(843,0,$value['uid']);
						$data[$key]['rbacrz'] = $is_rbacrz ? 1 : 0;
					}
				}
				$return_data = array(
					'code'=>0,
					'count'=>$count['0']['count(*)'],
					'data'=>$data
				);
				$this->ajaxReturn($return_data);
			} else {
				$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 && $rzlx != 2) {
				$rzlx == 1;
			}
			if ($rzlx == 1) {
				if (I('is_ajax')) {
					/*权限控制开始*/
					$condition = "e.gongsiid=".session('gongsiid');
					$x = _rbac_lists_where(843,0);
					if ($x && $x != "all" && $x != "me") {
						$condition .= " and e.bumen in ({$x})";
					} elseif ($x == "me") {
						$condition .= " and e.id=".session('uid');
					} elseif ($x == false) {
						$condition .= " and e.id=0";
					}
					/*权限控制结束*/
					$zylx=I('zylx');
					$czlx=I('czlx');
					$type=I('type');
					$fkplx=I('fkplx');
					$sousuo = I('sousuo');
					$shijian = I('shijian');
					if ($zylx) {
						$condition.=" and a.zylx=".$zylx;
					}
					if ($czlx) {
						$condition.=" and a.czlx=".$czlx;
					}
					if ($type) {
						$condition.=" and a.type=".$type;
					}
					if ($fkplx) {
						$condition.=" and a.fkplx=".$fkplx;
					}
					if ($sousuo) {
						$condition .= " and (a.zybh like '%$sousuo%' or e.ygmingcheng like '%$sousuo%' or e.pinyinjs like '%$sousuo%' or e.dianhua like '%$sousuo%')";
					}
					if ($shijian) {
						$time_s = explode(' - ',$shijian);
						$condition .= " and a.addtime>=".strtotime($time_s[0])." and a.addtime<".strtotime($time_s[0])+86400;
					}
					$field = I('field'); //排序字段名
					$order = I('order'); //排序方式
					$limit = I('limit',30,'int'); //每页条数
					$limit = $limit > 150 ? 150 : $limit;
					if ($field && $order) {
						$dlpx = "order by a.".$field." ".$order;
					} else {
						$dlpx = "order by a.id desc";
					}
					$Model = new \Think\Model();
					$count = $Model->query("select count(*) from __FPKP__ a join __YONGHU__ e on a.uid=e.id where {$condition}");
					$Page = new \Think\Page($count['0']['count(*)'],$limit);
					$list = "select a.*,e.ygmingcheng,e.bumen,e.dianhua from __FPKP__ a join __YONGHU__ e on a.uid=e.id where {$condition} ".$dlpx." limit ".$Page->firstRow.','.$Page->listRows;
					$data = $Model->query($list);
					foreach ($data as $key => $value) {
						$data[$key]['czrxx'] = $value['ygmingcheng']."/".zym($value['bumen'],6)."（".$value['dianhua']."）";
						$data[$key]['tjrxx'] = zym($value['adduid'],5)."（".zym($value['adduid'],14)."）";
						$data[$key]['shrxx'] = $value['shuid'] ? zym($value['shuid'],5)."（".zym($value['shuid'],14)."）" : "——";
						$data[$key]['addtime'] = $value['addtime'] ? date('Y-m-d H:i:s',$value['addtime']) : "——";
						$data[$key]['endunix'] = $value['endunix'] ? date('Y-m-d',$value['endunix']) : "——";
						$data[$key]['shshijian'] = $value['shshijian'] ? date('Y-m-d',$value['shshijian']) : "——";
						switch ($value['type']) {
							case '1':
								$shzt = "已通过";
								break;
							case '2':
								$shzt = "已驳回";
								break;
							case '3':
								$shzt = "审核中";
								break;
							default:
								break;
						}
						switch ($value['czlx']) {
							case '1':
								$caozuo = "封盘";
								switch ($value['fkplx']) {
									case '1':
										$fkpcz = "封电话";
										break;
									case '2':
										if ($value['zylx'] == 2) {$fkpcz = "封地址";}
										break;
									case '3':
										if ($value['zylx'] == 2) {$fkpcz = "封底价";}
										break;
									case '4':
										if ($value['zylx'] == 2) {$fkpcz = "封调取";}
										break;
									case '5':
										$fkpcz = "封查看";
										break;
									default:
										break;
								}
								break;
							case '2':
								$caozuo = "开盘";
								switch ($value['fkplx']) {
									case '1':
										$fkpcz = "开电话";
										break;
									case '2':
										if ($value['zylx'] == 2) {$fkpcz = "开地址";}
										break;
									case '3':
										if ($value['zylx'] == 2) {$fkpcz = "开底价";}
										break;
									case '4':
										if ($value['zylx'] == 2) {$fkpcz = "开调取";}
										break;
									case '5':
										$fkpcz = "开查看";
										break;
									default:
										break;
								}
								break;
							default:
								break;
						}
						switch ($value['zylx']) {
							case '1':
								$ziyuan = "房源";
								break;
							case '2':
								$ziyuan = "客源";
								break;
							default:
								break;
						}
						$data[$key]['shzt'] = $shzt ? $shzt : "——";
						$data[$key]['fkpcz'] = $fkpcz ? $fkpcz : "——";
						$data[$key]['caozuo'] = $caozuo ? $caozuo : "——";
						$data[$key]['ziyuan'] = $ziyuan ? $ziyuan : "——";
						if ($value['type'] == 3) {
							if ($value['zylx'] == 1) {
								$zyb=M('fangyuan');
								$jd=856;
							} else if ($value['zylx'] == 2) {
								$zyb=M('keyuan');
								$jd=857;
							}
							$zyzt=$zyb->where(array('id'=>$value['zyid']))->getField('zhuangtai');
							$is_rbacsh = _rbac_btn_show($jd,$zyzt,$value['uid']);
							$data[$key]['rbacsh'] = $is_rbacsh ? 1 : 0;
						} else {
							$is_rbacrz = _rbac_btn_show(843,0,$value['uid']);
							$data[$key]['rbacrz'] = $is_rbacrz ? 1 : 0;
						}
					}
					$return_data = array(
						'code'=>0,
						'count'=>$count['0']['count(*)'],
						'data'=>$data
					);
					$this->ajaxReturn($return_data);
				} else {
					$this->rzlx = 1;
					$this->display();
				}
			} 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 = 2;
				$this->display();
			}
		}
	}