
<div class="container-fluid d-print-none">
	<div class="row">
		<div class="col-md-8">
			<div class="card bg-light-transparent" style="margin-bottom: 2rem;">
				<div class="card-header bg-white">
					<div class="float-left">
						<h5 class="card-title">Calendar</h5>
					</div>
				</div>
				<div class="card-body">
					<div class="card bg-white no-shadow">
						<div class="card-body">
							<div class="row">
								<div class="col-12">
									<div id="calendar"></div>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="col-md-4">
			<div class="card bg-light-transparent" style="margin-bottom: 2rem;">
				<div class="card-header bg-white">
					<h5 class="card-title">Upcoming training request</h5>
				</div>
				<div class="card-body">
					<div class="card bg-white no-shadow">
						<div class="card-body">
							<div class="row">
								<table id="table_training_request" class="table table-hover mx-1">
									<thead>
										<?php if(!empty($upcoming_request)) foreach($upcoming_request as $upcoming){ ?>
											<tr>
												<th data-trigger="hover" data-tooltip="tooltip" title="<?php echo $upcoming['business_entity']; ?>" width="60%" style="border: none; font-weight: normal;"><?php echo $upcoming['topic']; ?></th>
												<th class="text-center" width="40%" style="border: none; font-weight: normal;"><?php echo date('d-m-Y',strtotime($upcoming['proposed_date'])); ?></th>
											</tr>
										<?php } ?>
									</thead>
									<tbody>
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>


<section class="container" style="padding-bottom: 2rem;">
	<div class="row">
		<div class="col-sm-12">
			<div class="card bg-light-transparent">
				<div class="card-header bg-light">
					<h4 class="card-title float-left"><?php echo (empty($ID))?'New ':'Edit '; ?>Training Request Form</h4>
				</div>
				<div class="card-body">
					<div class="card no-shadow">
						<div class="card-body">
							<?php if(!empty($ID)) { ?>
								<form id="requestForm" class="form" action="<?php echo base_url('hr/Serviceform/training_request_edit') ?>" method="POST" enctype="multipart/form-data" onsubmit="return confirm('Do you really want to submit the form?')">
								<?php } else { ?>
									<form id="requestForm" class="form" action="<?php echo base_url('hr/Serviceform/training_request_save') ?>" method="POST" enctype="multipart/form-data" onsubmit="return confirm('Do you really want to submit the form?')">
									<?php } ?>

									<input type="text" class="form-control" name="trainingRequestID" id="trainingRequestID" value="<?php echo $ID; ?>" hidden>
									<?php //die(debug($trainingRequestDetails[0]['uploadID'])); ?>

									<?php if(!empty($trainingRequestDetails)){ ?>
										<input type="text" class="form-control" name="upload_id" id="upload_id" value="<?php echo $trainingRequestDetails[0]['uploadID'];?>" hidden>
									<?php } ?>

									<div class="form-row">
										<div class="col-12 col-sm-6">
											<div class="form-group">
												<label>Name</label>
												<?php if(!empty($ID)){ ?>
													<?php if(!empty($trainingRequestDetails)): foreach($trainingRequestDetails as $row): ?>
														<input type="text" class="form-control" name="name" id="name" value="<?php echo $row['requestor']; ?>" readonly>
														<input type="text" class="form-control" name="reqName_id" id="reqName_id" value="<?php echo $row['user_id']; ?>" hidden>
													<?php endforeach;endif; ?>
												<?php } else { ?>
													<input type="text" class="form-control" name="name" id="name" value="<?php echo $_SESSION['user']['name']; ?>" readonly>
													<input type="text" class="form-control" name="reqName_id" id="reqName_id" value="<?php echo $_SESSION['user']['id']; ?>" hidden>
												<?php } ?>
											</div>
										</div>

										<div class="col-12 col-sm-3">
											<div class="form-group">
												<label for="department">Department</label>
												<?php if(!empty($ID)){ ?>
													<?php if(!empty($trainingRequestDetails)): foreach($trainingRequestDetails as $row): ?>
														<input type="text" class="form-control" name="department" id="department" value="<?php echo $row['dept_name']; ?>" readonly>
														<input type="text" class="form-control" name="department_id" id="department_id" value="<?php echo $row['department_id']; ?>" hidden>
													<?php endforeach;endif; ?>
												<?php } else { ?>
													<input type="text" class="form-control" name="department" id="department" value="<?php echo $_SESSION['user']['department_name']; ?>" readonly>
													<input type="text" class="form-control" name="department_id" id="department_id" value="<?php echo $_SESSION['user']['department_id']; ?>" hidden>
												<?php } ?>
											</div>
										</div>

										<div class="col-12 col-sm-3">
											<div class="form-group">
												<label for="date">Request Date </label>
												<?php if(!empty($ID)){ ?>
													<?php if(!empty($trainingRequestDetails)): foreach($trainingRequestDetails as $row): ?>
														<input type="" class="form-control" name="reqDate" id="reqDate" value="<?php echo (!empty($ID))?date('d-m-Y',strtotime($row['createdTS'])):''; ?>" readonly/>
													<?php endforeach;endif; } else { ?>
														<input type="" class="form-control" name="reqDate" id="reqDate" value="<?php echo date("d-m-Y"); ?>" readonly/>
													<?php } ?>
												</div>
											</div>
										</div>

									<div class="form-row">
										<div class="col-12 col-sm-6">
											<div class="form-group">
												<label for="department">Business Entity</label>
												<select  class="form-control" name="business_entity" id="business_entity" required>
													<option value="" disabled selected>-- Please Select --</option>
													<?php if(!empty($ID)){ ?>
														<?php if(!empty($trainingRequestDetails)): foreach($trainingRequestDetails as $row): ?>

															<option value="<?php echo (!empty($ID))?$row['business_entity_id']:''; ?>" selected>
																<?php echo (!empty($ID))?$row['business_entity']:''; ?>
															</option>

															<?php if(!empty($rsBusiness_entities)) foreach($rsBusiness_entities as $rows){ ?>

																<option value="<?php echo (!empty($rows['id']))?$rows['id']:''; ?>" >
																	<?php echo $rows['name']; ?>
																</option>
															<?php } ?>
														<?php endforeach;endif; } else { ?>
															<?php if(!empty($rsBusiness_entities)) foreach($rsBusiness_entities as $rows){ ?>

																<option value="<?php echo (!empty($rows['id']))?$rows['id']:''; ?>" ><?php echo $rows['name']; ?></option>
															<?php } ?>
														<?php } ?>
												</select>											
											</div>
										</div>

										<div class="col-12 col-sm-6">
											<div class="form-group">
												<label for="title">Training Title/Topic</label>
												<?php if(!empty($ID)){ ?>
													<?php if(!empty($trainingRequestDetails)): foreach($trainingRequestDetails as $row): ?>
														<input type="text" class="form-control" name="topic" id="topic" value="<?php echo (!empty($ID))?$row['topic']:''; ?>" required><?php //echo (!empty($ID))?$row['topic']:''; ?> 
													<?php endforeach;endif; } else{ ?>
														<input type="text" class="form-control" name="topic" id="topic" value="" required>
													<?php } ?>
											</div>
										</div>
									</div>

									<div class="form-row">
										<div class="col-12 col-sm-6">
											<div class="form-group">
												<label for="venue">Venue</label>
												<?php if(!empty($ID)){ ?>
													<?php if(!empty($trainingRequestDetails)): foreach($trainingRequestDetails as $row): ?>
														<input type="text" class="form-control" name="venue" id="venue" value="<?php echo (!empty($ID))?$row['venue']:''; ?>" required><?php //echo (!empty($ID))?$row['venue']:''; ?> 
													<?php endforeach;endif; } else{ ?>
														<input type="text" class="form-control" name="venue" id="venue" value="" required>
													<?php } ?>
											</div>
										</div>
										<div class="col-12 col-sm-6">
											<div class="form-group">
												<label for="org">Organizer</label>
												<?php if(!empty($ID)){ ?>
													<?php if(!empty($trainingRequestDetails)): foreach($trainingRequestDetails as $row): ?>
														<input type="text" class="form-control" name="organizer" id="organizer" value="<?php echo (!empty($ID))?$row['organizer']:''; ?>" required><?php //echo (!empty($ID))?$row['venue']:''; ?> 
													<?php endforeach;endif; } else{ ?>
														<input type="text" class="form-control" name="organizer" id="organizer" value="" required>
													<?php } ?>
											</div>
										</div>
									</div>

									<div class="form-row">
										<div class="col-12 col-sm-3">
											<div class="form-group">
												<label for="time">Time</label>
												<?php if(!empty($ID)){ ?>
													<?php if(!empty($trainingRequestDetails)): foreach($trainingRequestDetails as $row): ?>
														<input type="time" class="form-control" name="time" id="time" value="<?php echo (!empty($ID))?$row['time']:''; ?>" required><?php //echo (!empty($ID))?$row['venue']:''; ?> 
													<?php endforeach;endif; } else{ ?>
														<input type="time" class="form-control" name="time" id="time" value="" required>
													<?php } ?>
											</div>
										</div>

										<div class="col-12 col-sm-3">
											<div class="form-group">
												<label for="cost">Cost</label>
												<?php if(!empty($ID)){ ?>
													<?php if(!empty($trainingRequestDetails)): foreach($trainingRequestDetails as $row): ?>
														<input type="text" class="form-control" name="cost" id="cost" value="<?php echo (!empty($ID))?$row['cost']:''; ?>"><?php //echo (!empty($ID))?$row['venue']:''; ?> 
													<?php endforeach;endif; } else{ ?>
														<input type="text" class="form-control" name="cost" id="cost"  value="">
													<?php } ?>
											</div>
										</div>

										<div class="col-12 col-sm-6">
											<div class="form-group">
												<label for="participant">Participant</label>
												<!-- <?php if(!empty($ID)){ ?>
													<?php if(!empty($trainingRequestDetails)): foreach($trainingRequestDetails as $row): ?>
														<input type="text" class="form-control" name="participant" id="participant" value="<?php echo (!empty($ID))?$row['participant']:''; ?>" required><?php //echo (!empty($ID))?$row['venue']:''; ?> 
													<?php endforeach;endif; } else{ ?>
														<input type="text" class="form-control" name="participant" id="participant" value=""required>
													<?php } ?> -->
												<select class="form-control selectric" id="participant" name="participant[]" multiple>
												<option value="-1">--Please Select--</option>
												<?php foreach($user_list as $key => $val){ ?>
													<option value="<?php echo $val['id'] ?>"><?php echo $val['name']; ?></option>
												<?php } ?>
												</select>
												<?php if(!empty($ID)){ ?>
													<ol id="participant_list" style="padding-left: 1.5rem;">
														<?php if(!empty($trainingRequestDetails[0]['participant_name'])){
															$pName = explode(",",$trainingRequestDetails[0]['participant_name']); ?>
															<?php foreach($pName as $value){ ?>
																<li><?php echo ($value)?$value:''; ?></li>
															<?php } ?>
														<?php } else { ?>
																<li><?php echo ($trainingRequestDetails[0]['participant'])?$trainingRequestDetails[0]['participant']:''; ?></li>
														<?php } ?>
													</ol>
												<?php } ?>
											</div>
										</div>
									</div>

									<div class="form-row">
										<div class="col-12 col-sm-6">
											<div class="form-group">
												<label for="date">Proposed Sharing Session Date</label>
												<?php if(!empty($ID)){ ?>
													<?php if(!empty($trainingRequestDetails)): foreach($trainingRequestDetails as $row): ?>
														<input type="text" class="form-control datepicker" name="proposed_date" id="proposed_date" value="<?php echo date('d-m-Y',strtotime($row['proposed_date'])); ?>" required> 
													<?php endforeach;endif; } else{ ?>
														<input type="text" class="form-control datepicker" name="proposed_date" id="proposed_date" value="" required>
													<?php } ?>
											</div>
										</div>

										<div class="col-12 col-sm-6">
											<div class="form-group">
												<label>For training request and registration , kindly attach :- </label>
												<br>
												<small for="file" class="text-danger">
													&emsp;a) Staff Development Plan <br> 
													&emsp;b) Training brochure & information & registration form
												</small>
												<br><br>
												<input type="file" id="picture_upload" name="picture_upload" value="" aria-describedby="attachmentHelp">
												<br><br>	

												<div class="form-group form-inline">
													<?php if(!empty($ID)){ ?>
														<?php if(!empty($trainingRequestDetails)): foreach($trainingRequestDetails as $row): ?>
															<label for="curr_picture_upload" style="padding-right: 12px;">Current picture :</label>
															<input class="form-control" style="width: 76%;" type="text" name="curr_picture_upload" id="curr_picture_upload" value="<?php echo (!empty($ID))?$row['photo_title']:''; ?>" aria-describedby="attachmentHelp" readonly>
														<?php endforeach;endif; } ?>
												</div>											
											</div>
										</div>
									</div>

									<div class="form-row">
										<div class="col-12">
											<div class="float-right" style="padding-top: 2rem;">
												<a class="btn btn-light" href="<?php echo base_url('hr/serviceform/request_list/'); ?>">Cancel</a>
												<button type="submit" class="btn btn-primary">Submit</button>
											</div> 
										</div>
									</div>
								</form>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</section>

<?php echo (!empty($approval))?$approval:''; ?>

<style type="text/css">
	.tooltipevent
	{
		width:auto;
		height:auto;
		background:rgba(0,0,0,0.75);
		position:absolute;
		color: #fff;
		z-index:10001;
		padding:10px 10px 10px 10px ;  
		line-height: 200%;
	}
</style>

<script type="text/javascript">
	$(document).ready(function(){
		$('#table_training_request').DataTable().destroy();
		var fc_event= '';
		var resource = <?php echo $resource;?>;

		var fullcalendar = $('#calendar').fullCalendar({ 
			themeSystem: 'bootstrap4',
			header: 
			{
				left: 'prev,next today',
				center: 'title',
				right: 'month,listMonth'
			},
			buttonText: {
				today: 'Today',
				month: 'Calendar',
				listMonth: 'List'
			},

            navLinks: true, // can click day/week names to navigate views
            editable: true,
            eventLimit: true, // allow "more" link when too many events
            events: <?php echo $fc_event; ?>,
            eventMouseover: function (data, event, param, resource = <?php echo $resource;?>) {
            	var js_res = JSON.parse(JSON.stringify(resource));
                // console.log(js_res);
                var hover_item = js_res[data.id];
                var title = JSON.stringify(data.title);
                var start = moment(hover_item.proposed_date).format('DD-MM-YYYY');

                console.log(hover_item);

                tooltip = '<div class="tooltipevent"> Requester : '+hover_item.requestor+'['+hover_item.staff_id+']'+'<br/> Email : '+hover_item.email+'<br/> Department : '+hover_item.dept_name+'/'+hover_item.branchName+'<br/> Date : '+start+'<br/> Time : '+hover_item.time+'<br/> Business Entity : '+hover_item.business_entity+'</div>';

                // console.log(tooltip);
                $("body").append(tooltip);
                $(this).mouseover(function (e) {
                	$(this).css('z-index', 10000);
                	$('.tooltipevent').fadeIn('500');
                	$('.tooltipevent').fadeTo('10', 1.9);
                }).mousemove(function (e) {
                	$('.tooltipevent').css('top', e.pageY + 10);
                	$('.tooltipevent').css('left', e.pageX + 20);
                });
            },

            eventMouseout: function(data, event) {
            	$(this).css('z-index', 8);
            	$('.tooltipevent').remove();
            },
        });

		fullcalendar.fullCalendar();

		if('<?php echo $ID; ?>' > 0) selected_participant('<?php echo $trainingRequestDetails[0]['participant']; ?>');
	});

	function confirmation() {
		confirm('Do you really want to submit the form?');
	}

	function selected_participant(participant_id)
	{
		var participant_id = participant_id.split(',');

		$('#participant').val(participant_id).selectric('refresh');
	}

	// <select class="form-control selectric" id="participant" name="participant[]" multiple>
	// 	<option value="" disabled selected>--Please Select--</option>
	// 	<?php foreach($user_list as $key => $val){ ?>
	// 		<option value="<?php echo $val['id'] ?>"><?php echo $val['name']; ?></option>
	// 	<?php } ?>
	// </select>
	// <?php if(!empty($ID)){ ?>
	// 	<ol id="participant_list" style="padding-left: 1.5rem;">
	// 		<?php if(!empty($trainingRequestDetails[0]['participant_name'])){
	// 			$pName = explode(",",$trainingRequestDetails[0]['participant_name']); ?>
	// 			<?php foreach($pName as $value){ ?>
	// 				<li><?php echo ($value)?$value:''; ?></li>
	// 			<?php } ?>
	// 		<?php } else { ?>
	// 				<li><?php echo ($trainingRequestDetails[0]['participant'])?$trainingRequestDetails[0]['participant']:''; ?></li>
	// 		<?php } ?>
	// 	</ol>
	// <?php } ?>
</script>