{% extends 'tom_common/base.html' %}
{% load bootstrap4 targets_extras dataproduct_extras static %}
{% block title %}Share Target {{ target.name }}{% endblock %}
{% block additional_css %}
<link rel="stylesheet" href="{% static 'tom_common/css/main.css' %}">
<link rel="stylesheet" href="{% static 'tom_targets/css/main.css' %}">
<script src="https://cdn.jsdelivr.net/npm/js-base64@3.7.5/base64.min.js"></script>
{% endblock %}
{% block content %}
<h3> Share <a href="{% url 'targets:detail' pk=target.id %}" title="Back">{{ target.name }}</a></h3>
<div class="col-md-8">
  <ul class="nav nav-tabs" role="tablist" id="share-tabs">
    <li class="nav-item">
      <a class="nav-link active" id="share-tab" href="#share" role="tab" data-toggle="tab">Share {{ target.name }}</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" id="persistentshare-tab" href="#persistentshare" role="tab" data-toggle="tab">Persistent
        Sharing</a>
    </li>
  </ul>
</div>
<div class="tab-content">
  <div class="tab-pane active" id="share">
    <div class="col-md-12">
      <form method="post" class="form" id='target-share-form'>
        <div class="form-row" style="padding-inline:1rem">
          <div class="col-sm-3">
            {% bootstrap_field form.share_destination %}
          </div>
          <div class="col-sm-5">
            {% bootstrap_field form.share_title %}
          </div>
          <div class="col-sm-1">
            <input type="submit" class="btn btn-primary" formaction="{% url 'targets:share' pk=target.id %}"
              id="submit_target" value="Submit" name="share_target_form" style="position:absolute; bottom:1rem"
              onclick="setTarget('')">
          </div>
          {% if hermes_sharing %}
          <div class="col-sm-1">
            <b style="position:absolute; bottom:1.2rem; right:2rem;">or</b>
          </div>
          <div class="col-sm-2">
            <button class="btn btn-info" type="submit" formaction="{% url 'targets:hermes-preload' pk=target.id %}"
              onclick="setTarget('_blank')" style="position:absolute; bottom:1rem">Open in Hermes &#x1F5D7;</button>
          </div>
          {% endif %}
        </div>
        <div class="form-row" style="padding-inline:1rem">
          <div class="col-sm-12">
            {% bootstrap_field form.share_message %}
          </div>
        </div>
        <h4> Include Data </h4>
        {% with target_share=True %}
        {% get_photometry_data target target_share %}
        {% endwith %}
        {% buttons %}
        <a href="{% url 'targets:detail' pk=target.id %}" class="btn btn-primary" title="Back">Back</a>
        {% endbuttons %}
      </form>
    </div>
  </div>
  <div class="tab-pane" id="persistentshare">
    <div class="col-md-12">
      <h3>Continously Share data for Target <a href="{% url 'targets:detail' pk=target.id %}" title="Back">{{ target.name }}</a></h3>
      <div id='target-persistent-share-create'>
        {% create_persistent_share target %}
      </div>
      <h3>Manage Continuous Sharing for Target <a href="{% url 'targets:detail' pk=target.id %}"
          title="Back">{{ target.name }}</a></h3>
      <div id='target-persistent-share-table'>
        {% persistent_share_table target %}
      </div>
    </div>
  </div>
</div>
<script>
    const targetShareForm = document.getElementById('target-share-form');
    function setTarget(val) {
      if (val && val != '') {
        targetShareForm.setAttribute('target', val);
      }
      else{
        targetShareForm.removeAttribute('target');
      }
      return true;
    };
</script>
{% endblock %}
