'use strict';

app.directive('refreshHardware', function() {
  return {
    restrict: 'EA',
    require: '^ngModel',
    scope: {
      reset: '=',
      ngModel: '=',
      deviceType: '='
    },
    template: '<button  class="btn btn-success" type="button" ng-click="refresh()"><i class="fa fa-refresh"></i>&nbsp;&nbsp;Refresh the list</button>',
    replace: true,
    controller: function($scope, toastr, Systems) {
      $scope.refresh = function() {

        //Reset previously selected choice to null
        $scope.reset = null;
        
        Systems.searchHardware(true, $scope.deviceType).then(
          function(data) {
            console.log('searchedDevices', data);
            $scope.ngModel = data.searchedDevices;
            $scope.$parent.searchKey = data.searchKey;
          },
          function (error) {
            if (error.match('NO DEVICE')) {

            } else {
              toastr.error(error);
            }
          }
        );
        
      };
    }
  };
});