'use strict';

app.directive('dbDeviceForm', function (){
  return {
    restrict: 'EA',
    require: 'ngModel',
    replace: true,
    scope: {
      device: '=ngModel',
      brands: '=',
      formName: '=',
      categories: '='
    },
    templateUrl: 'views/installer/new/devices/db.html',
    controller: function ($scope) { 


      $scope.$watch('[device.id, device.db.type, device.db.source]', function(newValue, oldValue) {
        console.log('clearing query & models');
        $scope.device.db.brand = null;
        $scope.query = null;
        $scope.models = null;
        //$scope.formName.$setPristine();

      }, true);

      $scope.$watch('device.db.type', function(newValue, oldValue) {
        $scope.device.name = _.where($scope.categories, {
          name: $scope.device.db.type
        })[0].device;
      }, true);

    } 
  };
});
