"use strict"; (function () { 'use strict'; angular.module('mcsmModule') .directive('mcsmDetails', ['mcsmModel', '$filter', 'systemAlertService', 'i18nService', function (mcsmModel, $filter, systemAlertService, i18nService) { return { restrict: 'E', templateUrl: 'views/mcsm/mcsm-details.html', scope: { ticket: '=' }, link: function (scope) { scope.manualAssociationSupportingSystems = []; scope.manualAssociationSupportingSystemsFilter = ''; scope.isManualAssociationInProgress = false; scope.allowManualAssociationToMultipleVendors = false; scope.ticketType = i18nService.getLocalizedString('ticket.type.' + scope.ticket.type) || scope.ticket.type; scope.manualAssociationHelpText = i18nService.getLocalizedStringwithParams('ticket.vendor.label.helpText', [scope.ticketType, scope.ticketType, scope.ticketType]); mcsmModel.getManualAssociationSupportingSystems(scope.ticket.type) .then(function (response) { scope.manualAssociationSupportingSystems = response; if (scope.manualAssociationSupportingSystems.length > 1 && !scope.allowManualAssociationToMultipleVendors) { scope.manualAssociationHelpText += ' ' + i18nService.getLocalizedString('ticket.vendor.label.helpTextSingleVendor'); scope.$watchCollection('ticket.vendorInfo', function (newVendorInfo) { if (_.isArray(newVendorInfo) && newVendorInfo[0] && newVendorInfo[0].vendor && newVendorInfo[0].vendor.id) { scope.manualAssociationSupportingSystemsFilter = newVendorInfo[0].vendor.id; } else { scope.manualAssociationSupportingSystemsFilter = ''; } }); } }); scope.startManualAssociation = function (systemSelected) { scope.isManualAssociationInProgress = true; mcsmModel.updateAssociation(scope.ticket.type, scope.ticket.displayId, systemSelected.id) .then(function (response) { scope.ticket.vendorInfo = response; scope.ticket.brokerVendorName = systemSelected.providerName || systemSelected.name; }) .catch(function (error) { systemAlertService.error({ text: (error.data && (error.data.detailedMessage || error.data.error)) || error, clear: false }); }) .finally(function () { scope.isManualAssociationInProgress = false; }); }; scope.removeAssociation = function (item, $event) { $event.stopPropagation(); var modalInstance = systemAlertService.modal({ title: $filter('i18n')('common.notification.delete.title'), text: $filter('i18n')('common.notification.delete.message'), buttons: [ { text: $filter('i18n')('common.notification.delete.button1'), data: true }, { text: $filter('i18n')('common.notification.delete.button2'), data: false } ] }); modalInstance.result.then(function (data) { if (data) { item.isRemoveAssociationInProgress = true; mcsmModel.removeAssociation(scope.ticket.type, scope.ticket.displayId, item.id, item.vendor.id) .then(function () { systemAlertService.success({ text: $filter('i18n')('ticket.notification.unlink.message', 1), hide: 10000 }); _.remove(scope.ticket.vendorInfo, { id: item.id }); if (scope.ticket.vendorInfo.length === 0) { scope.ticket.brokerVendorName = ''; } }) .catch(function (error) { systemAlertService.error({ text: (error.data && (error.data.detailedMessage || error.data.error)) || error, clear: false }); }) .finally(function () { item.isRemoveAssociationInProgress = false; }); } }); }; } }; }]); })();