
<form action="<?php echo pageLink('actions/delete.php'); ?>" method="post" enctype="multipart/form-data">
    <?php if(isset($_GET['action']) && $_GET['action'] == 'search') : ?>
        <h1 class="title">Advanced Search for <?php echo printTableName($tableName); ?> <a href='<?php echo pageLink('generate.php?table='.$tableName);?>'>[ + ]</a></h1>
        <?php else : ?>
        <h1 class="title">Listing all Entries <a href='<?php echo pageLink('generate.php?table='.$tableName);?>'>[ + ]</a></h1>
        <?php endif; ?>
    <hr />

    <?php if(isset($_GET['action']) && $_GET['action'] == 'search' && isset($_POST['data']['Search'] ) && !empty($_POST['data']['Search'] ) ) : ?>
        <h5>Searching for : </h5><?php
            foreach($_POST['data']['Search'] as $index => $value){
                if(empty($value)){
                    continue;   
                }
                echo '<strong>' .printTableName($index) .'</strong> : '. $value .' <br /> ';   
            }
        ?>
        <?php endif; ?>


<?php include(ADMIN_PATH.DS."common/listTable.php"); ?>
</form>
<script>
    var oTable;
    /*
    * define the column number of position column
    *  function to read data from cells dynamically on every sort
    */
    <?php if($table->isTableSortable()) : ?>
        //var posColumn = <?php echo $columnsCount - 1; ?>;
        var posColumn = <?php echo $positionColumnNumber + 2;?>;

        $.fn.dataTableExt.afnSortData['dom-text'] = function  ( oSettings, iColumn ){
            return $.map( oSettings.oApi._fnGetTrNodes(oSettings), function (tr, i) {
                    return $('td:eq('+iColumn+')', tr).html();
            } );
        }
        <?php endif; ?>
    $(document).ready( function () {
            // initialize data tables
            // exclude sorting on last 2 columns
            // sort by 'pos' in case available
            var oTable = $('#example').dataTable( {
                    "iDisplayLength": 25,
                    "aLengthMenu": [[10, 25, 50, 100, 150, 200, 300, -1], [10, 25, 50, 100, 150, 200, 300, "All"]],
                    "sDom": 'C<"clear">lfrtip',
                    "oColVis": {
                        "aiExclude": [ <?php echo $columnsCount ; ?>,<?php echo $columnsCount + 1; ?> ]
                    },
                    "aoColumnDefs": [
                        { "bSortable": false, "aTargets": [ 0,1 ] }
                    ],
                    "oLanguage": { "sSearch": "" }
                    <?php if ($table->isTableSortable()): ?>
                        ,"aoColumns": [<?php for($i=1;$i<$columnsCount;$i++) {echo 'null,';}?>{ "sSortDataType": "dom-text" },null,null  ],
                        "aaSorting": [[ posColumn, "asc" ]]
                        <?php else : ?>
                        ,"aoColumns": [<?php for($i=1;$i<$columnsCount;$i++) {echo 'null,';}?>{ "sSortDataType": "dom-text" },null,null  ],
                        "aaSorting": [[ 0, "desc" ]]
                        <?php endif; ?>
            } );

            // add placeholder text to the search field
            $('.dataTables_filter input').attr("placeholder", "Search...");
            <?php if($table->isTableSortable()) : ?>
                $("#list tbody.content").sortable({opacity: 0.6,cursor: 'move',items: "tr:not(.ui-state-disabled)",helper: function(e, tr){
                            var $originals = tr.children();
                            var $helper = tr.clone();
                            $helper.children().each(function(index)
                                {
                                    // Set helper cell sizes to match the original sizes
                                    $(this).width($originals.eq(index).width())
                            });
                            return $helper;
                        },update: function() {
                            var order = $(this).sortable("serialize") + '&action=updateRecordsListings&table=<?php echo $tableName; ?>&page=<?php echo $page; ?>';
                            var sortedIDs = $( this ).sortable( "toArray" );

                            $.post("<?php echo ADMIN_PATH_HTML; ?>/actions/order.php", order, function(theResponse){
                                    //                        var rows = oTable.fnGetNodes();
                                    var rows = $("#example tr");
                                    var cells = [];
                                    console.log(posColumn);
                                    $.each(rows, function(i, row) {
                                            // $(row).find("td:eq("+posColumn+")").html(i);
                                           $(row).find('td[data-coltype="position"]').html(i);
                                    });

                                    //                        for(var i=0;i<rows.length;i++){
                                    //                            var newOrder = i;
                                    //                            $(rows[i]).find("td:eq(2)").html(i);
                                    //                        }
                                    $("#saveResult").html(theResponse);
                                    $("#saveResult").hide();
                                    $("#saveResult").html("<div class='response_class'>Rows order updated</div>");
                                    $('#saveResult').fadeIn('slow').delay(2000).fadeOut("fast");
                            });
                        }
                });
                $("#list tbody.content").disableSelection();
                <?php endif; ?>



            /* Apply the jEditable handlers to the table */
            $('td.jeditable', oTable.fnGetNodes()).editable( 'actions/jeditable.php', {
                    "callback": function( sValue, y ) {
                        var aPos = oTable.fnGetPosition( this );
                        oTable.fnUpdate( sValue, aPos[0], aPos[1] );
                    },
                    "submitdata": function ( value, settings ) {
                        return {
                            "row_id": this.parentNode.getAttribute('id'),
                            "column": oTable.fnGetPosition( this )[2],
                            "table" : "<?php echo $tableName; ?>",
                            "token" : "<?php echo CSRF::generateToken(); ?>"
                        };
                    },
                    cssclass : 'metroJedit',
                    tooltip: 'Click to edit...',
                    indicator: "<img src='images/loading.gif' />",
                    intercept: function (jsondata) {
                        obj = jQuery.parseJSON(jsondata);
                        // do something with obj.status and obj.other
                        if (obj.status == 'fail') {
                            var msg = "An error occured, value was not changed!";
                            if (obj.msg != '') {
                                msg = obj.msg;
                            }
                            alert (msg);
                        }
                        return(obj.value);
                    }
            } );



    } );

    $(document).ready(function()
        {
            // MAKE SURE YOUR SELECTOR MATCHES SOMETHING IN YOUR HTML!!!
            $('.showAddChildrenTooltip').each(function() {
                    $(this).qtip({
                            content: {
                                text: $(this).closest("td").find('.tooltipAddChildrenText')
                            },
                            position  : {
                                my: 'top right',  // Position my top left...
                                at: 'bottom right', 
                            },
                            hide: {
                                fixed: true
                            }
                    });
            });
    });
</script>