<?php

// 获取证书申请
require_once ("../user_include.inc");

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

//获取参数
$unionId = ParamUtil::getRequestString("unionId");
$certificateId = ParamUtil::getRequestNumber("certificateId", 0);
$name = ParamUtil::getRequestString("name");
$mobile = ParamUtil::getRequestString("mobile");
$address = ParamUtil::getRequestString("address");

$result = array();
//参数检查
if(empty($unionId) || empty($certificateId) || empty($name) || empty($mobile) || empty($address)) {
	$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);
}

//判断证书是否存在
$certificateMst = CertificateMst::getById($certificateId);
if(empty($certificateMst)) {
	$result["message"] = "证书不存在！";
	responseNG($result);
}

//是否有正在制作的证书，如有则提示
$userId = $userList[0]->id;
$param = array();
$param['user_id'] = $userId;
$param['status_NOT'] = "FINISH";
$param['delete_flg'] = false;
$userCertificateCount = UserCertificateDat::getListCount($param);
if($userCertificateCount > 0) {
	$result["message"] = "有证书正在途中！";
	responseNG($result);
}

//提交申请记录
$userCertificateDat = new UserCertificateDat();
$userCertificateDat->user_id = $userId;
$userCertificateDat->certificate_id = $certificateId;
$userCertificateDat->name = $name;
$userCertificateDat->mobile = $mobile;
$userCertificateDat->address = $address;
$userCertificateDat->status = 'WAITING';
$userCertificateDat->save();

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

$result["message"] = "申请成功！";
responseOK($result);

//返回结果
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;
}
?>