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

			//まずはカレントのDBをクリアする。
			_disp.dbId = null;
			//続いて、サーバから、DB一覧を取得する。
			_sysData._load("db",null, 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("dbmng_rollback").style.opacity = "1";
			_D.g("dbmng_rollback").disabled = false;
			_D.g("dbmng_commit").style.opacity = "1";
			_D.g("dbmng_commit").disabled = false;

			changeEnableforEachCall( _D.gN("_dbmng_btn_open_") );
			changeEnableforEachCall( _D.gN("_dbmng_btn_export_") );
		};
		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 impDB = function(){
			var reqValues = {};
			reqValues.DB = _D.g("dbmng_file_imp").files[0];
			_disp.request( "php/" + _urlMap.action.impDb , reqValues , init );
		};
		var expTarg = null;
		var expDb = function(){
			_prcName_="EXPORT";
			var reqValues = {};
			reqValues.id = expTarg;
			_disp.request( "php/" + _urlMap.action.expDb , reqValues , init );
		};
		var commit = function( afterImp ){
			if( ! afterImp ){
				_prcName_="COMMIT CHANGE";
			};
			var reqValues = {};
			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["name["+i+"]"] = _D.fEC( tr.children[1] ).value;
			};
			var next = init;
			if( afterImp ){
				next = impDB;
			};
			_disp.request( "php/" + _urlMap.action.chgDb , reqValues , next );
		};
		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.dbId = _D.par(_D.par(this)).dataset.id;
				_disp.dipsCtrl( _disp.id.TLst );
			},
			dbnameOnChange:function(){
				changeEnable();
			},
			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( "dbmng_1" , [ _D.fEC( _D.prE( _D.par(this) ) ).value ] , expDb );
				
			},
			deleteOnClick:function(){
				var tr = _D.par(_D.par(this));
				_disp.showMessageEz( "dbmng_0" , [ _D.fEC( _D.prE( _D.par(this) ) ).value ] , function(){ 
					changeEnable();
					deleteTr(tr);}
				);
			},
			addOnClick:function(){
				addTr();
				changeEnable();
			},
			importOnClick:function(){
				var fileName = "";
				if( _D.prE(this).value != "" ){
					fileName = _D.prE(this).files[ 0 ].name;
				};


				if( unCommitChange ){
					_disp.showMessageEz( "dbmng_2" , [ fileName ] , function(){
						setProcImp();
						commit( true );
					} );
				}else{
					_disp.showMessageEz( "dbmng_3" , [ fileName  ] , function(){ 
						setProcImp();
						impDB( );
					} );
				};
			},
			importFileOnChange:function(){
				if( this.value ){
					_D.g("dbmng_imp").style.opacity = "1";
					_D.g("dbmng_imp").disabled = false;
				}else{
					_D.g("dbmng_imp").style.opacity = "0.2";
					_D.g("dbmng_imp").disabled = true;
				};
			}
		}

		var nodeDef ={
			mainInner :{attr:{
				tagname:"div",
				className:"_vh_sc_auto_ _dbmng_"
			},children:[
				{attr:{
					tagname:"h1",
					innerHTML:"Data Base Manage",
					className:"_titles_"
				},children:[
					{attr:{
						tagname:"input",
						type:"button",
						value:"ROLLBACK CHANGES",
						id:"dbmng_rollback",
						disabled:true,
						onclick:eventFuncs.rollbackOnClick,
						className:"_btn_y_ _btn_in_title_"
					}},{attr:{
						tagname:"input",
						type:"button",
						value:"COMMIT CHANGES",
						id:"dbmng_commit",
						disabled:true,
						onclick:eventFuncs.commitOnClick,
						className:"_btn_y_ _btn_in_title_"
					}}
				]},{attr:{
					tagname:"table",
					id:"tbl_dbmng"
				},children:[
					{attr:{
						tagname:"thead",
						id:"thead_dbmng"
					},children:[
						{attr:{
							tagname:"tr",
							id:"tr_dbmng_head"
						},children:[
							{attr:{
								tagname:"th",
								id:"th_dbmng_ope"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"Operation"
								}}
							]},{attr:{
								tagname:"th",
								id:"th_dbmng_dbname"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"DataBaseName"
								}}
							]},{attr:{
								tagname:"th",
								id:"th_dbmng_act"
							},children:[
								{attr:{
									tagname:"span",
									innerHTML:"manage action"
								}}
							]}
						]}
					]},{attr:{
						tagname:"tbody",
						id:"tbody_dbmng"
					}},{attr:{
						tagname:"tfoot",
						id:"tfoot_dbmng"
					},children:[
						{attr:{
							tagname:"tr",
							id:"tr_dbmng_foot"
						},children:[
							{attr:{
								tagname:"td",
								className:"_td_dmy_"
							}},{attr:{
								tagname:"td",
								colSpan:2
							},children:[
								{attr:{
									tagname:"input",
									type:"button",
									value:"add",
									id:"dbmng_add",
									onclick:eventFuncs.addOnClick,
									className:"_btn_adjust_"
								}}
							]}
						]}
					]}
				]},{attr:{
					tagname:"h1",
					innerHTML:"Data Base Import",
					className:"_titles_"
				}},{attr:{
					tagname:"div",
					id:"dbmng_div_inp"
				},children:[

					{attr:{
						tagname:"input",
						type:"file",
						onchange:eventFuncs.importFileOnChange,
						id:"dbmng_file_imp"
					}},
					{attr:{
						tagname:"input",
						type:"button",
						value:"import",
						id:"dbmng_imp",
						disabled:true,
						onclick:eventFuncs.importOnClick,
						className : "_btn_y_"
					}}

				]},{attr:{
					tagname:"h1",
					innerHTML:"Export Files",
					className:"_titles_"
				}},{attr:{
					tagname:"ul",
					id:"dbmng_ul_exp"
				}}
			]},
			tableTr :{attr:{
				tagname:"tr",
				className:"_dbmng_tr_body_"
			},children:[
				{attr:{
					tagname:"td",
					className:"_dbmng_td_ope_"
				},children:[
					{attr:{
						tagname:"input",
						type:"button",
						value:"OPEN",
						onclick:eventFuncs.openOnClick,
						className:"_dbmng_btn_open_",
						name:"_dbmng_btn_open_"
					}}
				]},{attr:{
					tagname:"td",
					className:"_dbmng_td_dbname_"
				},children:[
					{attr:{
						tagname:"input",
						type:"text",
						onchange:eventFuncs.dbnameOnChange,
						className:"_dbmng_txt_dbname_"
					}}
				]},{attr:{
					tagname:"td",
					className:"_dbmng_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:"_dbmng_btn_export_",
						className:"_dbmng_btn_export_"
					}},
					{attr:{
						tagname:"input",
						onclick:eventFuncs.deleteOnClick,
						type:"button",
						value:"delete",
						className:"_dbmng_btn_del_ _btn_y_"
					}}
				]}
			]},
			expLi :{attr:{
						tagname:"li",
						className:"_dbmng_li_exp_"
			},children:[
				{attr:{
						tagname:"a",
						className:"_dbmng_a_exp_"
				}}
			]}

		};

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

			main.innerHTML = "";

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

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

			//tbodyにDB毎の要素を作成する。
			tbody = _D.g("tbody_dbmng");
			var dbTable = _sysData._getData("db");
			for( var i = 0 ; _U.isIdx( i , dbTable ) ; i ++ ){
				var tr = _D.aCAndCN( tbody , nodeDef.tableTr );
				tr.dataset.id=dbTable[i].id;
				_D.fEC( tr.children[ 1 ] ).value = dbTable[i].name;
			};

			ul = _D.g("dbmng_ul_exp");
			var aryUrl = _sysData._getData("expDb" , _sysData._getMode.expDB );
			for( var i = 0 ; _U.isIdx( i , aryUrl ) ; i ++ ){
				var str = "";
				var url = aryUrl[i];
				var wkA = url.slice( 0 , -3 ).split("_");
				for( var j = 0 ; _U.isIdx( j , dbTable ) ; j ++ ){
					if( dbTable[ j ].id == wkA[2] ){
						str += dbTable[ j ].name;
						break;
					};
				};
				if( ! _U.isIdx( j , dbTable ) ){
					str += "<Unknown>";
				};

				if( wkA[0] == "DEL"){
					str +=" Deleted"
				}else{
					str +=" Exported"
				};
				str += wkA[1].slice(0,4) + "/" + wkA[1].slice(4,6) + "/" +wkA[1].slice(6,8) + " " +
					wkA[1].slice(8,10) + ":" + wkA[1].slice(10,12) + ":" +wkA[1].slice(12,14) ;


				var li = _D.aCAndCN( ul , nodeDef.expLi );
				var a = _D.fEC( li );
				a.href = "exp_db/"+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 );
		};
	};
	constructorF.prototype = _protoEachDisp;
	_eachDisp.DBMng = new constructorF();
}();
delete _;
