<?php include_once('config.php'); ?>
<link href="../bootstrap/node_modules/bootstrap/dist/css/bootstrap.ali.css" rel="stylesheet">
<!------ Include the above in your HEAD tag ---------->

<!-- Pulled from http://www.avtex.com/blog/2015/01/27/drag-and-drop-sorting-of-table-rows-in-priority-order/ -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Steven Ray: Drag and drop sorting of table rows</title>

<link href="../demo.css" type="text/css" rel="stylesheet" />

<!-- Bootstrap CSS -->
<link href="../bootstrap/node_modules/bootstrap/dist/css/bootstrap.ali.css" rel="stylesheet">

<!-- jQuery
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
-->
<script src="../jquery/jquery-1.12.4.js"></script>

<!-- jQuery UI CSS
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
-->
<script src="../jquery/jquery-ui.js"></script>

<script type="text/javascript">

$(document).ready(function() {
    //Helper function to keep table row from collapsing when being sorted
	var fixHelperModified = function(e, tr) {
		var $originals = tr.children();
		var $helper = tr.clone();
		$helper.children().each(function(index)
		{
		  $(this).width($originals.eq(index).width())
		});
		return $helper;
	};

	//Make diagnosis table sortable
	$("#diagnosis_list tbody").sortable({
    	helper: fixHelperModified,
		stop: function(event,ui) {renumber_table('#diagnosis_list')}
	}).disableSelection();


	//Delete button in table rows
	$('table').on('click','.btn-delete',function() {
		tableID = '#' + $(this).closest('table').attr('id');
		alert(tableID);
		r = confirm('Delete this item?');
		if(r) {
			$(this).closest('tr').remove();
			renumber_table(tableID);
			}
	});

});

//Renumber table rows
function renumber_table(tableID) {
	$(tableID + " tr").each(function() {
		count = $(this).parent().children().index($(this)) + 1;
		$(this).find('.priority').html(count);
	});
}


</script>

         <script>
            $(document).ready(function(e) {
            	$('#sortable tbody').diagnosis_list({
            		handle: 'i.fa-arrows-alt',
            		placeholder: "ui-state-highlight",
            		//opacity: 0.9,
            		update : function () {
            			var order		=	$('#diagnosis_list tbody').diagnosis_list('toArray', { attribute: 'data-sort-id'});
            			console.log(order.join(','));
            			sortOrder	=	order.join(',');
            			$.post(
            				'action-form.ajax.php',
            				{'action':'updateSortedRows','sortOrder':sortOrder},
            				function(data){
            					var a	=	data.split('|***|');
            					if(a[1]=="update"){
            						$('#msg').html(a[0]);
            					}
            				}
            			);
            		}
            	});
            	$( "#diagnosis_list" ).disableSelection();

            	$('[data-toggle="tooltip"]').tooltip();
            });
         </script>

<style type="text/css">
.ui-sortable tr {
	cursor:pointer;
}

.ui-sortable tr:hover {
	background:rgba(244,251,17,0.45);
}

</style>

</head>

<body>

<div id="content" class="container">

<h1>Sortable table</h1>

            <table class="table" id="diagnosis_list">
                <thead>
                    <tr><th>Priority</th><th>Name</th><th>Favorite fruit</th><th>Vegetarian?</th><th>�</th></tr>
                </thead>
                <tbody>
					<tr><td class='priority'>1</td><td>George Washington</td><td>Apple</td><td>N</td><td><a class='btn btn-delete btn-danger'>Delete</a></td></tr>
					<tr><td class='priority'>2</td><td>John Adams</td><td>Pear</td><td>Y</td><td><a class='btn btn-delete btn-danger'>Delete</a></td></tr>
					<tr><td class='priority'>3</td><td>Thomas Jefferson</td><td>Banana</td><td>Y</td><td><a class='btn btn-delete btn-danger'>Delete</a></td></tr>
					<tr><td class='priority'>4</td><td>Ben Franklin</td><td>Kumquat</td><td>N</td><td><a class='btn btn-delete btn-danger'>Delete</a></td></tr>
					<tr><td class='priority'>5</td><td>Alexander Hamilton</td><td>Red grapes</td><td>N</td><td><a class='btn btn-delete btn-danger'>Delete</a></td></tr>
                </tbody>
            </table>

</div>

</body>
</html>