<?php

if (!class_exists('WP_List_Table')) {
    require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}

function view_type_list_initialisation()
{
    global $wpdb;
    //Delete element action
    if(isset($_GET["action"]) && $_GET["action"] = "delete" && isset($_GET["IDType"]) && intval($_GET["IDType"]) > 0 ){
        $group = new Type();
        $group->setId(intval($_GET["IDType"]));
        $group->delete();
    }
  

    $table = new CASS_View_Type_List();
    ?> 
    <div class="wrap">
        <h2>Liste des types de courses</h2>
        


    <form action='<?php echo admin_url()."admin.php?page=cass-admin-type-edit"; ?>' method='POST'>
    <input type="hidden" id="stateForm" name="stateForm" value="create">
    <button
        type="submit"
        class="button-secondary"
        id="refresh-cache">Ajouter
    </button>
    </form>
 

        <form method="post">
    

    <?php

    // Prepare table
    $table->prepare_items();
    // Display table
    $table->display();
    echo '</div></form>';
}

class CASS_View_Type_List extends WP_List_Table
{
    private $table_data;

    // Define table columns
    function get_columns()
    {
        $columns = array(
            'cb' => '<input type="checkbox" />',
            'name' => __('Name', 'cass-type-name-content'),
            'description' => __('Description', 'cass-type-description-content'),
        );
        return $columns;
    }


    function prepare_items()
    {
        $this->table_data = $this->get_table_data();

        $columns = $this->get_columns();
        $hidden = array();
        $sortable = $this->get_sortable_columns();
        $this->_column_headers = array($columns, $hidden, $sortable);

        usort($this->table_data, array(&$this, 'usort_reorder'));

        /* pagination */
        $per_page = 20;
        $current_page = $this->get_pagenum();
        $total_items = count($this->table_data);

        $this->table_data = array_slice($this->table_data, (($current_page - 1) * $per_page), $per_page);

        $this->set_pagination_args(
            array(
                'total_items' => $total_items,
                // total number of items
                'per_page' => $per_page,
                // items to show on a page
                'total_pages' => ceil($total_items / $per_page) // use ceil to round up
            )
        );

        $this->items = $this->table_data;
    }

    // Get table data
    private function get_table_data()
    {
        global $wpdb;

        $type = new Type();
        return $type->sqlWPTable();
    }
    function column_default($item, $column_name)
    {
        switch ($column_name) {
            case 'description':
                return ReadGlobal($item[$column_name]);
            default:
                return $item[$column_name];
        }
    }

    function column_cb($item)
    {
        return sprintf(
            '<input type="checkbox" name="element[]" value="%s" />',
            intval($item['IDType'])
        );
    }
    protected function get_sortable_columns()
    {
        $sortable_columns = array(
            'name' => array('name', false),
        );
        return $sortable_columns;
    }

    // Sorting function
    function usort_reorder($a, $b)
    {
        // If no sort, default to user_login
        $orderby = (!empty($_GET['orderby'])) ? $_GET['orderby'] : 'user_login';

        // If no order, default to asc
        $order = (!empty($_GET['order'])) ? $_GET['order'] : 'asc';

        // Determine sort order
        $result = strcmp($a[$orderby], $b[$orderby]);

        // Send final sort direction to usort
        return ($order === 'asc') ? $result : -$result;
    }

        // Adding action links to column
        function column_name($item)
        {
            ?>
             <script>
                // The function below will start the confirmation dialog
                function DeleteTypeConfirmAction(IDType) {
                    let confirmAction = confirm("Voulez supprimer le type de course <?php echo $item['name'] ?>  ?");
                    if (confirmAction) {
                        var url = window.location.href;    
                        url+="&action=delete&IDType="+IDType; 
                        location.href = url;
                    }   
                }
            </script>
            
            <?php

            $actions = array(
                    'edit'      => sprintf('<a href="'.admin_url()."admin.php?page=cass-admin-type-edit&action=edit&IDType=".intval($item['IDType']).'">Edit</a>',$_REQUEST['page'], 'edit', intval($item['IDType'])),
                    'delete'    => sprintf('<a onclick="DeleteTypeConfirmAction('.intval($item['IDType']).')" href="#">' . __('Delete', 'cass-admin-type-edit') . '</a>', $_REQUEST['page'], 'delete', intval($item['IDType'])),
            );
    
            return sprintf('%1$s %2$s', ReadGlobal($item['name']), $this->row_actions($actions));
        }
}




