<div class="container-fluid">
	<div class="card card-body">
		<div>
			<h4 class="card-title">Complaint Attachment [Ref. No : <?php echo str_pad($data_complaint[0]->ref_no, 4,'0', STR_PAD_LEFT); ?> complaint by <?php echo $data_complaint[0]->client_name; ?>]
			<button class="btn btn-primary btn-sm float-right" title="Add Attachments" onclick="openNewAttachmentModal();" <?php if((($_SESSION['module_data']['role']!='admin') && ($_SESSION['user']['id'] != $data_complaint[0]->created_by))){echo 'disabled'; }?>>Add New Attachment <i class="fas fa-plus"></i></button></h4>
		</div>
		<hr>
		<div class="table-responsive">
			<table id="complaint_attachment_table" class="table table-bordered" style="width:100%">
				<thead> 
                    <th class="text-center">#</th>
                    <th class="text-center">File Name</th>
                    <th class="text-center">Description</th>
                    <th class="text-center">Action</th>
                </thead>
                <tbody>
                	<?php 
                	$i = 1;
                	if($data_attachment){ foreach ($data_attachment as $value) { ?>
                		<tr>
                			<td><?php echo $i++; ?></td>
                			<td><a target="_blank" href="<?php echo base_url($value->file_location.$value->filename); ?>"><?php echo $value->filename; ?></a></td>
                			<td><?php echo $value->description; ?></td>
                			<td class="text-center">
								<div class="btn-group btn-group-sm" role="group">
									<a download href="<?php echo base_url($value->file_location.$value->filename); ?>" type="button" class="btn btn-primary" data-trigger="hover" data-toggle="tooltip" title="Download"><i class="fas fa-download"></i></a>
									<button type="button" class="btn btn-danger" onclick="deleteAttachment(<?php echo $value->id; ?>);" data-trigger="hover" data-toggle="tooltip" title="Delete" <?php if((($_SESSION['module_data']['role']!='admin') && ($_SESSION['user']['id'] != $value->created_by))){echo 'disabled'; }?>><i class="fas fa-trash"></i></button>
								</div>
							</td>
                		</tr>
                	<?php } } ?>
                </tbody>
			</table>
		</div>
		<br>
	</div>
</div>
<br>
<div class="modal fade" id="attachmentModal">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title">Add New Attachment</h5>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close">
					<span aria-hidden="true">&times;</span>
				</button>
			</div>
			<div class="modal-body">
				<form id="attachmentForm" class="form">
					<div class="form-group" hidden>
						<label for="attachmentType">Attachment Type</label>
						<select id="attachmentType" class="custom-select">
							<option value="">Choose</option>
							<?php foreach($attachmentType as $type): ?>
								<option value="<?php echo $type['id']; ?>"><?php echo $type['type']; ?></option>
							<?php endforeach; ?>
						</select>
					</div>
					<div class="form-group">
						<input type="file" required data-parsley-error-message="Attachment is required." class="form-control" name="complaint_attachment" id="complaint_attachment" aria-describedby="attachmentHelp">
						<small id="attachmentHelp" class="form-text text-muted">We only accept the following formats: pdf, doc, docx, jpg, jpeg, bmp, png, gif, svg, ico.</small>
					</div>
					<div class="form-group">
						<label for="complaint_attachment_desc">Description</label>
						<input type="text" class="form-control" name="complaint_attachment_desc" id="complaint_attachment_desc" aria-describedby="attachmentDescHelp" placeholder="Enter description.">
						<small id="attachmentHelp" class="form-text text-muted">Description of attachment.</small>
					</div>
				</form>
			</div>
			<div class="modal-footer">
				<div class="btn-group float-right">
					<button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>
					<button type="button" onclick="upload_complaint_attachment(<?php echo $data_complaint[0]->id; ?>);" class="btn btn-primary">Save</button>
				</div>
			</div>
		</div>
	</div>
</div>

<script>

	function openNewAttachmentModal()
	{
		clearForm('#attachmentForm');
		
		$("#complaint_attachment_desc").val();
		$("#attachmentModal").modal("show");
	}

	function upload_complaint_attachment(id)
	{
		validateForm("#attachmentForm", "Are you sure you want to upload this attachment?", function(){
			if($('#complaint_attachment').prop('files').length > 0)
			{
				var attachment_data = new FormData();
				
				attachment_data.append("complaint_id", id);
				attachment_data.append("attachment", $('#complaint_attachment').prop('files')[0]);
				attachment_data.append("description", $('#complaint_attachment_desc').val());
				attachment_data.append("type_id", $("#attachmentType").val());
				attachment_data.append("class_id", 1);

				$.ajax({
					url: "<?php echo base_url('hr/serviceform/upload_complaint_attachment'); ?>",
					type: "POST",
					data: attachment_data,
					dataType: "JSON",
					processData: false,
					contentType: false,
					success: function(data) {
						if(data.status == 'Success')
						{
							$('#attachmentModal').modal('hide');
							
							localStorage.setItem("status",data.status);
							localStorage.setItem("msg",data.msg);

							location.reload();
						}
					}
				});
			}
		});
	}

	function deleteAttachment(id)
	{
		toastConfirm("Are you sure you want to delete this attachment?", function(){
			$.ajax({
				type: 'POST',
				url: '<?php echo base_url('hr/serviceform/delete_complaint_attachment'); ?>',
				data: {
					'id' : id
				},
				dataType: 'JSON',
				success: function(data) {
					if(data.status == 'Success')
					{
						localStorage.setItem("status",data.status);
						localStorage.setItem("msg",data.msg);

						location.reload();
					}
				}
			});
		})
	}


</script>