#include "HTTPRequest.hpp"
#include "epm_handler_common.h"
#include <tc\tc_macros.h>
#include <tc\emh.h>
#include <tc\preferences.h>
#include <property\propdesc.h>
#include <epm\epm.h>
#include <epm\epm_toolkit_tc_utils.h>
#include <tccore\item.h>
#include <tccore\grmtype.h>
#include <tccore\grm.h>
#include <tccore\imantype.h>
#include <sa\am.h>
#include <sa\sa.h>
#include <tccore\aom.h>
#include <tccore\aom_prop.h>
#include <property\prop_errors.h>
#include <tccore\workspaceobject.h>
#include <qry\qry.h>
#include <bom\bom_attr.h>
#include <bom\bom.h>
#include <epm\signoff.h>
#include <pom\pom\pom.h>
#include <pom\pom\pom_errors.h>
#include <fclasses\tc_date.h>
#include <epm\cr.h>
#include <cfm\cfm.h>
#include <sa\am.h>
#include "tc/envelope.h"
#include <stdio.h>
#include "ae/dataset.h"
#include <ctype.h>
#include <string.h>
#include <string>
#include <iostream>
#include <iterator>
#include <map>
#include <vector>
#include "ce/ce.h"
#include <time.h>
#include <windows.h>  
#include <iostream>  
#include <sql.h>  
#include <sqlext.h>  
#include "string"
#include "sstream"
#include <sa/tcfile.h>
#include <tccore/aom.h>
#include <sa/tcfile_cache.h>
#include <form/form.h>
#include "ctime"
#include "tchar.h"
#include <iostream>  
#include <string>  
#include <fstream>

using namespace std;

DWORD WINAPI sendRequest(void *uid) {
	try {
		char *pref_values;
		PREF_ask_char_value("jd2_server_ip", 0, &pref_values);
		if (strlen(pref_values) != 0) {
			stringstream ss;
			ss << "http://";
			ss << pref_values;
			ss << ":8880/api/deleteJob?uid=";
			ss << (char *)uid;
			printf("request url====>%s\n", ss.str().c_str());
			http::Request request(ss.str().c_str());
			const http::Response response = request.send("GET");
			std::cout << std::string(response.body.begin(), response.body.end()) << '\n'; // print the result
		} else {
			printf("jd2_server_ip error\n");
		}
	} catch (const std::exception& e) {
		std::cerr << "Request failed, error: " << e.what() << '\n';
	}
	return 0;
}

int jd_batch_process(EPM_action_message_t msg) {

	int ifail = EPM_go, att_count, tag_cnt, rel_cnt, type = EPM_reference_attachment;
	tag_t rootTask, *attachments, win, top, *tags, tag, *rels, job, process_template, new_process;
	char *val;
	EPM_ask_root_task(msg.task, &rootTask);
	EPM_ask_job(msg.task, &job);
	EPM_ask_attachments(rootTask, EPM_target_attachment, &att_count, &attachments);
	if (att_count < 2)
		return 0;
	printf("att_count:%d\n", att_count);
	AOM_ask_value_tag(job, "process_template", &process_template);
	vector<tag_t> process_vec;

	
	for (int i = 0; i < att_count; i++) {

		char *object_string, *object_type;
		AOM_ask_value_string(attachments[i], "object_type", &object_type);
		AOM_ask_value_string(attachments[i], "object_string", &object_string);
		printf("object_type:%s object_string:%s\n", object_type, object_string);
		tag_t new_process;
		ITKCALL(EPM_create_process(object_string, "", process_template, 1, &attachments[i], &type, &new_process));
		process_vec.push_back(new_process);
	}
	int sub_cnt;
	tag_t *tasks;
	EPM_ask_sub_tasks(rootTask, &sub_cnt, &tasks);
	map<string, vector<tag_t>> task_map;
	for (int i = 0;i < sub_cnt;i++) {
		char *task_name, *task_type;
		AOM_ask_value_string(tasks[i], "object_name", &task_name);
		AOM_ask_value_string(tasks[i], "object_type", &task_type);
		vector<tag_t> tags;
		if (tc_strcmp("EPMDoTask", task_type) == 0) {
			tag_t assignee;

			AOM_ask_value_tag(tasks[i], "fnd0Assignee", &assignee);
			tags.push_back(assignee);
		} else {
			tag_t *signoffs;
			int signoff_cnt;
			AOM_ask_value_tags(tasks[i], "valid_signoffs", &signoff_cnt, &signoffs);
			printf("name:%s valid cnt:%d\n", task_name, signoff_cnt);
			for (int j = 0;j < signoff_cnt;j++) {
				tag_t member;
				AOM_ask_value_tag(signoffs[j], "group_member", &member);
				tags.push_back(member);
			}
		}
		task_map[task_name] = tags;
	}

	for (auto i = 0;i < process_vec.size();i++) {

		ITKCALL(AOM_refresh(process_vec[i], false));

		tag_t root;
		EPM_ask_root_task(process_vec[i], &root);


		EPM_ask_sub_tasks(root, &sub_cnt, &tasks);
		for (auto j = 0;j < sub_cnt;j++) {
			char *task_name, *task_type;
			AOM_ask_value_string(tasks[j], "object_name", &task_name);
			if (task_map[task_name].size() == 0)
				continue;
			AOM_ask_value_string(tasks[j], "object_type", &task_type);
			if (tc_strcmp("EPMDoTask", task_type) == 0) {
				EPM_assign_responsible_party(tasks[j], task_map[task_name][0]);
			} else {

				tag_t sub_task;
				int *types = (int*)MEM_alloc(task_map[task_name].size());

				EPM_ask_sub_task(tasks[j], "select-signoff-team", &sub_task);
				if (sub_task) {
					for (int k = 0;k < task_map[task_name].size();k++) {
						int temp_cnt;
						tag_t *temp;
						EPM_create_adhoc_signoff(sub_task, task_map[task_name][k], &temp_cnt, &temp);

					}
					EPM_set_adhoc_signoff_selection_done(sub_task, true);

					if (j == 0) {
						EPM_trigger_action(tasks[0], EPM_complete_action, "");
					}
				}
			}
		}
	}

	printf("开始将引用转换为目标\n");
	int ref_cnt;
	tag_t *refs;
	tag_t root_task1;
	for (int i = 0;i < att_count;i++) {
		EPM_ask_root_task(process_vec[i], &root_task1);
		EPM_ask_attachments(root_task1, EPM_reference_attachment, &ref_cnt, &refs);
		char *name1;
		AOM_ask_value_string(attachments[i], "object_string", &name1);
		for (int ii = 0;ii < ref_cnt; ii++) {
			char *name2;
			AOM_ask_value_string(refs[ii], "object_string", &name2);
			if (tc_strcmp(name1, name2) == 0) {
				printf("object_string======>%s\n", name1);
				tag_t *new_atts = (tag_t *)MEM_alloc(sizeof(tag_t));
				new_atts[0] = attachments[i];
				int types[1];
				types[0] = EPM_target_attachment;
				int types1[1];
				types1[0] = EPM_reference_attachment;
				ITKCALL(EPM_add_attachments(root_task1, 1, new_atts, types));
				EPM_remove_attachments_from_att_type(root_task1, 1, new_atts, types1);
				break;
			}
		}
	}

	EMH_clear_errors();
	char *uid;
	ITK__convert_tag_to_uid(job, &uid);
	printf("start delete job_uid======>%s\n", uid);
	HANDLE thread = CreateThread(NULL, 0, sendRequest, uid, 0, NULL);
	CloseHandle(thread);
	return 0;
}