#include "wsubformHTML.h"	//サブフォーム専用ヘッダファイルをインクルードする。
#include <fcgi_stdio.h>

extern char gridtitles[MAX_POS][MAX_LEN];	//wsubformHTMLが利用するgridのタイトル。

extern TextBox	 *begin;	//textboxで使用する連鎖ポインタの開始位置
extern TextBox	 *tp;	//textboxで使用する連鎖ポインタ

typedef struct stat Dir;	//ファイル操作用
static Dir dirptr;
static Dir  *fptr = &dirptr;

char tmp[MAX_STR];
static int sqlcode = 0;	//PostgreSQLの返すコード

int DBcount[MAX_FORM];	//wsubformHTMLで利用するDBの現在の最大行数
extern char KEY[256][MAX_STR];	//INSERTとUPDATEが使用する更新項目
char COMMANDNAME[128];

int DBConnect(void)
{
	// DB接続用変数宣言です。DB名、ユーザ名、パスワードを設定してください。
	EXEC SQL BEGIN DECLARE SECTION;
		char dbname[12];
		char user[12];
		char passwd[12];
	EXEC SQL END DECLARE SECTION;

	strcpy(dbname,"apsch");
	strcpy(user,"cms");
	strcpy(passwd,"cms");

#ifdef DEBUG
	// ECPGデバッグ文出力
	ECPGdebug (0, stderr);
#endif

	// 接続
	memset (sqlca.sqlstate, '0', 5);
	EXEC SQL CONNECT TO :dbname AS dbConnection user :user USING :passwd;
	if (memcmp (sqlca.sqlstate, "00000", 5) != 0)
	{
		sprintf(tmp,"CONNECT エラー。終了します。:%d - %s",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
	//	logmsg(tmp);
		exit (EXIT_FAILURE);
	}
}

int	otodoke_Update2(int no)
{
	static char DBbuff[MAX_STR];
	int i = 0;
	int j = 0;
	char *qs;
	int qcount;
	char *poststr;	//POSTメソッド用のポインタ
	char *ps;
	int len;
	int n;
	char *env1,*env2;

	DBConnect();

	memset (sqlca.sqlstate, '0', 5);

	EXEC SQL BEGIN DECLARE SECTION;
		int rec_count;
		char	key[256][MAX_STR];
		int	 ikey[256];
		int	No;	//TEXT
		char	yubinNo[7 + 1];	//TEXT
		char	todofuken[24 + 1];	//TEXT
		char	jusho1[96 + 1];	//TEXT
		char	jusho2[96 + 1];	//TEXT
		char	shimei1[96 + 1];	//TEXT
		char	hinmei[90 + 1];	//TEXT
		int		hinsyu;		//NUMERIC
		char	yoryo[5 + 1];	//TEXT
		char	hm_kubun[2 + 1];	//TEXT
		char	grade[144 + 1];	//TEXT
		char	size[10 + 1];	//TEXT
		int		kingaku;		//NUMERIC
		int		takuhai_s;		//NUMERIC
		char	expDATE[10 + 1];	//TEXT
		char	hopeDATE[10 + 1];	//TEXT
		char	hassoCD[5 + 1];	//TEXT
		unsigned char		hatuEND;		//BOOL
		int		denNUM;		//NUMERIC
		int		hakoNUM;		//NUMERIC
		unsigned char		reserve;		//BOOL
		char	resDATE[10 + 1];	//TEXT
		unsigned char		print;		//BOOL
		int		prNUM;		//NUMERIC
		char	daitaiCD[15 + 1];	//TEXT
		unsigned char		daitai;		//BOOL
		int		hopeTIME2;		//NUMERIC
		unsigned char		reitou;		//BOOL
		int		den_hakoNUM;		//NUMERIC
	EXEC SQL END DECLARE SECTION;

	sqlcode = 0;

	if((qs = getenv("REQUEST_METHOD")) != (char *)NULL)
	{
		if(!strcmp(qs,"GET"))
		{
			if((qs = getenv("QUERY_STRING")) != (char *)NULL)
			{
				len = strlen(qs);
				qcount = query_postHTML(qs,'&',len);
			//	qcount = query_getHTML(qs,'&');
				sf_tbegin();
				for(i=0;i < qcount;i++)
				{
					while(tp->stype == 0 && tp->next != (TextBox *)NULL)
					{
						sf_tnext();
					}
					if(strlen(decodeUTF(qt.get[i])) > tp->len)
					{
						strncpy(key[i],decodeUTF(qt.get[i]),tp->len);
					}
					else
					{
						sprintf(key[i],"%s",decodeUTF(qt.get[i]));
					}
//logmsg(key[i]);
//sprintf(tmp,"tp->len=%d",tp->len);
//logmsg(tmp);
					sf_tnext();
				}
			}
		}
		else if(!strcmp(qs,"POST"))
		{
			if((ps = getenv("CONTENT_LENGTH")) != (char *)NULL)
			{
				len = atoi(ps);
				poststr = malloc(len+1);
				n = fread(poststr,1,len,stdin);
//FireFoxの場合だけクエリーストリングの順序が狂うためうまいかない。
//logmsg(poststr);
sprintf(KEY[29],"%c%c%c%c%c",poststr[len-5],poststr[len-4],poststr[len-3],poststr[len-2],poststr[len-1]);
KEY[29][5] = '\0';
//logmsg(KEY[29]);
poststr[len - 10] = '\0';
//logmsg(poststr);
len = len -  10;
				qcount = query_postHTML(poststr,'&',len);
			//	qcount = query_getHTML(poststr,'&');
				sf_tbegin();
				for(i=0,j=0;i < qcount;i++)
				{
					while(tp->stype == 0 && tp->next != (TextBox *)NULL && j < 28)
					{
						sf_tnext();
						j++;
					}
					if(strlen(decodeUTF(qt.get[i])) > tp->len)
					{
						strncpy(key[i],decodeUTF(qt.get[i]),tp->len);
//logmsg(key[i]);
					}
					else
					{
						sprintf(key[i],"%s",decodeUTF(qt.get[i]));
//logmsg(key[i]);
//sprintf(tmp,"qcount=%d,i=%d",qcount,i);
//logmsg(tmp);
					}
					sf_tnext();
				}
			}
		}
	}
	else
	{
		sprintf(key[0],"NONE");
		printf("%s<br>\n",key[0]);
	}

//logmsg("1111");
	for(i=0;i<qcount;i++)
		sprintf(KEY[i],"%s",key[i]);
	sf_tgetDBallHTML(0);
	ikey[0] = atoi(key[0]);
//logmsg("2222");

	// アップデート
	// テーブル項目をsplitbDBbuff[]からDB配列にセットする
	sprintf(DBbuff, "%s",key[0]);
	sf_assputDB(no,0,DBbuff,5 + 1);
	No = sf_to_integer(DBbuff);	//文字(char)をNUMERIC型(int)に変換する。
	sprintf(yubinNo, "%s",key[1]);
	sf_assputDB(no,1,yubinNo,7 + 1);
	sprintf(todofuken, "%s",key[2]);
	sf_assputDB(no,2,todofuken,24 + 1);
	sprintf(jusho1, "%s",key[3]);
	sf_assputDB(no,3,jusho1,96 + 1);
	sprintf(jusho2, "%s",key[4]);
	sf_assputDB(no,4,jusho2,96 + 1);
	sprintf(shimei1, "%s",key[5]);
	sf_assputDB(no,5,shimei1,96 + 1);
	sprintf(hinmei, "%s",key[6]);
	sf_assputDB(no,6,hinmei,90 + 1);
	sprintf(DBbuff, "%s",key[7]);
	sf_assputDB(no,7,DBbuff,3 + 1);
	hinsyu = sf_to_integer(DBbuff);	//文字(char)をNUMERIC型(int)に変換する。
	sprintf(yoryo, "%s",key[8]);
	sf_assputDB(no,8,yoryo,5 + 1);
	sprintf(hm_kubun, "%s",key[9]);
	sf_assputDB(no,9,hm_kubun,2 + 1);
	sprintf(grade, "%s",key[10]);
	sf_assputDB(no,10,grade,144 + 1);
	sprintf(size, "%s",key[11]);
	sf_assputDB(no,11,size,10 + 1);
	sprintf(DBbuff, "%s",key[12]);
	sf_assputDB(no,12,DBbuff,5 + 1);
	kingaku = sf_to_integer(DBbuff);	//文字(char)をNUMERIC型(int)に変換する。
	sprintf(DBbuff, "%s",key[13]);
	sf_assputDB(no,13,DBbuff,3 + 1);
	takuhai_s = sf_to_integer(DBbuff);	//文字(char)をNUMERIC型(int)に変換する。
	sprintf(expDATE, "%s",key[14]);
	sf_assputDB(no,14,expDATE,10 + 1);
	sprintf(hopeDATE, "%s",key[15]);
	sf_assputDB(no,15,hopeDATE,10 + 1);
	sprintf(hassoCD, "%s",key[16]);
	sf_assputDB(no,16,hassoCD,5 + 1);
	sprintf(DBbuff, "%s",key[17]);
	sf_assputDB(no,17,DBbuff,2 + 1);
	hatuEND = (unsigned char)DBbuff[0];	//文字(char)をBOOL型(unsigned char)に入れ替える。
	sprintf(DBbuff, "%s",key[18]);
	sf_assputDB(no,18,DBbuff,3 + 1);
	denNUM = sf_to_integer(DBbuff);	//文字(char)をNUMERIC型(int)に変換する。
	sprintf(DBbuff, "%s",key[19]);
	sf_assputDB(no,19,DBbuff,3 + 1);
	hakoNUM = sf_to_integer(DBbuff);	//文字(char)をNUMERIC型(int)に変換する。
	sprintf(DBbuff, "%s",key[20]);
	sf_assputDB(no,20,DBbuff,2 + 1);
	reserve = (unsigned char)DBbuff[0];	//文字(char)をBOOL型(unsigned char)に入れ替える。
	sprintf(resDATE, "%s",key[21]);
	sf_assputDB(no,21,resDATE,10 + 1);
	sprintf(DBbuff, "%s",key[22]);
	sf_assputDB(no,22,DBbuff,2 + 1);
	print = (unsigned char)DBbuff[0];	//文字(char)をBOOL型(unsigned char)に入れ替える。
	sprintf(DBbuff, "%s",key[23]);
	sf_assputDB(no,23,DBbuff,3 + 1);
	prNUM = sf_to_integer(DBbuff);	//文字(char)をNUMERIC型(int)に変換する。
	sprintf(daitaiCD, "%s",key[24]);
	sf_assputDB(no,24,daitaiCD,15 + 1);
	sprintf(DBbuff, "%s",key[25]);
	sf_assputDB(no,25,DBbuff,2 + 1);
	daitai = (unsigned char)DBbuff[0];	//文字(char)をBOOL型(unsigned char)に入れ替える。
	sprintf(DBbuff, "%s",key[26]);
	sf_assputDB(no,26,DBbuff,2 + 1);
	hopeTIME2 = sf_to_integer(DBbuff);	//文字(char)をNUMERIC型(int)に変換する。
	sprintf(DBbuff, "%s",key[27]);
	sf_assputDB(no,27,DBbuff,2 + 1);
	reitou = (unsigned char)DBbuff[0];	//文字(char)をBOOL型(unsigned char)に入れ替える。
	sprintf(DBbuff, "%s",key[28]);
	sf_assputDB(no,28,DBbuff,3 + 1);
	den_hakoNUM = sf_to_integer(DBbuff);	//文字(char)をNUMERIC型(int)に変換する。

	// 対象レコードをUPDATE
	EXEC SQL UPDATE otodoke
	SET
		No =	:No,
		yubinNo =	coalesce(:yubinNo,' ','0'),
		todofuken =	coalesce(:todofuken,' ','0'),
		jusho1 =	coalesce(:jusho1,' ','0'),
		jusho2 =	coalesce(:jusho2,' ','0'),
		shimei1 =	coalesce(:shimei1,' ','0'),
		hinmei =	coalesce(:hinmei,' ','0'),
		hinsyu = 	:hinsyu,
		yoryo =	coalesce(:yoryo,' ','0'),
		hm_kubun =	coalesce(:hm_kubun,' ','0'),
		grade =	coalesce(:grade,' ','0'),
		size =	coalesce(:size,' ','0'),
		kingaku = 	:kingaku,
		takuhai_s = 	:takuhai_s,
		expDATE =	coalesce(:expDATE,' ','0'),
		hopeDATE =	coalesce(:hopeDATE,' ','0'),
		hassoCD =	coalesce(:hassoCD,' ','0'),
		hatuEND = 	:hatuEND,
		denNUM = 	:denNUM,
		hakoNUM = 	:hakoNUM,
		reserve = 	:reserve,
		resDATE =	coalesce(:resDATE,' ','0'),
		print = 	:print,
		prNUM = 	:prNUM,
		daitaiCD =	coalesce(:daitaiCD,' ','0'),
		daitai = 	:daitai,
		hopeTIME2 = 	:hopeTIME2,
		reitou = 	:reitou,
		den_hakoNUM = 	:den_hakoNUM
	WHERE hassoCD = :key[16] AND No = :ikey[0];

	if (memcmp (sqlca.sqlstate, "00000", 5) != 0)
	{
	// 更新エラー
		// ロールバック
		env1 = getenv("REMOTE_ADDR");
		env2 = getenv("HTTP_USER_AGENT");
		sprintf(tmp,"%s->UPDATEエラー:%d - %s:%s:%s",COMMANDNAME,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc,env1,env2);
		logmsg(tmp);
		printf("<br>%s\n",tmp);
		EXEC SQL ROLLBACK WORK;
		EXEC SQL END;
		return(-1);
	}

	//コミットする
	EXEC SQL COMMIT;
	EXEC SQL END;


	// 結果チェック
	if (sqlca.sqlcode == 0)
	{
		env1 = getenv("REMOTE_ADDR");
		env2 = getenv("HTTP_USER_AGENT");
		sprintf(tmp,"%s->UPDATE結果チェック:%d - %s:%s:%s",COMMANDNAME,sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc,env1,env2);
		logmsg(tmp);
		printf("<br>%s\n",tmp);
	}

	// 切断
	sqlca.sqlcode = 0;
	EXEC SQL DISCONNECT dbConnection;
	return(0);
}

// DBから入出力(画面)ウインドウに書き込みます。
void otodoke_Update2_text_writeHTML(int no, int post, int line,int maxcol, char **db_titles)
{
	int i;
	char s[MAX_TMP];

	printf("Content-type: text/html\n\n");
	printf("<html>\n");
	printf("<head>\n");
	printf("<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n"); 
	printf("<title>form text_writeHTML page</title>\n");
	printf("</head>\n");
	printf("<body>\n");
	printf("<br>\n");
	printf("<table border=0>\n");
	printf("<tr bgcolor=#ffffff>\n");
	printf("<a href=\"http://codoo2hk.corede.net/takuhai/takuhai.html\" target=\"_parent\">[TOPへ]</a>\n");
	printf("<a href=\"#\" onClick=\"history.back(); return false;\">[前に戻る]</a>\n");
	printf("<form method=\"post\"  action=\"/cgi-bin/kokyaku_Fetch_Fine.fcgi?field00=%s&amp;field01=#form%s\">\n",KEY[16],KEY[29]);
	printf("<input type=\"submit\" value=\"UPDATEが完了しました。戻ります\">\n");
// 以下は別の _Fetch.cgi を呼び出す場合の転送文字列を指定します。KEY[]の文字列はユニークが必要です。
	printf("<input type=\"hidden\" name=\"field00\" size=\"1\" value=\"%s\" maxlength=\"1\" ><br>\n",KEY[26]);
	printf("</tr>");
	printf("</form>\n");
}

int subformHTML(void)
{
	int i;
	char s[MAX_STR];

	int startline;

	int line;
	setlocale(LC_ALL,"");

//------------------------------------------------------------------------
//下ののハイフンのラインまでの以下の関数はDBと画面の設計を実際のフォームに適用します。
//これらの関数が無い場合は設計情報はDB設計以外は反映されません。

	static int xcol = 242;
	static int rows = 73;

//for label
	static int label_length[] = {0};
	static int label_no[] = {0};
	static int label_align[] = {0};
	static char *label_text[1][128] = {""};
	static int label_x[] = {0};
	static int label_y[] = {0};
//for textbox
	static int textbox_length[] = {5,7,24,96,96,96,192,96,200,13,90,5,5,2,144,10,15,3,3,3,13,3,10,10,10,256,5,7,3,3,5,5,1024,2,10,2,5,15,2,13,13,3,2,3,3,15,6,15,60,50,15,15,2,32,13,3,15,15,15,3,5,15,0};
	static int textbox_no[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,0};
	static int textbox_align[] = {2,2,2,2,2,2,2,2,2,2,1,2,2,2,2,2,1,2,1,2,2,2,2,2,2,2,2,2,2,2,1,1,2,2,2,2,1,2,2,2,2,1,1,1,2,1,1,1,2,2,1,1,2,2,2,2,1,1,1,2,1,1,0};
	static int textbox_action[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	static int textbox_skipstep[] = {1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,1,0,0,0,0,1,1,0,1,0,1,0,1,1,0,1,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0};
	static int textbox_field[] = {2,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,4,3,2,3,1,3,1,1,1,1,1,1,3,3,2,2,1,3,1,3,2,1,3,1,1,1,1,1,3,4,4,4,1,1,4,4,3,1,1,3,4,4,4,3,2,4,0};
	static int textbox_x[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	static int textbox_y[] = {6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,0};


//for grid
	static int cell_length[] = {2,2,2,2,2,2,3,3,3,3,3,2,2,2,2,2,5,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,2,3,2,3,2,2,3,3,3,2,3,3,4,4,4,3,3,4,4,2,3,3,3,4,4,4,3,3,4,0};
	static int cell_align[] = {2,2,2,2,2,2,2,2,2,2,1,2,2,2,2,2,1,2,1,2,2,2,2,2,2,2,2,2,2,2,1,1,2,2,2,2,1,2,2,2,2,1,1,1,2,1,1,1,2,2,1,1,2,2,2,2,1,1,1,2,1,1,0};
	static int cell_action[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	static int grid_field[] = {2,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,4,3,2,3,1,3,1,1,1,1,1,1,3,3,2,2,1,3,1,3,2,1,3,1,1,2,2,2,3,4,4,4,1,1,4,4,3,1,1,3,4,4,4,3,2,4,0};
	static int grid_skipstep[] = {1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,1,0,1,0,0,0,0,1,1,0,1,0,1,0,1,1,0,1,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0};
	char *grid_titles[63][128] = {"Grid_1","Grid_2","Grid_3","Grid_4","Grid_5","Grid_6","Grid_7","Grid_8","Grid_9","Grid_10","Grid_11","Grid_12","Grid_13","Grid_14","Grid_15","Grid_16","Grid_17","Grid_18","Grid_19","Grid_20","Grid_21","Grid_22","Grid_23","Grid_24","Grid_25","Grid_26","Grid_27","Grid_28","Grid_29","Grid_30","Grid_31","Grid_32","Grid_33","Grid_34","Grid_35","Grid_36","Grid_37","Grid_38","Grid_39","Grid_40","Grid_41","Grid_42","Grid_43","Grid_44","Grid_45","Grid_46","Grid_47","Grid_48","Grid_49","Grid_50","Grid_51","Grid_52","Grid_53","Grid_54","Grid_55","Grid_56","Grid_57","Grid_58","Grid_59","Grid_60","Grid_61","Grid_62",""};
	static char *db_titles[63][128] = {"No","yubinNo","todofuken","jusho1","jusho2","shimei1","shimei_f1","shimei2","shimei_f2","TEL","hinmei","hinsyu","yoryo","hm_kubun","grade","size","kingaku","jikankibou","takuhai_s","tuchi","denNo","tokui","uketukeDATE","expDATE","hopeDATE","bikou","hassoCD","hatutenCD","hatuEND","prEND","denNUM","hakoNUM","memo","reserve","resDATE","print","prNUM","daitaiCD","daitai","FAX","keitaiTEL","hopeTIME","hopeTIME2","hopeTIME3","umu","soryo","hatu_kosuu","hakodai","chitai","mail","tesuu","tac","reitou","YahooID","shiwakeCD","kariseikyu","cc_kingaku","st_kingaku","base_soryo","seikyuEND","den_hakoNUM","soukin_tesuuryo",0};
	static int grid_sx = 0;
	static int grid_ex = 239;
	static int grid_sy = 4;
	static int grid_ey = 4;

	static int maxtext;
	static int no = 0;

	char **gt;
	char **lt;

	gt = (char **)&grid_titles;

	xcol = MakeWsubFormHTML(no,xcol,rows,0,2,grid_sx,grid_sy,grid_ex,grid_ey,cell_length,cell_align,cell_action,grid_field,grid_skipstep,gt,62);

	lt = (char **)&label_text;

//	以下の関数は<SELECT>でコンボボックスを作成する場合、一つのCGIで重複して利用出来ませんのでどちらかを削除してください。
	maxtext = MakeTextBoxHTML(no,0,2,xcol,rows,label_no,lt,label_x,label_y,label_length,textbox_no,textbox_x,textbox_y,textbox_length,textbox_align,textbox_action,textbox_field,textbox_skipstep, 62);

//------------------------------------------------------------------------


	if(otodoke_Update2(0) != -1)
	{
		otodoke_Update2_text_writeHTML(no,0,0,maxtext,*db_titles);
	}
	else
	{
		printf("Content-type: text/html\n\n");
		printf("<html>\n");
		printf("<head>\n");
		printf("<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n"); 
		printf("<title>DB-ERROR</title>\n");
		printf("</head>\n");
		printf("<body>\n");
		printf("<br>\n");
		printf("<table border=0>\n");
		printf("<tr bgcolor=#ffffff>\n");
		printf("UPDATEが失敗しました。<br>\n");
		printf("</form>\n");
		printf("</tr>\n");
		printf("</form>\n");
	}
}

int main(int argc, char *argv[])
{
	sprintf(COMMANDNAME,"%s",argv[0]);
	while (FCGI_Accept() >= 0) {
		subformHTML();
	}
}

