<?php

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

/**
 * Description of rtgrid_field_callback
 *
 * @author Igor Ratsun
 */
class rtgrid_field_callback extends CI_Model {

    public $buffer = NULL;

    public function __construct() {

        $this->lang->load('rtgrid/field_callback');
    }

    public function default_field($name = '', $value = NULL) {

        return $this->input($name, $value);
    }

    public function input($name = '', $value = NULL) {

        return '<input type="text" name="' . $name . '" value="' . $value . '" />';
    }

    public function active($name = '', $value = NULL) {
        // 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">' . $this->lang->line('active_true') . '</button>
                    <button type="button" class="configurator_radio btn ' . (!$active ? 'active' : '') . '" value="0">' . $this->lang->line('active_false') . '</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\');
                    });
                });';

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

        return $template;
    }

    public function textarea($name = '', $value = NULL) {

        return '<textarea class="input-xxlarge" rows="10" name="' . $name . '">' . $value . '</textarea>';
    }

    public function texteditor($name = '', $value = NULL) {
        $this->ciext->load_jquery();
        $this->js_css->add_js('/assets/plugins/ckeditor/ckeditor.js');
        $this->js_css->add_js('/assets/plugins/ckeditor/adapters/jquery.js');
        $this->js_css->add_js_string('$(function(){$(".texteditor").ckeditor({toolbar:\'RTpanel\'});});');

        return '<textarea class="texteditor" name="' . $name . '">' . $value . '</textarea>';
    }

    protected function init_datepicker($name, $value, $time = FALSE) {
        
        $this->js_css->add_js('/' . $this->config->item('assets_folder') . '/plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js');

        if (in_array($this->uri->current_language, array('ua', 'ru'))) {
            $this->js_css->add_js('/' . $this->config->item('assets_folder') . '/plugins/bootstrap-datetimepicker/lang/' . $this->uri->current_language . '.js');
        }
        
        $this->js_css->add_css('/' . $this->config->item('assets_folder') . '/plugins/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css');

        
        $this->js_css->add_js_string("
            $(function() {
                $('.datepicker').datetimepicker({
                 language: '" . $this->uri->current_language ."',
                 " . (($time) ? "" : "pickTime: false," ). "
                weekStart: 1,
                });
            });"
        );
        
        $value = date('d.m.Y' . (($time) ? ' H:i:s' : ''), strtotime($value));
        
        return '<div class="datepicker input-append">
                  <input data-format="dd.MM.yyyy' . (($time) ? ' HH:mm:ss' : '') . '" type="text" name="' . $name . '" value="' . $value . '"></input>
                  <span class="add-on">
                    <i data-time-icon="icon-time" data-date-icon="icon-calendar">
                    </i>
                  </span>
                </div>';
    }
    
    public function date($name = '', $value = NULL) {
        
        return $this->init_datepicker($name, $value);
    }
    
    public function datetime($name = '', $value = NULL) {

        return $this->init_datepicker($name, $value, TRUE);
    }

    public function spinner($name = '', $value = 0) {

        return '<input name="' . $name . '" type="number" min="0" step="10" value ="' . $value . '"/>';
    }

    public function dropdown($name = '', $value = '') {
        if (!is_array($this->buffer[$name])) {

            return '';
        }

        $this->load->helper('form');

        return form_dropdown($name, $this->buffer[$name], $value);
    }

    protected function _price($name = '', $value = '', $currency = 'uah') {
        switch ($currency) {
            case 'uah':
                $currency = '₴';
                break;
            case 'usd':
                $currency = '$';
                break;
        }
        $value = number_format($value, 2, '.', '');
        
        $js = '$(function(){
                    $(\'.money\').keypress(function (e) {
                        if (e.which != 46 && e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
                            return false;
                        }
                    });
                });';
        $this->js_css->add_js_string($js);

        return '<div class="input-prepend"><span class="add-on">' . $currency . '</span><input class="span2 money" name="' . $name . '" type="text" placeholder="99.99" value="' . $value . '"></div>';
    }

    public function price_uah($name = '', $value = '') {
        
        return $this->_price($name, $value, 'uah');
    }
    
    public function price_usd($name = '', $value = '') {
        
        return $this->_price($name, $value, 'usd');
    }
    
    public function readonly($name = '', $value = '') {
        
        return '<input class="input" type="text" disabled value="' . $value .'" />';
    }

}
