#include <epm/epm.h>
#include <string>
#include <map>
#include "epm_handler_common.h"
#include <tccore/aom.h>
#include <tc/preferences.h>
#include <tccore/item.h>
#include <tccore/aom_prop.h>
#include "CRUL_server_call_httpserver.h"
#include "cJSON.h"

tag_t getCcpFolder(tag_t projectItem) {
	tag_t *folders;
	int num = 0;
	tag_t ccpFolder = NULLTAG;
	AOM_ask_value_tags(projectItem, "IMAN_reference", &num, &folders);
	for (int i = 0; i < num; i++) {
		char *folderName;
		AOM_ask_value_string(folders[i], "object_name", &folderName);
		if (strcmp(folderName, "项目启动") == 0) {
			int num2 = 0;
			tag_t *folders2;
			AOM_ask_value_tags(folders[i], "contents", &num2, &folders2);
			for (int j = 0; j < num2; j++){
				char *folderName2;
				AOM_ask_value_string(folders2[j], "object_name", &folderName2);
				if (strcmp(folderName2, "产成品") == 0) {
					ccpFolder = folders2[j];
				}
			}
			break;
		}
	}
	return ccpFolder;
}

char* askDateVal(tag_t new_rev_tag,char *propName) { //"zt2_DeliveryDate"
	date_t deliveryDate;
	AOM_ask_value_date(new_rev_tag, propName, &deliveryDate);
	char date[128] = "";
	sprintf_s(date, "%04d%02d%02d", deliveryDate.year, deliveryDate.month + 1, deliveryDate.day);
	return date;
}
/*
	添加项目的属性到JSON
*/
void addStringJson(cJSON *paramValue, char *propName, tag_t project, char*piName) {
	char *propVal;
	AOM_UIF_ask_value(project, propName, &propVal);
	cJSON_AddStringToObject(paramValue, piName, propVal);
}

int CHINT_SavePost(METHOD_message_t* msg, va_list args) {

	tag_t new_rev_tag = va_arg(args, tag_t);
	//ZT2_ProjectItem
	tag_t userTag;
	char *objType, *item_id, *zt2_ProjectNo, *object_name, *zt2_WBSNo, *userId;
	AOM_ask_value_string(new_rev_tag, "object_type", &objType);
	if (strcmp(objType, "ZT2_ProjectItem") == 0) {
		
		int num = 0, taskNum = 0;
		tag_t *ccpTags, *ztTasks;
		AOM_ask_value_string(new_rev_tag, "item_id", &item_id);
		AOM_ask_value_string(new_rev_tag, "zt2_ProjectNo", &zt2_ProjectNo);
		AOM_ask_value_string(new_rev_tag, "object_name", &object_name);
		AOM_ask_value_string(new_rev_tag, WBSNO, &zt2_WBSNo);
		AOM_ask_value_tag(new_rev_tag, "owning_user", &userTag);
		POM_ask_user_id(userTag, &userId);
		char *date = askDateVal(new_rev_tag,"zt2_DeliveryDate");//"zt2_DeliveryDate"
		printf("item_id===>%s \n", item_id);
		cJSON* paramValue = cJSON_CreateObject();
		//新增属性 
		cJSON_AddStringToObject(paramValue, "tcid", item_id);
		cJSON_AddStringToObject(paramValue, "objectName", object_name);
		cJSON_AddStringToObject(paramValue, "owningUser", userId);
		cJSON_AddStringToObject(paramValue, "zt2WBSNo", zt2_WBSNo);
		cJSON_AddStringToObject(paramValue, "zt2ProjectNo", zt2_ProjectNo);
		cJSON_AddStringToObject(paramValue, "zt2DeliveryDate", date);
		addStringJson(paramValue, "zt2_ShortName", new_rev_tag, "zt2ShortName");
		addStringJson(paramValue, "zt2_MaterialNo", new_rev_tag, "ztMaterialNo");
		addStringJson(paramValue, "zt2_ProductModel", new_rev_tag, "zt2ProductModel");
		addStringJson(paramValue, "zt2_ProjectCode", new_rev_tag, "zt2ProjectCode");
		addStringJson(paramValue, "zt2_ProjectPhase", new_rev_tag, "zt2ProjectPhase");
		addStringJson(paramValue, "zt2_DrawingNo", new_rev_tag, "zt2DrawingNo");
		addStringJson(paramValue, "zt2_ProjectPhase", new_rev_tag, "zt2ProjectPhase");
		addStringJson(paramValue, "zt2_model", new_rev_tag, "zt2model");

		addStringJson(paramValue, "zt2_Code", new_rev_tag, "zt2Code");
		addStringJson(paramValue, "zt2_Numberofunits", new_rev_tag, "zt2Numberofunits");
		addStringJson(paramValue, "zt2_Productionscheduling", new_rev_tag, "zt2Productionscheduling");
		addStringJson(paramValue, "zt2_agreementsigned", new_rev_tag, "zt2agreementsigned");
		addStringJson(paramValue, "zt2_classification", new_rev_tag, "zt2classification");
		addStringJson(paramValue, "zt2_assessor", new_rev_tag, "zt2assessor");
		addStringJson(paramValue, "zt2_Productcategory", new_rev_tag, "zt2Productcategory");
		addStringJson(paramValue, "zt2_BElectromagnetic", new_rev_tag, "zt2BElectromagnetic");

		addStringJson(paramValue, "zt2_StructuralSupervisor", new_rev_tag, "zt2StructuralSupervisor");
		addStringJson(paramValue, "zt2_schedule", new_rev_tag, "zt2schedule");
		addStringJson(paramValue, "zt2_month", new_rev_tag, "zt2month");
		addStringJson(paramValue, "zt2_notes", new_rev_tag, "zt2notes");
		tag_t ccpFolder = getCcpFolder(new_rev_tag);
		AOM_ask_value_tags(ccpFolder, "contents", &num, &ccpTags);
		cJSON* ccpValues = cJSON_CreateArray();
		//产成品信息
		for (int i = 0; i < num; i++) {
			tag_t ccpRev;
			char *ccpName, *ccpWbs, *ccpMatnrNo, *ccpQty;
			cJSON* ccpValue = cJSON_CreateObject();
			ITEM_ask_latest_rev(ccpTags[i], &ccpRev);
			AOM_ask_value_string(ccpRev, "zt2_Quantity", &ccpQty);
			AOM_ask_value_string(ccpRev, "zt2_MaterialNo", &ccpMatnrNo);
			AOM_ask_value_string(ccpRev, "object_name", &ccpName);
			AOM_ask_value_string(ccpRev, "zt2_WBSNo", &ccpWbs);
			cJSON_AddStringToObject(ccpValue, "objectname", ccpName);
			cJSON_AddStringToObject(ccpValue, "ztMaterialNo", ccpMatnrNo);
			string qtyBase = ccpQty;
			if (strcmp(ccpQty, "") == 0) {
				qtyBase = "1";
			}
			cJSON_AddStringToObject(ccpValue, "ztQuantity", qtyBase.c_str());
			cJSON_AddStringToObject(ccpValue, "ztWBSNo", ccpWbs);
			cJSON_AddStringToObject(ccpValue, "ztFactory", "M060");
			cJSON_AddItemToArray(ccpValues, ccpValue);
		}
		//APS信息
		cJSON_AddItemToObject(paramValue, "zt2productinformation", ccpValues);
		AOM_ask_value_tags(new_rev_tag, "zt2_Tasks", &taskNum, &ztTasks);
		cJSON* taskValues = cJSON_CreateArray();
		for (int i = 0; i < taskNum; i++) {
			cJSON* taskValue = cJSON_CreateObject();
			char *activeName, *activeNo;
			AOM_ask_value_string(ztTasks[i], "zt2_activeName", &activeName);
			AOM_ask_value_string(ztTasks[i], "zt2_activeNo", &activeNo);
			char *plansStart = askDateVal(ztTasks[i], "zt2_plansStart");
			char *plansEnd = askDateVal(ztTasks[i], "zt2_plansEnd");
			cJSON_AddStringToObject(taskValue, "ztactiveName", activeName);
			cJSON_AddStringToObject(taskValue, "ztnetWorkNo", activeNo);
			cJSON_AddStringToObject(taskValue, "ztplansStart", plansStart);
			cJSON_AddStringToObject(taskValue, "ztplansEnd", plansEnd);
			cJSON_AddItemToArray(taskValues, taskValue);
		}
		cJSON_AddItemToObject(paramValue, "zt2APSMasterPlan", taskValues);
		char *json_to_char = cJSON_PrintUnformatted(paramValue);
		printf("json_to_char===>%s\n", json_to_char);
		char* url;
		PREF_ask_char_value("CHINT_PIUrl", 0, &url);
		string ypUrl = url;
		ypUrl = ypUrl.append("/api/open/project/create");
		printf("ypUrl ==>%s\n", ypUrl.c_str());
		updatePiProject(json_to_char, ypUrl);
		//Request request = new Request.Builder().url(preference + "/api/open/project/create")
	}
	

	return ITK_ok;
}