<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 * Set active button
 *
 * @author Igor Ratsun
 */
class Rtgc_active extends \Abstract_rtgc_callback implements \Rtgc_filtrable
{

    public function column($value = '', $data = array())
    {
        // Prepare data
        $active = ((is_string($value) && ($value == 't' || $value == '1')) || (!is_string($value) && $value == TRUE)) ? TRUE : FALSE;

        return '<i class="icon-' . ($active ? 'ok' : 'remove') . '"></i>';
    }

    public function field($name = '', $value = NULL)
    {
        $ci = get_instance();
        // Prepare data
        $active = ((is_string($value) && ($value == 't' || $value == '1')) || (!is_string($value) && $value == TRUE)) ? TRUE : FALSE;

        $template = '
            <div>
                <div class="btn-group" data-toggle="buttons-radio">
                    <button type="button" class="configurator_radio btn ' . ($active ? 'active' : '') . '" value="1">' . $ci->lang->line('Yes') . '</button>
                    <button type="button" class="configurator_radio btn ' . (!$active ? 'active' : '') . '" value="0">' . $ci->lang->line('No') . '</button>
                </div>
                <input type="hidden" name="' . $name . '" value="' . intval($active) . '" />
            </div>';

        $js = '$(function(){
                    $(\'.configurator_radio\').click(function(){
                        var el = $(this);
                        el.parent().parent().find(\'input[type=hidden]\').val(el.val());
                        el.parent().find(\'.configurator_radio\').removeClass(\'active\');
                        el.addClass(\'active\');
                    });
                });';

        $ci->js_css->add_js_string($js);

        return $template;
    }

    public function filter($name, $value)
    {
        $ci = &get_instance();
        return (new \Rtgc_select(['1' => $ci->lang->line('Yes'), '2' => $ci->lang->line('No')]))->filter($name, $value);
    }

    public function filter_by($column, $value)
    {
        if ($value == '0') {
            return;
        }
        
        if ($value == '2') {
            $value = 0;
        }
        get_instance()->db->where($column, $value);
    }

}
