<?php !defined('APP_DIR') ? die('acceess not defined') : '';
class actions_broker_sell extends actions_action{
    
    public function __construct(){
        parent::__construct();
        if (!$this->loginuser){
            url::redirect('/login.php');
        }
    }
    
    /**
     * 系统拦截的经纪人发布的房源
     *
     */
    public function sysfrozen(){
        //翻页处理
        $page = intval($_GET['page']);
        $page = ($page > 0 ? $page : 1);
        
        //每页条数
        $perpage = 30;
        $offset = ($page-1)*$perpage;
        
        $query_struct = array(
            'where' => array(
                'esta' => 51,
            ),
            'orderby' => array(
                'id' => 'DESC',
            ),
            'limit' => array($offset,$perpage),
        );
        $sells = bm('sell')->get_all($query_struct);
        foreach ($sells as $k => $v){
            $broker = bm('broker')->get($v['uid'],$this->city);
            $sells[$k]['uname'] = $broker['username'];
        }
        
        //获取总数
        $total = bm('sell')->count($query_struct);
        $pagenations = pagebar($total,tool::getStr('','',url::current_url()),$perpage);
        
        //模板赋值
        $tpl_data = array(
            'sells' => $sells,
            'pagenations'    => $pagenations,
            //'where' => $where,
            //'districts' => $districts
        );
        $body_tpl = array(
            'body_tpl' => new tpl('broker/sell_sysfrozen.php',$tpl_data),
        );
        $this->tpl_data = $body_tpl;
        //print_r($sells);die();
    }
    
    /**
     * 系统拦截房源的申诉
     *
     */
    public function appeal(){
        //翻页处理
        $page = intval($_GET['page']);
        $page = ($page > 0 ? $page : 1);
        
        //每页条数
        $perpage = 30;
        $offset = ($page-1)*$perpage;
        if ( intval($_GET['deal'])==1 ){
            $result = array(10,20);
        }else {
            $result = 0;
        }
        
        $query_struct = array(
            'where' => array(
                'city' => $this->city,
                'result' => $result
            ),
            'orderby' => array(
                'id' => 'DESC',
            ),
            'limit' => array($offset,$perpage),
        );

		if(!empty($_GET['cond']) && !empty($_GET['keyword']))
		{
			if($_GET['cond'] == 'houseId')
			{
				 $query_struct['where']['id'] = intval($_GET['keyword']);
			}
			elseif($_GET['cond'] == 'username')
			{
				$brokerData = bm('broker')->get_all(array('where'=> array('username' => $_GET['keyword'])));
				
				if(!empty($brokerData))
				{
					foreach($brokerData as $key => $item)
					{
						$query_struct['where']['uid'][] = $item['uid'];
					}
				}
			}
			elseif($_GET['cond'] == 'truename')
			{
				$brokerData = bm('broker')->get_all(array('where'=> array('truename' => $_GET['keyword'])));
				
				if(!empty($brokerData))
				{
					foreach($brokerData as $key => $item)
					{
						$query_struct['where']['uid'][] = $item['uid'];
					}
				}
			}
		}
		
		$appeals = array();
		$sells = array();
		$total = 0;
		$pagenations = array();

		if(!empty($brokerData) || !empty($query_struct['where']['id']) || empty($_GET['keyword']))
		{
			$appeals = DAO::instance('sell_appeal')->get_all($query_struct,'id');
			$ids = array_keys($appeals);
			$sells = bm('sell')->get_all(array('where'=> array('id' => $ids)),'id');
			foreach ($sells as $k => $v){
				$broker = bm('broker')->get($v['uid'],$this->city);
				$sells[$k]['uname'] = $broker['username'];
			}
			
			//获取总数
			$total =  DAO::instance('sell_appeal')->count($query_struct);
			$pagenations = pagebar($total,tool::getStr('','',url::current_url()),$perpage);
		}
        
        //模板赋值
        $tpl_data = array(
            'appeals' => $appeals,
            'sells' => $sells,
            'pagenations'    => $pagenations,
			'searchArg' => $_GET
            //'where' => $where,
            //'districts' => $districts
        );
        $body_tpl = array(
            'body_tpl' => new tpl('broker/sell_appeal_wang.php',$tpl_data),
        );
        $this->tpl_data = $body_tpl;
        //print_r($sells);die();
    }
    
    /**
     * 审核通过
     *
     */
    public function appeal_pass(){
        $date_time = date('Y-m-d H:i:s');
        
        $id = intval($_GET['id']);
        
        $sql = "UPDATE sell_appeal 
                SET result = 10, 
                manager_id = {$this->loginuser['uid']},
                upd_time = '$date_time'
                WHERE city='$this->city' 
                AND id = $id";
        DAO::instance('sell_appeal')->query($sql);
        
        $id = intval($_GET['id']);
        $data = array(
            'id' => $id,
            'esta' => 1,
        );
        bm('sell')->edit($data);
        url::redirect('http://esfadmin.house365.com/365frame/index.php?ctrl=broker_sell&method=appeal');
    }
    /**
     * 审核不通过
     *
     */
    public function appeal_refuse(){
        $date_time = date('Y-m-d H:i:s');
        
        $id = intval($_GET['id']);
        
        $sql = "UPDATE sell_appeal 
                SET result = 20,
                manager_id = {$this->loginuser['uid']},
                upd_time = '$date_time'
                WHERE city='$this->city' 
                AND id = $id";
        DAO::instance('sell_appeal')->query($sql);
        //echo $sql;die();
        url::redirect('http://esfadmin.house365.com/365frame/index.php?ctrl=broker_sell&method=appeal');
    }
}