<?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 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('Yes') . '</button>
                    <button type="button" class="configurator_radio btn ' . (!$active ? 'active' : '') . '" value="0">' . $this->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\');
                    });
                });';

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

        return $template;
    }

    public function textarea($name = '', $value = NULL) {
        return (new \Rtgc_textarea())->field($name, $value);
    }

    public function texteditor($name = '', $value = NULL) {
        return (new \Rtgc_texteditor())->field($name, $value);
    }

    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((!empty($value) ? $value : date('d-m-Y H:i:s'))));
        
        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" value ="' . (int) $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);
    }

    public function price($name = '', $value = '', $currency = 'uah') {
        
        return $this->load->library('rtgrid/callback/rtgc_price')->set_currency($currency)->field($name, $value);
    }

    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 price_rub($name = '', $value = '') {
        
        return $this->price($name, $value, 'rub');
    }

    public function readonly($name = '', $value = '') {
        
        return '<input class="input" type="text" disabled value="' . $value .'" />';
    }

}
