<?php

namespace GriffinForms\Admin\Html\Pages\Lists;

class Fields extends \GriffinForms\Admin\Html\Pages\Lists\Format
{   
    protected $meta;
    protected $inline_js;
    protected $is_reusable;
    
    protected function setType()
    {
        $this->type = 'field';
    }
    
    protected function registerColumns(): void
    {
        $r = $this->column_registry;

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

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

        $r->register('field', 'id',          ['label' => __('ID', 'griffinforms-form-builder'),                  'default_on' => false]);
        $r->register('field', 'description', ['label' => __('Description', 'griffinforms-form-builder'),         'default_on' => false]);

        parent::registerColumns();
    }

    protected function pageModals()
    {
        $this->deleteItemsModal();
        $this->createFieldModal();
        $this->deleteListItemModal();
        (new \GriffinForms\Admin\Html\Modals\ColumnManager())->html();
    }

    public function createFieldModal()
    {
        $for = 'createfield';
        $class = 'modal-xl';
        $data = array('context' => 'list');
        $this->newModal($for, $class, $data);
    }

    protected function addNewButton()
    {
        echo '<a class="page-title-action" data-bs-toggle="modal" data-bs-target="#' . esc_attr($this->modal_ids['createfield']) . '">';
        echo esc_html($this->lang->getText('add_new', $this->type));
        echo '</a>';
    }

    protected function getItemUrl($item)
    {
        return get_admin_url() . 'admin.php?page=griffinforms-' . $this->type . '&id=' . $item->id . '&field-type=' . $item->field_type;
    }

    protected function headingCellText($list_item, $item)
    {
        if (empty($item->heading)) {
            echo '—';
        } else {
            echo esc_html($item->heading);
        }
    }

    protected function fieldTypeCellText($list_item, $item)
    {
        $type = $item->field_type;
        $icon_url = $this->config->getRootUrl() . 'admin/images/field-icons/griffinforms_icon_' . $type . '.svg';
        echo '<img src="' . esc_url($icon_url) . '" alt="' . esc_attr($type) . '" class="gf-icon" />';
        echo '<span class="small ms-2">' . esc_html($this->config->getFriendlyName($type)) . '</span>';
    }

    protected function rowActionEdit($item)
    {
        echo '<a href="' . esc_url($this->getItemUrl($item)) . '">' . esc_html($this->lang->getText('edit', 'row_action')) . '</a>'; 
    }

    protected function setFilterNames()
    {
        $this->filter_names = array(
            'search',
            'date_from',
            'date_to',
            'is_reusable'
        );
    }

    protected function setFilterLinks()
    {
        $this->filter_links = array('all', 'reusable');
    }

    protected function reusableFilterLink()
    {
        $count = $this->sql->getReusableFieldCount($this->type);

        $link_class = '';
        $filters = $this->getListFilters();

        if (!empty($filters) and isset($filters['is_reusable']) and $filters['is_reusable']) {
            $link_class = 'current';
        }

        $filter_url = $this->list_url . '&is_reusable=1';

        echo '<a href="' . esc_url($filter_url) . '" class="' . esc_attr($link_class) . '" aria-current="page">';
        echo esc_html($this->lang->getText('reusable_field', 'filter_link'));
        echo ' <span class="count">(';
        echo absint($count);
        echo ')</span>';
        echo '</a>';
    }

    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, '…'));
    }
}
