<?php

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

	namespace Admin\Controller;
	use Think\Controller;
	class FkyfkpController extends CommonController {

		//封开盘审核列表
		public function fkpsh(){
			if (I('is_ajax')) {
				$condition = " where b.gongsiid=".session('gongsiid');
				$x = _rbac_lists_where(841,0);
				if ($x && $x != "all" && $x != "me") {
					$condition .= " and b.bumen in ({$x})";
				} elseif ($x == "me") {
					$condition .= " and b.id=".session('uid');
				} elseif ($x == false) {
					$condition .= " and b.id=0";
				}
				$type = I('type');
				$czlx = I('czlx');
				$zylx = I('zylx');
				$fkplx = I('fkplx');
				$zylxs = "1,2,13,14,15,16,17,18,19,20,21,22";
				$zylxs1 = explode(",",$zylxs);
				if (in_array($zylx,$zylxs1)) {
					$condition .= " and a.zylx = ".$zylx;
				} else {
					$condition .= " and a.zylx in (".$zylxs.")";
				}
				if ($czlx) {
					$condition .= " and a.czlx=".$czlx;
				}
				if ($type) {
					$condition .= " and a.type=".$type;
				}
				if ($fkplx) {
					$condition .= " and a.fkplx=".$fkplx;
				}
				$shijian1 = I('shijian1');
				$shijian2 = I('shijian2');
				if ($shijian1) {
					$shijian3 = strtotime($shijian1);
				}
				if ($shijian2) {
					$shijian4 = strtotime($shijian2)+86400;
				}
				if ($shijian3 and $shijian4) {
					if ($shijian3 > $shijian4) {
						$shijian3 = strtotime($shijian2);
						$shijian4 = strtotime($shijian1)+86400;
					}
					$condition .= " and a.addtime>".$shijian3." and a.addtime<".$shijian4;
				} elseif ($shijian3) {
					$condition .= " and a.addtime>".$shijian3;
				} elseif ($shijian4) {
					$condition .= " and a.addtime<".$shijian4;
				}
				$field = I('field'); //排序字段名
				$order = I('order'); //排序方式
				$limit = I('limit',30,'int'); //每页条数
				$limit = $limit > 150 ? 150 : $limit;
				if ($field && $order) {
					$gjpx = "order by a.".$field." ".$order;
				} else {
					$gjpx = "order by a.id desc";
				}
				$Model = new \Think\Model();
				$count = $Model->query("select count(*) from __FPKP__ a join __YONGHU__ b on a.uid = b.id {$condition}");
				$Page = new \Think\Page($count['0']['count(*)'],$limit);
				$list = "select a.*,b.ygmingcheng as fkpren,b.dianhua as fkpdh,b.bumen as fkpbm from __FPKP__ a join __YONGHU__ b on a.uid = b.id {$condition} ".$fppx." limit ".$Page->firstRow.','.$Page->listRows;
				$data = $Model->query($list);
				$fylxs = "1,13,14";
				$fylxs1 = explode(",",$fylxs);
				$kylxs = "2,15,16";
				$kylxs1 = explode(",",$kylxs);
				foreach ($data as $key => $value) {
					if (in_array($value['zylx'],$fylxs1)) {
						switch ($value['zylx']) {
							case '1':
								$zyb = M('fangyuan');
								$zylxm = "住宅";
								$zyxqlj = U('fangyuan/neirong',array('id'=>$value['zyid']));
								break;
							case '13':
								$zyb = M('fyxiezilou');
								$zylxm = "写字楼";
								$zyxqlj = U('fyxiezilou/neirong',array('id'=>$value['zyid']));
								break;
							case '14':
								$zyb = M('fyshangpu');
								$zylxm = "商铺";
								$zyxqlj = U('fyshangpu/neirong',array('id'=>$value['zyid']));
								break;
						}
						if ($zyb) {
							$zyxq = $zyb->where(array('id'=>$value['zyid']))->field('mmzl,zhuangtai,weihurenid')->find();
						}
						switch ($zyxq['mmzl']) {
							case '1':
								$mmzl = "（出售）";
								break;
							case '2':
								$mmzl = "（出租）";
								break;
							default:
								$mmzl = "";
								break;
						}
						$node = 46;
						$nodesh = 856;
						$noderz = 843;
					} elseif (in_array($value['zylx'],$kylxs1)) {
						switch ($value['zylx']) {
							case '2':
								$zyb = M('keyuan');
								$zylxm = "住宅客源";
								$zyxqlj = U('keyuan1/neirong1',array('id'=>$value['zyid']));
								break;
							case '15':
								$zyb = M('kyxiezilou');
								$zylxm = "写字楼客源";
								$zyxqlj = U('kyxiezilou/neirong',array('id'=>$value['zyid']));
								break;
							case '16':
								$zyb = M('kyshangpu');
								$zylxm = "商铺客源";
								$zyxqlj = U('kyshangpu/neirong',array('id'=>$value['zyid']));
								break;
						}
						if ($zyb) {
							$zyxq = $zyb->where(array('id'=>$value['zyid']))->field('mmzl,zhuangtai,weihurenid')->find();
						}
						switch ($zyxq['mmzl']) {
							case '1':
								$mmzl = "（求购）";
								break;
							case '2':
								$mmzl = "（求租）";
								break;
							default:
								$mmzl = "";
								break;
						}
						$nodexq = 75;
						$nodesh = 857;
						$noderz = 843;
					} else {
						switch ($value['zylx']) {
							case '17':
								$zyb = M('kyxinfang');
								$zylxm = "新房客源";
								$zyxqlj = U('kyxinfang/neirong1',array('id'=>$value['zyid']));
								break;
							case '18':
								$zyb = M('kyjinrong');
								$zylxm = "金融客源";
								$zyxqlj = U('kyjinrong/neirong',array('id'=>$value['zyid']));
								break;
							case '19':
								$zyb = M('kyzhuangxiu');
								$zylxm = "装修客源";
								$zyxqlj = U('kyzhuangxiu/neirong',array('id'=>$value['zyid']));
								break;
							case '20':
								$zyb = M('kygongshang');
								$zylxm = "工商客源";
								$zyxqlj = U('kygongshang/neirong1',array('id'=>$value['zyid']));
								break;
							case '21':
								$zyb = M('kykuaiji');
								$zylxm = "会计客源";
								$zyxqlj = U('kykuaiji/neirong',array('id'=>$value['zyid']));
								break;
							case '22':
								$zyb = M('kyjiaju');
								$zylxm = "家具客源";
								$zyxqlj = U('kyjiaju/neirong',array('id'=>$value['zyid']));
								break;
							default:
								$zyb = "";
								$zylxm = "";
								$zyxqlj = "";
								break;
						}
						$mmzl = "";
						if ($zyb) {
							$zyxq = $zyb->where(array('id'=>$value['zyid']))->field('zhuangtai,weihurenid')->find();
						}
						$nodexq = 75;
						$nodesh = 857;
						$noderz = 843;
					}
					$zylxm = $zylxm.$mmzl;
					if ($zyxq) {
						$is_rbacxq = _rbac_btn_show($nodexq,$zyxq['zhuangtai'],$zyxq['weihurenid']);
					} else {
						$is_rbacxq = "";
					}
					switch ($value['type']) {
						case '1':
							$type = "已通过";
							break;
						case '2':
							$type = "已驳回";
							break;
						case '3':
							$type = "审核中";
							break;
						default:
							$type = "";
							break;
					}
					switch ($value['czlx']) {
						case '1':
							$isfk = "封";
							$data[$key]['czlxm'] = "封盘";
							break;
						case '2':
							$isfk = "开";
							$data[$key]['czlxm'] = "开盘";
							break;
					}
					switch ($value['fkplx']) {
						case '1':
							$fkplx = $isfk."电话";
							break;
						case '2':
							$fkplx = $isfk."地址";
							break;
						case '3':
							$fkplx = $isfk."底价";
							break;
						case '4':
							$fkplx = $isfk."调取";
							break;
						case '5':
							$fkplx = $isfk."查看";
							break;
					}
					if ($value['type'] == 3) {
						$is_rbacsh = _rbac_btn_show($nodesh,$zyxq['zhuangtai'],$value['uid']);
						$data[$key]['rbacsh'] = $is_rbacsh ? 1 : 0;
					} else {
						$is_rbacrz = _rbac_btn_show($noderz,0,$value['uid']);
						$data[$key]['rbacrz'] = $is_rbacrz ? 1 : 0;
					}
					$data[$key]['rbacxq'] = $is_rbacxq ? 1 : 0;
					$data[$key]['zyxqlj'] = $zyxqlj;
					$data[$key]['zylxm'] = $zylxm;
					$data[$key]['fkplx'] = $fkplx;
					$data[$key]['type'] = $type;
					$data[$key]['fkprxx'] = $value['fkpren']."/".zym($value['fkpbm'],6)."（".$value['fkpdh']."）";
					$data[$key]['sqren'] = zym($value['adduid'],5)."（".zym($value['adduid'],14)."）";
					$data[$key]['endunix'] = $value['endunix'] ? date('Y-m-d',$value['endunix']) : "";
					$data[$key]['addtime'] = $value['addtime'] ? date('Y-m-d H:i:s',$value['addtime']) : "";
				}
				$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();
			$fylxs = "1,13,14";
			$fylxs1 = explode(",",$fylxs);
			$kylxs = "2,15,16,17,18,19,20,21,22";
			$kylxs1 = explode(",",$kylxs);
			if (in_array($fpkp['zylx'],$fylxs1)) {
				$node = 856;
				$cxzd = "bianhao";
			}
			if (in_array($fpkp['zylx'],$fylxs1)) {
				$node = 857;
				$cxzd = "xqbianhao";
			}
			switch ($fpkp['czlx']) {
				case '1':
					$isfk1 = "封";
					$isfk2 = "fp";
					break;
				case '2':
					$isfk1 = "开";
					$isfk2 = "kp";
					break;
			}
			switch ($fpkp['zylx']) {
				case '1':
					$zyb = M('fangyuan');
					$zyxqlj = U('fangyuan/neirong',array('id'=>$fpkp['zyid']));
					$zyzs = "住宅";
					break;
				case '2':
					$zyb = M('keyuan');
					$zyxqlj = U('keyuan1/neirong1',array('id'=>$fpkp['zyid']));
					$zyzs = "住宅客源";
					break;
				case '13':
					$zyb = M('fyxiezilou');
					$zyxqlj = U('fyxiezilou/neirong',array('id'=>$fpkp['zyid']));
					$zyzs = "写字楼";
					break;
				case '14':
					$zyb = M('fyshangpu');
					$zyxqlj = U('fyshangpu/neirong',array('id'=>$fpkp['zyid']));
					$zyzs = "商铺";
					break;
				case '15':
					$zyb = M('kyxiezilou');
					$zyxqlj = U('kyxiezilou/neirong',array('id'=>$fpkp['zyid']));
					$zyzs = "写字楼客源";
					break;
				case '16':
					$zyb = M('kyshangpu');
					$zyxqlj = U('kyshangpu/neirong',array('id'=>$fpkp['zyid']));
					$zyzs = "商铺客源";
					break;
				case '17':
					$zyb = M('kyxinfang');
					$zyxqlj = U('kyxinfang/neirong',array('id'=>$fpkp['zyid']));
					$zyzs = "新房客源";
					break;
				case '18':
					$zyb = M('kyjinrong');
					$zyxqlj = U('kyjinrong/neirong',array('id'=>$fpkp['zyid']));
					$zyzs = "金融客源";
					break;
				case '19':
					$zyb = M('kyzhuangxiu');
					$zyxqlj = U('kyzhuangxiu/neirong',array('id'=>$fpkp['zyid']));
					$zyzs = "装修客源";
					break;
				case '20':
					$zyb = M('kygongshang');
					$zyxqlj = U('kygongshang/neirong',array('id'=>$fpkp['zyid']));
					$zyzs = "工商客源";
					break;
				case '21':
					$zyb = M('kykuaiji');
					$zyxqlj = U('kykuaiji/neirong',array('id'=>$fpkp['zyid']));
					$zyzs = "会计客源";
					break;
				case '22':
					$zyb = M('kyjiaju');
					$zyxqlj = U('kyjiaju/neirong',array('id'=>$fpkp['zyid']));
					$zyzs = "家具客源";
					break;
			}
			$zyzt = $zyb->where(array('id'=>$fpkp['zyid']))->getField('zhuangtai');
			$is_rbac2 = _rbac_btn_show($node,$zyzt,$fpkp['uid']);
			if (!$is_rbac2) {
				$this->redirect('Index/error');
			}
			if (IS_POST) {
				$this->error('请联系客服（19965783436）获取正式版功能！');
			}else{
				$this->display();
			}
		}

		//封开盘日志
		public function fkprizhi(){
			$rzlx = I('rzlx');
			if ($rzlx == 1) {
				if (I('is_ajax')) {
					$condition = " where b.gongsiid=".session('gongsiid');
					$x = _rbac_lists_where(843,0);
					if ($x && $x != "all" && $x != "me") {
						$condition .= " and b.bumen in ({$x})";
					} elseif ($x == "me") {
						$condition .= " and b.id=".session('uid');
					} elseif ($x == false) {
						$condition .= " and b.id=0";
					}
					$type = I('type');
					$czlx = I('czlx');
					$zylx = I('zylx');
					$fkplx = I('fkplx');
					$zylxs = "1,2,13,14,15,16,17,18,19,20,21,22";
					$zylxs1 = explode(",",$zylxs);
					if (in_array($zylx,$zylxs1)) {
						$condition .= " and a.zylx = ".$zylx;
					} else {
						$condition .= " and a.zylx in (".$zylxs.")";
					}
					if ($czlx) {
						$condition .= " and a.czlx=".$czlx;
					}
					if ($type) {
						$condition .= " and a.type=".$type;
					}
					if ($fkplx) {
						$condition .= " and a.fkplx=".$fkplx;
					}
					$shijian1 = I('shijian1');
					$shijian2 = I('shijian2');
					if ($shijian1) {
						$shijian3 = strtotime($shijian1);
					}
					if ($shijian2) {
						$shijian4 = strtotime($shijian2)+86400;
					}
					if ($shijian3 and $shijian4) {
						if ($shijian3 > $shijian4) {
							$shijian3 = strtotime($shijian2);
							$shijian4 = strtotime($shijian1)+86400;
						}
						$condition .= " and a.addtime>".$shijian3." and a.addtime<".$shijian4;
					} elseif ($shijian3) {
						$condition .= " and a.addtime>".$shijian3;
					} elseif ($shijian4) {
						$condition .= " and a.addtime<".$shijian4;
					}
					$field = I('field'); //排序字段名
					$order = I('order'); //排序方式
					$limit = I('limit',30,'int'); //每页条数
					$limit = $limit > 150 ? 150 : $limit;
					if ($field && $order) {
						$gjpx = "order by a.".$field." ".$order;
					} else {
						$gjpx = "order by a.id desc";
					}
					$Model = new \Think\Model();
					$count = $Model->query("select count(*) from __FPKP__ a join __YONGHU__ b on a.uid = b.id {$condition}");
					$Page = new \Think\Page($count['0']['count(*)'],$limit);
					$list = "select a.*,b.ygmingcheng as fkpren,b.dianhua as fkpdh,b.bumen as fkpbm from __FPKP__ a join __YONGHU__ b on a.uid = b.id {$condition} ".$fppx." limit ".$Page->firstRow.','.$Page->listRows;
					$data = $Model->query($list);
					$fylxs = "1,13,14";
					$fylxs1 = explode(",",$fylxs);
					$kylxs = "2,15,16";
					$kylxs1 = explode(",",$kylxs);
					foreach ($data as $key => $value) {
						if (in_array($value['zylx'],$fylxs1)) {
							switch ($value['zylx']) {
								case '1':
									$zyb = M('fangyuan');
									$zylxm = "住宅";
									$zyxqlj = U('fangyuan/neirong',array('id'=>$value['zyid']));
									break;
								case '13':
									$zyb = M('fyxiezilou');
									$zylxm = "写字楼";
									$zyxqlj = U('fyxiezilou/neirong',array('id'=>$value['zyid']));
									break;
								case '14':
									$zyb = M('fyshangpu');
									$zylxm = "商铺";
									$zyxqlj = U('fyshangpu/neirong',array('id'=>$value['zyid']));
									break;
							}
							if ($zyb) {
								$zyxq = $zyb->where(array('id'=>$value['zyid']))->field('mmzl,zhuangtai,weihurenid')->find();
							}
							switch ($zyxq['mmzl']) {
								case '1':
									$mmzl = "（出售）";
									break;
								case '2':
									$mmzl = "（出租）";
									break;
								default:
									$mmzl = "";
									break;
							}
							$node = 46;
						} elseif (in_array($value['zylx'],$kylxs1)) {
							switch ($value['zylx']) {
								case '2':
									$zyb = M('keyuan');
									$zylxm = "住宅客源";
									$zyxqlj = U('keyuan1/neirong1',array('id'=>$value['zyid']));
									break;
								case '15':
									$zyb = M('kyxiezilou');
									$zylxm = "写字楼客源";
									$zyxqlj = U('kyxiezilou/neirong',array('id'=>$value['zyid']));
									break;
								case '16':
									$zyb = M('kyshangpu');
									$zylxm = "商铺客源";
									$zyxqlj = U('kyshangpu/neirong',array('id'=>$value['zyid']));
									break;
							}
							if ($zyb) {
								$zyxq = $zyb->where(array('id'=>$value['zyid']))->field('mmzl,zhuangtai,weihurenid')->find();
							}
							switch ($zyxq['mmzl']) {
								case '1':
									$mmzl = "（求购）";
									break;
								case '2':
									$mmzl = "（求租）";
									break;
								default:
									$mmzl = "";
									break;
							}
							$nodexq = 75;
						} else {
							switch ($value['zylx']) {
								case '17':
									$zyb = M('kyxinfang');
									$zylxm = "新房客源";
									$zyxqlj = U('kyxinfang/neirong1',array('id'=>$value['zyid']));
									break;
								case '18':
									$zyb = M('kyjinrong');
									$zylxm = "金融客源";
									$zyxqlj = U('kyjinrong/neirong',array('id'=>$value['zyid']));
									break;
								case '19':
									$zyb = M('kyzhuangxiu');
									$zylxm = "装修客源";
									$zyxqlj = U('kyzhuangxiu/neirong',array('id'=>$value['zyid']));
									break;
								case '20':
									$zyb = M('kygongshang');
									$zylxm = "工商客源";
									$zyxqlj = U('kygongshang/neirong1',array('id'=>$value['zyid']));
									break;
								case '21':
									$zyb = M('kykuaiji');
									$zylxm = "会计客源";
									$zyxqlj = U('kykuaiji/neirong',array('id'=>$value['zyid']));
									break;
								case '22':
									$zyb = M('kyjiaju');
									$zylxm = "家具客源";
									$zyxqlj = U('kyjiaju/neirong',array('id'=>$value['zyid']));
									break;
								default:
									$zyb = "";
									$zylxm = "";
									$zyxqlj = "";
									break;
							}
							$mmzl = "";
							if ($zyb) {
								$zyxq = $zyb->where(array('id'=>$value['zyid']))->field('zhuangtai,weihurenid')->find();
							}
							$nodexq = 75;
						}
						$zylxm = $zylxm.$mmzl;
						if ($zyxq) {
							$is_rbacxq = _rbac_btn_show($nodexq,$zyxq['zhuangtai'],$zyxq['weihurenid']);
						} else {
							$is_rbacxq = "";
						}
						switch ($value['type']) {
							case '1':
								$type = "已通过";
								break;
							case '2':
								$type = "已驳回";
								break;
							case '3':
								$type = "审核中";
								break;
							default:
								$type = "";
								break;
						}
						switch ($value['czlx']) {
							case '1':
								$isfk = "封";
								$data[$key]['czlxm'] = "封盘";
								break;
							case '2':
								$isfk = "开";
								$data[$key]['czlxm'] = "开盘";
								break;
						}
						switch ($value['fkplx']) {
							case '1':
								$fkplx = $isfk."电话";
								break;
							case '2':
								$fkplx = $isfk."地址";
								break;
							case '3':
								$fkplx = $isfk."底价";
								break;
							case '4':
								$fkplx = $isfk."调取";
								break;
							case '5':
								$fkplx = $isfk."查看";
								break;
						}
						$data[$key]['rbacxq'] = $is_rbacxq ? 1 : 0;
						$data[$key]['zyxqlj'] = $zyxqlj;
						$data[$key]['zylxm'] = $zylxm;
						$data[$key]['fkplx'] = $fkplx;
						$data[$key]['type'] = $type;
						$data[$key]['fkprxx'] = $value['fkpren']."/".zym($value['fkpbm'],6)."（".$value['fkpdh']."）";
						$data[$key]['sqren'] = zym($value['adduid'],5)."（".zym($value['adduid'],14)."）";
						$data[$key]['shren'] = $value['shuid'] ? zym($value['shuid'],5)."（".zym($value['shuid'],14)."）" : "——";
						$data[$key]['endunix'] = $value['endunix'] ? date('Y-m-d',$value['endunix']) : "";
						$data[$key]['addtime'] = $value['addtime'] ? date('Y-m-d H:i:s',$value['addtime']) : "";
						$data[$key]['shshijian'] = $value['shshijian'] ? date('Y-m-d H:i:s',$value['shshijian']) : "——";
						$data[$key]['remark'] = $value['remark'] ? $value['remark'] : "——";
					}
					$return_data = array(
						'code'=>0,
						'count'=>$count['0']['count(*)'],
						'data'=>$data
					);
					$this->ajaxReturn($return_data);
				} else {
					$this->display();
				}
			} elseif ($rzlx == 2) {
				$fkp = M('fpkp')->where(array('id'=>I('id')))->field('uid,shuid,shshijian,remark,type')->find();
				$is_rbac = _rbac_btn_show(843,0,$fkp['uid']);
				if (!$is_rbac) {
					$this->redirect('Index/error');
				}
				$this->fkp=$fkp;
			}
			$this->rzlx=$rzlx;
			$this->display();
		}

	}