<section class="container" style="padding-bottom: 2rem;">
    <div class="card bg-light-transparent">

        <div class="card-header bg-light" <?php if(strtolower($this->logged_module['role'])!='admin') echo 'hidden'; ?> >
            <div class="float-left">
                <?php if($this->logged_module['role'] == 'admin'): ?>
                    <button onclick="openTncModal();" type="button" class="btn btn-outline-dark btn-sm"><i class="fa fa-plus"></i> Add T&C</button>
                <?php endif; ?>
                
            </div>
            <div class="float-right">
                <nav>
                    <div class="nav nav-tabs btn-group btn-group-sm" role="tablist">
                        <a class="btn btn-outline-dark active" id="table-view-tab" data-toggle="tab" href="#tableView" role="tab" aria-controls="nav-table" aria-selected="true" data-tooltip="tooltip" title="Show List View"><i class="fas fa-list"></i></a>
                        <a class="btn btn-outline-dark" id="card-view-tab" data-toggle="tab" href="#cardView" role="tab" aria-controls="nav-card" aria-selected="false" data-tooltip="tooltip" title="Show Card View"><i class="fas fa-th-large"></i></a>
                    </div>
                </nav>
            </div>
        </div>

        <div class="tab-content">
            <div class="tab-pane fade show active" id="tableView" role="tabpanel" aria-labelledby="nav-table-tab">
                <div class="card-body">
                    <div class="card no-shadow">
                        <div class="card-body">
                            <div class="table-responsive">
                                <table id="table_forms" class="table table-bordered table-hover">
                                    <thead>
                                        <th>Title</th>
                                        <th>Description</th>
                                        <th width="10%"></th>
                                    </thead>
                                    <tbody>
                                    <?php if($tncs): ?>
                                        <?php foreach($tncs as $tnc): ?>
                                            <tr>
                                                <td><a target="_blank" href="<?php echo base_url($tnc['file_location'].$tnc['file_name']); ?>"><?php echo $tnc['title']; ?></a></td>
                                                <td><?php echo $tnc['description']; ?></td>
                                                <td class="text-center">
                                                    <div class="btn-group btn-group-sm" role="group">
                                                        <a download href="<?php echo base_url($tnc['file_location'].$tnc['file_name']); ?>" class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="Download Terms & Conditions"><i class="far fa-arrow-alt-circle-down"></i></a>
                                                        <?php if($this->logged_module['role'] == 'admin'): ?>
                                                            <button type="button" class="btn btn-outline-dark" onclick="openTncModal(<?php echo $tnc['id']; ?>)" data-trigger="hover" data-tooltip="tooltip" title="Edit Terms & Conditions"><i class="far fa-edit"></i></button>
                                                            <button type="button" class="btn btn-outline-dark" onclick="deleteTnc(<?php echo $tnc['id']; ?>);" data-trigger="hover" data-tooltip="tooltip" title="Delete Terms & Conditions"><i class="far fa-trash-alt"></i></button>
                                                        <?php endif; ?>
                                                    </div>
                                                </td>
                                            </tr>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="tab-pane fade" id="cardView" role="tabpanel" aria-labelledby="nav-card-tab">
                <div class="card-body">
                    <div class="card no-shadow">
                        <div class="card-body">
                            <?php if($tncs): ?>
                                <div class="row">
                                    <?php $count = 0; ?>
                                    <?php foreach($tncs as $tnc): ?>
                                            <?php if($count==3): echo "<div class='w-100' style='padding-bottom: 2rem;'></div>"; $count = 0; endif; ?>
                                            <?php $count++; ?>
                                            <div class="col-12 col-sm-4 mb-3">
                                                <div class="card no-shadow">
                                                    <div class="card-header">
                                                        <div class="float-left">
                                                            <h5 class="card-title"><a target="_blank" href="<?php echo base_url($tnc['file_location'].$tnc['file_name']); ?>"><?php echo $tnc['title']; ?></a></h5>
                                                        </div>
                                                        <div class="float-right">
                                                            <div class="btn-group btn-group-sm" role="group">
                                                                <a download href="<?php echo base_url($tnc['file_location'].$tnc['file_name']); ?>" class="btn btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="Download Terms & Conditions"><i class="far fa-arrow-alt-circle-down"></i></a>
                                                                <?php if($this->logged_module['role'] == 'admin'): ?>
                                                                    <button type="button" class="btn btn-outline-dark" onclick="openTncModal(<?php echo $tnc['id']; ?>)" data-trigger="hover" data-tooltip="tooltip" title="Edit Terms & Conditions"><i class="far fa-edit"></i></button>
                                                                    <button type="button" class="btn btn-outline-dark" onclick="deleteTnc(<?php echo $tnc['id']; ?>);" data-trigger="hover" data-tooltip="tooltip" title="Delete Terms & Conditions"><i class="far fa-trash-alt"></i></button>
                                                                <?php endif; ?>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="card-body">
                                                        <p class="card-text"><?php echo $tnc['description']; ?></p>
                                                    </div>
                                                </div>
                                            </div>
                                    <?php endforeach; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
<div class="modal fade" id="tncModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="tnc-modal-title">New Terms & Conditions</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form id="tncForm" class="form">
                <div class="modal-body">
					<div class="col" hidden>
						<div class="form-group">
							<label for="tnc_id">ID</label>
							<input type="text" class="form-control" name="tnc_id" id="tnc_id" value="">
						</div>
                    </div>
                    <div class="form-group">
                        <label for="tnc_title">Title</label>
                        <input type="text" required data-parsley-error-message="Title is required." class="form-control" name="tnc_title" id="tnc_title" placeholder="Title">
                    </div>
                    <div class="form-group">
                        <label for="tnc_description">Description</label>
                        <input type="text" class="form-control" name="tnc_description" id="tnc_description" placeholder="Description">
                    </div>
                    <div class="form-group">
                        <label for="tnc_file">File Upload</label>
                        <input type="file" required data-parsley-error-message="Form file is required." class="form-control" name="tnc_file" id="tnc_file" aria-describedby="uploadHelp">
                        <small id="uploadHelp" class="form-text text-muted">We only accept the following formats: pdf, doc, docx</small>
                    </div>
                </div>
                <div class="modal-footer">
                   <div class="btn-group float-right">
						<button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
						<button type="button" id="modalSubmitBtn" onclick="uploadTnc();" class="btn btn-light">Save</button>
					</div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    $('#collapseFilter').on('show.bs.collapse', function () {
        $('#btnShowFilters').addClass('active');
    })
    $('#collapseFilter').on('hide.bs.collapse', function () {
        $('#btnShowFilters').removeClass('active');
    })

    function openTncModal(id)
    {
        if(id != null)
        {
            clearForm("#tncForm");

            $.ajax({
                url: "<?php echo base_url('hr/tnc/getTncs'); ?>",
				type: "POST",
				data: {id : id},
				dataType: "JSON",
				success: function(data) {
					if(data.status == 'Success')
					{
                        var tnc = data.tnc;

                        $('#tnc_id').val(id);
                        $('#tnc_title').val(tnc.title);
                        $('#tnc_description').val(tnc.description);

                        $('#form-modal-title').html('Update Terms & Conditions');
                        $('#modalSubmitBtn').attr('onclick', 'updateTnc()');
                        $('#tnc_file').attr('required', false);
					}
				}
            });
        }
        else
        {
            $('#form-modal-title').html('Add New Terms & Conditions');
            $('#tnc_file').attr('required', true);
            $('#modalSubmitBtn').attr('onclick', 'uploadTnc()');

            clearForm("#tncForm");
        }

        $('#tncModal').modal('show');
    }

    function uploadTnc()
	{
		validateForm("#tncForm", "Are you sure you want to add this Terms & Conditions?", function(){
			var tnc_data = new FormData();

			// tnc_data.append("category", $('#form_category').val());
			tnc_data.append("title",  $('#tnc_title').val());
			tnc_data.append("description",  $('#tnc_description').val());
            
			tnc_data.append("tnc", $('#tnc_file').prop('files')[0]);

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

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

    function updateTnc()
    {
        validateForm("#tncForm", "Are you sure you want to update this Terms & Conditions?", function(){
			var tnc_data = new FormData();

			tnc_data.append("id", $('#form_id').val());
			tnc_data.append("title",  $('#form_title').val());
			tnc_data.append("description",  $('#form_description').val());

            if($('#tnc_file').get(0).files.length > 0){
                tnc_data.append("tnc", $('#tnc_file').prop('files')[0]);
            }
             
			$.ajax({
				url: "<?php echo base_url('hr/tnc/updateTnc'); ?>",
				type: "POST",
				data: tnc_data,
				dataType: "JSON",
				processData: false,
				contentType: false,
				success: function(data) {
					if(data.status == 'Success')
					{
						$('#formModal').modal('hide');
						
						localStorage.setItem("status",data.status);
						localStorage.setItem("msg",data.msg);

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

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

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

    function clearForm(e)
	{
		$(e).parsley().reset();

		$(e).find("input, textarea").val("");
		$(e).find("select").val("").trigger('change.select2');
	}
</script>