<?php

// 圈主发布志愿者活动
require_once ("../user_include.inc");

ErrorLogger::doOutput("Compass...ajax_set_circle_event_new.php....Start.", 0);

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$circleId = ParamUtil::getRequestNumber("circleId", 0);//圈子id
$title = ParamUtil::getRequestString("title");//活动标题
$comment = ParamUtil::getRequestString("comment");//活动内容描述
$startTime = ParamUtil::getRequestString("startTime");//活动开始时间
$finishTime = ParamUtil::getRequestString("finishTime");//活动结束时间
$enrollTime = ParamUtil::getRequestString("enrollTime");//截止报名时间
$position = ParamUtil::getRequestString("position");//活动地点
$includeSocialUser = ParamUtil::getRequestBoolean("includeSocialUser");//是否允许社会人士参加
$maxMember = ParamUtil::getRequestNumber("maxMember");//最大参加人数
$timeLength = ParamUtil::getRequestString("timeLength");//活动时长
$leaderName = ParamUtil::getRequestString("leaderName");//负责人姓名
$leaderContact = ParamUtil::getRequestString("leaderContact");//负责人联系方式
$venue = ParamUtil::getRequestString("venue");//集合地点
$venueTime = ParamUtil::getRequestString("venueTime");//集合时间

$result = array();

//判断用户是否存在
$param = array();
$param['unionid'] = $unionId;
$param['delete_flg'] = false;
$userList = UserMst::getList($param,'id','desc', 0, 1);
if(empty($userList)) {
	$result["message"] = "用户不存在！";
	responseNG($result);
}
$userMst = $userList[0];

//判断圈子是否存在
$circleDat = CircleDat::getById($circleId);
if(empty($circleDat)) {
	$result["message"] = "圈子不存在！";
	responseNG($result);
}

//判断是否为圈主
if($circleDat->owner_id != $userMst->id) {
	$result["message"] = "圈主才有权限发布活动！";
	responseNG($result);
}

if(!empty($title) && !empty($comment)) {
	//是否有正在审核的活动
	$param = array();
	$param['circle_id'] = $circleDat->id;
	$param['status'] = "NEW";
	$param['delete_flg'] = false;
	$volunteerEventList = VolunteerEventDat::getList($param, "id", "desc", 0, 1);
	if(!empty($volunteerEventList)) {
		$result["message"] = "已经有一条活动待审核！";
		responseNG($result);
	}
	//判断输入的活动地点是否合法
	$addressUrl = 'https://apis.map.qq.com/ws/geocoder/v1/?address=' . $position . '&key=' . LBS_KEY;
	$lbsObj =  file_get_contents($addressUrl);
	$jsonObj = json_decode($lbsObj); //对JSON格式的字符串进行编码
	$lbsArray = get_object_vars($jsonObj);//转换成数组
	if($lbsArray['status'] != 0) {
		$result["message"] = "请输入完整的活动地点！";
		responseNG($result);
	}
	$resultObj = $lbsArray['result'];

	$positionLongitude = $resultObj->location->lng;//活动地点经度
	$positionLatitude = $resultObj->location->lat;//活动地点纬度
	$province = $resultObj->address_components->province;
	$city = $resultObj->address_components->city;
	$district = $resultObj->address_components->district;

	//登陆活动数据
	$volunteerEventDat = new VolunteerEventDat();
	$volunteerEventDat->title = $title;
	$volunteerEventDat->comment = $comment;
	$volunteerEventDat->start_time = $startTime;
	$volunteerEventDat->finish_time = $finishTime;
	$volunteerEventDat->enroll_time = $enrollTime;
	$volunteerEventDat->position = $position;
	$volunteerEventDat->position_longitude = $positionLongitude;
	$volunteerEventDat->position_latitude = $positionLatitude;
	$volunteerEventDat->position_latitude = $positionLatitude;
	$volunteerEventDat->circle_id = $circleDat->id;
	$volunteerEventDat->province = $province;
	$volunteerEventDat->city = $city;
	$volunteerEventDat->district = $district;
	$volunteerEventDat->include_social_user = $includeSocialUser;
	$volunteerEventDat->max_member = $maxMember;
	$volunteerEventDat->time_length = $timeLength;
	$volunteerEventDat->leader_name = $leaderName;
	$volunteerEventDat->leader_contact = $leaderContact;
	$volunteerEventDat->venue = $venue;
	$volunteerEventDat->venue_time = $venueTime;
	$volunteerEventDat->status = 'NEW';
	$volunteerEventDat->author = $userMst->name;
	$volunteerEventDat->save();
}


//处理上传图片
$aliHandler = new AliUploadHandler();
if(!empty($_FILES['frontImage']['tmp_name'])){
	$upload_file = FileUploadUtil::getScalarFile(@$_FILES['frontImage']);
	$tmp_file_name = date("Ymd") . "_" . trim(basename($upload_file->name));//保存的名字自定义，这里如果图片的名字原来的是aa.jp，保存成20200220_aa.jpg

	$url = $aliHandler->uploadImg($tmp_file_name, $upload_file->tmp_name);
	ErrorLogger::doOutput($url);
	//获取最后一条
	$param = array();
	$param['circle_id'] = $circleDat->id;
	$param['status'] = "NEW";
	$param['delete_flg'] = false;
	$volunteerEventList = VolunteerEventDat::getList($param, "id", "desc", 0, 1);
	$volunteerEventDat = $volunteerEventList[0];
	//这里的url就是http://compass-dev.oss-cn-beijing.aliyuncs.com/20200220_aa.jpg
	$volunteerEventDat->front_image = $url;
	$volunteerEventDat->save();
}

ErrorLogger::doOutput("Compass...ajax_set_circle_event_new.php....End.", 0);

//返回结果
$result["message"] = "发布成功,请等待审核！";
responseOK($result);

function responseNG($result) {
	$result = array("status"=>"NG", "result"=>$result);
	print json_encode($result);
	exit;
}
function responseOK($result) {
	$result = array("status"=>"OK", "result"=>$result);
	print json_encode($result);
	exit;
}
?>