<?php


$conf_basedir	= "../../";	//相对主目录的位置
$conf_where		= "admin";	//选择调用相应模块
$directory		= basename(dirname(__FILE__));	//定义当前所在的目录，引入模块公用函数
include_once($conf_basedir."base.inc.php");

 $complainRecord = new ComplainRecord();
if($_POST['action'] =='save') {
    if($_POST['data']) {
        $data = $_POST['data'];        
      
       $id = intval($data['id']);
       $broker = intval($data['broker']);
       $conf_where = "id = ".$id;      
       $fieldData['check_time'] = date('Y-m-d H:i:s'); 
       $fieldData['status'] = intval($data['status']);
       $returnNum = $complainRecord->update($fieldData, $conf_where);
       if($returnNum>0){ 
            //冻结被举报房源
            $freezehouse= getHouseIdById($id);
            freezeHouse($freezehouse['tbl'],$freezehouse['houseid']);
            //被举报人加入黑名单
            $complainbidblacklist = new ComplainBidBlackList();
            $fieldData2['broker_id'] = $broker;
            $fieldData2['add_time'] = date('Y-m-d H:i:s');
            $fieldData2['end_time'] = date('Y-m-d H:i:s',strtotime('+2 month'));
            $fieldData2['operater'] = $_SESSION['esfadmin']['username'];
            $complainbidblacklist->insert($fieldData2);
            //奖励举报成功经纪人           
            $brokerGroup = new BrokerGroup();
            $brokerInfo = $brokerGroup->getBrokerGroupInfo($broker);  
            $arrBroker = array(
                'id' => $broker,
                'groupid' => $brokerInfo['groupid'],
                'username'=> $brokerInfo['username']
            ); 
            $payBrokerHousebean = new PaymentBrokerHousebean($arrBroker); 
            $payBrokerHousebean->increase('complainBid','举报虚假竞投房源赠送');
            //举报人举报失败次数
            $complainFailure = getBrokerComplainFailure($broker);
            $logMessage = "竞投举报审核,data:".  json_encode($data);
            $operateRecord->insertData('', 'update', 
            $logMessage, $_SESSION['esfadmin']['username']);
            $result = array('result'=>'1','failureNum'=>$complainFailure);
       }else{
            $result = array('result'=>'2');
       }
   
    }
    echo json_encode($result);
}elseif($_POST['action']=='feedback'){
    //反馈
    $id = intval($data['id']);
    $conf_where = "id = ".$id;       
    $fieldData['feedback'] = textConvert($data['feedback']);
    $returnNum = $complainRecord->update($fieldData, $conf_where);
    if($returnNum>0){       
        $logMessage = "竞投举报失败回馈,data:".  json_encode($data);
        $operateRecord->insertData('', 'update', 
        $logMessage, $_SESSION['esfadmin']['username']);
        $result = array('result'=>'1');//成功
    }else{
        $result = array('result'=>'2');//失败
    }
    echo json_encode($result);
    
}elseif($_POST['action']=='more'){
    $houseid = intval($data['id']);
    $houseArr = $complainRecord->findByHouseId($houseid);
    $newHouseArr = sortData($houseArr); 
    $newHouseArr = gbk2utf($newHouseArr);
    echo json_encode(array('result'=>1,'data'=>$newHouseArr));
    
}elseif($_POST['action']=='blacklist'){
    //举报人加入黑名单   
    $brokerBlacklist = new ComplainBrokerBlacklist();    
    $fieldData2['broker_id'] = intval($data['broker']);
    $fieldData2['operater'] = $_SESSION['esfadmin']['username'];
   // $fieldData2['add_time'] = date('Y-m-d H:i:s');  
    $returnNum = $brokerBlacklist->insert($fieldData2);
    if($returnNum>0){
        $logMessage = "举报人加入黑名单,data:".  json_encode($data);
        $operateRecord->insertData('', 'update', 
        $logMessage, $_SESSION['esfadmin']['username']);
        $result = array('result'=>'1');//成功
    }else{
        $result = array('result'=>'2');//失败
    }
    echo json_encode($result);
}elseif($_POST['action']=='deletebid'){
    //移除加入黑名单的竞投房源虚假的经纪人
    $complainbidblacklist = new ComplainBidBlackList();
    $id = intval($data['id']);
    $conf = "id= $id";
    $return = $complainbidblacklist->delBy($conf);
    if($return>0){
        $result = array('result'=>'1');//成功
    }else{
        $result = array('result'=>'2');//失败
    }
    echo json_encode($result);
}elseif($_POST['action']=='deletebroker'){
    //移除加入虚假举报的经纪人
    $brokerBlacklist = new ComplainBrokerBlacklist();    
    $id = intval($data['id']);
    $conf = "id= $id";
    $return = $brokerBlacklist->delBy($conf);
    if($return>0){
        $result = array('result'=>'1');//成功
    }else{
        $result = array('result'=>'2');//失败
    }
    echo json_encode($result);
    
}
//经纪人举报失败次数
function getBrokerComplainFailure($broker){
     $complainRecord = new ComplainRecord();
     $failurStatus =3;
     $failureNum = $complainRecord->findCountByBrokerAndStatus($broker,$failurStatus);
     return $failureNum;
}

//根据记录id获取房源id
function getHouseIdById($id){
     $complainRecord = new ComplainRecord();
     $record = $complainRecord->findById($id);
     return array('houseid'=>$record['house_id'],'tbl'=>$record['tbl']);
}


//数据处理
function sortData($data){
    if(is_array($data)&&!empty($data)){
        foreach ($data as $key=>$value){
            //原因
            $data[$key]['new_reason'] = getComplainReason($value['reason']);          
            //举报人
            $data[$key]['broker'] = getUsernameByUid($value['broker_id']);
            //时间
            $data[$key]['new_time'] = $value['check_time'] == '0000-00-00 00:00:00' ? '──' :  $value['check_time']; 
            //是否加入黑名单
            $data[$key]['join_black'] = isJoinBlackList($value['broker_id']);
            //举报失败次数
            $data[$key]['failure_num']= getBrokerComplainFailure($value['broker_id']);
            
        }       
    }
    return $data;
}


//是否加入黑名单
function isJoinBlackList($broker){
    $brokerBlacklist = new ComplainBrokerBlacklist();
    $conf = "broker_id=$broker";
    $blacklistNum = $brokerBlacklist->findNumberBy($conf);
    $isJoin = $blacklistNum>0 ? TRUE : FALSE;
    return $isJoin;
}





/**
 * 获取举报原因
 */
function getComplainReason($reason_type){
    $complain_reason = array('1'=>'面积虚假','2'=>'价格虚假','3'=>'房源已成交');
    $reason_arr= explode('|', $reason_type);
    if(count($reason_arr)>0){
         if(CommonBase::isFullArray($reason_arr)){
             foreach ($reason_arr as $value){
                 $reason[] = $complain_reason[$value];
             }
            $reason = implode('/', $reason);                 
         }
    }else{
        $reason = "";
    }        
    return $reason;
}


//根据经纪人id获取username
function getUsernameByUid($uid){
    global $mc,$dbback;
    $sql = SqlCombinHelper::select_sql('broker', 'username', "uid = '".$uid."'");	   
	$username = $mc->get($sql);
	//if ($brokerUid === false) {
		$username = $dbback->getOne($sql);
	//	$mc->add($sql, $brokerUid, 3600);
	//}
    return $username;
    
}

function ajaxResponse($data, $encode = 'gbk', $type = 'json') {
    if ($type == 'json') {
        if ($encode == 'gbk') {
            $data = gbk2utf($data);
        }
        $res = json_encode($data); 
        header("Content-type: application/json;"); 
    } else {
        $res = $data;
        header("text/html;charset=GB2312");         
    }
   
}

function gbk2utf($data) {
    if (is_array($data)) {
        foreach ($data as &$value) {
            $value = gbk2utf($value);
        }
    } else {
        $data = iconv('gbk', 'utf-8', $data);
    }
    return $data;
}

//中文编码转换
function textConvert($answerText){
    $answerText = urldecode($answerText);
    return  iconv('utf-8','gb2312',$answerText);        
}

//冻结举报房源
function freezeHouse($tbl,$house){
    $sql = "update $tbl set esta=5 where id=$house";
    $db->Execute($sql);
}


