<?php

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

/**
 * @author Igor Ratsun
 */
class Rtgc_date extends \Abstract_rtgc_callback implements \Rtgc_filtrable
{

    protected $config = [
        'with_time' => false,
    ];

    public function __construct($config = [])
    {
        $this->config = array_merge($this->config, $config);
    }
    
    public function set_field_name($field_name)
    {
        parent::set_field_name($field_name);
        
        if (!is_null($this->get_crud())) {
            $format = 'Y-m-d' . ($this->config['with_time'] ? ' H:i:s' : '');
            $this->get_crud()->callback_before_edit(function ($post) use ($field_name, $format) {
                $post[$field_name] = date($format, strtotime($post[$field_name]));
                return $post;
            });
        }
        
        return $this;
    }

    public function column($value = '', $data = array())
    {
        return !empty($value) ? $this->date_format($value) : $value;
    }

    public function field($name = '', $value = NULL)
    {
        return $this->init_datepicker($name, $value, $this->config['with_time']);
    }

    public function filter($name, $value)
    {
        return '<input type="date" name="' . $name . '" value="' . $value . '" />';
    }

    public function filter_by($search_name, $value)
    {
        if (!empty($value)) {
            $from = date('Y-m-d 00:00:00', strtotime($value));
            $to = date('Y-m-d 23:59:59', strtotime($value));
            get_instance()->db->where(sprintf('(' . $search_name . ' BETWEEN \'%s\' AND \'%s\')', $from, $to), '', false);
        }
    }

    protected function date_format($value)
    {
        return date('d.m.Y' . (($this->config['with_time']) ? ' H:i:s' : ''), strtotime((!empty($value) ? $value : date('d-m-Y H:i:s'))));
    }

    protected function init_datepicker($name, $value, $time = false)
    {

        $ci = &get_instance();
        $ci->js_css->add_js('/' . $ci->config->item('assets_folder') . '/plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js');

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

        $ci->js_css->add_css('/' . $ci->config->item('assets_folder') . '/plugins/bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css');


        $ci->js_css->add_js_string("
            $(function() {
                $('.datepicker').datetimepicker({
                 language: '" . $ci->uri->current_language . "',
                 " . (($time) ? "" : "pickTime: false,") . "
                weekStart: 1,
                });
            });"
        );

        return '<div class="datepicker input-append">
                  <input data-format="dd.MM.yyyy' . (($time) ? ' hh:mm:ss' : '') . '" type="text" name="' . $name . '" value="' . $this->date_format($value) . '"></input>
                  <span class="add-on"><i data-time-icon="icon-time" data-date-icon="icon-calendar"></i></span>
                </div>';
    }

}
