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

	if ($actiontype) doaction($actiontype,$actionid);
    $css="<link href='http://esfadmin.house365.com/script/css/autocomplete.css' rel='stylesheet' type='text/css' />";
    $js = "<script type='text/javascript' src='http://esfadmin.house365.com/script/js/showpic.js'></script>
    <script type='text/javascript' src='http://esfadmin.house365.com/script/js/public.js'></script>
    <script type='text/javascript' src='http://esfadmin.house365.com/script/js/formValidator.js'></script>
   <script type='text/javascript' src='http://esfadmin.house365.com/script/js/jquery_ui_autocomplete/ui/jquery.ui.core.js'></script>
   <script type='text/javascript' src='http://esfadmin.house365.com/script/js/jquery_ui_autocomplete/ui/jquery.ui.position.js'></script>
   <script type='text/javascript' src='http://esfadmin.house365.com/script/js/jquery_ui_autocomplete/ui/jquery.ui.widget.js'></script>
   <script type='text/javascript' src='http://esfadmin.house365.com/script/js/jquery_ui_autocomplete/ui/jquery.ui.autocomplete.js'></script>";
    $tbl='block';
    $spell=new spell();

   if (($action<>"insert")and($action<>"modify")and ($action<>'update')and ($action<>'add')) exit;

     /***修改信息***/
if ($action == "modify") {

    $q = "select * from $tbl where id=$id";
    if ($q_ext != "")
        $q.=" and " . $q_ext;
    $data_info = $db->GetRow($q);

    if (count($data_info) == 0)
        echo "<script>alert('没有发现您查询的记录');window.opener.location.reload();window.close();</script>";

    $data_info["bi_s"] = csubstr($data_info["bi_s"], 1);

	$house['street'] = $dbback->GetAssoc("select id,streetname from street where district='$data_info[district]'");
    
	$img = $data_info["b_img"] != "" ? "<a href=" . str_replace("thumb/", '', $data_info["b_img"]) . " target=_blank>查看</a>&nbsp;&nbsp;<a href=block_modify.php?action=del_pic&id=" . $data_info["id"] . ">删除</a>" : "";
	$b_property_type = array();
	if($data_info['b_property_type']!=''){
		$b_property_type = explode(',',$data_info['b_property_type']);
	}

    $bbsarea = array();
	if($data_info['bbsarea']!=''){
		$bbsarea = explode(',',$data_info['bbsarea']);
	}


    $feature = array();
	if($data_info['feature']!=''){
		$feature = explode('$',$data_info['feature']);
	}
    unset($feature[0]);

	//获取地铁信息
	$sarr = $dbback->GetAll("select tb.id, t.bid, t.station, t.line from traffic_block tb left join traffic t on tb.bid = t.bid where tb.type = '2' and tb.blockid = '".$id."'");
	if($sarr)
	{
		foreach($sarr as $val)
		{
			$subwayarr[$val['line']][] = $val;
		}
	}
    
    //获取BRT公交信息
	$sarr = $dbback->GetAll("select tb.id, t.bid, t.station, t.line from traffic_block tb left join traffic t on tb.bid = t.bid where tb.type = '3' and tb.blockid = '".$id."'");
	if($sarr)
	{
		foreach($sarr as $val)
		{
			$brtarr[$val['line']][] = $val;
		}
	}

    //获取公交信息
	$sarr = $dbback->GetAll("select tb.id, t.bid, t.station, t.line from traffic_block tb left join traffic t on tb.bid = t.bid where tb.type = '1' and tb.blockid = '".$id."'");
	if($sarr)
	{
		foreach($sarr as $val)
		{
			$busarr[$val['line']][] = $val;
		}
	}
}

if ($action == 'insert') {

   
    //产生数据
    $ss = array();
    $ss["blockname"] = $blockname;
    $ss["district"] = $district;
    $ss["streetid"] = $streetid;
    $ss["averprice"] = $averprice;
	if(count($b_property_type) > 0 ){
		$ss["b_property_type"] = implode(",",$b_property_type);
	}else{
		$ss["b_property_type"] = '';
	}
    if(count($bbsarea) > 0 ){
		$ss["bbsarea"] = implode(",",$bbsarea);
	}else{
		$ss["bbsarea"] = '';
	}
	$ss["property_fees"] = $property_fees;
    $ss["forumid"] = $forumid;
    $ss["newhouseid"] = $newhouseid;
    $ss["b_green"] = $b_green;
    $ss["b_parking"] = $b_parking;
    $ss["b_developers"] = $b_developers;
    $ss["b_property_company"] = $b_property_company;
    $ss["address"] = $address;
    $ss["b_bus"] = $b_bus;
    $ss["b_metro"] = $b_metro;
    $ss["area"] = $area;
    $ss["b_introduction"] = $b_introduction;
    $ss["b_other"] = $b_other;
    $ss["b_num"] = $b_num;
    $ss["bi_s"] = $spell->s_spell($blockname);
    $ss["bi_spell"] = $spell->c_spell($blockname);
    $ss["map_x"] = $map_x;
    $ss["map_y"] = $map_y;
    $ss["b_map_x"] = $b_map_x;
    $ss["b_map_y"] = $b_map_y;
	$ss["nofee"] = intval($nofee);
	$ss["streetview"] = $streetview;
	$ss["esta"] = intval($esta);
    if($conf_city=='nj')
    {
         $ss["cbd"] = $cbd;
         $ss["park_high"] = $park_high;
         $ss["school_house"] = $school_house;
		 $ss["b_address"] = $b_address;
    }else{
         $ss["measurelink"] = $measurelink;
         $ss["app"]= $app;
    }
   
    $ss["alias"] = $alias;
    $ss["total_room"]  =  $total_room;
    $ss["updateprice"] = $updateprice;
    $ss["b_area"] = $build_area;
    $ss["plot_ratio"] = $plot_ratio;
    $ss["turn_time"] = $turn_time;
    $ss["sd_mark"] = $sd;
    if($config_GrowthCredit == 1){
        $ss["lock_layout"] = $lock_layout;//室内图锁定
        $ss["lock_outside"] = $lock_outside;//外景图锁定
    }  
    $ss["addtime"] = time();
    $ss["updatetime"] = time();
    $ss['addname'] = $_SESSION[$web_auth]["username"];
    //房源特色
		$ss['feature'] = '';
		if(is_array($feature) && !empty($feature))
		{
			foreach($feature as $val)
			{	
                $ss['feature'] .= '$'.$val;
			}
		}
	

    $data_info = $ss; //返回值
    //写入数据库
    $q = insert_sql($tbl, $ss);

    $db->Execute($q);

	$id = $db->Insert_ID();
	if($config_windows==1){ // 橱窗关键词同步
	   $sort = $spell->s_spell($blockname);
	   $sort = substr($sort,0,1);
	   $sql = "insert into window_keys(blockid,keys_title,window_id,category_id,district,dateline,sort) values ('".$id."','".$blockname."','2','1','".$district."','".$time."','".$sort."') ON DUPLICATE KEY UPDATE blockid='".$id."',dateline='".$time."' ";
	   $db->Execute($sql);
	}
	$ss = array();
	$ss['blockid'] = $id;
	$ss['sort0_num'] = 0;
	$ss['sort1_num'] = 0;
	$ss['last_audit_time'] = 0;
	$ss['last_auditor'] = '';

	$q = insert_sql("block_img_num", $ss);
    $db->Execute($q);

	//清理缓存
	if($ss["nofee"] == 1)
	{
		$memkey = "nofee_block_".$conf_cityflag;
		$mc->delete($memkey);
	}

    echo "<script>window.opener.location.reload();window.close();</script>";
}

if ($action == 'update') {
    //$spell=new spell();
    //产生数据

    $ss = array();
    $ss["id"] = intval($id);
    $ss["blockname"] = $blockname;
    $ss["district"] = $district;
    $ss["streetid"] = $streetid;
    $ss["averprice"] = $averprice;
	if(count($b_property_type) > 0 ){
		$ss["b_property_type"] = implode(",",$b_property_type);
	}else{
		$ss["b_property_type"] = '';
	}
    if(count($bbsarea) > 0 ){
		$ss["bbsarea"] = implode(",",$bbsarea);
	}else{
		$ss["bbsarea"] = '';
	}

    $ss["forumid"] = $forumid;
    $ss["newhouseid"] = $newhouseid;
    $ss["b_green"] = $b_green;
    $ss["b_parking"] = $b_parking;
    $ss["b_developers"] = $b_developers;
    $ss["b_property_company"] = $b_property_company;
    $ss["address"] = $address;
    $ss["b_property_fees"] = $b_property_fees;
    $ss["b_bus"] = $b_bus;
    $ss["b_metro"] = $b_metro;
    $ss["area"] = $area;
    $ss["b_introduction"] = $b_introduction;
    $ss["b_other"] = $b_other;
    $ss["b_num"] = $b_num;
    $ss["bi_s"] = $spell->s_spell($blockname);
    $ss["bi_spell"] = $spell->c_spell($blockname);
    $ss["map_x"] = $map_x;
    $ss["map_y"] = $map_y;
    $ss["b_map_x"] = $b_map_x;
    $ss["b_map_y"] = $b_map_y;
	$ss["nofee"] = intval($nofee);
	$ss["streetview"] = $streetview;
	$ss["esta"] = intval($esta);
        if($conf_city=='nj')
    {
         $ss["cbd"] = $cbd;
         $ss["park_high"] = $park_high;
         $ss["school_house"] = $school_house;
		 $ss["b_address"] = $b_address;
    }else{
         $ss["measurelink"] = $measurelink;
         $ss["app"]= $app;
    }
   
    $ss["total_room"]  =  $total_room;
    $ss["updateprice"] = $updateprice;
    $ss["b_area"] = $build_area;
    $ss["plot_ratio"] = $plot_ratio;
    $ss["turn_time"] = $turn_time;
    if($config_GrowthCredit == 1){
        $ss["lock_layout"] = $lock_layout;//室内图锁定
        $ss["lock_outside"] = $lock_outside;//外景图锁定
    }    
    $ss["updatetime"] = time();
    $ss["alias"] = $alias;
	 

    //房源特色
		$ss['feature'] = '';
		if(is_array($feature) && !empty($feature))
		{
			foreach($feature as $val)
			{	
				$val = $val;
                $ss['feature'] .= '$'.$val;
			}
		}

    $data_info = $ss; //返回值\

	//用来判断该小区是否已经修改免佣房源设置
	$old_nofee = intval($old_nofee);
	if($old_nofee == 1 && $ss["nofee"] == 0)
	{
		if($_SESSION['admin']['groupid'] == 1)
		{
			$db->Execute("UPDATE sell SET nofee = 0 WHERE blockid = '".$ss["id"]."' AND nofee = 1");	
		}
		else
		{
			$ss["nofee"] = 1;
			echo "<script>alert('取消设置需发起工作流由技术处理！');</script>";	
		}
	}
	//================END================//

    //写入数据库
    $q = update_sql($tbl, $ss, " where id = '".$ss["id"]."'");

    $db->Execute($q);

	//清理缓存
	if($old_nofee != $ss["nofee"])
	{
		$memkey = "nofee_block_".$conf_cityflag;
		$mc->delete($memkey);
	}

    if($config_windows==1){// 橱窗关键词同步
	   $sort = $spell->s_spell($blockname);
	   $sort = substr($sort,0,1);
       $sql = "update window_keys set keys_title='".$blockname."',district='".$district."',sort='".$sort."',dateline='".$time."' where blockid='".$id."' and category_id=1";
	   $db->Execute($sql);
	}

    $sql = "update sell set streetid='$streetid' where blockid='$id'";
    //$db->Execute($sql);
    $sql = "update rent set streetid='$streetid' where blockid='$id'";
    //$db->Execute($sql);



    echo "<script>alert('已成功修改');window.close();</script>";
}


    if($data_info["district"] != ""){
	$condext = " WHERE district ='".$data_info["district"]."'";
    }

    $empty = array (""=>" ");
    $streetsql = "SELECT id,streetname FROM street ".$condext;
      
    $house['street']	= $empty+$db->GetAssoc($streetsql);
    

    if($conf_city=='nj')
    {
    $house['cbd']		= $empty+$db->GetAssoc("SELECT id,cbdname FROM cbd");
    }

    //引用模版文件
    include_once(Template("header"));
    include_once(Template("block/block_modify"));
    include_once(Template("footer"));
?>