#include "wsubformHTML.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が使用する更新項目

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 Fetch_kokyaku_Menu(int no)
{
	static char DBbuff[MAX_STR];
	int i = 0;
	char *qs;
	int qcount;

	DBConnect();

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

	EXEC SQL BEGIN DECLARE SECTION;
		int rec_count;
		char	 key[256][MAX_STR];
		char	johoID[3 + 1];	//TEXT
		char	kokyakuCD[5 + 1];	//TEXT
		int		outaikubunID;		//NUMERIC
		char	shimei[128 + 1];	//TEXT
		char	furigana[156 + 1];	//TEXT
		char	shokugyou[12 + 1];	//TEXT
		char	kaisyamei[64 + 1];	//TEXT
		char	yakushoku[12 + 1];	//TEXT
		char	yubin[7 + 1];	//TEXT
		char	todoufuken[24 + 1];	//TEXT
		char	jusho1[64 + 1];	//TEXT
		char	jusho2[64 + 1];	//TEXT
		char	mail[50 + 1];	//TEXT
		char	kaisyaTEL[13 + 1];	//TEXT
		char	naisenTEL[13 + 1];	//TEXT
		char	FAX[13 + 1];	//TEXT
		char	keitaiTEL[13 + 1];	//TEXT
		char	jitakuTEL[13 + 1];	//TEXT
		unsigned char		insatu;		//BOOL
		unsigned char		henjinashi;		//BOOL
		unsigned char		fumei;		//BOOL
		unsigned char		mocyu;		//BOOL
		char	bikou[128 + 1];	//TEXT
		char	YahooID[32 + 1];	//TEXT
	EXEC SQL END DECLARE SECTION;

	sqlcode = 0;
	DBcount[no] = 0;

	rec_count = 0;

	if((qs = getenv("QUERY_STRING")) != (char *)NULL)
	{
		qcount = query_getHTML(qs,'&');
		for(i=0;i < qcount;i++)
		{
			sprintf(key[i],"%s",qt.get[i]);
		}
	}
	else
	{
		sprintf(key[0],"NONE");
		printf("%s<br>\n",key[0]);
	}

//	以下の部分はリレーションテーブルを利用する場合にコメントを外して使用可能にして利用してください。
//	sprintf(KEY[0],"%s",key[0]);

	// DBの行数を取得
	EXEC SQL 
	SELECT 
	COUNT(*)
	INTO :rec_count
	FROM	kokyaku
	WHERE kokyakuCD = :key[0];

	// SELECTエラー 
	if(memcmp (sqlca.sqlstate, "00000", 5) != 0)
	{
		sprintf(tmp,"SELECT COUNTエラー:%d - %s",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
	//	logmsg(tmp);
		return(-1) ;
	}

	DBcount[no] = rec_count;

	EXEC SQL DECLARE kokyaku_Menu_CURSOR CURSOR FOR 
	SELECT 
		coalesce(johoID,' ','0'),
		coalesce(kokyakuCD,' ','0'),
		outaikubunID,
		coalesce(shimei,' ','0'),
		coalesce(furigana,' ','0'),
		coalesce(shokugyou,' ','0'),
		coalesce(kaisyamei,' ','0'),
		coalesce(yakushoku,' ','0'),
		coalesce(yubin,' ','0'),
		coalesce(todoufuken,' ','0'),
		coalesce(jusho1,' ','0'),
		coalesce(jusho2,' ','0'),
		coalesce(mail,' ','0'),
		coalesce(kaisyaTEL,' ','0'),
		coalesce(naisenTEL,' ','0'),
		coalesce(FAX,' ','0'),
		coalesce(keitaiTEL,' ','0'),
		coalesce(jitakuTEL,' ','0'),
		insatu,
		henjinashi,
		fumei,
		mocyu,
		coalesce(bikou,' ','0'),
		coalesce(YahooID,' ','0')
	FROM	kokyaku
	WHERE kokyakuCD = :key[0]
	ORDER BY kokyakuCD;

	// カーソルオープン
	EXEC SQL OPEN kokyaku_Menu_CURSOR;

	// CURSOR OPENエラー 
	if (memcmp (sqlca.sqlstate, "00000", 5) != 0)
	{
		sprintf(tmp,"CURSOR OPENエラー:%d - %s",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
	//	logmsg(tmp);
		return(-1);
	}

	for(i=0;i<DBcount[no];i++)
	{
		EXEC SQL FETCH kokyaku_Menu_CURSOR INTO
			:johoID,
			:kokyakuCD,
			:outaikubunID,
			:shimei,
			:furigana,
			:shokugyou,
			:kaisyamei,
			:yakushoku,
			:yubin,
			:todoufuken,
			:jusho1,
			:jusho2,
			:mail,
			:kaisyaTEL,
			:naisenTEL,
			:FAX,
			:keitaiTEL,
			:jitakuTEL,
			:insatu,
			:henjinashi,
			:fumei,
			:mocyu,
			:bikou,
			:YahooID;

	// テーブル項目をsplitbDBbuff[][]にセットする
		sf_assgetDB(no,i,0,johoID,3 + 1);
		sf_assgetDB(no,i,1,kokyakuCD,5 + 1);
		sf_to_text(outaikubunID,DBbuff);	//NUMERIC型(int)を文字に変換する。
		sf_assgetDB(no,i,2,DBbuff,5 + 1);
		sf_assgetDB(no,i,3,shimei,128 + 1);
		sf_assgetDB(no,i,4,furigana,156 + 1);
		sf_assgetDB(no,i,5,shokugyou,12 + 1);
		sf_assgetDB(no,i,6,kaisyamei,64 + 1);
		sf_assgetDB(no,i,7,yakushoku,12 + 1);
		sf_assgetDB(no,i,8,yubin,7 + 1);
		sf_assgetDB(no,i,9,todoufuken,24 + 1);
		sf_assgetDB(no,i,10,jusho1,64 + 1);
		sf_assgetDB(no,i,11,jusho2,64 + 1);
		sf_assgetDB(no,i,12,mail,50 + 1);
		sf_assgetDB(no,i,13,kaisyaTEL,13 + 1);
		sf_assgetDB(no,i,14,naisenTEL,13 + 1);
		sf_assgetDB(no,i,15,FAX,13 + 1);
		sf_assgetDB(no,i,16,keitaiTEL,13 + 1);
		sf_assgetDB(no,i,17,jitakuTEL,13 + 1);
		sprintf(DBbuff,"%c",insatu);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,18,DBbuff,2 + 1);
		sprintf(DBbuff,"%c",henjinashi);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,19,DBbuff,2 + 1);
		sprintf(DBbuff,"%c",fumei);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,20,DBbuff,2 + 1);
		sprintf(DBbuff,"%c",mocyu);	//BOOL型(unsigned char)を文字に変換する。
		sf_assgetDB(no,i,21,DBbuff,2 + 1);
		sf_assgetDB(no,i,22,bikou,128 + 1);
		sf_assgetDB(no,i,23,YahooID,32 + 1);

	}
	// カーソルクローズ 
	EXEC SQL CLOSE kokyaku_Menu_CURSOR;

	DBcount[no] = rec_count;

	// 結果チェック
	if (sqlca.sqlcode == 0)
	{
		sprintf(tmp,"FETCH結果チェック:%d - %s",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
	//	logmsg(tmp);
	}
	// 切断
	sqlca.sqlcode = 0;
	EXEC SQL DISCONNECT dbConnection;
	return(0);
}

// DBから入出力(画面)ウインドウに書き込みます。
void kokyaku_Menu_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 Menu_text_writeHTML page</title>\n");
	printf("</head>\n");
	printf("<body>\n");
	printf("<br>\n");
	//送信はGETを使用します。
	printf("<form method=\"get\" accept-charset=\"utf-8\" action=\"/cgi-bin/kokyaku.cgi\">\n");
	printf("<select name=\"field0\" size=\"8\" maxlength=\"64\"><br>\n");
	for(i=0;i<DBcount[no];i++)
	{
		printf("<option value=\"%s\"> %s\n",ft[post].splitDBbuff[i][1],ft[post].splitDBbuff[i][1]);
	}
	printf("</select>\n");
	printf("<td><input type=\"submit\" name=\"field\" value=\"%s\"></td>",KEY[1]);
	printf("</form>\n");
}

// FETCHした後にHTMLのテーブルにDBの行列を書き込みます。
int kokyaku_Menu_grid_dispHTML(int no, int startline, int maxcol, int grid_ey, int grid_sy)
{
	int i, j;
	char s[MAX_TMP];
	char hs[MAX_TMP];

	if((Fetch_kokyaku_Menu(no)) == 0)
	{
		for(i=startline;i<DBcount[no];i++)
		{
			for(j=0;j<maxcol;j++)
			{
				sf_gputDBHTML(no,i,j,startline,s);
			}
		}

		return(0);
	}
	else
	{
		return(-1);
	}
}


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

	int startline;

	int line;
	setlocale(LC_ALL,"");

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

	static int xcol = 174;
	static int rows = 47;

//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,5,5,64,64,12,64,12,7,20,64,64,50,13,13,13,13,13,2,2,2,2,128,32,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,0};
	static int textbox_align[] = {2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0};
	static int textbox_action[] = {1,1,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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0};
	static int textbox_field[] = {1,1,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,1,1,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};
	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,0};


//for grid
	static int cell_length[] = {5,5,5,4,4,4,4,4,4,7,8,8,6,11,9,8,7,7,2,2,2,2,9,20,0};
	static int cell_align[] = {2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,0};
	static int cell_action[] = {1,1,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[] = {1,1,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,1,1,0};
	static int grid_skipstep[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0};
	char *grid_titles[25][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",""};
	static char *db_titles[25][128] = {"johoID","kokyakuCD","outaikubunID","shimei","furigana","shokugyou","kaisyamei","yakushoku","yubin","todoufuken","jusho1","jusho2","mail","kaisyaTEL","naisenTEL","FAX","keitaiTEL","jitakuTEL","insatu","henjinashi","fumei","mocyu","bikou","YahooID",0};
	static int grid_sx = 0;
	static int grid_ex = 169;
	static int grid_sy = 3;
	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,24);

	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, 24);

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

	for(i=0;i<MAX_POS;i++)
	{
		memset(gridtitles[i],'\0',MAX_LEN);
	}

	sf_startcell(0);

	startline = 0;	//画面初期行

	if(kokyaku_Menu_grid_dispHTML(no,startline,maxtext,grid_sy,grid_ey) == -1)	//*splitDBbuff[][]を表示する。
	{
		printf("grid_dispHTML():error\n");
	}

	//DBへのメニュー用フォームの作成
	kokyaku_Menu_text_writeHTML(no,0,i,maxtext,*db_titles);
}

int main(void)
{
	subformHTML();
}

