'use strict';

// Attestations controller
angular.module('attestations').controller('AttestationsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Attestations',
  function ($scope, $stateParams, $location, Authentication, Attestations) {
    $scope.authentication = Authentication;
    $scope.searchName = '';
    $scope.searchID = '';
    
    $scope.sortType = 'originating_system_name'; // set the default sort type
    $scope.sortReverse = false;  // set the default sort order
    
    // Create new Attestation
    $scope.create = function (isValid) {
      $scope.error = null;

      if (!isValid) {
        $scope.$broadcast('show-errors-check-validity', 'attestationForm');

        return false;
      }

      // Create new Attestation object
      var attestation = new Attestations({
        business_unit_id: this.attestation.business_unit_id,
        department: this.attestation.department,
        statement: this.attestation.statement,
        date_start_string: this.attestation.date_start_string,
        date_end_string: this.attestation.date_end_string,
        response: this.attestation.response,
        originating_system_name: this.attestation.originating_system_name,
        originating_system_id: this.attestation.originating_system_id
      });
      // console.log("Client saving -- %j",attestation);
      // Redirect after save
      attestation.$save(function (response) {
        $location.path('attestations/' + response._id);
        // Clear form fields
        $scope.attestation.business_unit_id = '';
        $scope.attestation.department = '';
        $scope.attestation.statement = '';
        $scope.attestation.date_start_string = '';
        $scope.attestation.date_end_string = '';
        $scope.attestation.response = '';
        $scope.attestation.originating_system_name = '';
        $scope.attestation.originating_system_id = '';
      }, function (errorResponse) {
        $scope.error = errorResponse.data.message;
      });
    };

    // Remove existing Attestation
    $scope.remove = function (attestation) {
      if (attestation) {
        attestation.$remove();

        for (var i in $scope.attestations) {
          if ($scope.attestations[i] === attestation) {
            $scope.attestations.splice(i, 1);
          }
        }
      } else {
        $scope.attestation.$remove(function () {
          $location.path('attestations/list');
        });
      }
    };

    // Update existing Attestation
    $scope.update = function (isValid) {
      $scope.error = null;

      if (!isValid) {
        $scope.$broadcast('show-errors-check-validity', 'attestationForm');

        return false;
      }

      var attestation = $scope.attestation;

      attestation.$update(function () {
        $location.path('attestations/' + attestation._id);
      }, function (errorResponse) {
        $scope.error = errorResponse.data.message;
      });
    };
    
    // Find a list of Attestations
    $scope.find = function () {
      $scope.attestations = Attestations.query();       
    };
    
    // Find existing Attestation
    $scope.findOne = function () {
      $scope.attestation = Attestations.get({
        attestationId: $stateParams.attestationId
      });
    };    
  }
]);
