<?php

namespace GriffinForms\Admin\Html\Pages\Lists;

class Messages extends \GriffinForms\Admin\Html\Pages\Lists\Format
{   
    protected $meta;
    protected $sql;
    protected $inline_js;
    protected $form_id = '';
    protected $status = '';
    
    protected function setType()
    {
        $this->type = 'message';
    }
    
    protected function setFilterNames()
    {
        $this->filter_names = array(
            'search'
        );
    }
    
    protected function setFilterLinks()
    {
        $this->filter_links = array('all');
    }
    
    protected function registerColumns(): void
    {
        $r = $this->column_registry;

        $r->register('message', 'checkbox',     ['label' => '',                                                     'locked' => true, 'default_on' => true]);
        $r->register('message', 'name',         ['label' => $this->lang->getText('name', 'list_head'),              'locked' => true, 'default_on' => true, 'sortable' => true]);

        $r->register('message', 'heading',      ['label' => $this->lang->getText('heading', 'list_head'),           'default_on' => true]);
        $r->register('message', 'author',       ['label' => $this->lang->getText('author', 'list_head'),            'default_on' => true]);
        $r->register('message', 'created',      ['label' => $this->lang->getText('created', 'list_head'),           'default_on' => true, 'sortable' => true]);

        $r->register('message', 'id',           ['label' => __('ID', 'griffinforms-form-builder'),                  'default_on' => false]);
        $r->register('message', 'description',  ['label' => __('Description', 'griffinforms-form-builder'),         'default_on' => false]);
        $r->register('message', 'lastedited',   ['label' => __('Last Edited', 'griffinforms-form-builder'),         'default_on' => false]);
        $r->register('message', 'lasteditor',  ['label' => __('Last Editor', 'griffinforms-form-builder'),         'default_on' => false,
            'variants' => [
                'default'  => __('Username + full name', 'griffinforms-form-builder'),
                'nicename' => __('Username only', 'griffinforms-form-builder'),
            ],
            'default_variant' => 'default',
        ]);

        parent::registerColumns();
    }

    protected function itemFilters()
    {
        echo '<div class="alignleft actions">';
        echo '</div>';
    }

    protected function idCellText($list_column, $item)
    {
        echo '<span class="badge text-bg-secondary">' . absint($item->id) . '</span>';
    }

    protected function descriptionCellText($list_column, $item)
    {
        $desc = trim((string) ($item->description ?? ''));
        if ($desc === '') {
            echo '<span class="text-muted">—</span>';
            return;
        }
        echo esc_html(mb_strimwidth($desc, 0, 60, '…'));
    }

}