<?php

// 上传亲子活动
require_once ("../user_include.inc");

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

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$eventId = ParamUtil::getRequestNumber("eventId", 0);
$comment = ParamUtil::getRequestString("comment");//说明

$result = array();

//参数检查
if(empty($unionId || empty($eventId))) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

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

//查询亲子活动活动
//todo 以后改为家校系统查询
$familyEventDat = FamilyEventDat::getById($eventId);
if(empty($familyEventDat)) {
	$result["message"] = "参数错误！";
	responseNG($result);
}

$userMst = $userList[0];

//添加用户完成情况
if(!empty($comment)) {
	//没有图片，只有感想的情况
	$familyEventMemberDat = new FamilyEventMemberDat();
	$familyEventMemberDat->family_event_id = $familyEventDat->id;
	$familyEventMemberDat->user_id = $userMst->id;
	$familyEventMemberDat->comment = $comment;
	$familyEventMemberDat->point = ABILITY_POINT;
	$familyEventMemberDat->save();
	
	//给用户添加能力分（todo）
	$userMst->ability_point += ABILITY_POINT;
	$userMst->save();
} else {
	//批量接受上传图
	$param = array();
	$param['social_event_id'] = $familyEventDat->id;
	$param['user_id'] = $userMst->id;
	$param['delete_flg'] = false;
	$tmpList = FamilyEventMemberDat::getList($param,'id','desc', 0, 1);
	if(!empty($tmpList)) {
		$familyEventMemberDat = $tmpList[0];
		//接受图片并更新
		//处理上传图片
		$aliHandler = new AliUploadHandler();
		if(!empty($_FILES['photo']['tmp_name'])){
		  $upload_file = FileUploadUtil::getScalarFile(@$_FILES['photo']);
		  $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);
		  //这里的url就是http://compass-dev.oss-cn-beijing.aliyuncs.com/20200220_aa.jpg
		  ErrorLogger::doOutput("Compass...ajax_set_social_event_result.php....images=" . $familyEventMemberDat->images, 0);
		  if(empty($familyEventMemberDat->images)) {
			  $familyEventMemberDat->images = $url;
		  } else {
			  $familyEventMemberDat->images = $familyEventMemberDat->images . "|" . $url;
		  }
		  
		  $familyEventMemberDat->save();
		}		
	}
}


//todo 改为通知家校系统用户

ErrorLogger::doOutput("Compass...ajax_set_family_event_result.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;
}
?>