/* ** Views/Partial/LCA/BOM.cshtml is using this controller */ angular.module('CarbonFootprint') .controller('SignificanceAssmtController', ['$scope', '$http', '$routeParams', 'MultiLanguageService', '$q', '$filter', '$timeout', 'Notification', function ($scope, $http, $routeParams, MultiLanguageService, $q, $filter, $timeout, Notification) { // Multi-language resource object var resource, inited = false, _timeout; // This property controlls modal form, will be passed to table-edit-modal directive $scope.modalFormOption = {}; $scope.model = []; $scope.lifeCycleStage = { value: '' }; $scope.gradingScore = 100; $scope.showHighRisk = false; // set $scope.baseUrl var baseUrl = $scope.baseUrl; if (typeof (baseUrl) === 'undefined' || baseUrl == null) { var arr = window.location.href.split("/"); if (arr[3].indexOf('app') == 0) baseUrl = ''; else baseUrl = '/' + arr[3]; $scope.baseUrl = baseUrl; } $scope.getStageLabel = function (stage) { return $scope.stageOptions[stage]; }; $scope.getStageOption = function (stage) { var index = stage.indexOf("C"); return stage.substring(0, index); }; $scope.changeStage = function () { //console.log($scope.lifeCycleStage.value); getAllDataAsync($routeParams.LCAID) .then(function (response) { $scope.model = response.Significances; //$scope.stageOptions = response.StageOptions; }, function (error) { console.log(error); }); }; $scope.getCategoryLabel = function (category) { return 'category' + category }; $scope.getSignificance = function (significance) { return significance == 0 ? 'non-significant' : 'significant'; }; $scope.hasFactor = function (assmtFactorId) { return Enumerable.From($scope.assmtFactors) .Where(function (x) { return x.ID == assmtFactorId; }).Count() >= 1; }; $scope.getFactorName = function (assmtFactorId) { return Enumerable.From($scope.assmtFactors) .Where(function (x) { return x.ID == assmtFactorId; }) .First().FactorName; }; $scope.getFactorScores = function (assmtFactorId) { return Enumerable.From($scope.assmtFactors) .Where(function (x) { return x.ID == assmtFactorId; }) .First().FactorScores; }; $scope.changeHighRisk = function () { getAllDataAsync($routeParams.LCAID) .then(function (response) { $scope.model = response.Significances; }); }; $scope.changeScore = function (factorScores) { //console.log('changeScore', factorScores); var score = 1; angular.forEach(factorScores, function (factor) { score *= factor.Score; }); $scope.selectRow.toBeEdit.Score = score; $scope.selectRow.toBeEdit.Significance = score >= $scope.gradingScore ? 1 : 0; }; $scope.changeGradingScore = function () { // if there is already a timeout in process cancel it if (_timeout) { $timeout.cancel(_timeout); } _timeout = $timeout(function () { $scope.gradingScore = $scope.gradingScore || 100; $http.post($scope.baseUrl+"/api/SignificanceAssmt/UpdateGradingScore/" + $routeParams.LCAID + "/" + $scope.gradingScore) .success(function () { $scope.updateGradingScore($scope.gradingScore); angular.forEach($scope.model, function (item) { item.Significance = item.Score >= $scope.gradingScore ? 1 : 0; }); }); _timeout = null; }, 500); }; $scope.onListDefaultPurposeChange = function () { $http.post($scope.baseUrl+"/api/SignificanceAssmt/UpdateListDefaultPurpose/" + $routeParams.LCAID + "/" + $scope.listDefaultPurpose) .success(function () { console.log('UpdateListDefaultPurpose done'); }) .error(function (error) { console.log(error); Notification.error({ message: error.ExceptionMessage, positionX: 'center', delay: 3000 }); }); }; $scope.createProcess = function () { $http.post($scope.baseUrl+'/api/SignificanceAssmt/Sync/' + $routeParams.LCAID) .success(function (data) { getAllDataAsync($routeParams.LCAID) .then(function (response) { $scope.model = response.Significances; }, function (error) { console.log(error); Notification.error({ message: error.ExceptionMessage, positionX: 'center', delay: 3000 }); }); }) .error(function (error) { console.log(error); Notification.error({ message: error.ExceptionMessage, positionX: 'center', delay: 3000 }); }); }; $scope.editProcess = function (selected) { if (selected.FactorScores.length < 2) { alert('評估因子少於兩個,請先新增評估因子'); return; } var toBeEdit = $scope.selectRow.toBeEdit = angular.copy(selected); $scope.modalFormOption.show = true; $scope.save = function () { if (!validateModel(toBeEdit)) { return; } $http.post($scope.baseUrl+'/api/SignificanceAssmt/Save/' + toBeEdit.LCAID + "/" + toBeEdit.ID, toBeEdit) .success(function (data) { angular.copy(data, selected); $scope.modalFormOption.show = false; }); }; }; $scope.deleteProcess = function (selected) { var isConfirm = confirm(resource.DeleteItemMsg); if (isConfirm) { $http.delete($scope.baseUrl+"/api/SignificanceAssmt/Delete/" + selected.ID) .success(function () { var index = $scope.model.indexOf(selected); $scope.model.splice(index, 1); // This operate is base on selectRow directive in TableEdit.js file $scope.selectRow.unSelect(); }); } }; $scope.$on('changeSelectedTab', function (event, args) { //if (!inited && args.selectedTab == 'SignificanceAssmt') { // inited = true; // Get data and prepare $scope.model getAllDataAsync($routeParams.LCAID) .then(function (response) { //console.log('getAllDataAsync', response); $scope.model = response.Significances; $scope.stageOptions = response.StageOptions; $scope.assmtFactors = response.AssmtFactors; $scope.gradingScore = response.GradingScore; $scope.listDefaultPurpose = response.ListDefaultPurpose; }, function (error) { console.log(error); }); // Get multilanguage resource MultiLanguageService.getResourceAsync() .then(function (response) { resource = response; }, function (error) { console.log(error); }); //} }); /** * Get all list data from server * @param {[Int]} : LCAID * @return {[promise]} */ function getAllDataAsync(LCAID) { var deferred = $q.defer(); $http.get($scope.baseUrl+'/api/SignificanceAssmt/GetByLcaId/' + LCAID + '?lifeCycleStage=' + ($scope.lifeCycleStage.value || 0)) .success(function (data) { deferred.resolve(data); }) .error(function () { deferred.resolve(null); }) return deferred.promise; } function validateModel(model) { var score = model.Score || 0; if (score <= 0) { Notification.error({ message: '總分必须大于零', positionX: 'center', delay: 3000 }); return false; } return true; } }]);