define(['dashboard/dashboard'], function (dashboard) { dashboard.controller('DashboardProfile', ['$scope', '$state', '$http', '$mdDialog', 'Helpers', function ($scope, $state, $http, $mdDialog, Helpers) { $scope.EditFinancialDetails = false; $scope.PostalAddressSame = false; $scope.PostalAddressValid = true; $scope.PostalAddressEmpty = false; HHMRemove = false; $scope.PostalAddress = {}; if (($scope.Dashboard.Policy.AccountDetails.PostalAddress.Address1 == $scope.Dashboard.Policy.AccountDetails.ResidentialAddress.Address1 || $scope.Dashboard.Policy.AccountDetails.PostalAddress.Address1 == "") && ($scope.Dashboard.Policy.AccountDetails.PostalAddress.Address2 == $scope.Dashboard.Policy.AccountDetails.ResidentialAddress.Address2 || $scope.Dashboard.Policy.AccountDetails.PostalAddress.Address2 == "") && ($scope.Dashboard.Policy.AccountDetails.PostalAddress.Address3 == $scope.Dashboard.Policy.AccountDetails.ResidentialAddress.Address3 || $scope.Dashboard.Policy.AccountDetails.PostalAddress.Address3 == "") && ($scope.Dashboard.Policy.AccountDetails.PostalAddress.SuburbName == $scope.Dashboard.Policy.AccountDetails.ResidentialAddress.SuburbName || $scope.Dashboard.Policy.AccountDetails.PostalAddress.SuburbName == "") && ($scope.Dashboard.Policy.AccountDetails.PostalAddress.PostalCode == $scope.Dashboard.Policy.AccountDetails.ResidentialAddress.PostalCode || $scope.Dashboard.Policy.AccountDetails.PostalAddress.PostalCode == "")) { $scope.PostalAddressSame = true; $scope.PostalAddressEmpty = true; $scope.PostalAddress = { Address1: angular.copy($scope.Dashboard.Policy.AccountDetails.ResidentialAddress.Address1), Address2: angular.copy($scope.Dashboard.Policy.AccountDetails.ResidentialAddress.Address2), Address3: angular.copy($scope.Dashboard.Policy.AccountDetails.ResidentialAddress.Address3), Suburb: { SuburbName: angular.copy($scope.Dashboard.Policy.AccountDetails.ResidentialAddress.SuburbName), PostCode: angular.copy($scope.Dashboard.Policy.AccountDetails.ResidentialAddress.PostalCode) }, PostalAddressSame: $scope.PostalAddressSame, UpdateMessage: null, UpdateSuccess: null, Loading: false }; } else { $scope.PostalAddress = { Address1: angular.copy($scope.Dashboard.Policy.AccountDetails.PostalAddress.Address1), Address2: angular.copy($scope.Dashboard.Policy.AccountDetails.PostalAddress.Address2), Address3: angular.copy($scope.Dashboard.Policy.AccountDetails.PostalAddress.Address3), Suburb: { SuburbName: angular.copy($scope.Dashboard.Policy.AccountDetails.PostalAddress.SuburbName), PostCode: angular.copy($scope.Dashboard.Policy.AccountDetails.PostalAddress.PostalCode) }, PostalAddressSame: $scope.PostalAddressSame, UpdateMessage: null, UpdateSuccess: null, Loading: false }; } $scope.ContactDetails = { EmailAddress: angular.copy($scope.Dashboard.Policy.AccountDetails.PolicyHolder.Email), ContactNumber: angular.copy($scope.Dashboard.Policy.AccountDetails.PolicyHolder.ContactNumber), FirstName: angular.copy($scope.Dashboard.Policy.AccountDetails.PolicyHolder.FirstName), LastName: angular.copy($scope.Dashboard.Policy.AccountDetails.PolicyHolder.LastName), UpdateMessage: null, UpdateSuccess: null, Loading: false }; $scope.Dashboard.Policy.AccountDetails.PolicyHolder.BirthDate.setHours(12); var paymentStatus = ''; if ($scope.Dashboard !== null) paymentStatus = $scope.Dashboard.Policy.FinancialDetails.PaymentDetails.PaymentStatus; $scope.Profile = { HasPolicyStatus: (paymentStatus == null || paymentStatus == ""), IsActivePolicyStatus: false }; if (paymentStatus !== null) $scope.Profile.IsActivePolicyStatus = $scope.Profile.HasPolicyStatus && ((paymentStatus.indexOf("Your premium has been paid") !== -1) || (paymentStatus.indexOf("is active") !== -1) || paymentStatus === "Active") $scope.OriginalDetails = angular.copy($scope.ContactDetails); hiddenprop($scope, 'EmailChanged', function () { var hasChanged = $scope.ContactDetails.EmailAddress != $scope.OriginalDetails.EmailAddress; return hasChanged; }); hiddenprop($scope, 'ContactNumberChanged', function () { var hasChanged = $scope.ContactDetails.ContactNumber != $scope.OriginalDetails.ContactNumber; return hasChanged; }); hiddenprop($scope, 'FirstNameChanged', function () { var hasChanged = $scope.ContactDetails.FirstName != $scope.OriginalDetails.FirstName; return hasChanged; }); hiddenprop($scope, 'LastNameChanged', function () { var hasChanged = $scope.ContactDetails.LastName != $scope.OriginalDetails.LastName; return hasChanged; }); $scope.offsetEdit = function (person) { return (person.IsPolicyHolder) ? 'col-md-offset-7' : 'half-col-padding-right'; }; $scope.UpdateContactDetails = function () { $scope.ContactDetails.Loading = true; $scope.ContactDetails.EmailChanged = $scope.EmailChanged; $scope.ContactDetails.ContactNumberChanged = $scope.ContactNumberChanged; $scope.ContactDetails.FirstNameChanged = $scope.FirstNameChanged; $scope.ContactDetails.LastNameChanged = $scope.LastNameChanged; $scope.DashboardPost('DashboardProfile/UpdateContactDetails/', $scope.ContactDetails) .then(function (data) { $scope.ContactDetails.UpdateMessage = data.data.Message; $scope.ContactDetails.UpdateSuccess = data.data.Success; if ($scope.ContactDetails.UpdateSuccess) { $scope.Dashboard.Policy.AccountDetails.PolicyHolder.Email = data.data.NewEmailAddress; $scope.Dashboard.Policy.AccountDetails.PolicyHolder.ContactNumber = data.data.NewContactNumber; $scope.Dashboard.Policy.AccountDetails.PolicyHolder.FirstName = data.data.NewFirstName; $scope.Dashboard.Policy.AccountDetails.PolicyHolder.LastName = data.data.NewLastName; $scope.OriginalDetails = angular.copy($scope.ContactDetails); } $scope.ContactDetails.Loading = false; }, function (data) { $scope.ContactDetails.Loading = false; }); } $scope.OriginalPostalDetails = angular.copy($scope.PostalAddress); hiddenprop($scope, 'Address1Changed', function () { var hasChanged = $scope.PostalAddress.Address1 != $scope.OriginalPostalDetails.Address1; return hasChanged; }); hiddenprop($scope, 'Address2Changed', function () { var hasChanged = $scope.PostalAddress.Address2 != $scope.OriginalPostalDetails.Address2; return hasChanged; }); hiddenprop($scope, 'Address3Changed', function () { var hasChanged = $scope.PostalAddress.Address3 != $scope.OriginalPostalDetails.Address3; return hasChanged; }); hiddenprop($scope, 'SuburbNameChanged', function () { var hasChanged = false; if ($scope.PostalAddress.Suburb.SuburbName != undefined && $scope.OriginalPostalDetails.Suburb.SuburbName != undefined) { hasChanged = $scope.PostalAddress.Suburb.SuburbName.toUpperCase() != $scope.OriginalPostalDetails.Suburb.SuburbName.toUpperCase(); } return hasChanged; }); hiddenprop($scope, 'PostalCodeChanged', function () { var hasChanged = $scope.PostalAddress.Suburb.PostCode != $scope.OriginalPostalDetails.Suburb.PostCode; return hasChanged; }); $scope.$watch("PostalAddress.PostalAddressSame", function (v) { if (v) { $scope.PostalAddress.Address1 = $scope.Dashboard.Policy.AccountDetails.ResidentialAddress.Address1; $scope.PostalAddress.Address2 = $scope.Dashboard.Policy.AccountDetails.ResidentialAddress.Address2; $scope.PostalAddress.Address3 = $scope.Dashboard.Policy.AccountDetails.ResidentialAddress.Address3; $scope.PostalAddress.Suburb = { SuburbName: angular.copy($scope.Dashboard.Policy.AccountDetails.ResidentialAddress.SuburbName), PostCode: angular.copy($scope.Dashboard.Policy.AccountDetails.ResidentialAddress.PostalCode) } } else { if (!$scope.PostalAddressEmpty) { $scope.PostalAddress = { Address1: angular.copy($scope.Dashboard.Policy.AccountDetails.PostalAddress.Address1), Address2: angular.copy($scope.Dashboard.Policy.AccountDetails.PostalAddress.Address2), Address3: angular.copy($scope.Dashboard.Policy.AccountDetails.PostalAddress.Address3), Suburb: { SuburbName: angular.copy($scope.Dashboard.Policy.AccountDetails.PostalAddress.SuburbName), PostCode: angular.copy($scope.Dashboard.Policy.AccountDetails.PostalAddress.PostalCode) }, PostalAddressSame: $scope.PostalAddressSame, UpdateMessage: null, UpdateSuccess: null, Loading: false }; } $scope.PostalAddress.PostalAddressSame = v; } }); $scope.validateAddress = function (address) { var ac = $scope.PolicyHolderPostalAddress.Address1; if (ac) ac.$setValidity("number", !address.Address1 || !!address.Address1.match(/\d/)); //return $http({ // method: 'POST', // url: '/Quote/AcceptHouseholdMember/ValidateEntireAddress', // data: { // address1: address.Address1, // address2: address.Address2, // address3: address.Address3, // } //}).success(function (data, status, headers, config) { // $scope.PostalAddressValid = data; //}); } $scope.$watch('PostalAddressValid', function (v) { if ($scope.PolicyHolderPostalAddress != undefined) { $scope.PolicyHolderPostalAddress.Address1.$setValidity("addressValid", v || $scope.PostalAddress.PostalAddressSame); } }); $scope.$watch("PostalAddress.Address1", function () { if ($scope.PolicyHolderPostalAddress != undefined) { $scope.validateAddress($scope.PostalAddress); } }); $scope.UpdatePostalDetails = function () { $scope.PostalAddress.Loading = true; $scope.PostalAddress.Address1Changed = $scope.Address1Changed; $scope.PostalAddress.Address2Changed = $scope.Address2Changed; $scope.PostalAddress.Address3Changed = $scope.Address3Changed; $scope.PostalAddress.SuburbNameChanged = $scope.SuburbNameChanged; $scope.PostalAddress.PostalCodeChanged = $scope.PostalCodeChanged; $scope.DashboardPost('DashboardProfile/UpdatePostalDetails/', $scope.PostalAddress) .then(function (data) { $scope.PostalAddress.UpdateMessage = data.data.Message; $scope.PostalAddress.UpdateSuccess = data.data.Success; if ($scope.PostalAddress.UpdateSuccess) { $scope.Dashboard.Policy.AccountDetails.PostalAddress.Address1 = data.data.NewAddress1; $scope.Dashboard.Policy.AccountDetails.PostalAddress.Address2 = data.data.NewAddress2; $scope.Dashboard.Policy.AccountDetails.PostalAddress.Address3 = data.data.NewAddress3; $scope.Dashboard.Policy.AccountDetails.PostalAddress.SuburbName = data.data.NewSuburbName; $scope.Dashboard.Policy.AccountDetails.PostalAddress.PostalCode = data.data.NewPostalCode; $scope.OriginalPostalDetails = angular.copy($scope.PostalAddress); } $scope.PostalAddress.Loading = false; }, function (data) { $scope.PostalAddress.Loading = false; }); } //for suburb autocomplete. TODO: replace with directive $scope.suburbAutocompleteFormatter = function suburbAutocompleteFormatter(value) { if (value && value.SuburbName) { return "" + value.SuburbName + ' ' + value.PostCode; } else { return ""; } } $scope.PasswordDetails = { CurrentPassword: null, NewPassword: null, ConfirmPassword: null, UpdateMessage: null, UpdateSuccess: null, Loading: false }; var doesPasswordMatch = function () { if ($scope.PasswordDetails.NewPassword && $scope.PasswordDetails.ConfirmPassword) { var isMatch = ($scope.PasswordDetails.NewPassword == $scope.PasswordDetails.ConfirmPassword) $scope.UpdatePasswordForm.ConfirmPassword.$setValidity("passwordMatch", isMatch); } } $scope.$watch('PasswordDetails.ConfirmPassword', doesPasswordMatch); $scope.$watch('PasswordDetails.NewPassword', doesPasswordMatch); $scope.UpdatePassword = function () { $scope.PasswordDetails.Loading = true; $scope.DashboardPost('DashboardProfile/UpdatePassword/', $scope.PasswordDetails) .then(function (data) { $scope.PasswordDetails.UpdateMessage = data.data.Message; $scope.PasswordDetails.UpdateSuccess = data.data.Success; $scope.PasswordDetails.CurrentPassword = null; $scope.PasswordDetails.NewPassword = null; $scope.PasswordDetails.ConfirmPassword = null; $scope.PasswordDetails.Loading = false; }, function (data) { $scope.PasswordDetails.Loading = false; }); }; // Household Member stuff $scope.busyDelete = false; var RemoveDialog = function (ev) { var template = Helpers.dialog('Confirm'); return $mdDialog.show({ clickOutsideToClose: true, disableParentScroll: false, scope: $scope, preserveScope: true, targetEvent: ev, template: template, controller: 'DashboardProfile' }); }; $scope.deleteHH = function deleteHH(person, ev) { if (!$scope.busyDelete) { $scope.PersonalNumber = person.PersonNumber; $scope.ReferenceNumber = $scope.Dashboard.Policy.ReferenceNumber; $scope.portableList = person.PortablePossessions; $scope.vehicles = person.Vehicles; $scope.isRegularDriver = person.IsRegularDriver; //show the confirm dialog RemoveDialog(ev); } }; $scope.updateHH = function (person) { if (!$scope.busyDelete) { var amendmentType = 'regularDriver'; if (!person.IsRegularDriver) amendmentType = 'householdMember' window.location = "/quote/?quoteType=amendment&amendmentType=" + amendmentType + "&amendmentMode=edit&referenceNumber=" + $scope.Dashboard.Policy.ReferenceNumber + "&sequenceNumber=" + person.PersonNumber; } }; $scope.acceptRemove = function (choice) { $scope.busyDelete = false; var reassign = false; var amendmentType = ''; if ($scope.isRegularDriver) amendmentType = 'regularDriver'; else amendmentType = 'householdMember'; if (($scope.portableList && $scope.portableList.length) <= 0 && ($scope.vehicles && $scope.vehicles.length <= 0)) { $scope.busyDelete = true; $http({ method: 'POST', url: "/dashboard/" + $scope.Dashboard.Policy.PolicyNumber + "/DashboardHouseholdMembers/DeleteHH", data: { ReferenceNumber: $scope.ReferenceNumber, PersonalNumber: $scope.PersonalNumber, PolicyNumber: $scope.Dashboard.Policy.PolicyNumber } }).then(function (results) { // window.location = "/dashboard/" + $scope.Dashboard.Policy.PolicyNumber + "/householdmembers"; HHMRemove = false; window.location.reload(true); }); } else { var url = "/quote/?quoteType=amendment&amendmentType=" + amendmentType + "&amendmentMode=delete&sequenceNumber=" + $scope.PersonalNumber + "&referenceNumber=" + $scope.ReferenceNumber; if (choice === 'reassign') url += "&reassign=true"; window.location = url; } $mdDialog.hide(); }; $scope.cancelRemove = function () { HHMRemove = false; $mdDialog.hide(); }; }]); });