var pwa = pwa || {};
/*
Manage user data for current login user

From user data = pwa.state.data.userlogin

*/
//--##
pwa.main = pwa.main || {};
pwa.main.tpldata = pwa.main.tpldata || {};
pwa.main.tpl = pwa.main.tpl || {};
pwa.main.ref = pwa.main.ref || {};
//Add template here to make it available after authentification
// in pwa.state.data.tpl[name]
//    pwa.state.data.tpldata[name] in the user's language by adding at the end of value _{pwa.state.data.ctx.lang}.json
pwa.main.tpl.usersettingaccount = `static/components/userprofile/usersettingaccount.mustache`;
pwa.main.tpldata.topbaruserprofile = `static/components/userprofile/topbaruserprofile`;
pwa.main.tpldata.topbaruseractivity = `static/components/userprofile/topbaruseractivity`;
pwa.main.tpldata.topbaruserLogout = `static/components/userprofile/topbaruserLogout`;
pwa.main.referential.users = 'url ... obeject/users.json';
//header check if user is allow to get this

pwa.userprofile = pwa.userprofile || {};

pwa.userprofile.settings = ( e ) => {
	console.groupCollapsed( 'load user settings' );
	logger.info( Object.keys( pwa ) )
	//data form init from pwa.state or axios.get( data from user )
	const data = pwa.state.data.userlogin;
	// add meta data object to create forms
	const meta = { "users": {} };
	pwa.state.data.app.referentials.object.users.forEach( f => {
		// genere tag
		const test = Object.keys( f )
		f.tag = " object='users' " + test.reduce( ( ac, k ) => {
			return ac + k + '="' + f[ k ] + '" ';
		} );
		// genere html field care do html after tag if not
		f.html = pwa.form.tpl[ f.tpl ].html( f )
		meta.users[ f.idfield ] = f
		//logger.info( f.idfield )
		//logger.info( f )
	} );
	pwa.state.data.app.referentials.json.usersetting.submenuitems[ 0 ].meta = meta;
	logger.info( "meta", pwa.state.data.app.referentials.json.usersetting )
	// tpl in #usersetting data in referentials json usersetting
	document.getElementById( 'maincontent' )
		.innerHTML = Mustache.render( document.getElementById( 'setting' )
			.innerHTML, pwa.state.data.app.referentials.json.usersetting );
	// load each content of tab then init form with data merged with tab info
	pwa.state.data.app.referentials.json.usersetting.submenuitems.forEach( tab => {
		document.getElementById( tab.id )
			.innerHTML = Mustache.render( document.getElementById( `${tab.id}tpl` )
				.innerHTML, tab );
		logger.info( tab.id, tab )
		// Convert each div with formfieldtype to a form field set with value if exist and listen button to run callback
		pwa.form.init( tab.id, { ...tab, ...data }, pwa.userprofile.save )
	} )
	console.groupEnd();
}
pwa.userprofile.save = ( data ) => {
	logger.info( "data to save", data )
}
pwa.userprofile.activities = () => {
	console.group( 'load user activity' );
}
