<?php 
$conf_basedir = "../../"; //相对主目录的位置
$conf_where = "admin"; //选择调用相应模块
include_once($conf_basedir . "/base.inc.php");

//有关本页面的信息
checkAuth('34');
//基本配置
include_once($conf_basedir."model/univercityChannel/univercityChannelFunction.php");
$uc = new UnivercityChannel('test');
$tbl = $uc->getSchoolTable();
$tblSchoolImg = $uc->getSchoolImgTable();
$tblSchoolBlock = $uc->getSchoolBlockTable();
$schoolEnableCity = array('nj','hz','sz','hf','wx','cz','cq','xa','ks','wh');
$blockId = intval($blockid);//echo 1;echo $blockId;exit;
$schoolId = intval($schoolId);
$res = array();
if($action=='add') {
    if($blockId===0||$schoolId===0) {
        $res['status']=300;
        $res['requestData'] = $_POST;
        $res['msg'] = iconv('GBK', 'UTF-8', '参数错误');
    } else {
        //检查该小区是否关联当前学校
        $sql = "select count(id) as num from $tblSchoolBlock where school_id=$schoolId and block_id=$blockId";
        $num = $dbback->getOne($sql);
        if($num>0){
            $res = array();
            $res['status']=301;
            $res['requestData'] = $_POST;
            $res['msg'] = iconv('GBK', 'UTF-8', '不能重复添加小区');;
        } else {
            $q = "select * from $tbl where id = $schoolId";
            $data_info = $db->GetRow($q);
            if($data_info) {
                $dis = get_school_distance($data_info,$blockid); 
                if($dis!==false) {
                    $sql = "insert into $tblSchoolBlock (school_id,block_id,block_distance) value ($schoolId,$blockId,$dis)";
                } else {
                    $sql = "insert into $tblSchoolBlock (school_id,block_id) value ($schoolId,$blockId)";
                }
                //echo $sql;
                $db->execute($sql);
                $sbid = mysql_insert_id();
                $res = array();
                $res['status']=200;
                $data = array('sbid'=>$sbid,
                    'block_id'=>$blockId,
                    'school_id'=>$schoolId,
                    'dis'=>$dis,
                    'block_name'=>$blockname,//iconv('GBK', 'UTF-8', $blockname)
                );
                $res['requestData'] = $_POST;
                $res['data'] = $data;
                $res['msg'] = '成功';
                //更新school表
                $sql2 = "select blockid from school where id=$schoolId";
                $bids = $dbback->getOne($sql2);
                if($bids) {
                    $bids = explode(",",$bids);
                } else {
                    $bids = array();
                }
                $bids[] = $blockId;
                $sql3 = "update school set blockid='".implode(",",$bids)."' where id=$schoolId";
                $db->execute($sql3);
            } else {
                $res = array();
                $res['status']=302;
                $res['requestData'] = $_POST;
                $res['msg'] = iconv('GBK', 'UTF-8', '学校不存在');;
            }
        }
    }
}
if($action =='del') {
    if($by =='id') {
        $id = intval($id);
        if($id==0) {
            $res['status']=300;
            $res['requestData'] = $_POST;
            $res['msg'] = iconv('GBK', 'UTF-8', '参数错误');
        } else {
            $sql = "delete from $tblSchoolBlock where id='$id'";
            $db->execute($sql);
            //$sbid = mysql_insert_id();
            $row = mysql_affected_rows();
            if($row==1) {
                $res['status']=200;
                $res['requestData'] = $_POST;
                $res['msg'] = iconv('GBK', 'UTF-8', '成功');
            } else {
                $res['status']=301;
                $res['requestData'] = $_POST;
                $res['msg'] = iconv('GBK', 'UTF-8', '失败');
            }
        }
    } else {
        if($blockId===0||$schoolId===0) {
            $res['status']=300;
            $res['requestData'] = $_POST;
            $res['msg'] = iconv('GBK', 'UTF-8', '参数错误');
        } else {
            $sql = "delete from $tblSchoolBlock where school_id='$schoolId' and block_id='$blockId'";
            $db->execute($sql);
            //$sbid = mysql_insert_id();
            $row = mysql_affected_rows();
            $sql2 = "select block_id from $tblSchoolBlock where school_id='$schoolId'"; 
            $bidsx = $db->getAll($sql2);
            $bids = array();
            foreach($bidsx as $v) {
                $bids[] = $v['block_id'];
            }
            $sql3 = "update school set blockid='".implode(",",$bids)."' where id=$schoolId";
            $db->execute($sql3);

            if($row==1) {
                $res['status']=200;
                $res['requestData'] = $_POST;
                $res['row'] = $row;
                $res['msg'] = iconv('GBK', 'UTF-8', '成功');
            } else {
                $res['status']=301;
                $res['requestData'] = $_POST;
                $res['row'] = $row;
                $res['msg'] = iconv('GBK', 'UTF-8', '失败');
            }
        }
    }
    
}
echo json_encode($res);


function get_school_distance($data_info,$blockid) {
    global $db,$dbback,$uc,$conf_city;
    $citys = array('wx'=>'无锡','hz'=>'杭州','hf'=>'合肥','sz'=>'苏州','cz'=>'常州','wh'=>'芜湖','cq'=>'重庆','sy'=>'沈阳','ks'=>'昆山','nj'=>'南京',);//,'hz'=>'杭州'
	$sb = new SchoolBlock($db,$dbback,$uc);
	$sbOrder = new order('a');
	$sbOrder->setOrder('id','asc');
	$sbConf = new Conf();
    $id = $data_info['id'];
	$sbConf->add('school_id',$id)->add('block_id',$blockid);
	$schoolBlock = $sb->getAllBy($sbConf,$sbOrder);
    $sName = trim($data_info['schoolname']);
    $sLat = $data_info['b_map_y'];;
    $sLng = $data_info['b_map_x'];
    $city = $citys[$conf_city];//exit;
    $b = $dbback->getRow("select * from block where id = $blockid");
    if($city&&$sName&&$sLat&&$sLng&&$b) {
        //print_r($schoolBlock);exit;
            //echo $blockId;exit;
        $bName = trim($b['blockname']);
        $bLat = $b['b_map_y'];
        $bLng = $b['b_map_x'];
        $dis = distance_t($city,$sName,$sLat,$sLng,$bName,$bLat,$bLng);
        return $dis = number_format($dis, 2, '.', '');
    } else {
        return false;
    }
}
function distance_t($city,$sName,$sLat,$sLng,$bName,$bLat,$bLng) {
        // 创建一个cURL资源
        $ch = curl_init();
        $url = "http://api.map.baidu.com/direction/v1";
        $p['origin'] = 'origin='.$sName."|".$sLat.",".$sLng;
        $p['destination'] = 'destination='.$bName."|".$bLat.",".$bLng;
        $p['origin_region'] = 'origin_region='.$city;
        $p['destination_region'] = 'destination_region='.$city;
        $p['output'] = 'output=json';
        $p['ak'] = 'ak=L03h2Teoucd5OlIgmaD3llM8';
        //?mode=driving&origin=上地五街&destination=北京大学&origin_region=北京&destination_region=北京&output=json&ak=E4805d16520de693a3fe707cdc962045
        $url = $url.'?'.implode("&",$p);
        // 设置URL和相应的选项
        if($city=='苏州'){
            //echo $url;exit;
        }
        
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        
        // 抓取URL并把它传递给浏览器
        $nano = time_nanosleep(0, 20000000);
        if($nano === true) {
            $res = curl_exec($ch);
        curl_close($ch);
        $res = json_decode($res);
        
        if($res->status==0) {
            return $res->result->taxi->distance/1000;
        } else {
            return 0;
        }
        } else {
            echo '失败';
        }
        // 关闭cURL资源，并且释放系统资源

}
?>