#include <tc\folder.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <tc\preferences.h>
#include <winsock2.h> 
#include <tc/prm.h>
#include <comdef.h>
#include <windows.h>
#include "epm_handler_common.h"

#include "RB_AutoSignServer.h";
#define DOFREE(obj)								\
{												\
	if(obj)										\
	{											\
		MEM_free(obj);							\
		obj = NULL;								\
	}											\
}

//提取流程节点自动跳过
int RB_AutoSignServer(void *returnValue)
{
	int ifail = 0;
	char * myResult = NULL,
		*myType = NULL;
	char * signoffUid = NULL;
	char * parentUid = NULL;
	string  resultString;
	tag_t parentTag = NULLTAG, signoffTag = NULLTAG;
	POM_AM__set_application_bypass(true);
	ITKCALL(ifail = USERARG_get_string_argument(&signoffUid));
	ITKCALL(ifail = USERARG_get_string_argument(&parentUid));
	myResult = (char *)MEM_alloc(8 * sizeof(char));
	printf("signoffUid=%s\n", signoffUid);
	printf("parentUid=%s\n", parentUid);
	tc_strcpy(myResult, "-1");
	ITK__convert_uid_to_tag(parentUid, &parentTag);
	if (parentTag == NULLTAG) {
		tc_strcpy(myResult, "0");
	}
	ITK__convert_uid_to_tag(signoffUid, &signoffTag);
	if (signoffTag == NULLTAG) {
		tc_strcpy(myResult, "0");
	}
	char * taskname = NULL, *type = NULL;
	ITKCALL(AOM_ask_value_string(parentTag, "object_type", &type));
	printf("name=%s\n", taskname);
	printf("type=%s\n", type);
	if (strcmp(type, "EPMReviewTask") == 0)
	{
		//int num = 0;
		//tag_t *sub_tags = NULL;
		//ITKCALL(EPM_ask_sub_tasks(task_tag, &num, &sub_tags));
		//for (int j = 0; j < num; j++)
		//{
		//char * taskname2 = NULL, *type2 = NULL;
		//ITKCALL(EPM_ask_name2(sub_tags[j], &taskname2));
		//ITKCALL(AOM_ask_value_string(sub_tags[j], "object_type", &type2));
		//if (strcmp(type2, "EPMPerformSignoffTask") == 0)
		//{
		//printf("name2=%s,type2=%s\n", taskname2, type2);		
		ITKCALL(EPM_promote_task(parentTag, ""));
		//}
		//DOFREE(taskname2);
		//DOFREE(type2);

		//	}
		//DOFREE(sub_tags);
	}
	DOFREE(taskname);
	DOFREE(type);
	POM_AM__set_application_bypass(false);
	/*if (parentTag != NULLTAG && signoffTag != NULLTAG) {
	POM_AM__set_application_bypass(true);
	char *realState = NULL;
	logical isPexist = false;
	logical isAexist = false;
	ITKCALL(POM_instance_exists(parentTag, &isPexist));
	ITKCALL(POM_instance_exists(signoffTag, &isAexist));
	if (isPexist && isAexist) {
	ITKCALL(ifail = AOM_refresh(parentTag, 0));
	ITKCALL(ifail = AOM_refresh(signoffTag, 0));
	ITKCALL(AOM_ask_value_string(parentTag, "real_state", &realState));
	printf("realState=%s\n", realState);
	if (strcmp(realState, "Started") == 0) {
	ITKCALL(ifail = EPM_set_task_decision2(parentTag, signoffTag, CR_approve_decision, "跳过"));
	if (ifail == ITK_ok) {
	tc_strcpy(myResult, "0");
	printf("执行跳过成功，跳过");
	}
	else {
	printf("执行跳过失败，不跳过");
	}
	//MEM_free(realState);
	//realState = NULL;
	}
	else if (strcmp(realState, "Completed") == 0) {
	tc_strcpy(myResult, "0");
	printf("节点状态为完成，跳过\n");
	}
	MEM_free(realState);
	realState = NULL;
	POM_AM__set_application_bypass(false);
	}
	else {
	tc_strcpy(myResult, "0");
	printf("对象不存在，跳过\n");
	}
	}
	*/
	*((char**)returnValue) = myResult;
	return  0;
}