#include <stdarg.h>
#include <base_utils\Mem.h>
#include <tccore\grm.h>
#include <tccore\grmtype.h>
#include <tc\iman.h>
#include <user_exits\user_exits.h>
#include <tccore\method.h>
#include <property\prop.h>
#include <tccore\aom_prop.h>
#include <property\prop_msg.h>
#include <tccore\imantype.h>
#include <property\prop_errors.h>
#include <tccore\item.h>
#include <lov\lov.h>
#include <sa\sa.h>
#include <tc\emh.h>
#include <res\reservation.h>
#include <tccore\workspaceobject.h>
#include <tc\wsouif_errors.h>
#include <tccore\aom.h>
#include <form\form.h>
#include <bom\bom.h>
#include <epm\cr.h>
#include <epm\releasestatus.h>


/**
* @headerfile			standard c & cpp header files
*/
#include  <stdio.h>
#include  <stdlib.h>
#include <time.h>
#include <string>
#include <vector>
#include <map>
#include <fstream>
#include <iostream>
using namespace std;

/**
* @headerfile			user's header files
*/

#include "epm_handler_common.h"
#include "error_handling.h"
#include "common_itk_util.h"
#include "string_helper.h"


#define SAFECALL(x)                       \
{                                         \
	err_function = #x; err_line = __LINE__; \
	if((rcode = (x)) != ITK_ok)             \
	{                                       \
		goto CLEANUP;                         \
	}                                       \
}
void va_copy( va_list *dest , va_list * src ){
	*dest = *src ;
}
static int SMP_ask_bomline_value( METHOD_message_t *  m, va_list  args )
{
	int rcode = ITK_ok;
	char *err_string,*err_function;
	int err_line = -1;
	tag_t objTag = NULLTAG;
	const char * prop_name;
	char** value = NULL;
	char *bom_rev_status = NULL;
	char sQty[] = "";
	char sUnRead[] = "UNREADABLE";
	va_list largs;
	tag_t bom_line_item = NULLTAG;
	tag_t bom_line_rev = NULLTAG;
	tag_t last_rev = NULLTAG;
	tag_t iman_type_tag = NULLTAG ;
	char obj_class_name[TCTYPE_class_name_size_c+1];
	char obj_type_name[TCTYPE_name_size_c+1];
	int attr_bom_item = 0;
	int attr_bom_rev = 0;
	int attr_bom_rev_status = 0;
	char item_rev_id[ITEM_id_size_c+1]="",item_id[ITEM_id_size_c+1]="",rev_id[ITEM_id_size_c+1]="",
		strRow[BUFSIZ]="",txtfile[BUFSIZ]="", obj_type[WSO_object_type_size_c+1]="";
	tag_t	master_form_rel_type = NULLTAG;
	int form_count = 0, attr_qty_id = 0;
	tag_t	*form_list=NULL, master_form = NULLTAG;
	char *d_value=NULL, *qty_value = NULL;

	METHOD_PROP_MESSAGE_OBJECT(m, objTag)  /* objTag is the tag of the property's owning object */
	METHOD_PROP_MESSAGE_PROP_NAME(m, prop_name) /* prop_name is the name of the current property */
	printf("\n 属性 name : %s\n",prop_name);

	va_copy( &largs, &args );
	va_arg( largs, tag_t );  /* ignore the first tag_t arg */
	value = va_arg( largs, char**);
	va_end( largs );
	//获取目标对象的类型
	ITKCALL( TCTYPE_ask_object_type( objTag,&iman_type_tag ) );
	ITKCALL( TCTYPE_ask_class_name(iman_type_tag,obj_class_name));//get ClassName of attachment
	ITKCALL( TCTYPE_ask_name(iman_type_tag,obj_type_name) );	
	printf("\n Type name : %s\n",obj_type_name);	
	TC_write_syslog("\n Type name : %s\n",obj_type_name);
	//if ( strcmp( obj_type_name , "Mfg0BvrPart" ) == 0 )
	//{
	//
	//}
	if ( strcmp( obj_type_name , "Mfg0BvrPart" ) == 0 )
	//if ( strcmp( obj_type_name , "BOMLine" ) == 0 )
	{
		printf("\n is Mfg0BvrPart\n");
		SAFECALL( BOM_line_look_up_attribute( bomAttr_lineItemTag ,	& attr_bom_item	) );
		SAFECALL( BOM_line_look_up_attribute( bomAttr_lineItemRevTag ,	& attr_bom_rev	) );
		SAFECALL( BOM_line_look_up_attribute( bomAttr_occQty ,	& attr_qty_id	) );
		SAFECALL( BOM_line_ask_attribute_tag( objTag,	attr_bom_item ,	&bom_line_item	));
		SAFECALL( BOM_line_ask_attribute_tag( objTag,	attr_bom_rev ,	&bom_line_rev	));
		SAFECALL( BOM_line_ask_attribute_string( objTag, attr_qty_id, &qty_value ));
		SAFECALL( WSOM_ask_object_type(bom_line_item, obj_type ));
		printf("\n obj_type=%s",obj_type);
		TC_write_syslog("\n obj_type=%s",obj_type);
		//if( strcmp(obj_type, "Mfg0BvrPart") == 0)
		if( strcmp(obj_type, "C7Part") == 0)
		{
			SAFECALL( GRM_find_relation_type( TC_master_form_rtype, &master_form_rel_type ) );
			SAFECALL( GRM_list_secondary_objects_only(bom_line_rev, master_form_rel_type, &form_count, &form_list ));
			master_form = form_list[0];
			if( master_form != NULLTAG )
			{
				SAFECALL(AOM_UIF_ask_value(master_form, "c7GeneralClfion", &d_value));
				printf("\n d_value=%s\n",d_value);
				TC_write_syslog("\n d_value=%s\n",d_value);
				//二期修改增加标识
				if( strcmp(d_value, "C") == 0 
					|| strcmp(d_value, "F") == 0 
					|| strcmp(d_value, "F1") == 0 
					|| strcmp(d_value, "F2") == 0
					|| strcmp(d_value, "F3") == 0 )
				{
					strcpy(sQty,"");
				}
				else
				{
					if( strcmp(qty_value,"") == 0)
					{
						strcpy(sQty,"1");
					}
					else
						strcpy(sQty,qty_value);
				}
				MEM_free(d_value);
			}
			MEM_free(form_list);
		}
		
	}
CLEANUP :
	
	if( rcode != ITK_ok)
	{
		if ( 515001 == rcode || err_line == 80 ){
			if ( *value != NULL ) MEM_free( *value );
			*value = NULL;
			*value = (char*)MEM_alloc ( 10 + 1 );
			strcpy ( *value, "1" );
			EMH_clear_errors( );
			rcode = ITK_ok;
		}else
		{
			EMH_ask_error_text (rcode, &err_string);
			printf ("ERROR: %d ERROR MSG: %s.\n", rcode, err_string);
			printf ("Function: %s FILE: %s LINE: %d\n", err_function, __FILE__, err_line);
			MEM_free (err_string);
		}
		TC_write_syslog("\n rcode != ITK_ok");
	}else{
		if ( *value != NULL ) MEM_free( *value );
		*value = NULL ;
		*value = (char*)MEM_alloc ( strlen ( sQty ) + 1 );
		strcpy ( *value, sQty );
		TC_write_syslog("\n else sQty=%s",sQty);
	}

	return rcode;
}

int SMP_init_user_bomline_props(METHOD_message_t* m, va_list args)
{
	int                 rcode           = ITK_ok;
	char *err_string,*err_function;
	int err_line = -1;
	tag_t               type_tag;
	tag_t               pd_tag          = NULLTAG;
	METHOD_id_t         method;
	char                typename_cpp[TCTYPE_name_size_c+1];
	tag_t               lov_tag;
	tag_t               tmp_lov_tag;
	
	
	va_list largs;
	va_copy( &largs, &args );
	type_tag = va_arg( largs, tag_t );
	va_end( largs );
	
	ITKCALL( TCTYPE_ask_name( type_tag, typename_cpp ) );
	
	/* ====== add properties for Bomline class types ====== */
	/* ---- add superseded runtime property ----  */
	ITKCALL( TCTYPE_add_runtime_property( type_tag, "JK8SupplyType",PROP_string,1,&pd_tag ) );
	
	/* --- set display name...can also be done in user_property_names.uil --- */
	ITKCALL( PROPDESC_set_display_name(pd_tag,"JK8SupplyType") );

	/* --- register method to ask value --- */
	ITKCALL( METHOD_register_prop_method( (const char*)typename_cpp, "JK8SupplyType", PROP_ask_value_string_msg, SMP_ask_bomline_value , 0 , &method ) );	
	printf("\n METHOD_register_prop_method");
	/* --- register method to set value --- */
	//SAFECALL( PROPDESC_set_protection( pd_tag, PROP_write ) );
	//SAFECALL( METHOD_register_prop_method( (const char*)typename, "superseded", PROP_set_value_string_msg, SMP_set_bomline_superseded , 0 , &method ) );	

CLEANUP:

	if( rcode != ITK_ok)
	{
		EMH_ask_error_text (rcode, &err_string);
		printf ("ERROR: %d ERROR MSG: %s.\n", rcode, err_string);
		printf ("Function: %s FILE: %s LINE: %d\n", err_function, __FILE__, err_line);
		MEM_free (err_string);
	}
	
	return( ITK_ok );

}


extern int Run_Time_register_properties( void )
{
	int rcode = ITK_ok;
	char *err_string,*err_function;
	int err_line = -1;
	
	USER_prop_init_entry_t user_types_methods[]= { { "BOMLine" , SMP_init_user_bomline_props , NULL } };

	int n_types = sizeof(user_types_methods)/sizeof(USER_prop_init_entry_t);
	
	SAFECALL( TCTYPE_register_properties(user_types_methods, n_types ));
	printf("\n TCTYPE_register_properties\n");
	TC_write_syslog("\n TCTYPE_register_properties\n");
CLEANUP:	

	if( rcode != ITK_ok)
	{
		EMH_ask_error_text (rcode, &err_string);
		printf ("ERROR: %d ERROR MSG: %s.\n", rcode, err_string);
		printf ("Function:  FILE: %s LINE: %d\n", __FILE__, err_line);
		MEM_free (err_string);
	}
	
	return rcode;
}
