<?php

namespace GriffinForms\Admin\Html\Pages\Lists;

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

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

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

        $r->register('folder', 'id',          ['label' => __('ID', 'griffinforms-form-builder'),                  'default_on' => false]);
        $r->register('folder', 'formcount',   ['label' => __('Forms', 'griffinforms-form-builder'),               'default_on' => false]);

        parent::registerColumns();
    }

    protected function descriptionCellText($list_column, $item)
    {
        $description = is_array($item) ? ($item['description'] ?? '') : ($item->description ?? '');
        $description = trim((string) $description);

        if ($description === '') {
            echo '';
            return;
        }

        if (strlen($description) > 80) {
            $description = substr($description, 0, 80) . '...';
        }

        echo esc_html($description);
    }

    protected function iconCellText($list_column, $item)
    {
        $icon = is_array($item) ? $item['icon'] ?? '' : $item->icon ?? '';

        $icon_dir = $this->config->getRootPath() . 'admin/images/folder/';
        $icon_url = $this->config->getRootUrl() . 'admin/images/folder/';
        $icon_file = $icon ? $icon . '.png' : '';

        if ($icon && is_file($icon_dir . $icon_file)) {
            $url = esc_url($icon_url . $icon_file);
        } else {
            $url = esc_url($icon_url . 'default.png');
        }

        echo '<img src="' . esc_url($url) . '" alt="" width="18" />';
    }

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

    protected function formcountCellText($list_column, $item)
    {
        global $wpdb;
        $table = $this->config->getTable('form');
        $count = (int) $wpdb->get_var($wpdb->prepare(
            "SELECT COUNT(*) FROM %i WHERE folder = %d",
            $table,
            $item->id
        ));
        if ($count === 0) {
            echo '<span class="text-muted">—</span>';
            return;
        }
        $url = admin_url('admin.php?page=griffinforms-forms&folder_id=' . absint($item->id));
        echo '<a href="' . esc_url($url) . '" class="badge text-bg-primary text-decoration-none">' . esc_html($count) . '</a>';
    }
}
