<div class="row mt-2 mb-4">
	<div class="col">
		<h3>Browse</h3>
		<div class="row mt-4">
			<div class="col">
				<table id="table_bgcs" class="table table-striped table-bordered table-hover nowrap">
					<thead class="thead-light">
						<tr>
							<th>genome</th>
							<th>BGC</th>
							<th>taxon</th>
							<th>class</th>
							<th>length (kb)</th>
							<th>#genes</th>
							<th>completeness</th>
							<th>details</th>
						</tr>
					</thead>
					<tbody class="tbody">
					</tbody>
				</table>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
	$(document).ready(function(){
		$("#table_bgcs").DataTable({
			searching: false,
			ordering:  false,
			serverSide: true,
						responsive: true,
			ajax: {
				url: "/api/dataset/get_bgc_table",
				type: "GET",
				data: {
					"dataset_id": $("#dataset_id").val()
				}
			},
			"columnDefs": [
				{
					"targets": 2, // taxonomy
					render: function(data, type, row, meta) {
						let html_text = "";
						let taxon_desc = meta.settings.json.taxon_desc;
						for (let index = taxon_desc.length - 1; index >= 0; index--) {
							let value = taxon_desc[index];
							if (data.hasOwnProperty(value[0])) {
								html_text += "<i>" + data[value[0]] + "</i> (" + value[1] + ")";
								break;
							}
						}

						let popover_content = "<div>";
						taxon_desc.forEach(function(value, index) {
							if (data.hasOwnProperty(value[0])) {
								popover_content += "<div class=\"row\"><div class=\"col-4\"><strong>" + value[1] + "</strong></div><div class=\"col-8 text-right\"><i>" + data[value[0]] + "</i></div></div>";
							} else {
								popover_content += "<div class=\"row\"><div class=\"col-4\"><strong>" + value[1] + "</strong></div><div class=\"col-8 text-right\">n/a</div></div>";
							}
						});
						popover_content += "</ul>";

						if (html_text.length < 1) {
							html_text = "n/a";
						} else {
							html_text += "<a href='javascript:void(0)' class='taxonomy_pops badge badge-info badge-pill ml-2' data-trigger='hover' data-html='true' data-content='" + popover_content + "' title='Taxonomy'>?</a>"
						}
						return html_text;
					}
				},
				{
					"targets": 3, // chem_class
					render: function(data, type, row, meta) {
						let html_text = "<ul class='list-unstyled'>";
						data.forEach(function(value, index) {
							html_text += "<li>" + value[0] + ":" + value[1] + "</li>";
						});
						html_text += "</ul>"
						return html_text;
					}
				},
				{
					"targets": 7, // details
					render: function(data, type, row, meta) {
						return "<a href='/dataset/" + meta.settings.json.dataset_id + "/bgc/" + data + "'><button type='button' class='btn btn-sm btn-info'>view</button></a>";
					}
				},
			{ className: "text-right", "targets": [4, 5] },
			{ className: "text-center", "targets": [6, 7] }
			],
			"drawCallback": function( settings ) {
		        $(".taxonomy_pops").popover({
		        	"template": '<div class="popover popover-taxonomy" role="tooltip"><div class="arrow"></div><h3 class="popover-header"></h3><div class="popover-body"></div></div>'
		        });
		    }
		});
	});
</script>