// app management for search request
/*
@todo  create a search/index content for an apixtribe instance
//store request to analyse kind of looking for

*/
var pwa = pwa || {};
pwa.search = {};
pwa.search.tpl = `
<div class="container-fluid p-0">
  <h1 class="h3 mb-3">Recherche pour: {{searchtxt}}</h1>
  <div class="row">
   {{#results}}
    <div class="col-12">
    <div class="card">
    	<div class="card-header">
        <p><a href="{{url}}" target="_blank">{{url}}</a> - {{format}}</p>
    		<h5 class="card-title mb-0">{{{title}}}</h5>
    		</div>
    		<div class="card-body">
         {{{desc}}}
    		</div>
        <div class="card-footer">
         {{#relatedtag}}
           <button type="button" class="btn btn-dark" >{{.}}</button>
         {{/relatedtag}}
    		</div>
    	</div>
    </div>
    {{/results}}
  </div>
</div>
`;
pwa.search.dataexample = {
	searchtxt: "search string to looking for",
	results: [ {
		url: "https://apixtribe.org",
		format: "pdf",
		title: "Search finding title of the page",
		desc: "sentence contexte of the searchtxt",
		relatedtag: [ "keyword1", "keyword2" ]
} ]
}

pwa.search.req = ( info ) => {
	if( info[ 1 ] == "?" ) {
		const urlparse = new URLSearchParams( info );
		//Then set param with urlpar.get('variable name')
		info = {
			idsearch: urlparse.get( 'idsearchtxt' )
		}
	}
	const searchtxt = document.getElementById( info )
		.value;
	logger.info( info, searchtxt );

	const req = { searchtxt };
	req.results = [ {
		url: "",
		format: "",
		title: "Nous n'avons rien à vous proposer sur ce sujet",
		desc: `<p>To find more click on <a href='https://google.com/search?q=${searchtxt.replace(/ /g,'+')}' target="_blanck"> google search </a></p> `,
		relatedtag: [ 'tag1', "tag2", "tag3" ]
} ];

	document.getElementsByTagName( 'main' )[ 0 ]
		.innerHTML = Mustache.render( pwa.search.tpl, req );
}
