{% 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>
      {% if not lot %}
        {% if all_devices %}
          <li class="breadcrumb-item active">All devices</li>
        {% else %}
          <li class="breadcrumb-item active">Unassigned</li>
        {% endif %}
      {% elif lot.is_temporary %}
      <li class="breadcrumb-item active">Temporary Lot</li>
      <li class="breadcrumb-item active">{{ lot.name }}</li>
      {% elif lot.is_incoming %}
      <li class="breadcrumb-item active">Incoming Lot</li>
      <li class="breadcrumb-item active">{{ lot.name }}</li>
      {% elif lot.is_outgoing %}
      <li class="breadcrumb-item active">Outgoing Lot</li>
      <li class="breadcrumb-item active">{{ lot.name }}</li>
      {% endif %}
    </ol>
  </nav>
</div><!-- End Page Title -->

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

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

      <div class="card">
      {% if lot %}
        <div class="card-body pt-3">
          <!-- Bordered Tabs -->

          <div class="d-flex align-items-center justify-content-between row">
            <div class="col-sm-12 col-md-5">
              <h3>
                <a href="{{ url_for('inventory.lot_edit', id=lot.id) }}">{{ lot.name }}</a>
              </h3>
            </div>

            <div class="col-sm-12 col-md-7 d-md-flex justify-content-md-end"><!-- lot actions -->
              {% if lot.is_temporary or not lot.transfer.closed %}

                {% if lot and lot.is_temporary %}
                    <a type="button" href="{{ url_for('inventory.lot_new_transfer', lot_id=lot.id, type_id='outgoing') }}" class="btn btn-primary doTransfer" >
                      Create Outgoing Lot
                    </a>
                    <a type="button" href="{{ url_for('inventory.lot_new_transfer', lot_id=lot.id, type_id='incoming') }}" class="btn btn-primary doTransfer">
                      Create Incoming Lot
                    </a>
                {% endif %}
                <a class="text-danger" href="javascript:removeLot()">
                  <i class="bi bi-trash"></i> Delete Lot
                </a>
                <span class="d-none" id="activeRemoveLotModal" data-bs-toggle="modal" data-bs-target="#btnRemoveLots"></span>
              {% endif %}
            </div>
          </div>
        </div>
      {% endif %}
        <div class="card-body pt-3" style="min-height: 650px;">
          <!-- Bordered Tabs -->
          {% if lot %}
          <ul class="nav nav-tabs nav-tabs-bordered">

            <li class="nav-item">
              <button class="nav-link active" data-bs-toggle="tab" data-bs-target="#devices-list">Devices</button>
            </li>

            {% if lot and not lot.is_temporary %}
            <li class="nav-item">
              <button class="nav-link" data-bs-toggle="tab" data-bs-target="#trade-documents-list">Documents</button>
            </li>

            {% if lot.transfer %}
            <li class="nav-item">
              <button class="nav-link" data-bs-toggle="tab" data-bs-target="#edit-transfer">
              Transfer ({% if lot.transfer.closed %}<span class="text-danger">Closed</span>{% else %}<span class="text-success">Open</span>{% endif %})
              </button>
            </li>
            <li class="nav-item">
              <button class="nav-link" data-bs-toggle="tab" data-bs-target="#edit-delivery-note">
                Delivery Note
              </button>
            </li>
            <li class="nav-item">
              <button class="nav-link" data-bs-toggle="tab" data-bs-target="#edit-receiver-note">
                Receiver Note
              </button>
            </li>
            {% endif %}
            {% endif %}

          </ul>
          {% endif %}
          <div class="tab-content pt-1">
            <div id="devices-list" class="tab-pane fade devices-list active show">
              <label class="btn btn-primary " for="SelectAllBTN"><input type="checkbox" id="SelectAllBTN" autocomplete="off"></label>
              <div class="btn-group dropdown ml-1">
                <button id="btnLots" type="button" onclick="processSelectedDevices()" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                  <i class="bi bi-folder2"></i>
                  Lots
                  <span class="caret"></span>
                </button>
                <span class="d-none" id="activeTradeModal" data-bs-toggle="modal" data-bs-target="#tradeLotModal"></span>

                <ul class="dropdown-menu" aria-labelledby="btnLots" id="dropDownLotsSelector">
                  <div class="row w-100">
                    <div class="input-group mb-3 mx-2">
                      <div class="input-group-prepend">
                        <span class="input-group-text" id="basic-addon1"><i class="bi bi-search"></i></span>
                      </div>
                      <input type="text" class="form-control" id="lots-search" placeholder="search" aria-label="search" aria-describedby="basic-addon1">
                    </div>
                  </div>
                  <h6 class="dropdown-header">Select lots where to store the selected devices</h6>
                  <ul class="mx-3" id="LotsSelector"></ul>
                  <li><hr /></li>
                  <li>
                    <a href="#" class="dropdown-item" id="ApplyDeviceLots">
                      <i class="bi bi-check"></i>
                      Apply
                    </a>
                  </li>
                </ul>
              </div>
              <div class="btn-group dropdown m-1" uib-dropdown="">
                <button id="btnActions" type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                    <i class="bi bi-plus"></i>
                    Actions
                </button>
                <span class="d-none" id="activeActionModal" data-bs-toggle="modal" data-bs-target="#actionModal"></span>
                <span class="d-none" id="activeAllocateModal" data-bs-toggle="modal" data-bs-target="#allocateModal"></span>
                <span class="d-none" id="activeDatawipeModal" data-bs-toggle="modal" data-bs-target="#datawipeModal"></span>
                <ul class="dropdown-menu" aria-labelledby="btnActions">
                  <li>
                      Status actions
                  </li>
                  <li>
                    <a href="javascript:newAction('Recycling')" class="dropdown-item">
                      <i class="bi bi-recycle"></i>
                      Recycling
                    </a>
                  </li>
                  <li>
                    <a href="javascript:newAction('Use')" class="dropdown-item">
                      <i class="bi bi-play-circle-fill"></i>
                      Use
                    </a>
                  </li>
                  <li>
                    <a href="javascript:newAction('Refurbish')" class="dropdown-item">
                      <i class="bi bi-tools"></i>
                      Refurbish
                    </a>
                  </li>
                  <li>
                    <a href="javascript:newAction('Management')" class="dropdown-item">
                      <i class="bi bi-mastodon"></i>
                      Management
                    </a>
                  </li>
                  <li>
                      Allocation
                  </li>
                  <li>
                    <a href="javascript:newAllocate('Allocate')" class="dropdown-item">
                      <i class="bi bi-house-fill"></i>
                      Allocate
                    </a>
                  </li>
                  <li>
                    <a href="javascript:newAllocate('Deallocate')" class="dropdown-item">
                      <i class="bi bi-house"></i>
                      Deallocate
                    </a>
                  </li>
                  <li>
                      Physical actions
                  </li>
                  <li>
                    <a href="javascript:newAction('ToPrepare')" class="dropdown-item">
                      <i class="bi bi-tools"></i>
                      ToPrepare
                    </a>
                  </li>
                  <li>
                    <a href="javascript:newAction('Prepare')" class="dropdown-item">
                      <i class="bi bi-egg"></i>
                      Prepare
                    </a>
                  </li>
                  <li>
                    <a href="javascript:newDataWipe('DataWipe')" class="dropdown-item">
                      <i class="bi bi-eraser-fill"></i>
                      DataWipe
                    </a>
                  </li>
                  <li>
                    <a href="javascript:newAction('ToRepair')" class="dropdown-item">
                      <i class="bi bi-screwdriver"></i>
                      ToRepair
                    </a>
                  </li>
                  <li>
                    <a href="javascript:newAction('Ready')" class="dropdown-item">
                      <i class="bi bi-check2-all"></i>
                      Ready
                    </a>
                  </li>
                </ul>
              </div>

              <div class="btn-group dropdown m-1" uib-dropdown="">
                <button id="btnExport" type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                    <i class="bi bi-reply"></i>
                    Exports
                </button>
                <span class="d-none" id="exportAlertModal" data-bs-toggle="modal" data-bs-target="#exportErrorModal"></span>
                <ul class="dropdown-menu" aria-labelledby="btnExport">
                  <li>
                    <a href="javascript:export_file('devices')" class="dropdown-item">
                      <i class="bi bi-file-spreadsheet"></i>
                      Devices Spreadsheet
                    </a>
                  </li>
                  <li>
                    {% if not all_devices %}
                    <span class="dropdown-item" style="color: #999ea4;">
                      <i class="bi bi-file-spreadsheet"></i>
                      Devices Lots Spreadsheet
                    </span>
                    {% else %}
                    <a href="javascript:export_file('devices_lots')" class="dropdown-item">
                      <i class="bi bi-file-spreadsheet"></i>
                      Devices Lots Spreadsheet
                    </a>
                    {% endif %}
                  </li>
                  <li>
                    <a href="javascript:export_file('obada_standard')" class="dropdown-item">
                      <i class="bi bi-file-spreadsheet"></i>
                      Obada Standard Spreadsheet
                    </a>
                  </li>
                  <li>
                    <a href="javascript:export_file('certificates')" class="dropdown-item">
                      <i class="bi bi-eraser-fill"></i>
                      Erasure Certificate
                    </a>
                  </li>
                </ul>
              </div>

              <div class="btn-group dropdown m-1" uib-dropdown="">
                <button id="btnTags" type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                    <i class="bi bi-tag"></i>
                    Labels
                </button>
                <ul class="dropdown-menu" aria-labelledby="btnTags">
                  <li>
                    <form id="print_labels" method="post" action="{{ url_for('labels.print_labels') }}">
                      {% for f in form_print_labels %}
                        {{ f }}
                      {% endfor %}
                      <a href="javascript:$('#print_labels').submit()" class="dropdown-item">
                        <i class="bi bi-printer"></i>
                        Print labels
                      </a>
                    </form>
                  </li>
                </ul>
              </div>

              <div class="btn-group dropdown m-1" uib-dropdown="">
                <button id="btnSnapshot" type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                    <i class="bi bi-laptop"></i>
                    Placeholders
                </button>
                <ul class="dropdown-menu" aria-labelledby="btnSnapshot">
                  <li>
                    {% if lot %}
                    <a href="{{ url_for('inventory.lot_upload_placeholder', lot_id=lot.id) }}" class="dropdown-item">
                    {% else %}
                    <a href="{{ url_for('inventory.upload_placeholder') }}" class="dropdown-item">
                    {% endif %}
                      <i class="bi bi-upload"></i>
                      Upload Spreadsheet
                    </a>
                  </li>
                  <li>
                    {% if lot %}
                    <a href="{{ url_for('inventory.lot_device_add', lot_id=lot.id) }}" class="dropdown-item">
                    {% else %}
                    <a href="{{ url_for('inventory.device_add') }}" class="dropdown-item">
                    {% endif %}
                      <i class="bi bi-plus"></i>
                      Create a new
                    </a>
                  </li>
                </ul>
              </div>

              <div class="btn-group dropdown m-1" uib-dropdown="">
                <button id="btnSnapshot" type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                    <i class="bi bi-laptop"></i>
                    Snapshots
                </button>
                <ul class="dropdown-menu" aria-labelledby="btnSnapshot">
                  <li>
                    {% if lot %}
                    <a href="{{ url_for('inventory.lot_upload_snapshot', lot_id=lot.id) }}" class="dropdown-item">
                    {% else %}
                    <a href="{{ url_for('inventory.upload_snapshot') }}" class="dropdown-item">
                    {% endif %}
                      <i class="bi bi-upload"></i>
                      Upload files
                    </a>
                  </li>
                </ul>
              </div>

              <div id="select-devices-info" class="alert alert-info mb-0 mt-3 d-none" role="alert">
                If this text is showing is because there are an error
              </div>

              <div class="tab-content pt-2">
                <form method="get">
                  <div class="d-flex mt-4 mb-4">
                    {% for f in form_filter %}
                      {{ f }}
                    {% endfor %}
                    <input type="submit" class="ms-2 btn btn-primary" value="Filter" />
                  </div>
                </form>

                <p class="mt-3">
                  Displaying devices of type
                  <em>{{ form_filter.filter.data or "Computer" }}</em>
                </p>

                <table class="table">
                  <thead>
                    <tr>
                      <th scope="col">Select</th>
                      <th scope="col">Title</th>
                      <th scope="col">DHID</th>
                      <th scope="col">PHID</th>
                      <th scope="col">Type</th>
                      <th scope="col">Unique Identifiers</th>
                      <th scope="col">Lifecycle Status</th>
                      <th scope="col">Allocated Status</th>
                      <th scope="col">Physical Status</th>
                      <th scope="col" data-type="date" data-format="YYYY-MM-DD">Updated in</th>
                      <th scope="col" data-type="date" data-format="YYYY-MM-DD hh:mm:ss">Registered in</th>
                      <th scope="col"></th>
                    </tr>
                  </thead>
                  <tbody>
                    {% for dev in devices %}
                    {% if dev.placeholder and (not dev.parent_id or dev.parent.placeholder.kangaroo) %}
                    <tr>
                      <td>
                          <input type="checkbox" class="deviceSelect" data="{{ dev.id }}"
                               data-device-type="{{ dev.type }}" data-device-manufacturer="{{ dev.manufacturer }}"
                               data-device-dhid="{{ dev.devicehub_id }}" data-device-vname="{{ dev.verbose_name }}"
                               {% if form_new_allocate.type.data and dev.id in list_devices %}
                                 checked="checked"
                               {% endif %}
                               />
                      </td>
                      <td>
                        <a href="{{ url_for('inventory.device_details', id=dev.devicehub_id)}}">
                        {% if dev.get_type_logo() %}
                        <i class="{{ dev.get_type_logo() }}" title="{{ dev.type }}"></i>
                        {% endif %}
                          {{ dev.verbose_name }}
                        </a>
                        {% if dev.lots | length > 0 %}
                        <h6 class="d-inline">
                          {% for lot in dev.get_lots_for_template() %}
                            <span class="badge rounded-pill bg-light text-dark">{{ lot }}</span>
                          {% endfor %}
                        </h6>
                        {% endif %}
                      </td>
                      <td>
                        <a href="{{ url_for('inventory.device_details', id=dev.devicehub_id)}}">
                          {{ dev.devicehub_id }}
                        </a>
                      </td>
                      <td>
                          {{ dev.binding and dev.binding.phid or dev.placeholder and dev.placeholder.phid  or '' }}
                      </td>
                      <td>
                          {{ dev.is_abstract() }}
                      </td>
                      <td>
                        {% for t in dev.tags | sort(attribute="id") %}
                         <a href="{{ url_for('labels.label_details', id=t.id)}}">{{ t.id }}</a>
                         {% if not loop.last %},{% endif %}
                        {% endfor %}
                      </td>
                      <td>{% if dev.status %}{{ dev.status.type }}{% endif %}</td>
                      <td>{% if dev.allocated_status %}{{ dev.allocated_status.type }}{% endif %}</td>
                      <td>{% if dev.physical_status %}{{ dev.physical_status.type }}{% endif %}</td>
                      <td>{{ dev.get_updated.strftime('%Y-%m-%d %H:%M:%S')}}</td>
                      <td>{{ dev.created.strftime('%Y-%m-%d %H:%M:%S')}}</td>
                      <td>
                        <a href="{{ dev.public_link }}" target="_blank">
                          <i class="bi bi-box-arrow-up-right"></i>
                        </a>
                      </td>
                    </tr>
                    {% endif %}
                    {% endfor %}
                  </tbody>
                </table>

                </div>
              </div>
              {% if lot and not lot.is_temporary %}
              <div id="trade-documents-list" class="tab-pane fade trade-documents-list">
              <div class="btn-group dropdown ml-1 mt-1" uib-dropdown="">
                <a href="{{ url_for('inventory.trade_document_add', lot_id=lot.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" data-type="date" data-format="YYYY-MM-DD hh:mm">Uploaded on</th>
                    </tr>
                  </thead>
                  <tbody>
                    {% for doc in lot.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.created.strftime('%Y-%m-%d %H:%M')}}
                      </td>
                    </tr>
                    {% endfor %}
                    {% for doc in lot.trade.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.created.strftime('%Y-%m-%d %H:%M')}}
                      </td>
                    </tr>
                    {% endfor %}
                  </tbody>
                </table>
              </div>
              <div id="edit-transfer" class="tab-pane fade edit-transfer">
                <h5 class="card-title">Transfer</h5>
                <form method="post" action="{{ url_for('inventory.edit_transfer', lot_id=lot.id) }}" class="row g-3 needs-validation" novalidate>
                  {{ form_transfer.csrf_token }}

                  {% for field in form_transfer %}
                    {% if field != form_transfer.csrf_token %}
                      <div class="col-12">
                      {% if field != form_transfer.type %}
                        {{ field.label(class_="form-label") }}
                        {% if field == form_transfer.code %}
                        <span class="text-danger">*</span>
                        {% endif %}
                        {{ field }}
                        <small class="text-muted">{{ field.description }}</small>
                        {% if field.errors %}
                          <p class="text-danger">
                            {% for error in field.errors %}
                              {{ error }}<br/>
                            {% endfor %}
                          </p>
                        {% endif %}
                      {% endif %}
                      </div>
                    {% endif %}
                  {% endfor %}

                  <div>
                    <a href="{{ url_for('inventory.lotdevicelist', lot_id=lot.id) }}" class="btn btn-danger">Cancel</a>
                    <button class="btn btn-primary" type="submit">Save</button>
                  </div>
                </form>
              </div>
              <div id="edit-delivery-note" class="tab-pane fade edit-delivery-note">
                <h5 class="card-title">Delivery Note</h5>
                <form method="post" action="{{ url_for('inventory.delivery_note', lot_id=lot.id) }}" class="row g-3 needs-validation" novalidate>
                  {{ form_delivery.csrf_token }}

                  {% for field in form_delivery %}
                    {% if field != form_delivery.csrf_token %}
                      <div class="col-12">
                      {% if field != form_delivery.type %}
                        {{ field.label(class_="form-label") }}
                        {{ field }}
                        <small class="text-muted">{{ field.description }}</small>
                        {% if field.errors %}
                          <p class="text-danger">
                            {% for error in field.errors %}
                              {{ error }}<br/>
                            {% endfor %}
                          </p>
                        {% endif %}
                      {% endif %}
                      </div>
                    {% endif %}
                  {% endfor %}

                  {% if lot.transfer and form_receiver.is_editable() %}
                  <div>
                    <a href="{{ url_for('inventory.lotdevicelist', lot_id=lot.id) }}" class="btn btn-danger">Cancel</a>
                    <button class="btn btn-primary" type="submit">Save</button>
                  </div>
                  {% endif %}
                </form>
              </div>
              <div id="edit-receiver-note" class="tab-pane fade edit-receiver-note">
                <h5 class="card-title">Receiver Note</h5>
                <form method="post" action="{{ url_for('inventory.receiver_note', lot_id=lot.id) }}" class="row g-3 needs-validation" novalidate>
                  {{ form_receiver.csrf_token }}

                  {% for field in form_receiver %}
                    {% if field != form_receiver.csrf_token %}
                      <div class="col-12">
                      {% if field != form_receiver.type %}
                        {{ field.label(class_="form-label") }}
                        {{ field }}
                        <small class="text-muted">{{ field.description }}</small>
                        {% if field.errors %}
                          <p class="text-danger">
                            {% for error in field.errors %}
                              {{ error }}<br/>
                            {% endfor %}
                          </p>
                        {% endif %}
                      {% endif %}
                      </div>
                    {% endif %}
                  {% endfor %}

                  {% if lot.transfer and form_receiver.is_editable() %}
                  <div>
                    <a href="{{ url_for('inventory.lotdevicelist', lot_id=lot.id) }}" class="btn btn-danger">Cancel</a>
                    <button class="btn btn-primary" type="submit">Save</button>
                  </div>
                  {% endif %}
                </form>
              </div>
              {% endif %}

            </div><!-- End Bordered Tabs -->
          </div>
        </div>
      </div>

      <div id="NotificationsContainer" style="position: absolute; bottom: 0; right: 0; margin: 10px; margin-top: 70px; width: calc(100% - 310px);"></div>

    </div>
  </div>
</section>
{% include "inventory/lot_delete_modal.html" %}
{% include "inventory/actions.html" %}
{% include "inventory/allocate.html" %}
{% include "inventory/data_wipe.html" %}
{% include "inventory/trade.html" %}
{% include "inventory/alert_export_error.html" %}
{% include "inventory/alert_lots_changes.html" %}

<!-- Custom Code -->
<script>
  let table = new simpleDatatables.DataTable("table", {
    perPageSelect: [5, 10, 15, 20, 25, 50, 100],
    perPage: 20
  })
</script>
{% if config['DEBUG'] %}
  <script src="{{ url_for('static', filename='js/main_inventory.js') }}"></script>
{% else %}
  <script src="{{ url_for('static', filename='js/main_inventory.build.js') }}"></script>
{% endif %}


{% endblock main %}
