define(['quote/quote'], function (quote) {
quote.controller('TermsAndConditions', ['$scope', '$mdDialog', 'Helpers', '$http', function ($scope, $mdDialog, Helpers, $http) {
$scope.deductionsLoading = false;
$scope.$watch('deductionsLoading', function (val) {
if (val == true) {
$scope.SetStepLoading();
$scope.SetStepHeader('
We\'re busy processing your Auto & General Insurance agreement
', 'You are mere moments away from getting cover that is always there for you.');
}
else if (val == false) {
$scope.SetStepHeader('Terms and conditions
', '');
$scope.SetStepLoaded();
}
});
var termsDialogTemplate = Helpers.dialog("termsDialog");
var disclaimerDialogTemplate = Helpers.dialog("disclaimerDialog");
function dialogFunction(template, accept, reject) {
return function dialogFunctionFunction(event) {
$mdDialog.show({
clickOutsideToClose: true,
disableParentScroll: false,
scope: $scope,
preserveScope: true,
targetEvent: event,
template: template,
controller: ['$scope', '$mdDialog', function ($scope, $mdDialog) {
$scope.accept = $mdDialog.hide;
$scope.cancel = $mdDialog.cancel;
}]
}).then(accept, reject);
}
}
$scope.updateDeductions = function () {
// reset deductions t&c's
$scope.deductionsLoading = true;
$scope.deductionsConfirmed = false;
$scope.disclaimerAccepted = false;
$scope.termsAccepted = false;
$scope.deductionsLoadedSuccessfully = false;
$scope.TermsAndConditionsForm.$setPristine();
$scope.TermsAndConditionsForm.$pending = true;
return $http({
method: 'POST',
url: '/Quote/TermsAndConditions/UpdateDeductions',
data: { referenceNumber: $scope.Quote.ReferenceNumber, effectiveDate: $scope.Quote.EffectiveDate }
}).then(function (response) {
$scope.Deductions = response.data;
if (response.status == 200 && $scope.Deductions != null && $scope.Deductions != "") {
$scope.deductionsLoadedSuccessfully = true;
}
else {
$scope.hasAdminFee = false;
for (var i = 0; i < $scope.Deductions.Deduct.length; i++) {
var val = $scope.Deductions.Deduct[i].TranDescription;
if (val.match(/admin fee/ig)) {
$scope.hasAdminFee = true;
}
}
}
}, function () {
}).finally(function () {
$scope.deductionsLoading = false;
$scope.TermsAndConditionsForm.$pending = false;
});
};
var TodaysDate = new Date().getDateOnly();
if (TodaysDate > $scope.Quote.PolicyStartDate)
$scope.minEffectiveDate = TodaysDate;
else
$scope.minEffectiveDate = $scope.Quote.PolicyStartDate;
$scope.maxEffectiveDate = $scope.minEffectiveDate.addDays(45);
$scope.$watch('Quote.EffectiveDate', $scope.updateDeductions);
$scope.Quote.EffectiveDate = $scope.minEffectiveDate;
$scope.showDisclaimer = dialogFunction(disclaimerDialogTemplate, function () { $scope.disclaimerAccepted = true; }, function () { $scope.disclaimerAccepted = false; });
$scope.showTermsAndConditions = dialogFunction(termsDialogTemplate, function () { $scope.termsAccepted = true; }, function () { $scope.termsAccepted = false; });
}]);
});