demo20230512/Browser_Local/js/Directive/ReplyMode.js
2023-05-12 10:20:28 +08:00

101 lines
5.0 KiB
JavaScript

/*
Logic Solution
WeeeCarbonFootprint
Template Directive
paramsObj:{
selectedRequest: ProductLCAReplyRequest in LCA.cs
}
*/
angular.module('View.Directive')
.directive('replyMode', function (CONSTANTS) {
return {
restrict: 'E',
templateUrl: CONSTANTS.TB + 'reply_mode',
scope: {
paramsObj: '=params'
},
link: function (scope) {
//this logic is some how reated to ProductLCAReplyRequest.SupplierLCAStatus
scope.$watch('paramsObj.selectedRequest', function (newValue) {
if (newValue == null) {//do nothing
}
else if (newValue.RepliedLCAID != null) {
scope.reply_mode_selected = 2;
angular.forEach(scope.model.productLCAs, function (entry) {
if (entry.ID == newValue.RepliedLCAID) {
scope.selectedProductLCA = entry;
scope.previewReplyValue(scope.paramsObj.selectedRequest, scope.selectedProductLCA);
}
})
} else if (newValue.RepliedValue != null) {
scope.reply_mode_selected = 3;
}
else {
scope.reply_mode_selected = "";
}
})
},
controller: function ($scope, $location, $http, $q, $window, MultiLanguageService) {
$scope.model = {};
if (typeof (baseUrl) === 'undefined' || baseUrl == null) {
var arr = window.location.href.split("/");
if (arr[3].indexOf('app') == 0)
baseUrl = '';
else
baseUrl = '/' + arr[3];
}
//console.log('replyMode', baseUrl);
$q.all([$http.get(baseUrl+'/api/LCA/GetMyProductLCA'),
MultiLanguageService.getResourceAsync()]).then(function (response) {
$scope.model.productLCAs = response[0].data;
var resource = response[1];
$scope.StartReplyLCA = function (replyMode, selectedProductLCA) {
//console.log('StartReplyLCA', replyMode, $scope.paramsObj.selectedRequest.Uid);
if (replyMode == 1) {
//this is bad , because reply mode directive should not modify ouside parameter
$scope.paramsObj.show = false;
$location.path('/LCA/ProductInfoSurvey/' + $scope.paramsObj.selectedRequest.Uid);
}
else if (replyMode == 2) {
$http.post(baseUrl+'/api/LCA/ReplyRequest/' + $scope.paramsObj.selectedRequest.Uid + '/' + selectedProductLCA.ID + '/' + $scope.paramsObj.selectedRequest.DivideBy).then(function (response) {
angular.copy(response.data, $scope.paramsObj.selectedRequest);
if (!selectedProductLCA.CanReplyOtherRequest) {
if (confirm(resource['StaticLabelInfoReplyModeString6'])) {
$scope.paramsObj.show = false;
$location.path('/LCA/Detail/' + selectedProductLCA.ID);
}
} else {
alert(resource['StaticLabelInfoReplyModeString7']);
$scope.paramsObj.show = false;
}
})
}
else if (replyMode == 3) {
$window.location.href = '/?uid=' + $scope.paramsObj.selectedRequest.Uid
}
}
$scope.previewReplyValue = function (lcaRequest, selectedProductLCA) {
lcaRequest.RepliedValue = selectedProductLCA.DistributeKgCO2e;
//if (selectedProductLCA.CanReplyOtherRequest) {
// if (lcaRequest.DivideBy == 0)
// lcaRequest.RepliedValue = selectedProductLCA.DistributeKgCO2eInWeight
// else if (lcaRequest.DivideBy == 1)
// lcaRequest.RepliedValue = selectedProductLCA.DistributeKgCO2eInArea
// else if (lcaRequest.DivideBy == 2)
// lcaRequest.RepliedValue = selectedProductLCA.DistributeKgCO2eInWorkHour
// else
// lcaRequest.RepliedValue = "";;//throw "NYI"
//}
//else {
// lcaRequest.RepliedValue = "";
//}
}
})
}
}
})