(function() {
  'use strict';

  angular.module('Monitour.pages.register')
    .controller('registerCtrl', registerCtrl);

  /** @ngInject */
  function registerCtrl($scope, $timeout,  $translate, $state, RegisterService, $filter) {

    var vm = this;

     vm.sentence = {static: 'Your total platrom needs for'};

      // should be resolved by the router, and injected in this controller
      // that way, words are ready when the controller is instantiated
      // and when the link function of the directive is triggered
      vm.sentence.options = ['Calls', 'Sms', 'Programable Voice', 'Call Flows', 'Voice Mail'];

      vm.languages = [
          {name:'English', id:'1'}
      ];
	  console.log( vm.languages.id );

      $scope.$watch(function() {
          $('#languages').selectpicker('refresh');
      });
  vm.formParams = {};
  $scope.stage = "";
  $scope.formValidation = false;
  vm.price = "19.99";
	  vm.standardItem = {};
	  $scope.states = [  
         {  
            "Id":'02',
            "Description":"NSW"
         },
         {  
            "Id":'03',
            "Description":"VIC"
         },
         {  
            "Id":'07',
            "Description":"QLD"
         },
		   {  
            "Id":'08',
            "Description":"WA"
         },
		   {  
            "Id":'08',
            "Description":"NT"
         },
		   {  
            "Id":'02',
            "Description":"ACT"
         }
		  
      ];
		  
 console.log($scope.states.id);

	  $scope.checkselection = function () {

if ($scope.userSelect != "" && $scope.userSelect != undefined)

$scope.msg = 'Selected Value: '+$scope.userSelect;

else

$scope.msg = 'Please Select Dropdown Value';

}
	   $scope.card = {};
console.log($scope.card);
  $scope.isVisa = function(){
    if($scope.card.number && $scope.card.number.charAt(0) == 4){
      return true;
    }else{
      return false;
    }
  };

  $scope.isMasterCard = function(){
    if($scope.card.number && $scope.card.number.charAt(0) == 5){
      return true;
    }else{
      return false;
    }
  };
  // Navigation functions
  $scope.next = function (stage) {
    //$scope.direction = 1;
    //$scope.stage = stage;
    
    $scope.formValidation = true;
    
    if ($scope.multiStepForm.$valid) {
      $scope.direction = 1;
      $scope.stage = stage;
      $scope.formValidation = false;
    }
  };

  $scope.back = function (stage) {
    $scope.direction = 0;
    $scope.stage = stage;
  };
  vm.heading = 'callio calling solution';

	  
	  console.log($scope.expiry);
    $scope.today = new Date();

		$scope.addMonths = function(months) {
			var result = new Date($scope.today);
			result.setMonth(result.getMonth() + months);
			return result;
		};
    vm.selectPlan = function(planId) {
      switch (planId) {
          case 1:
              vm.selectedPlan = {
                  id: 1,
                  heading: $filter('translate')('purchase.titlebronze'),
                  monthlyCost: '29.99',
                  price: '29.99',
                  plan: 'Per Month',
                  month: $scope.addMonths(1)
              };
              break;
          case 2:
              vm.selectedPlan = {
                  id: 2,
                  heading: $filter('translate')('purchase.titlesilver'),
                  monthlyCost: '28.50',
                  price: '85.50',
                  plan: '3 Month Plan',
                  saving: '$4.47 of savings',
                  month: $scope.addMonths(3)
              };
              break;
          case 3:
              vm.selectedPlan = {
                  id: 3,
                  heading: $filter('translate')('purchase.titlegold'),
                  monthlyCost: '27.00',
                  price: '162.00',
                  plan: '6 Month Plan',
                  saving: '$17.94 of savings',
                  month: $scope.addMonths(6)
              };
              break;
          case 4:
              vm.selectedPlan = {
                  id: 4,
                  heading: $filter('translate')('purchase.titleplatinum'),
                  monthlyCost: '25.50',
                  price: '229.50',
                  plan: '9 Month Plan',
                  saving: '$40.41 of savings',
                  month: $scope.addMonths(9)
              };
              break;
          case 5:
              vm.selectedPlan = {
                  id: 5,
                  heading: $filter('translate')('purchase.titlediamond'),
                  monthlyCost: '24.00',
                  price: '288.00',
                  plan: '12 Month Plan',
                  saving: '$71.88 of savings',
                  month: $scope.addMonths(12)
              };
              break;
      }
      console.log("Selected plan:", vm.selectedPlan);
      $scope.stage = "stage2";
  };
  
  $scope.goBack = function() {
    if ($scope.stage === 'stage3') {
        $scope.stage = 'stage2';
    } else if ($scope.stage === 'stage2') {
        $scope.stage = 'stage1';
    } else if ($scope.stage === 'stage1') {
        $scope.stage = ''; // Assuming '' is your initial stage
    }
};
// $scope.sendForm = function(){
//	 
//	 
//	 var date = vm.formParams.date;
//    var parts = date.split('/');
//    var month = parts[0];
//    var year = parts[1];
//
//			console.log(year);
//    alert('plan' +  vm.heading +  "\n" + 'first name' + vm.formParams.firstName + "\n" + 'Last name' + vm.formParams.lastname + "\n" +'Card number: ' + vm.formParams.number + "\n" + 'Card holder: ' + vm.formParams.holder + "\n" + 'Expiry date: ' + vm.formParams.date + "\n" + 'Card CVV: ' + vm.formParams.cvv + "\n" + 'State' + vm.formParams.state.Id + "\n" + 'password' + vm.formParams.password);
//  };

$scope.items = [1, 2, 3, 4, 5, 6, 7, 8];

    $scope.properties = {
        items: 2,
        onChange: function () {
            console.dir(arguments);
        }
    };




      vm.register = register;

      function register() {

          var register =  { 
			  plan: vm.price,
			  amount: '2.50',
              contactfirstname: vm.formParams.firstName,
              contactlastname:  vm.formParams.lastname,
              language_id: '1',
              email: vm.formParams.email,
              password: vm.formParams.password,
              companyphone: vm.formParams.companyphone,
              companystate: vm.formParams.state.Description,
			  areaCode: vm.formParams.state.Id,
			  number: vm.formParams.number,
			  expiryMonth: month,
			  expiryYear: year,
              cvv: vm.formParams.cvv,
			  cardholder:  vm.formParams.holder,
              userlimit: '0' };
		  
          console.log(register);
          vm.dataLoading = true;


          RegisterService.Create(register).then(function (response) {

              if (response.success = true) {
                   swal({
                      title: 'Succesfully Registration',
                      text: '<span style="text-align:left; padding-bottom:15px; ">you have registered, please check your email for activation of account</span>',
                      html: true
                  });
                  $state.go('login', {}, {reload: true});
              } else {
                  console.log(response);
                  vm.dataLoading = false;
                  swal({
                      title: 'Registration was unsuccessful',
                      text: '<span style="text-align:left; padding-bottom:15px; ">please check your details you entered in</span>',
                      html: true
                  });
              }
          });
      }

 
 
  }

})();