<?php

namespace GriffinForms\Admin\Html\FormControls\Forms;

class FolderFilter extends \GriffinForms\Admin\Html\FormControls\Forms\Format
{    
    public function html()
    {
        $folders = $this->sql->getItems('folder', array('name', 'id'));
        
        echo '<select name="folder_id">';
        echo '<option value="-1">' . esc_html($this->lang->getText('all_folders', 'folder_filter')) . '</option>';
        
        if (!empty($_GET['folder_id']) and $_GET['folder_id'] === "root") {
            echo '<option value="root" selected>' . esc_html($this->lang->getText('root_folder', 'folder_filter')) . '</option>';;
        } else {
            echo '<option value="root">' . esc_html($this->lang->getText('root_folder', 'folder_filter')) . '</option>';
        }
        
        foreach ($folders as $folder) {
            $selected = '';
            
            if ($folder->id == $this->value) {
                $selected = ' selected';
            }            
            
            echo '<option value="' . absint($folder->id) . '"' . esc_attr($selected) . '>';
            echo esc_html($folder->name);
            echo '</option>';
        }
        
        echo '</select>';
    }
}