/* Setup general page controller */
angular
.module('MetronicApp')
.controller('incAdverseeventReport_check01_controller', ['$rootScope', '$scope', '$http', '$modal', '$state', '$location', 'settings', 'messageUtils', 'activitiService',  function($rootScope, $scope, $http, $modal, $state, $location, settings, messageUtils, activitiService) {
	$scope.$on('$viewContentLoaded', function() {   
		// initialize core components
		App.initAjax();

		// set default layout mode
		$rootScope.settings.layout.pageContentWhite = true;
		$rootScope.settings.layout.pageBodySolid = false;
		$rootScope.settings.layout.pageSidebarClosed = true;
	});

	
	/**
	 * 评分项目
	 */
	$scope.loadEstList = function (estItemCode, scope, target) {
		var uri = '../inc/eventreport/incEstimationItems'+'?' + 'id.estItemCode=' + estItemCode;
		$http.get(uri)
		.success(function(data){
			scope[target] = data;
		})
		.error(function (error){
			console.log(error);
		});
	}
	$scope.loadEstList('INC_ADV_EVENT_TYPE', $scope, 'est_list');
	
	$scope.taskId = $location.search()['taskId'];

	$scope.processInstanceId = $location.search()['processInstanceId'];
	
	$scope.incAdverseeventReport = {};

	$scope.loadIncAdverseeventReport = function () {
		var uri = '../inc/eventreport/incAdverseeventReports/processInstanceId/' + $scope.processInstanceId;
		$http.get(uri)
		.success(function(data){
			console.log(data);
			$scope.incAdverseeventReport = data.item;
			$scope.pat_info = angular.fromJson($scope.incAdverseeventReport.patInfo);
			$scope.seletIncTypes = angular.fromJson($scope.incAdverseeventReport.incType);
			$scope.comments = data.comments;	// 加载流程comments
		})
		.error(function (error){
			console.log(error);
		});
	}
	$scope.loadIncAdverseeventReport();
	
	/**
	 * 点击提交按钮
	 */
	$scope.complete = function (){
		bootbox.dialog({  
			message: "确定提交流程？",  
			buttons: {  
				Cancel: {  
					label: "取消",  
					className: "btn-default",  
					callback: function () {  
						messageUtils.warning('已取消'); 
					}  
				}, 
				OK: {  
					label: "确定",  
					className: "btn-primary",  
					callback: function () {  
						$scope.submitTask();
					}  
				}  
			}  
		});
	};

	/**
	 * 提交任务流程
	 */
	$scope.submitTask = function (){
		var promise = activitiService.complete($scope.taskId);
		promise.then(function(data) {  
			messageUtils.success('流程已提交！');
			$state.go('inc_drug_report');
		}, function(data) {  // 处理错误 .reject  
			messageUtils.danger('流程提交失败');
		});
	}

	/**
	 * 提交流程批注
	 */
	$scope.createTaskComment = function () {
		activitiService.createTaskComment($scope.taskId, $scope.taskComment);
		messageUtils.success('流程意见已提交！');
		$scope.taskComment = null;
		$scope.loadIncAdverseeventReport();
	}
}]);
