<?php

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

	namespace Admin\Controller;
	use Think\Controller;
	class PeizhiController extends CommonController {
		//配置列表
		public function Peizhi(){
			$is_rbac = _rbac_node_show(92);
			if (!$is_rbac) {
				$this->redirect('Index/error');
			}
			$condition = "zhuangtai = 1";
			$pzfl=I('pzfl');
			$sousuo=I('sousuo');
			if ($pzfl) {
				$condition.=" and pzfl=$pzfl";
			}
			if ($sousuo) {
				$condition.=" and ((pzming like '%$sousuo%') or (zhushi like '%$sousuo%'))";
			}
			$count=M('peizhi')->query("select count(*) from (select count(*) from jjrxt_peizhi where {$condition} group by pzming) a");
			$Page = new \Think\Page($count['0']['count(*)'],20);
			$show = $Page->show();
			$list="select a.*,b.pzflm from (select max(id) as id,pzming,zhushi,pzfl from __PEIZHI__ where {$condition} group by pzming) a left join (select lxid as b_id,lxming as pzflm from __PEIZHI__ where pzming = 'pzfl') b on a.pzfl=b.b_id order by a.id DESC limit ".$Page->firstRow.','.$Page->listRows;
			$Peizhi=M('peizhi')->query($list);
			$this->assign('firstRow',$Page->firstRow);
			$this->assign('peizhi',$Peizhi);
			$this->assign('count',$count);
			$this->assign('page',$show);
			$this->pzfl=M('Peizhi')->where(array('pzming'=>'pzfl','zhuangtai'=>1))->order('xsshunxu asc')->select();
			$this->display();
		}
		//添加配置
		public function addPeizhi(){
			$is_rbac = _rbac_node_show(93);
			if (!$is_rbac) {
				$this->redirect('Index/error');
			}
			if (IS_POST) {
				$peizhi=M("peizhi");
				$pzm=$peizhi->where(array('pzming' =>I('pzming'),'gongsiid'=>I('gongsiid')))->getField('pzming');
				if ($pzm) {
					$this->error('该配置已存在');
				}
				$data['pzming']=I('pzming');
				$data['zhushi']=I('zhushi');
				$data['lxid']=I('lxid');
				$data['lxming']=I('lxming');
				$data['pzfl']=I('pzfl');
				$isok=$peizhi->add($data);
				if ($isok) {
					$this->success('添加成功');
				}else{
					$this->error('添加失败');
				}
			}else{
				$this->pzfl=M('Peizhi')->where(array('pzming'=>'pzfl','zhuangtai'=>1))->order('xsshunxu asc')->select();
				$this->display();
			}
		}
		//更新配置
		public function editPeizhi(){
			$is_rbac = _rbac_node_show(95);
			if (!$is_rbac) {
				$this->redirect('Index/error');
			}
			if (IS_POST) {
				$peizhi=M("peizhi");
				$yzs=M('Peizhi')->where(array('id'=>I('id')))->getField('zhushi');
				$ypzm=M('Peizhi')->where(array('id'=>I('id')))->getField('pzming');
				$zs=$peizhi->where(array('zhushi' =>I('zhushi'),'gongsiid'=>I('gongsiid')))->getField('zhushi');
				$pzm=$peizhi->where(array('pzming' =>I('pzming'),'gongsiid'=>I('gongsiid')))->getField('pzming');
				if ($pzm && $pzm != $ypzm) {
					$this->error('该配置已存在');
				}
				if ($pzm == $ypzm && $zs != $yzs) {
					$data['zhushi']=I('zhushi');
					$data['pzfl']=I('pzfl');
					$isok=$peizhi->where(array('zhushi'=>$yzs))->save($data);
				}
				elseif ($pzm != $ypzm && $zs == $yzs) {
					$data['pzming']=I('pzming');
					$data['pzfl']=I('pzfl');
					$isok=$peizhi->where(array('pzming'=>$ypzm))->save($data);
				}
				else {
					$data['zhushi']=I('zhushi');
					$data['pzming']=I('pzming');
					$data['pzfl']=I('pzfl');
					$isok=$peizhi->where(array('pzming'=>$ypzm))->save($data);
				}
				if ($isok) {
					$this->success('更新成功');
				}else{
					$this->error('更新失败');
				}
			}else{
				$this->peizhi=M('Peizhi')->where(array('id'=>I('id')))->field('id,pzming,zhushi,pzfl')->find();
				$this->pzfl=M('Peizhi')->where(array('pzming'=>'pzfl','zhuangtai'=>1))->order('xsshunxu asc')->select();
				$this->display();
			}
		}
		//删除配置
		public function delPeizhi(){
			$is_rbac = _rbac_node_show(97);
			if (!$is_rbac) {
				$this->error('异常错误！');
			}
			$id=I('id');
			$dp=M('Peizhi');
			$dp->where(array('id'=>$id))->delete();
			if ($dp){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
		}
		//配置设置列表
		public function Pzshezhi(){
			$is_rbac = _rbac_node_show(421);
			if (!$is_rbac) {
				$this->redirect('Index/error');
			}
			$condition = "zhuangtai = 1";
			$pzfl=I('pzfl');
			$sousuo=I('sousuo');
			if ($pzfl) {
				$condition.=" and pzfl=$pzfl";
			}
			if ($sousuo) {//搜索
				$condition.=" and ((pzming like '%$sousuo%') or (zhushi like '%$sousuo%'))";
			}
			$count=M('peizhi')->query("select count(*) from (select count(*) from jjrxt_peizhi where {$condition} group by pzming) a");
			$Page=new \Think\Page($count['0']['count(*)'],15);
			$show=$Page->show();
			$list="select max(id),pzming,zhushi from jjrxt_peizhi where {$condition} group by pzming order by id DESC limit ".$Page->firstRow.','.$Page->listRows;
			$Peizhi=M('peizhi')->query($list);
			$this->assign('peizhi',$Peizhi);
			$this->assign('count',$count);
			$this->assign('page',$show);
			$this->pzfl=M('Peizhi')->where(array('pzming'=>'pzfl','zhuangtai'=>1))->order('xsshunxu asc')->select();
			$this->display();
		}
		//添加配置类型
		public function addPzshezhi(){
			$is_rbac = _rbac_node_show(423);
			if (!$is_rbac) {
				$this->redirect('Index/error');
			}
			if (IS_POST) {
				$dqpz=M('peizhi')->where(array('pzming' =>I('pzming')))->order('lxid desc')->field('lxid,pzfl')->find();
				$data['pzming']=I('pzming');
				$data['zhushi']=I('zhushi');
				$data['lxid']=$dqpz['lxid']+1;
				$data['lxming']=I('lxming');
				$data['xsshunxu']=I('xsshunxu');
				$data['pzfl']=$dqpz['pzfl'];
				if (M('peizhi')->add($data)) {
					$this->success('添加成功');
				}else{
					$this->error('添加失败');
				}
			} else {
				$this->peizhi=M('peizhi')->where(array('id'=>I('id')))->find();
				$this->display();
			}
		}
		//更新配置类型
		public function editPzshezhi(){
			$is_rbac = _rbac_node_show(425);
			if (!$is_rbac) {
				$this->redirect('Index/error');
			}
			if (IS_POST) {
				$Peizhi=M("peizhi");
				$data['lxming']=I('lxming');
				$data['xsshunxu']=I('xsshunxu');
				$Peizhi->where(array('id'=>I('id')))->save($data);
				if ($Peizhi){
					$this->success('更新成功');
				}else{
					$this->error('更新失败');
				}
			} else {
				$this->peizhi=M('peizhi')->where(array('id'=>I('id')))->find();
				$this->display();
			}
		}
		//删除配置类型
		public function delPzshezhi(){
			$is_rbac = _rbac_node_show(427);
			if (!$is_rbac) {
				$this->error('异常错误！');
			}
			$data['zhuangtai']=0;
			if (M('peizhi')->where(array('id'=>I('id')))->save($data)) {
				$this->success('修改成功');
			}else{
				$this->error('修改失败');
			}
		}
	}