<!DOCTYPE html>
<html>

<head>
  <title>Tags - AIL</title>
	<link rel="icon" href="{{ url_for('static', filename='image/ail-icon.png') }}">

  <!-- Core CSS -->
	<link href="{{ url_for('static', filename='css/bootstrap4.min.css') }}" rel="stylesheet">
	<link href="{{ url_for('static', filename='css/font-awesome.min.css') }}" rel="stylesheet">
	<link href="{{ url_for('static', filename='css/daterangepicker.min.css') }}" rel="stylesheet">
	<link href="{{ url_for('static', filename='css/dataTables.bootstrap4.min.css') }}" rel="stylesheet">
	<link href="{{ url_for('static', filename='css/tags.css') }}" rel="stylesheet" type="text/css" />

  <!-- JS -->
	<script src="{{ url_for('static', filename='js/jquery.js')}}"></script>
	<script src="{{ url_for('static', filename='js/popper.min.js')}}"></script>
	<script src="{{ url_for('static', filename='js/bootstrap4.min.js')}}"></script>
	<script src="{{ url_for('static', filename='js/jquery.dataTables.min.js')}}"></script>
	<script src="{{ url_for('static', filename='js/dataTables.bootstrap.min.js')}}"></script>
	<script language="javascript" src="{{ url_for('static', filename='js/moment.min.js') }}"></script>
	<script language="javascript" src="{{ url_for('static', filename='js/jquery.daterangepicker.min.js') }}"></script>
	<script src="{{ url_for('static', filename='js/tags.js') }}"></script>

	<style>
	.rotate{
		-moz-transition: all 0.1s linear;
		-webkit-transition: all 0.1s linear;
		transition: all 0.1s linear;
	}

	.rotate.down{
		-moz-transform:rotate(180deg);
		-webkit-transform:rotate(180deg);
		transform:rotate(180deg);
	}
	</style>

</head>
<body>

  {% include 'nav_bar.html' %}

	<!-- Modal -->
  <div id="mymodal" class="modal fade" role="dialog">
   <div class="modal-dialog modal-lg">

     <!-- Modal content-->
     <div id="mymodalcontent" class="modal-content">
       <div id="mymodalbody" class="modal-body" max-width="850px">
         <p>Loading paste information...</p>
         <img id="loading-gif-modal" src="{{url_for('static', filename='image/loading.gif') }}" height="26" width="26" style="margin: 4px;">
       </div>
       <div class="modal-footer">
         <a id="button_show_path" target="_blank" href=""><button type="button" class="btn btn-info">Show saved paste</button></a>
         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
       </div>
     </div>
   </div>
  </div>

	<div class="container-fluid">
		<div class="row">

			{% include 'tags/menu_sidebar.html' %}

			<div class="col-12 col-lg-10" id="core_content">

				<div class="card mb-3 mt-1">
					<div class="card-header text-white bg-dark">
						<h5 class="card-title">Search Tags by date range :</h5>
					</div>
					<div class="card-body">

						<div class="row mb-3">
							<div class="col-md-6">
								<div class="input-group" id="date-range-from">
									<div class="input-group-prepend"><span class="input-group-text"><i class="far fa-calendar-alt" aria-hidden="true"></i></span></div>
									<input class="form-control" id="date-range-from-input" placeholder="yyyy-mm-dd" value="{{ date_from }}" name="date_from" autocomplete="off">
								</div>
							</div>
							<div class="col-md-6">
								<div class="input-group" id="date-range-to">
									<div class="input-group-prepend"><span class="input-group-text"><i class="far fa-calendar-alt" aria-hidden="true"></i></span></div>
									<input class="form-control" id="date-range-to-input" placeholder="yyyy-mm-dd" value="{{ date_to }}" name="date_to" autocomplete="off">
								</div>
							</div>
						</div>

						<div class="input-group mb-3">
						  <div class="input-group-prepend">
								<button class="btn btn-outline-danger" type="button" id="button-clear-tags" style="z-index: 1;" onclick="emptyTags()">
									<i class="fas fa-eraser"></i>
								</button>
						  </div>
							<input id="ltags" name="ltags" type="text" class="form-control" aria-describedby="button-clear-tags" autocomplete="off">
						</div>

						<button class="btn btn-primary" type="button" id="button-search-tags" onclick="searchTags()">
							<i class="fas fa-search"></i> Search Tags
						</button>

					</div>
				</div>

				<div>
					{%if all_path%}
						<table class="table table-bordered table-hover" id="myTable_">
				      <thead class="thead-dark">
				        <tr>
									<th>Date</th>
				          <th style="max-width: 800px;">Path</th>
				          <th># of lines</th>
				          <th>Action</th>
				        </tr>
				      </thead>
				      <tbody>

				        {% for path in all_path %}
				          <tr>
										<td class="pb-0">{{ paste_date[loop.index0] }}</td>
				            <td class="pb-0"><a target="_blank" href="{{ url_for('showsavedpastes.showsavedpaste') }}?paste={{path}}" class="text-secondary">
													<div style="line-height:0.9;">{{ path }}</div>
												</a>
				              <div class="mb-2">
				                {% for tag in paste_tags[loop.index0] %}
				                <a href="{{ url_for('Tags.Tags_page') }}?ltags={{ tag[1] }}">
				                  <span class="badge badge-{{ bootstrap_label[loop.index0 % 5] }} pull-left">{{ tag[0] }}</span>
				                </a>
				                {%  endfor %}
				              </div>
				            </td class="pb-0">
				            <td class="pb-0">{{ paste_linenum[loop.index0] }}</td>
				            <td class="pb-0"><p>
											<span class="fas fa-info-circle" data-toggle="tooltip" data-placement="left" title="{{ content[loop.index0] }} "></span>
											<button type="button" class="btn btn-light" data-num="{{ loop.index0 + 1 }}" data-toggle="modal" data-target="#mymodal" data-url="{{ url_for('showsavedpastes.showsaveditem_min') }}?paste={{ path }}&num={{ loop.index0+1 }}" data-path="{{ path }}">
												<span class="fas fa-search-plus"></span>
											</button></p>
										</td>
				          </tr>
				        {%  endfor %}

				      </tbody>
				    </table>

						<div class="d-flex justify-content-center border-top mt-2">
							<nav class="mt-4" aria-label="...">
								<ul class="pagination">
									<li class="page-item {%if page==1%}disabled{%endif%}">
										<a class="page-link" href="{{ url_for('Tags.Tags_page') }}?page={{page-1}}&date_from={{date_from}}&date_to={{date_to}}&ltags={{tags}}">Previous</a>
									</li>

									{%if page>3%}
										<li class="page-item"><a class="page-link" href="{{ url_for('Tags.Tags_page') }}?page=1&date_from={{date_from}}&date_to={{date_to}}&ltags={{tags}}">1</a></li>
										<li class="page-item disabled"><a class="page-link" aria-disabled="true" href="#">...</a></li>
										<li class="page-item"><a class="page-link" href="{{ url_for('Tags.Tags_page') }}?page={{page-1}}&date_from={{date_from}}&date_to={{date_to}}&ltags={{tags}}">{{page-1}}</a></li>
										<li class="page-item active"><a class="page-link" href="{{ url_for('Tags.Tags_page') }}?page={{page}}&date_from={{date_from}}&date_to={{date_to}}&ltags={{tags}}">{{page}}</a></li>
									{%else%}
									{%if page>2%}<li class="page-item"><a class="page-link" href="{{ url_for('Tags.Tags_page') }}?page={{page-2}}&date_from={{date_from}}&date_to={{date_to}}&ltags={{tags}}">{{page-2}}</a></li>{%endif%}
									{%if page>1%}<li class="page-item"><a class="page-link" href="{{ url_for('Tags.Tags_page') }}?page={{page-1}}&date_from={{date_from}}&date_to={{date_to}}&ltags={{tags}}">{{page-1}}</a></li>{%endif%}
									<li class="page-item active"><a class="page-link" href="{{ url_for('Tags.Tags_page') }}?page={{page}}&date_from={{date_from}}&date_to={{date_to}}&ltags={{tags}}">{{page}}</a></li>
									{%endif%}

									{%if nb_page_max-page>3%}
										<li class="page-item"><a class="page-link" href="{{ url_for('Tags.Tags_page') }}?page={{page+1}}&date_from={{date_from}}&date_to={{date_to}}&ltags={{tags}}">{{page+1}}</a></li>
										<li class="page-item disabled"><a class="page-link" aria-disabled="true" href="#">...</a></li>
										<li class="page-item"><a class="page-link" href="{{ url_for('Tags.Tags_page') }}?page={{nb_page_max}}&date_from={{date_from}}&date_to={{date_to}}&ltags={{tags}}">{{nb_page_max}}</a></li>
									{%else%}
										{%if nb_page_max-page>2%}<li class="page-item"><a class="page-link" href="{{ url_for('Tags.Tags_page') }}?page={{nb_page_max-2}}&date_from={{date_from}}&date_to={{date_to}}&ltags={{tags}}">{{nb_page_max-2}}</a></li>{%endif%}
										{%if nb_page_max-page>1%}<li class="page-item"><a class="page-link" href="{{ url_for('Tags.Tags_page') }}?page={{nb_page_max-1}}&date_from={{date_from}}&date_to={{date_to}}&ltags={{tags}}">{{nb_page_max-1}}</a></li>{%endif%}
										{%if nb_page_max-page>0%}<li class="page-item"><a class="page-link" href="{{ url_for('Tags.Tags_page') }}?page={{nb_page_max}}&date_from={{date_from}}&date_to={{date_to}}&ltags={{tags}}">{{nb_page_max}}</a></li>{%endif%}
									{%endif%}

									<li class="page-item {%if page==nb_page_max%}disabled{%endif%}">
										<a class="page-link" href="{{ url_for('Tags.Tags_page') }}?page={{page+1}}&date_from={{date_from}}&date_to={{date_to}}&ltags={{tags}}" aria-disabled="true">Next</a>
									</li>
								</ul>
							</nav>
						</div>

					{%endif%}
				</div>

				<div>
					<br></br>
					<a class="btn btn-light text-secondary" href="{{ url_for('Tags.taxonomies') }}" target="_blank">
							<i class="fas fa-wrench fa-2x"></i>
							<br></br>
							<span class="label-icon">Edit Taxonomies List </span>
					</a>
					<a class="btn btn-light text-secondary" href="{{ url_for('Tags.galaxies') }}" target="_blank">
							<i class="fas fa-rocket fa-2x"></i>
							<br></br>
							<span class="label-icon">Edit Galaxies List</span>
					</a>
				</div>

				<div>
					<br></br>
					<a class="btn btn-light text-secondary" href="{{ url_for('PasteSubmit.edit_tag_export') }}" target="_blank">
							<i class="fas fa-cogs fa-2x"></i>
							<br></br>
							<span class="label-icon">MISP and Hive, auto push</span>
					</a>
				</div>

    	</div>
		</div>
	</div>

</body>

<script>
var ltags;
var search_table;
var last_clicked_paste;
var can_change_modal_content = true;

$(document).ready(function(){
	$("#nav_quick_search").removeClass("text-muted");
	$("#nav_tag_{{tag_nav}}").addClass("active");
	search_table = $('#myTable_').DataTable({ "order": [[ 0, "asc" ]] });

	// Use to bind the button with the new displayed data
  // (The bind do not happens if the dataTable is in tabs and the clicked data is in another page)
  search_table.on( 'draw.dt', function () {
    // Bind tooltip each time we draw a new page
    $('[data-toggle="tooltip"]').tooltip();
    // On click, get html content from url and update the corresponding modal
    $("[data-toggle='modal']").off('click.openmodal').on("click.openmodal", function (event) {
        get_html_and_update_modal(event, $(this));
    });
  } );

	search_table.draw()


	var valueData = [
    {% for tag in list_tag %}
      '{{tag|safe}}',
    {%  endfor %}
  ];

	$.getJSON("{{ url_for('Tags.get_all_tags') }}",
  function(data) {
    ltags = $('#ltags').tagSuggest({
      data: data,
			value: valueData,
      sortOrder: 'name',
      maxDropHeight: 200,
      name: 'ltags'
    });
  });

	$('#date-range-from').dateRangePicker({
			separator : ' to ',
			getValue: function(){
					if ($('#date-range-from-input').val() && $('#date-range-to-input').val() )
							return $('#date-range-from-input').val() + ' to ' + $('#date-range-to-input').val();
					else
							return '';
					},
					setValue: function(s,s1,s2){
							$('#date-range-from-input').val(s1);
							$('#date-range-to-input').val(s2);
					}
	});
	$('#date-range-to').dateRangePicker({
			separator : ' to ',
			getValue: function(){
					if ($('#date-range-from-input').val() && $('#date-range-to-input').val() )
							return $('#date-range-from-input').val() + ' to ' + $('#date-range-to-input').val();
					else
							return '';
			},
			setValue: function(s,s1,s2){
					$('#date-range-from-input').val(s1);
					$('#date-range-to-input').val(s2);
			}
	});
});
</script>

<script>
function searchTags() {
	var data = ltags.getValue();
	var date_from = $('#date-range-from-input').val();
	var date_to =$('#date-range-to-input').val();
	window.location.replace("{{ url_for('Tags.Tags_page') }}?date_from="+date_from+"&date_to="+date_to+"&ltags=" + data);
}
function emptyTags() {
	ltags.clear();
}

function toggle_sidebar(){
	if($('#nav_menu').is(':visible')){
		$('#nav_menu').hide();
		$('#side_menu').removeClass('border-right')
		$('#side_menu').removeClass('col-lg-2')
		$('#core_content').removeClass('col-lg-10')
	}else{
		$('#nav_menu').show();
		$('#side_menu').addClass('border-right')
		$('#side_menu').addClass('col-lg-2')
		$('#core_content').addClass('col-lg-10')
	}
}
</script>

<!-- Dynamically update the modal -->
<script>
  // static data
  var alert_message = '<div class="alert alert-info alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><strong>No more data.</strong> Full paste displayed.</div>';
  var complete_paste = null;
  var char_to_display = {{ char_to_display }};
  var start_index = 0;

  // When the modal goes out, refresh it to normal content
  $("#mymodal").on('hidden.bs.modal', function () {
      can_change_modal_content = true;
      $("#mymodalbody").html("<p>Loading paste information...</p>");
      var loading_gif = "<img id='loading-gif-modal' class='img-center' src=\"{{url_for('static', filename='image/loading.gif') }}\" height='26' width='26' style='margin: 4px;'>";
      $("#mymodalbody").append(loading_gif); // Show the loading GIF
      $("#button_show_path").attr('href', '');
      $("#button_show_path").hide();
      complete_paste = null;
      start_index = 0;
  });

  // Update the paste preview in the modal
  function update_preview() {
     	if (start_index + char_to_display > complete_paste.length-1){ // end of paste reached
         	var final_index = complete_paste.length-1;
          var flag_stop = true;
      } else {
          var final_index = start_index + char_to_display;
      }

      if (final_index != start_index){ // still have data to display
          // Append the new content using text() and not append (XSS)
          $("#mymodalbody").find("#paste-holder").text($("#mymodalbody").find("#paste-holder").text()+complete_paste.substring(start_index+1, final_index+1));
          start_index = final_index;
          if (flag_stop)
              nothing_to_display();
      } else {
          nothing_to_display();
      }
  }
  // Update the modal when there is no more data
  function nothing_to_display() {
      var new_content = $(alert_message).hide();
      $("#mymodalbody").find("#panel-body").append(new_content);
      new_content.show('fast');
      $("#load-more-button").hide();
  }

  function get_html_and_update_modal(event, truemodal) {
      event.preventDefault();

      var modal=truemodal;
      var url = " {{ url_for('showsavedpastes.showpreviewpaste')  }}?paste=" + modal.attr('data-path') + "&num=" + modal.attr('data-num');
      last_clicked_paste =  modal.attr('data-num');
      $.get(url, function (data) {

          // verify that the reveived data is really the current clicked paste. Otherwise, ignore it.
          var received_num = parseInt(data.split("|num|")[1]);
          if (received_num == last_clicked_paste && can_change_modal_content) {
              can_change_modal_content = false;

              // clear data by removing html, body, head tags. prevent dark modal background stack bug.
              var cleared_data = data.split("<body>")[1].split("</body>")[0];
              $("#mymodalbody").html(cleared_data);

              var button = $('<button type="button" id="load-more-button" class="btn btn-outline-primary rounded-circle px-1 py-0" data-url="' + $(modal).attr('data-path') +'" data-toggle="tooltip" data-placement="bottom" title="Load more content"><i class="fas fa-arrow-circle-down mt-1"></i></button>');
							button.tooltip(button);
              $("#container-show-more").append(button);

							$("#button_show_path").attr('href', '{{ url_for('showsavedpastes.showsavedpaste')  }}?paste=' + $(modal).attr('data-path'));
              $("#button_show_path").show('fast');
              $("#loading-gif-modal").css("visibility", "hidden"); // Hide the loading GIF
              if ($("[data-initsize]").attr('data-initsize') < char_to_display) { // All the content is displayed
                  nothing_to_display();
              }
							// collapse decoded
							$('#collapseDecoded').collapse('hide');
              // On click, donwload all paste's content
              $("#load-more-button").on("click", function (event) {
                  if (complete_paste == null) { //Donwload only once
                      $.get("{{ url_for('showsavedpastes.getmoredata') }}"+"?paste="+$(modal).attr('data-path'), function(data, status){
                          complete_paste = data;
                          update_preview();
                      });
                  } else {
                      update_preview();
                  }
              });
          } else if (can_change_modal_content) {
              $("#mymodalbody").html("Ignoring previous not finished query of paste #" + received_num);
          }
      });
  }


</script>


</html>
