// Initialize object used to manage selections from ArchivesSpace
OrdersCtrl.prototype.initializeArchivesSpaceRecordSelect = function() {
  this.archivesSpaceRecordSelect = {
    uri: '',
    loading: false,
    alert: null,
    digitalObject: false,
    show: false
  };
  this.showItemSelector = false;
}


OrdersCtrl.prototype.showArchivesSpaceRecordSelector = function() {
  this.showItemSelector = 'archivesspace';
}


OrdersCtrl.prototype.orderArchivesSpaceRecords = function () {
  var records = [];
  if ((this.order) && this.order['item_orders']) {
    this.order['item_orders'].forEach(function(orderItem) {
      records = records.concat(orderItem['archivesspace_uri']);
    });
  }
  return records;
}


OrdersCtrl.prototype.addItemsFromArchivesSpace = function(callback) {
  var _this = this;
  var uri = this.archivesSpaceRecordSelect['uri'];
  // ensure that leading slash is included in URI
  if (!uri.match(/^\//)) {
    uri = '/' + uri;
  }

  this.archivesSpaceRecordSelect['loading'] = true;
  this.archivesSpaceRecordSelect['alert'] = null;

  var addArchivesSpaceUriToItemOrder = function(itemId, uri) {
    var targetIndex = _this.getItemOrderIndex(_this.order['item_orders'], itemId);
    if (targetIndex) {
      _this.order['item_orders'][targetIndex]['archivesspace_uri'].push(uri);
    }
  }

  if (_this.orderArchivesSpaceRecords().indexOf(uri) >= 0) {
    _this.initializeArchivesSpaceRecordSelect();
    _this.archivesSpaceRecordSelect['alert'] = 'The ArchivesSpace record at ' + uri + ' is already included in this order.';
  }
  else {
    _this.apiRequests.post('items/create_from_archivesspace', { 'archivesspace_uri': uri, 'digital_object': _this.archivesSpaceRecordSelect['digitalObject'] } ).then(function(response) {

      _this.archivesSpaceRecordSelect['loading'] = false;

      if (response.status == 200) {
        if (Array.isArray(response.data['items'])) {
          $.each(response.data['items'], function(index, item) {
            if (_this.itemIds.indexOf(item['id']) < 0) {
              _this.itemIds.push(item['id']);
              item['archivesspace_uri'] = uri;

              _this.addItemOrder(item, uri);
            }
            else {
              _this.archivesSpaceRecordSelect['alert'] = "One or more containers corresponding to the ArchivesSpace record is already included in the order. The ArchivesSpace URI entered will be added for reference.";
              addArchivesSpaceUriToItemOrder(item['id'], uri);
            }
          });
          // _this.archivesSpaceRecordSelect['uri'] = null;
          // _this.archivesSpaceRecordSelect['digitalObject'] = false;
          _this.commonUtils.executeCallback(callback, response.data);
        }
        else {
          _this.archivesSpaceRecordSelect['alert'] = 'The ArchivesSpace record at ' + uri + ' has no linked containers and cannot be ordered.';
        }
      }
      else {
        _this.archivesSpaceRecordSelect['alert'] = response.data['error']['detail'];
      }
      _this.initializeArchivesSpaceRecordSelect();
    });
  }

}

@@ -1315,16 +1315,19 @@
 %7D%0A  %7D%0A%0A 
+ //
  if (_th
@@ -1375,24 +1375,27 @@
 i) %3E= 0) %7B%0A 
+ //
    _this.ini
@@ -1432,18 +1432,21 @@
 ct();%0A  
+//
   
+ 
 _this.ar
@@ -1563,20 +1563,26 @@
 ';%0A 
+ //
  %7D%0A 
+ //
  else %7B%0A
     
@@ -1573,24 +1573,25 @@
   // else %7B%0A
+%0A
     _this.ap
@@ -2403,18 +2403,21 @@
 will be 
-ad
+recor
 ded for 
@@ -2425,16 +2425,40 @@
 eference
+ if unique to this order
 .%22;%0A    
@@ -3097,15 +3097,19 @@
     %7D);%0A
- 
+%0A  //
  %7D%0A%0A%7D%0A

remove duplicate uri restriction when adding AS items to order