
/*以下、表管理画面の表示オブジェクト*/
var _ =function(){
	var constructorF = function(){
		var _this = this;
		var unCommitChange = false;
		var init = function(){
			unCommitChange = false;

			//まずはカレントのDBをクリアする。
			_disp.tblId = null;
			//続いて、サーバから、DB一覧を取得する。
			_sysData._load("tbl",{ db_id : _this.dbId }, loadExpAndDisp )

		};
		var changeEnableforEachCall = function( nodeList ){
			Array.prototype.forEach.call( nodeList , function(elm){
				elm.disabled = true;
				elm.style.opacity = "0.2";
			});
		};
		var changeEnable = function(){
			unCommitChange = true;

			_D.g("tmng_rollback").style.opacity = "1";
			_D.g("tmng_rollback").disabled = false;
			_D.g("tmng_commit").style.opacity = "1";
			_D.g("tmng_commit").disabled = false;

			changeEnableforEachCall( _D.gN("_tmng_btn_open_") );
			changeEnableforEachCall( _D.gN("_tmng_btn_export_") );
			changeEnableforEachCall( _D.gN("_tmng_file_imp_") );
			changeEnableforEachCall( _D.gN("_tmng_btn_imp_") );
		};
		var moveTr = function( elmTr , isUp ){
			var targElm = null;
			//iA,iBともに、targElm!=nullである必要がある。
			if( isUp ){
				targElm = _D.prE( elmTr );
				if( targElm ){
					_D.iB( elmTr , targElm );
				}else{
					_D.aC( _D.par( elmTr ) , elmTr );
				};
			}else{
				targElm = _D.nxE( elmTr );
				if( targElm ){
					_D.iA( elmTr , targElm );
				}else{
					_D.iB( elmTr , _D.fEC( _D.par( elmTr ) ) );
				};
			};
		};
		var deleteTr = function( elmTr ){
			_D.rC( elmTr );
		};
		var setProcImp=function(){_prcName_="IMPORT"};
		var impTarg = null;
		var impTargId = null;
		var impDB = function(){
			var reqValues = { db_id : _this.dbId };
			reqValues.CSV = impTarg;
			reqValues.id = impTargId;
			_disp.request( "php/" + _urlMap.action.impTbl , reqValues , init );
		};
		var expTarg = null;
		var expDb = function(){
			_prcName_="EXPORT";
			var reqValues = { db_id : _this.dbId };
			reqValues.id = expTarg;
			_disp.request( "php/" + _urlMap.action.expTbl , reqValues , init );
		};
		var commit = function(  ){
			_prcName_="COMMIT CHANGE";
			var reqValues = { db_id : _this.dbId };
			for( var i = 0 ; _U.isIdx( i , tbody.children ) ; i ++ ){
				var tr = tbody.children[i];
				if( tr.dataset && tr.dataset.id ){
					reqValues["id["+i+"]"] = tr.dataset.id;
				}else{
					reqValues["id["+i+"]"] = "";
				};
				reqValues["table_name["+i+"]"] = _D.fEC( tr.children[1] ).value;
			};
			var next = init;
			_disp.request( "php/" + _urlMap.action.chgTbl , reqValues , next );
		};
		var validateTname = function( elm ){
			var strTname = elm.value;
			var errN,errD,errC,errS;

			if( strTname == "" ){
				errN = true;
			}else{
				errS = ( strTname.charAt( 0 ) == "_" );
				errC = (
					strTname.indexOf( "]" ) != -1 ||
					strTname.indexOf( "[" ) != -1 ||
					strTname.indexOf( "\'" ) != -1 ||
					strTname.indexOf( "\"" ) != -1
				);

			};
			if( ! errN && ! errS && ! errC ){
				var aryTname = _D.gN("_tmng_txt_tname_");
				for( var i = 0 ; _U.isIdx( i , aryTname ) ; i ++ ){
					if( aryTname[ i ] == elm ) continue;
					if( aryTname[ i ].value == elm.value ){
						errD = true;
						break;
					};
					
				};
			};

			if( ! errN && ! errS && ! errC && ! errD ){
				if( elm.value != elm.dataset.org ){
					changeEnable();
				};
				return;
			};

			var msg = _msg.getMessage( "tblmng_W0" );
			var mainMsgObj =  msg.main ;
			var msgN = msg.sub.n;
			var msgC = msg.sub.c;
			var msgS = msg.sub.nm;
			var msgD = msg.sub.d;
			if( errN ){
				mainMsgObj.discription = msgN;
			}else if( errD ){
				mainMsgObj.discription = msgD;
			}else{
				if( errS ){
					mainMsgObj.discription = msgS;
				};
				if( errS && errC ){
					mainMsgObj.discription += "\r\n";
				};
				if( errC ){
					mainMsgObj.discription += msgC;
				};
			};

			_disp.showMessage( mainMsgObj , function(){ elm.focus(); } );
		};
		var eventFuncs = {
			rollbackOnClick:function(){
				_disp.showMessageEz( "cmmn_101" , null , function(){_prcName_="ROLL BACK"; init();} );
			},
			commitOnClick:function(){
				_disp.showMessageEz( "cmmn_100" , null , commit );
			},
			openOnClick:function(){
				_disp.tblId = _D.par(_D.par(this)).dataset.id;
				_disp.dipsCtrl( _disp.id.CMng );
			},
			tnameOnChange:function(){
				validateTname( this );
			},
			upOnClick:function(){
				changeEnable();
				moveTr( _D.par(_D.par(this)) , true );
			},
			downOnClick:function(){
				changeEnable();
				moveTr( _D.par(_D.par(this)) );
			},
			exportOnClick:function(){
				expTarg = _D.par(_D.par(this) ).dataset.id;
				_disp.showMessageEz( "tblmng_1" , [ _D.fEC( _D.prE( _D.par(this) ) ).value ] , expDb );
				
			},
			deleteOnClick:function(){
				var tr = _D.par(_D.par(this));
				_disp.showMessageEz( "tblmng_0" , [ _D.fEC( _D.prE( _D.par(this) ) ).value ] , function(){ 
					changeEnable();
					deleteTr(tr);}
				);
			},
			addOnClick:function(){
				addTr();
				changeEnable();
			},
			importOnClick:function(){
				impTargId = _D.par( _D.par( this ) ).dataset.id;
				var fileName ="";
				if( _D.prE(this).value ){
					impTarg = _D.prE(this).files[ 0 ];
					fileName = impTarg.name;
				}else{
					impTarg = null;
				};
				_disp.showMessageEz( "tblmng_2" , [ fileName ] , function(){ 
					setProcImp();
					impDB( );
				} );
			},
			importFileOnChange:function(){
				if( this.value ){
					_D.nxE( this ).style.opacity = "1";
					_D.nxE( this ).disabled = false;

				}else{
					_D.nxE( this ).style.opacity = "0.2";
					_D.nxE( this ).disabled = true;
				};
			}
		}

		var nodeDef ={
			mainInner :{attr:{
				tagname:"div",
				className:"_vh_sc_auto_ _tmng_"
			},children:[
				{attr:{
					tagname:"h1",
					innerHTML:"Table Manage",
					className:"_titles_"
				},children:[
					{attr:{
						tagname:"input",
						type:"button",
						value:"ROLLBACK CHANGES",
						id:"tmng_rollback",
						disabled:true,
						onclick:eventFuncs.rollbackOnClick,
						className:"_btn_y_ _btn_in_title_"
					}},{attr:{
						tagname:"input",
						type:"button",
						value:"COMMIT CHANGES",
						id:"tmng_commit",
						disabled:true,
						onclick:eventFuncs.commitOnClick,
						className:"_btn_y_ _btn_in_title_"
					}}
				]},{attr:{
					tagname:"table",
					id:"tbl_tmng"
				},children:[
					{attr:{
						tagname:"thead",
						id:"thead_tmng"
					},children:[
						{attr:{
							tagname:"tr",
							id:"tr_tmng_head"
						},children:[
							{attr:{
								tagname:"th",
								id:"th_tmng_ope"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"Operation"
								}}
							]},{attr:{
								tagname:"th",
								id:"th_tmng_tname"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"Table Name"
								}}
							]},{attr:{
								tagname:"th",
								id:"th_tmng_act"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"manage action"
								}}
							]}
						]}
					]},{attr:{
						tagname:"tbody",
						id:"tbody_tmng"
					}},{attr:{
						tagname:"tfoot",
						id:"tfoot_tmng"
					},children:[
						{attr:{
							tagname:"tr",
							id:"tr_tmng_foot"
						},children:[
							{attr:{
								tagname:"td",
								className:"_td_dmy_"
							}},{attr:{
								tagname:"td",
								colSpan:2
							},children:[
								{attr:{
									tagname:"input",
									type:"button",
									value:"add",
									id:"tmng_add",
									onclick:eventFuncs.addOnClick,
									className:"_btn_adjust_"
								}}
							]}
						]}
					]}
				]},{attr:{
					tagname:"h1",
					innerHTML:"Export Files",
					className:"_titles_"
				}},{attr:{
					tagname:"ul",
					id:"tmng_ul_exp"
				}}
			]},
			tableTr :{attr:{
				tagname:"tr",
				className:"_tmng_tr_body_"
			},children:[
				{attr:{
					tagname:"td",
					className:"_tmng_td_ope_"
				},children:[
					{attr:{
						tagname:"button",
						type:"button",
						innerHTML:"Column<br />Manage",
						onclick:eventFuncs.openOnClick,
						className:"_tmng_btn_open_ _btn_adjust_",
						name:"_tmng_btn_open_"
					}}
				]},{attr:{
					tagname:"td",
					className:"_tmng_td_tname_"
				},children:[
					{attr:{
						tagname:"input",
						type:"text",
						onblur:eventFuncs.tnameOnChange,
						className:"_tmng_txt_tname_",
						name:"_tmng_txt_tname_"
					}}
				]},{attr:{
					tagname:"td",
					className:"_tmng_td_act_"
				},children:[
					{attr:{
						tagname:"input",
						type:"button",
						onclick:eventFuncs.upOnClick,
						value:"up"
					}},
					{attr:{
						tagname:"input",
						onclick:eventFuncs.downOnClick,
						type:"button",
						value:"down"
					}},
					{attr:{
						tagname:"input",
						onclick:eventFuncs.exportOnClick,
						type:"button",
						value:"export",
						name:"_tmng_btn_export_",
						className : "_tmng_btn_export_"
					}},
					{attr:{
						tagname:"input",
						onclick:eventFuncs.deleteOnClick,
						type:"button",
						value:"delete",
						className:"_tmng_btn_del_ _btn_y_"
					}},
					{attr:{
						tagname:"br"
					}},
					{attr:{
						tagname:"input",
						type:"file",
						onchange:eventFuncs.importFileOnChange,
						name:"_tmng_file_imp_",
						className:"_tmng_file_imp_"
					}},
					{attr:{
						tagname:"input",
						type:"button",
						value:"import",
						disabled:true,
						onclick:eventFuncs.importOnClick,
						name:"_tmng_btn_imp_",
						className : "_tmng_btn_imp_ _btn_y_"
					}}

				]}
			]},
			expLi :{attr:{
						tagname:"li",
						className:"_tmng_li_exp_"
			},children:[
				{attr:{
						tagname:"a",
						className:"_tmng_a_exp_",
						download:null
				}}
			]}

		};

		var main = _D.g("main");
		this.init = init;
		var tbody;
		var ul;
		var loadExpAndDisp = function( ){
			//エキスポートデータのロード
			_sysData._load("expTbl", { db_id : _this.dbId }, disp );
			
		};
		var _prcName_;
		var disp = function( ){
			//ここまででエラーがあった場合は既に画面通知済みのはず。

			main.innerHTML = "";

			//ひとまず、基本の画面要素作成
			_D.aCAndCN( main , nodeDef.mainInner );

			//デフォルトでは、コミット、ロールバック、インポートは押下不可。
			_D.g("tmng_rollback").style.opacity = "0.2";
			_D.g("tmng_commit").style.opacity = "0.2";

			//tbodyにDB毎の要素を作成する。
			tbody = _D.g("tbody_tmng");
			var tables = _sysData._getData("tbl");
			for( var i = 0 ; _U.isIdx( i , tables ) ; i ++ ){
				var tr = _D.aCAndCN( tbody , nodeDef.tableTr );

				_D.lEC( _D.lEC( tr ) ).style.opacity = "0.2";

				tr.dataset.id = tables[i].id;
				_D.fEC( tr.children[ 1 ] ).value = tables[i].name;
				_D.fEC( tr.children[ 1 ] ).dataset.org = tables[i].name;
			};

			ul = _D.g("tmng_ul_exp");
			var aryUrl = _sysData._getData("expTbl" , _sysData._getMode.expDB );
			for( var i = 0 ; _U.isIdx( i , aryUrl ) ; i ++ ){
				var str = "";
				var url = aryUrl[i];
				var wkA = url.slice( 0 , -4 ).split("_");
				if( wkA[1] != _this.dbId ){
					continue;
				}
				for( var j = 0 ; _U.isIdx( j , tables ) ; j ++ ){
					if( tables[ j ].id == wkA[2] ){
						str += tables[ j ].name;
						break;
					};
				};
				if( ! _U.isIdx( j , tables ) ){
					str += "&lt;Unknown&gt;";
				};

				str +=" Exported"

				str += wkA[0].slice(0,4) + "/" + wkA[0].slice(4,6) + "/" +wkA[0].slice(6,8) + " " +
					wkA[0].slice(8,10) + ":" + wkA[0].slice(10,12) + ":" +wkA[0].slice(12,14) ;


				var li = _D.aCAndCN( ul , nodeDef.expLi );
				var a = _D.fEC( li );
				a.href = "exp_csv/"+url;
				a.download = url;
				a.innerHTML = _U.htmlEscape( str );
			};
			if( _prcName_ ){
				procFin();
			};
		};
		var procFin = function(){
			_disp.showMessageEz( "cmmn_10" , [_prcName_] , null );
			_prcName_ = null;
		};
		var addTr = function(){
			var tr = _D.aCAndCN( tbody , nodeDef.tableTr );
			_D.lEC( _D.lEC( tr ) ).style.opacity = "0.2";
		};
	};
	constructorF.prototype = _protoEachDisp;
	_eachDisp.TMng = new constructorF();
}();
delete _;


