angular.module('ff9cert.controllers', [])

.controller('CertificateCtrl', function($scope, CertificateService){
  $scope.buttonText = "Generate";

  $scope.generate = function(){
    if($scope.barcode !== undefined){
      $scope.buttonText = "Please wait...";

      CertificateService.get_attendee($scope.barcode).then(function(data){
        if(data === undefined || data.barcodes === undefined){
          $scope.buttonText = "Invalid Code!";

          $scope.redInput = "red-input";
          $scope.redButton = "red-button";
        }else if(data.barcodes[0].status !== 'used'){
           $scope.buttonText = "Ticket Not Used!";

           $scope.redInput = "red-input";
           $scope.redButton = "red-button";
        }else{
          CertificateService.generate_pdf(data.profile.name, data.ticket_class_name);

          $scope.buttonText = "Downloading...";
        }
      });
    }
  };

  $scope.reset = function(){
    $scope.buttonText = "Generate";

    $scope.redInput = "";
    $scope.redButton = "";
  };
});
