{% extends "ereuse_devicehub/base_site.html" %}
{% block main %}

<div class="pagetitle">
  <h1>Inventory</h1>
  <nav>
    <ol class="breadcrumb">
      <li class="breadcrumb-item"><a href="{{ url_for('inventory.devicelist')}}">Inventory</a></li>
      <li class="breadcrumb-item active">{{ page_title }}</li>
    </ol>
  </nav>
</div><!-- End Page Title -->

<section class="section profile">
  <div class="row">

    <div class="col-xl-12">

      <div class="card">
        <div class="card-body pt-3">
          <h3>{{ placeholder.device.devicehub_id }}</h3>
          <div class="tab-pane active show mb-5">
              <div class="btn-group dropdown" uib-dropdown="" style="float: right; margin-right: 15px;">
                <button id="btnUniqueID" type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                    <i class="bi bi-tag"></i>
                    Unique Identifiers (Tags)
                </button>
                <span class="d-none" id="unlinkTagAlertModal" data-bs-toggle="modal" data-bs-target="#unlinkTagErrorModal"></span>
                <span class="d-none" id="addTagAlertModal" data-bs-toggle="modal" data-bs-target="#addingTagModal"></span>
                <ul class="dropdown-menu" aria-labelledby="btnUniqueID">
                  <li>
                    <a href="javascript:addTag()" class="dropdown-item">
                      <i class="bi bi-plus"></i>
                      Add Unique Identifier
                    </a>
                  </li>
                  <li>
                    <a href="{{ url_for('inventory.tag_devices_del', dhid=placeholder.device.devicehub_id) }}" class="dropdown-item">
                      <i class="bi bi-x"></i>
                      Remove Unique Identifier
                    </a>
                  </li>
                </ul>
              </div>
              {% if placeholder.status in ['Snapshot', 'Placeholder'] %}
              <a type="button" href="{{ url_for('inventory.binding_search', dhid=placeholder.device.devicehub_id) }}" class="btn btn-primary" style="float: right; margin-right: 15px;">
                New Twin
              </a>
              {% elif placeholder.status == 'Twin' %}
              <a type="button" href="{{ url_for('inventory.unbinding', phid=placeholder.phid) }}" class="btn btn-primary" style="float: right; margin-right: 15px;">
                Delete Twin
              </a>
              {% endif %}
              <div style="display: block;"></div>

          </div>
          <!-- Bordered Tabs -->
          <ul class="nav nav-tabs nav-tabs-bordered">

            <li class="nav-item">
              <button class="nav-link" data-bs-toggle="tab" data-bs-target="#details">General details</button>
            </li>

            <li class="nav-item">
              <a class="nav-link" href="{{ device.public_link }}" target="_blank">Web</a>
            </li>

            <li class="nav-item">
              <button class="nav-link" data-bs-toggle="tab" data-bs-target="#documents">Documents</button>
            </li>

            <li class="nav-item">
              <button class="nav-link" data-bs-toggle="tab" data-bs-target="#lots">Lots</button>
            </li>

            <li class="nav-item">
              <button class="nav-link" data-bs-toggle="tab" data-bs-target="#status">Status</button>
            </li>

            <li class="nav-item">
              <button class="nav-link" data-bs-toggle="tab" data-bs-target="#traceability">Traceability log</button>
            </li>

            <li class="nav-item">
              <button class="nav-link" data-bs-toggle="tab" data-bs-target="#components">Components</button>
            </li>

            {% if placeholder.binding %}
            <li class="nav-item">
              <button class="nav-link" data-bs-toggle="tab" data-bs-target="#dpps">Digital Passports</button>
            </li>
            <li class="nav-item">
              <button class="nav-link" data-bs-toggle="tab" data-bs-target="#proofs">Proofs</button>
            </li>
            {% endif %}

          </ul>
          <div class="tab-content pt-2">

            <div class="tab-pane fade show active" id="details">
              <h5 class="card-title">Editable details</h5>
              <div class="row mb-3">
                <div class="col-lg-3 col-md-4 label ">
                  (<a href="{{ url_for('inventory.device_edit', id=placeholder.device.devicehub_id)}}">Edit Device</a>)
                </div>
                <div class="col-lg-9 col-md-8">{{ placeholder.status }}</div>
              </div>

              <div class="row">
                <div class="col-lg-3 col-md-4 label ">Phid</div>
                <div class="col-lg-9 col-md-8">{{ placeholder.phid }}</div>
              </div>

              <div class="row">
                <div class="col-lg-3 col-md-4 label ">Id device internal</div>
                <div class="col-lg-9 col-md-8">{{ placeholder.id_device_internal or '' }}</div>
              </div>

              <div class="row">
                <div class="col-lg-3 col-md-4 label ">Type</div>
                <div class="col-lg-9 col-md-8">{{ placeholder.device.type }}</div>
              </div>

              <div class="row">
                <div class="col-lg-3 col-md-4 label">Manufacturer</div>
                <div class="col-lg-9 col-md-8">{{ placeholder.device.manufacturer or ''}}</div>
              </div>

              <div class="row">
                <div class="col-lg-3 col-md-4 label">Model</div>
                <div class="col-lg-9 col-md-8">{{ placeholder.device.model or ''}}</div>
              </div>

              <div class="row">
                <div class="col-lg-3 col-md-4 label">Part Number</div>
                <div class="col-lg-9 col-md-8">{{ placeholder.device.part_number or ''}}</div>
              </div>

              <div class="row">
                <div class="col-lg-3 col-md-4 label">Serial Number</div>
                <div class="col-lg-9 col-md-8">{{ placeholder.device.serial_number or ''}}</div>
              </div>

              {% if placeholder.binding %}
              <h5 class="card-title">Fixed details</h5>
              <div class="row">
                <div class="col-lg-3 col-md-4 label ">Type</div>
                <div class="col-lg-9 col-md-8">{{ placeholder.binding.type }}</div>
              </div>

              <div class="row">
                <div class="col-lg-3 col-md-4 label">Manufacturer</div>
                <div class="col-lg-9 col-md-8">{{ placeholder.binding.manufacturer or ''}}</div>
              </div>

              <div class="row">
                <div class="col-lg-3 col-md-4 label">Model</div>
                <div class="col-lg-9 col-md-8">{{ placeholder.binding.model or ''}}</div>
              </div>

              <div class="row">
                <div class="col-lg-3 col-md-4 label">Serial Number</div>
                <div class="col-lg-9 col-md-8">{{ placeholder.binding.serial_number or ''}}</div>
              </div>
              {% endif %}
            </div>

            <div class="tab-pane fade profile-overview" id="lots">
              <h5 class="card-title">Incoming Lots</h5>

              <div class="row">
              {% for lot in placeholder.device.lots %}
                {% if lot.is_incoming %}
                <div class="col">
                  <a class="ms-3" href="{{ url_for('inventory.lotdevicelist', lot_id=lot.id) }}">
                    <span>{{ lot.name }}</span>
                  </a>
                </div>
                {% endif %}
              {% endfor %}
              </div>

              <h5 class="card-title">Outgoing Lots</h5>

              <div class="row">
              {% for lot in placeholder.device.lots %}
                {% if lot.is_outgoing %}
                <div class="col">
                  <a class="ms-3" href="{{ url_for('inventory.lotdevicelist', lot_id=lot.id) }}">
                    <span>{{ lot.name }}</span>
                  </a>
                </div>
                {% endif %}
              {% endfor %}
              </div>

              <h5 class="card-title">Temporary Lots</h5>

              <div class="row">
              {% for lot in placeholder.device.lots %}
                {% if lot.is_temporary %}
                <div class="col">
                  <a class="ms-3" href="{{ url_for('inventory.lotdevicelist', lot_id=lot.id) }}">
                    <span>{{ lot.name }}</span>
                  </a>
                </div>
                {% endif %}
              {% endfor %}
              </div>
            </div>

            <div class="tab-pane fade profile-overview" id="documents">
              <div class="btn-group dropdown ml-1 mt-1" uib-dropdown="">
                <a href="{{ url_for('inventory.device_document_add', dhid=placeholder.device.devicehub_id) }}" class="btn btn-primary">
                  <i class="bi bi-plus"></i>
                  Add new document
                  <span class="caret"></span>
                </a>
              </div>

              <h5 class="card-title">Documents</h5>
              <table class="table">
                <thead>
                  <tr>
                    <th scope="col">File</th>
                    <th scope="col">Type</th>
                    <th scope="col">Description</th>
                    <th scope="col" data-type="date" data-format="YYYY-MM-DD hh:mm">Uploaded on</th>
                    <th></th>
                    <th></th>
                  </tr>
                </thead>
                <tbody>
                  {% for doc in placeholder.documents %}
                  <tr>
                    <td>
                      {% if doc.get_url() %}
                        <a href="{{ doc.get_url() }}" target="_blank">{{ doc.file_name}}</a>
                      {% else %}
                        {{ doc.file_name}}
                      {% endif %}
                    </td>
                    <td>
                        {{ doc.type }}
                    </td>                    
                    <td>
                        {% if doc.description %}
                        {{ doc.description|truncate(30, True) }}
                        {% endif %}
                    </td>
                    <td>
                        {{ doc.created.strftime('%Y-%m-%d %H:%M') }}
                    </td>                    
                    <td>
                        <a href="{{ url_for('inventory.device_document_edit', dhid=doc.device.dhid, doc_id=doc.id) }}" title="Edit document">
                          <i class="bi bi-pencil-square"></i>
                        </a>
                    </td>
                    <td>
                        <a href="javascript:javascript:void(0)" data-bs-toggle="modal" data-bs-target="#btnRemoveDocument{{ loop.index }}" title="Remove document">
                          <i class="bi bi-trash-fill"></i>
                        </a>
                        <div class="modal fade" id="btnRemoveDocument{{ loop.index }}" tabindex="-1" style="display: none;" aria-hidden="true">
                          <div class="modal-dialog">
                            <div class="modal-content">

                              <div class="modal-header">
                                <h5 class="modal-title">Delete Document</h5>
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                              </div>

                                <div class="modal-body">
                                    Are you sure that you want to delete this Document?<br />
                                    <strong>{{ doc.file_name }}</strong>
                                    <p class="text-danger">
                                      This action cannot be undone.
                                    </p>
                                </div>

                                <div class="modal-footer">
                                  <button type="button" class="btn btn-secondary-outline" data-bs-dismiss="modal">Cancel</button>
                                  <a href="{{ url_for('inventory.device_document_del', dhid=doc.device.dhid, doc_id=doc.id) }}" type="button" class="btn btn-danger">
                                    Delete it!
                                  </a>
                                </div>

                            </div>
                          </div>
                        </div>
                    </td>
                  </tr>
                  {% endfor %}
                </tbody>
              </table>
            </div>

            <div class="tab-pane fade profile-overview" id="status">
              <h5 class="card-title">Status Details</h5>
              <div class="row">
                <div class="col-lg-3 col-md-4 label">Physical State</div>
                <div class="col-lg-9 col-md-8">
                    {% if placeholder.device.physical_status %}
                    {{ placeholder.device.physical_status.type }}
                    {% endif %}
                </div>
              </div>
              <div class="row">
                <div class="col-lg-3 col-md-4 label">Lifecycle State</div>
                <div class="col-lg-9 col-md-8">
                    {% if placeholder.device.status %}
                    {{ placeholder.device.status.type }}
                    {% endif %}
                </div>
              </div>
              <div class="row">
                <div class="col-lg-3 col-md-4 label">Allocated State</div>
                <div class="col-lg-9 col-md-8">
                    {% if placeholder.device.allocated_status %}
                    {{ placeholder.device.allocated_status.type }}
                    {% endif %}
                </div>
              </div>
            </div>

            <div class="tab-pane fade profile-overview" id="traceability">
              <h5 class="card-title">Traceability log Details</h5>
              <div class="list-group col-6">
                {% for action in placeholder.actions %}
                <div class="list-group-item d-flex justify-content-between align-items-center">
                  {{ action.type }} {{ action.severity }}
                  <small class="text-muted">{{ action.created.strftime('%H:%M %d-%m-%Y') }}</small>
                </div>
                {% endfor %}
              </div>
            </div>

            <div class="tab-pane fade profile-overview" id="components">
              <h5 class="card-title">Components Placeholder</h5>
              <div class="list-group col-6">
                  {{ placeholder.components  or '' }}
              </div>

              {% if placeholder.binding %}
              <h5 class="card-title">Components Snapshot</h5>
              <div class="list-group col-6">
                {% for component in placeholder.binding.components|sort(attribute='type') %}
                <div class="list-group-item">
                  <div class="d-flex w-100 justify-content-between">
                    <h5 class="mb-1">{{ component.type }}</h5>
                    <small class="text-muted">{{ component.created.strftime('%H:%M %d-%m-%Y') }}</small>
                  </div>
                  <p class="mb-1">
                    {{ component.manufacturer }}<br />
                    {{ component.model }}<br />
                  </p>
                  <small class="text-muted">
                    {% if component.type in ['RamModule', 'HardDrive', 'SolidStateDrive'] %}
                      {{ component.size }}MB
                    {% endif %}
                  </small>
                </div>
                {% endfor %}
              </div>
              {% endif %}
            </div>

            {% if placeholder.binding %}
            <div class="tab-pane fade profile-overview" id="dpps">
              <h5 class="card-title">Digital Passports</h5>
              {% for dpp in placeholder.binding.dpps %}
              <div class="list-group col-12">
                <div class="list-group-item d-flex justify-content-between align-items-center">
                  <div class="row">
                    <div class="col-12">
                      <div class="d-flex w-100 justify-content-between">
                        <h5 class="mb-1">Time Stamp</h5>
                        <small class="text-muted">{{ dpp.timestamp }}</small>
                      </div>
                    </div>
                    <div class="col-12">
                      <p class="mb-1">
                        <a href="{{ url_for('Did.DidView', dpp=dpp.key) }}" target="_blank">{{ dpp.key }}</a><br />
                      </p>
                      <small class="text-muted">{{ dpp.created.strftime('%H:%M %d-%m-%Y') }}</small>
                    </div>
                  </div>
                </div>
              </div>
              {% endfor %}
            </div>
            {% endif %}

            <div class="tab-pane fade profile-overview" id="proofs">
              <h5 class="card-title">Proofs</h5>
              {% for proof in placeholder.proofs %}
              <div class="list-group col-12">
                <div class="list-group-item d-flex justify-content-between align-items-center">
                  <div class="row">
                    <div class="col-12">
                      <div class="d-flex w-100 justify-content-between">
                        <h5 class="mb-1">Time Stamp</h5>
                        <small class="text-muted">{{ proof.timestamp }}</small>
                      </div>
                    </div>
                    <div class="col-12">
                      <p class="mb-1">
                        {{ proof.type }}<br />
                      </p>
                      <small class="text-muted">{{ proof.created.strftime('%H:%M %d-%m-%Y') }}</small>
                    </div>
                  </div>
                </div>
              </div>
              {% endfor %}
              {% for component in placeholder.binding.components %}
              {% for proof in component.proofs %}
              <div class="list-group col-12">
                <div class="list-group-item d-flex justify-content-between align-items-center">
                  <div class="row">
                    <div class="col-12">
                      <div class="d-flex w-100 justify-content-between">
                        <h5 class="mb-1">Time Stamp</h5>
                        <small class="text-muted">{{ proof.timestamp }}</small>
                      </div>
                    </div>
                    <div class="col-12">
                      <p class="mb-1">
                        {{ proof.type }}<br />
                      </p>
                      <small class="text-muted">{{ proof.created.strftime('%H:%M %d-%m-%Y') }}</small>
                    </div>
                  </div>
                </div>
              </div>
              {% endfor %}
              {% endfor %}
            </div>

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

<script>
function addTag() {
    const devices_id = [{{ placeholder.device.id }}];
    if (devices_id.length == 1) {
        $("#addingTagModal .pol").hide();
        $("#addingTagModal .btn-primary").show();
    } else {
        $("#addingTagModal .pol").show();
        $("#addingTagModal .btn-primary").hide();
    }

    $("#addTagAlertModal").click();
}
</script>
{% include "inventory/addDevicestag.html" %}
{% endblock main %}