<div class="row mt-2 mb-4">
	<div class="col">
		<h3>Similar BGCs</h3>
		<div class="row mt-4">
			<div class="col">
				<table id="table_homologs" class="table table-striped table-bordered table-hover nowrap">
					<thead class="thead-light">
						<tr>
							<th>BGC</th>
							<th>GCF</th>
							<th>#shared<br />features</th>
							<th>distance</th>
							<th>length (kb)</th>
							<th style="max-width: 5em;">taxon</th>
							<th>class</th>
							<th>dataset</th>
							<th>compare</th>
						</tr>
					</thead>
					<tbody class="tbody">
					</tbody>
				</table>
			</div>
		</div>
		<!-- scripts -->
		<script type="text/javascript">
			$(document).ready(function(){
				$("#table_homologs").DataTable({
					searching: false,
					ordering:  false,
					serverSide: true,
					responsive: true,
					ajax: {
						url: "/api/reports/module/query/detail/get_homologous_bgcs",
						type: "GET",
						data: {
							"bgc_id": $("#bgc_id").val(),
							"report_id": $("#report_id").val(),
							"run_id": $("#run_id").val()
						}
					},
					"columnDefs": [
						{
							"targets": 0, // bgc_name
							render: function(data, type, row, meta) {
								return "<a class='btn btn-info' href='/dataset/" + data[1] + "/bgc/" + data[0] + "/run/" + meta.settings.json.run_id + "'>" + data[2] + "</a>";
							}
						},
						{
							"targets": 1, // gcf_name
							render: function(data, type, row, meta) {
								return "<a class='btn btn-info' href='/run/" + meta.settings.json.run_id + "/gcf/" + data[0] + "'>" + data[1] + "</a>";
							}
						},
						{
							"targets": 2, // features count
							render: function(data, type, row, meta) {
								return data + " / " + meta.settings.json.total_features;
							}
						},
						{
							"targets": 5, // 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": 6, // 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, // dataset
							render: function(data, type, row, meta) {
								return "<a class='btn btn-info' href='/dataset/" + data[0] + "'>" + data[1] + "</a>";
							}
						},
						{
							"targets": 8, // view alignment
							render: function(data, type, row, meta) {
								let titles = "{";
								titles += data[0] + ": \"[" + meta.settings.json.dataset_name + "] " + meta.settings.json.bgc_name +"\"";
								if (row[3] <= meta.settings.json.threshold) {
									titles += ", " + data[1] + ": \"[" + row[7][1] + "] " + row[0][2] + " <strong>(dist: " + row[3] + ")</strong>\"";
								} else {
									titles += ", " + data[1] + ": \"[" + row[7][1] + "] " + row[0][2] + " (dist: " + row[3] + ")\"";
								}
								titles += "}";
								return "<span class='btn btn-info' onclick='javascript:openComparisonWindow(" + data[0] + ", [" + data[1] + "], " + titles + ");'>view</span>";
							}
						},
						{ className: "text-right", "targets": [2, 3, 4] },
						{ className: "text-center", "targets": [6, 7] }
					],
					"createdRow": function( row, data, dataIndex ) {
					    if (data[3] <= parseFloat($("#threshold").val())) {
					    	$(row).addClass("table-success");
					    } else {
					    	$(row).addClass("table-danger");
					    }
					},
					"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>
	</div>
</div>