"use strict";
appCore.controller("ResetPasswordCtrl", ["$scope", "$http", "$timeout", "vServices", "jwtHelper", "authManager", "$location", "$state", function ($scope, $http, $timeout, vServices, jwtHelper, authManager, $location, $state) {
  $scope.data = {emp_password: "", emp_confirmpassword: "", captcha: null};
  $scope.params = $state.params;
  $scope.resetPasswordValid = false;

  $scope.resetPasswordSubmit = function () {
    if (!$scope.data.emp_password) {
      toastr.warning("Please fill the Password.", "Warning!", {timeOut: 5000});
    }
    else if (!$scope.data.emp_confirmpassword) {
      toastr.warning("Please fill the Confirm Password.", "Warning!", {timeOut: 5000});
    }
    else if ($scope.data.emp_password !== $scope.data.emp_confirmpassword) {
      toastr.error("Your new password and confirmation password do not match", "Error!", {timeOut: 5000});
    }
    else {
      grecaptcha.ready(function () {
        grecaptcha.execute($scope.$root.constant.googleCaptchaSiteKey, {action: "login"}).then(function (captcha) {
          $scope.data.captcha = captcha;
          vServices.callApi("auth/reset_password/"+ $scope.params.token, "POST", $scope.data, {}, true).then(function (res, err) {
            if (res) {
              toastr.success(res, "success", {timeOut: 5000});
              $(document).find('div.grecaptcha-badge').remove();
              $state.go("login", {}, {});
            }
            else if (err) {
              toastr.error(res, "Error", {timeOut: 5000});
            }
          });
        });
      });
    }
  };
  const init = function () {
    vServices.callApi("auth/reset_password/" + $scope.params.token, "GET", $scope.data, {}, true).then(function (res, err) {
      if (res) {
        toastr.success(res, "success", {timeOut: 5000});
        $scope.resetPasswordValid = true;
      }
      else {
        $state.go("v404", {}, {reload: true});
      }
    });
  };
  init();
}]);
