<commit_before>env = require './env.coffee'
Promise = require 'promise'

MenuCtrl = ($scope) ->
	_.extend $scope,
		env: env
		navigator: navigator

TimeslotCtrl = ($scope, model, $location) ->
	_.extend $scope,
		model: model
		save: ->			
			$scope.model.$save().then =>
				$location.url "/timeslot"
	
	
TimeslotListCtrl = ($scope, collection, $location) ->
	_.extend $scope,
		collection: collection
		create: ->
			$location.url "/timeslot/create"			
		read: (id) ->
			$location.url "/timeslot/read/#{id}"						
		edit: (id) ->
			$location.url "/timeslot/edit/#{id}"			
		delete: (obj) ->
			collection.remove obj
		loadMore: ->
			collection.$fetch()
				.then ->
					$scope.$broadcast('scroll.infiniteScrollComplete')
				.catch alert
				

LocationCtrl = ($scope, model, $location) ->
	_.extend $scope,
		model: model
		save: ->			
			$scope.model.$save().then =>
				$location.url "/location"
	
	
LocationListCtrl = ($scope, collection, $location) ->
	_.extend $scope,
		collection: collection
		create: ->
			$location.url "/location/create"			
		read: (id) ->
			$location.url "/location/read/#{id}"						
		edit: (id) ->
			$location.url "/location/edit/#{id}"			
		delete: (obj) ->
			collection.remove obj
		loadMore: ->
			collection.$fetch()
				.then ->
					$scope.$broadcast('scroll.infiniteScrollComplete')
				.catch alert
	

ResourceCtrl = ($scope, cliModel, model, locationList, $location) ->
	_.extend $scope,
		model: model
		save: ->			
			$scope.model.$save().then =>
				$location.url "/resource"
		
	modelEvents = 
		'Select Location':	'location'
		'UC Facility'	:	'ucFacility'
		'Admin Acceptance' : 'adminAccept'
		
	_.each modelEvents, (modelField, event) =>
		$scope.$on event, (event, item) =>
			$scope.model[modelEvents[event.name]] = item	
	
	$scope.selectLocationList = [new cliModel.Location name: '-- Please select location --', label: '-- Please select location --']
	_.each locationList.models, (location) =>
		location.label = location.name
		$scope.selectLocationList.push location

	cliModel.User.me().then (user) =>
		$scope.me = user
	
	
ResourceListCtrl = ($scope, collection, $location) ->
	_.extend $scope,
		collection: collection
		create: ->
			$location.url "/resource/create"			
		read: (id) ->
			$location.url "/resource/read/#{id}"						
		edit: (id) ->
			$location.url "/resource/edit/#{id}"			
		delete: (obj) ->
			collection.remove obj
		loadMore: ->
			collection.$fetch()
				.then ->
					$scope.$broadcast('scroll.infiniteScrollComplete')
				.catch alert
	
ReservationCtrl = ($scope, cliModel, model, resourceList, timeslotList, $filter, $location, source, $ionicPopup) ->
	_.extend $scope,
		model: model
		resourceList: resourceList
		timeslotList: timeslotList
		save: ->
			saveRecords = ->
				new Promise (fulfill, reject) ->
					_.each selectedTimeslots, (timeslot) ->
						$scope.model = _.omit($scope.model,'_id')			
						$scope.model.time = timeslot				
						$scope.model.$save().catch alert
					fulfill()
			
			backToMainPage = ->
				if source == 'date'
					$location.url "/reservation"
					$location.search "date", $scope.datepickerObject.inputDate.getTime()
				else
					$location.url "/reservationByResource"
					$location.search "resource", $scope.model.resource
			
			selectedTimeslots = _.where(timeslotList.models, {checked: true})
			saveRecords().then ->
				backToMainPage()
						
		datepickerObject: { 
			inputDate: new Date($filter('date')(model.date, 'MMM dd yyyy UTC')),
			callback: (val) ->
				$scope.datePickerCallback(val) }								
		datePickerCallback: (val) ->
			if val
				today = new Date(val)
				today.setHours(0,0,0,0)
				$scope.datepickerObject.inputDate = new Date($filter('date')(today, 'MMM dd yyyy UTC'))
				$scope.model.date = $scope.datepickerObject.inputDate
				$scope.getAvailableTimeslot()						
		getAvailableTimeslot: ->
			reservationList = new cliModel.ReservationList
			reservationList.$fetch({params: {date: $scope.model.date.getTime(), resource: $scope.model.resource?._id}}).then ->
				$scope.$apply ->	
					_.each timeslotList.models, (obj) =>
						obj.date = $scope.model.date.getTime()
						@reservation = _.findWhere reservationList.models, {time: "#{obj._id}"}
						if @reservation
							obj.disabled = true
							obj.reservedBy = '[ ' + @reservation.createdBy.username + ' ]'
						else
							obj.disabled = false
							obj.reservedBy = ''	
	
	$scope.$on 'Select Resource', (event, item) ->
		$scope.model.resource = item
		$scope.getAvailableTimeslot()
	
	if !$scope.model.resource
		$scope.model.resource = resourceList.models[0] 
	$scope.model.date = new Date($filter('date')(model.date, 'MMM dd yyyy UTC'))	 	
	$scope.getAvailableTimeslot()
	
MyReservationListCtrl = ($scope, collection, $location, $ionicModal, $ionicListDelegate, $state) ->
	_.extend $scope,
		collection: collection
		isToday: (d) ->
			today = new Date
			today.setHours(0,0,0,0)
			iDate = new Date(d)<commit_msg>Set the selected timeslot in Reservation<commit_after>env = require './env.coffee'
Promise = require 'promise'

MenuCtrl = ($scope) ->
	_.extend $scope,
		env: env
		navigator: navigator

TimeslotCtrl = ($scope, model, $location) ->
	_.extend $scope,
		model: model
		save: ->			
			$scope.model.$save().then =>
				$location.url "/timeslot"
	
	
TimeslotListCtrl = ($scope, collection, $location) ->
	_.extend $scope,
		collection: collection
		create: ->
			$location.url "/timeslot/create"			
		read: (id) ->
			$location.url "/timeslot/read/#{id}"						
		edit: (id) ->
			$location.url "/timeslot/edit/#{id}"			
		delete: (obj) ->
			collection.remove obj
		loadMore: ->
			collection.$fetch()
				.then ->
					$scope.$broadcast('scroll.infiniteScrollComplete')
				.catch alert
				

LocationCtrl = ($scope, model, $location) ->
	_.extend $scope,
		model: model
		save: ->			
			$scope.model.$save().then =>
				$location.url "/location"
	
	
LocationListCtrl = ($scope, collection, $location) ->
	_.extend $scope,
		collection: collection
		create: ->
			$location.url "/location/create"			
		read: (id) ->
			$location.url "/location/read/#{id}"						
		edit: (id) ->
			$location.url "/location/edit/#{id}"			
		delete: (obj) ->
			collection.remove obj
		loadMore: ->
			collection.$fetch()
				.then ->
					$scope.$broadcast('scroll.infiniteScrollComplete')
				.catch alert
	

ResourceCtrl = ($scope, cliModel, model, locationList, $location) ->
	_.extend $scope,
		model: model
		save: ->			
			$scope.model.$save().then =>
				$location.url "/resource"
		
	modelEvents = 
		'Select Location':	'location'
		'UC Facility'	:	'ucFacility'
		'Admin Acceptance' : 'adminAccept'
		
	_.each modelEvents, (modelField, event) =>
		$scope.$on event, (event, item) =>
			$scope.model[modelEvents[event.name]] = item	
	
	$scope.selectLocationList = [new cliModel.Location name: '-- Please select location --', label: '-- Please select location --']
	_.each locationList.models, (location) =>
		location.label = location.name
		$scope.selectLocationList.push location

	cliModel.User.me().then (user) =>
		$scope.me = user
	
	
ResourceListCtrl = ($scope, collection, $location) ->
	_.extend $scope,
		collection: collection
		create: ->
			$location.url "/resource/create"			
		read: (id) ->
			$location.url "/resource/read/#{id}"						
		edit: (id) ->
			$location.url "/resource/edit/#{id}"			
		delete: (obj) ->
			collection.remove obj
		loadMore: ->
			collection.$fetch()
				.then ->
					$scope.$broadcast('scroll.infiniteScrollComplete')
				.catch alert
	
ReservationCtrl = ($scope, cliModel, model, resourceList, timeslotList, $filter, $location, source, $ionicPopup) ->
	_.extend $scope,
		model: model
		resourceList: resourceList
		timeslotList: timeslotList
		save: ->
			saveRecords = ->
				new Promise (fulfill, reject) ->
					_.each selectedTimeslots, (timeslot) ->
						$scope.model = _.omit($scope.model,'_id')			
						$scope.model.time = timeslot				
						$scope.model.$save().catch alert
					fulfill()
			
			backToMainPage = ->
				if source == 'date'
					$location.url "/reservation"
					$location.search "date", $scope.datepickerObject.inputDate.getTime()
				else
					$location.url "/reservationByResource"
					$location.search "resource", $scope.model.resource
			
			selectedTimeslots = _.where(timeslotList.models, {checked: true})
			saveRecords().then ->
				backToMainPage()
						
		datepickerObject: { 
			inputDate: new Date($filter('date')(model.date, 'MMM dd yyyy UTC')),
			callback: (val) ->
				$scope.datePickerCallback(val) }								
		datePickerCallback: (val) ->
			if val
				today = new Date(val)
				today.setHours(0,0,0,0)
				$scope.datepickerObject.inputDate = new Date($filter('date')(today, 'MMM dd yyyy UTC'))
				$scope.model.date = $scope.datepickerObject.inputDate
				$scope.getAvailableTimeslot()						
		getAvailableTimeslot: ->
			reservationList = new cliModel.ReservationList
			reservationList.$fetch({params: {date: $scope.model.date.getTime(), resource: $scope.model.resource?._id}}).then ->
				$scope.$apply ->	
					_.each timeslotList.models, (obj) =>
						obj.date = $scope.model.date.getTime()
						@reservation = _.findWhere reservationList.models, {time: "#{obj._id}"}
						if @reservation
							obj.disabled = true
							obj.reservedBy = '[ ' + @reservation.createdBy.username + ' ]'
							obj.checked = false
						else
							obj.disabled = false
							obj.reservedBy = ''
							if obj._id == $scope.model.time._id
								obj.checked = true
	
	$scope.$on 'Select Resource', (event, item) ->
		$scope.model.resource = item
		$scope.getAvailableTimeslot()
	
	if !$scope.model.resource
		$scope.model.resource = resourceList.models[0] 
	$scope.model.date = new Date($filter('date')(model.date, 'MMM dd yyyy UTC'))	 	
	$scope.getAvailableTimeslot()
	
MyReservationListCtrl = ($scope, collection, $location, $ionicModal, $ionicListDelegate, $state) ->
	_.extend $scope,
		collection: collection
		isToday: (d) ->
			today = new Date
			today.setHours(0,0,0,0)
			iDate = new Date(d)
{'fraction_non_alphanumeric': 0.15833582759652798, 'fraction_numerical': 0.0017958695001496557, 'mean_word_length': 3.1541649399088274, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4520386', 'n_tokens_mistral': 4011, 'n_tokens_neox': 3236, 'n_words': 681}