﻿app.controller("ChangePwdController", function ($scope, $http, $log, $window) {
    var Base = $scope.baseUrl;

    $scope.ExistingPwd = function () {
        $scope.Logindata = sessionStorage.getItem("loginSession");
        if ($scope.Logindata) {
            $scope.Logindata = JSON.parse($scope.Logindata);
            $scope.Userid = $scope.Logindata.UserID;
        }
        var GetAll = new Object();
        GetAll.UserId = $scope.Userid;
        GetAll.Password = $scope.ExistPwd;
        GetAll.Condition = 'ChkExistpwd';

        $http({
            url: Base + "Login/AdminProfileUpdate",
            method: 'POST',
            data: GetAll
        }).then(function (response) {
            if (response.data.ds.Table[0].Result === "False") {
                alert("Your Existing Password is not Valid");
                $scope.ExistPwd = "";
            }

        }, function (response) {
            alert('Error' + response.status + ': ' + response.data);
        });
    };

    $scope.OnUpdate = function () {
        debugger;
        $scope.Logindata = sessionStorage.getItem("loginSession");
        if ($scope.Logindata) {
            $scope.Logindata = JSON.parse($scope.Logindata);
            $scope.Userid = $scope.Logindata.UserID;
        }
        var GetAll = new Object();
        GetAll.UserId = $scope.Userid;
        GetAll.Password = $scope.EPassword;
        GetAll.Condition = 'UpdatePwd';
        if ($scope.ExistPwd !== "" && $scope.ExistPwd !== undefined) {
            if ($scope.EPassword !== "" && $scope.EPassword !== undefined && $scope.ECPassword !== undefined && $scope.ECPassword !== "") {
                if ($scope.EPassword === $scope.ECPassword) {
                    $http({
                        url: Base + "Login/AdminProfileUpdate",
                        method: 'POST',
                        data: GetAll
                    }).then(function (response) {
                        if (response.data.ds.Table[0].Result === "True") {
                            alert("Password Updated Successfully..");
                            window.location.href = "/Dashboard";
                        }
                        else {
                            alert("Password Not Updated..Please Try again later");

                        }

                    }, function (response) {
                        alert('Error' + response.status + ': ' + response.data);
                    });
                }
                else {
                    alert('Passwords not matched..Please check');
                }
            }
            else {
                alert('Please fill Mandatory feilds');
            }
        }
        else {
            alert('Existing Password should not be empty');
        }

    };
});

app.directive("compareTo", function () {
    return {
        require: "ngModel",
        scope:
        {
            confirmPassword: "=compareTo"
        },
        link: function (scope, element, attributes, modelVal) {
            modelVal.$validators.compareTo = function (val) {
                return val === scope.confirmPassword;
            };
            scope.$watch("confirmPassword", function () {
                modelVal.$validate();
            });
        }
    };
});  